-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Prefer tx=rollback #1659
Merged
steve-chavez
merged 1 commit into
PostgREST:master
from
wolfgangwalther:prefer-tx-rollback
Nov 22, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
module Feature.RollbackSpec where | ||
|
||
import Network.Wai (Application) | ||
|
||
import Network.HTTP.Types | ||
import Test.Hspec | ||
import Test.Hspec.Wai | ||
import Test.Hspec.Wai.JSON | ||
|
||
import Protolude hiding (get) | ||
import SpecHelper | ||
|
||
-- two helpers functions to make sure that each test can setup and cleanup properly | ||
|
||
-- creates Item to work with for PATCH and DELETE | ||
postItem = | ||
request methodPost "/items" | ||
[("Prefer", "resolution=ignore-duplicates")] | ||
[json|{"id":0}|] | ||
`shouldRespondWith` | ||
"" | ||
{ matchStatus = 201 } | ||
|
||
-- removes Items left over from POST, PUT, and PATCH | ||
deleteItems = | ||
delete "/items?id=lte.0" | ||
`shouldRespondWith` | ||
"" | ||
{ matchStatus = 204 } | ||
|
||
preferDefault = [("Prefer", "return=representation")] | ||
preferCommit = [("Prefer", "return=representation"), ("Prefer", "tx=commit")] | ||
preferRollback = [("Prefer", "return=representation"), ("Prefer", "tx=rollback")] | ||
|
||
withoutPreferenceApplied = [] | ||
withPreferenceCommitApplied = [ "Preference-Applied" <:> "tx=commit" ] | ||
withPreferenceRollbackApplied = [ "Preference-Applied" <:> "tx=rollback" ] | ||
|
||
shouldRespondToReads reqHeaders respHeaders = do | ||
it "responds to GET" $ do | ||
request methodGet "/items?id=eq.1" | ||
reqHeaders | ||
"" | ||
`shouldRespondWith` | ||
[json|[{"id":1}]|] | ||
{ matchHeaders = respHeaders } | ||
|
||
it "responds to HEAD" $ do | ||
request methodHead "/items?id=eq.1" | ||
reqHeaders | ||
"" | ||
`shouldRespondWith` | ||
"" | ||
{ matchHeaders = respHeaders } | ||
|
||
it "responds to GET on RPC" $ do | ||
request methodGet "/rpc/search?id=1" | ||
reqHeaders | ||
"" | ||
`shouldRespondWith` | ||
[json|[{"id":1}]|] | ||
{ matchHeaders = respHeaders } | ||
|
||
it "responds to POST on RPC" $ do | ||
request methodPost "/rpc/search" | ||
reqHeaders | ||
[json|{"id":1}|] | ||
`shouldRespondWith` | ||
[json|[{"id":1}]|] | ||
{ matchHeaders = respHeaders } | ||
|
||
shouldPersistMutations reqHeaders respHeaders = do | ||
it "does persist post" $ do | ||
request methodPost "/items" | ||
reqHeaders | ||
[json|{"id":0}|] | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
{ matchStatus = 201 | ||
, matchHeaders = respHeaders } | ||
get "items?id=eq.0" | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
deleteItems | ||
|
||
it "does persist put" $ do | ||
request methodPut "/items?id=eq.0" | ||
reqHeaders | ||
[json|{"id":0}|] | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
{ matchHeaders = respHeaders } | ||
get "items?id=eq.0" | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
deleteItems | ||
|
||
it "does persist patch" $ do | ||
postItem | ||
request methodPatch "/items?id=eq.0" | ||
reqHeaders | ||
[json|{"id":-1}|] | ||
`shouldRespondWith` | ||
[json|[{"id":-1}]|] | ||
{ matchHeaders = respHeaders } | ||
get "items?id=eq.0" | ||
`shouldRespondWith` | ||
[json|[]|] | ||
get "items?id=eq.-1" | ||
`shouldRespondWith` | ||
[json|[{"id":-1}]|] | ||
deleteItems | ||
|
||
it "does persist delete" $ do | ||
postItem | ||
request methodDelete "/items?id=eq.0" | ||
reqHeaders | ||
"" | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
{ matchHeaders = respHeaders } | ||
get "items?id=eq.0" | ||
`shouldRespondWith` | ||
[json|[]|] | ||
|
||
shouldNotPersistMutations reqHeaders respHeaders = do | ||
it "does not persist post" $ do | ||
request methodPost "/items" | ||
reqHeaders | ||
[json|{"id":0}|] | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
{ matchStatus = 201 | ||
, matchHeaders = respHeaders } | ||
get "items?id=eq.0" | ||
`shouldRespondWith` | ||
[json|[]|] | ||
|
||
it "does not persist put" $ do | ||
request methodPut "/items?id=eq.0" | ||
reqHeaders | ||
[json|{"id":0}|] | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
{ matchHeaders = respHeaders } | ||
get "items?id=eq.0" | ||
`shouldRespondWith` | ||
[json|[]|] | ||
|
||
it "does not persist patch" $ do | ||
request methodPatch "/items?id=eq.1" | ||
reqHeaders | ||
[json|{"id":0}|] | ||
`shouldRespondWith` | ||
[json|[{"id":0}]|] | ||
{ matchHeaders = respHeaders } | ||
get "items?id=eq.0" | ||
`shouldRespondWith` | ||
[json|[]|] | ||
get "items?id=eq.1" | ||
`shouldRespondWith` | ||
[json|[{"id":1}]|] | ||
|
||
it "does not persist delete" $ do | ||
request methodDelete "/items?id=eq.1" | ||
reqHeaders | ||
"" | ||
`shouldRespondWith` | ||
[json|[{"id":1}]|] | ||
{ matchHeaders = respHeaders } | ||
get "items?id=eq.1" | ||
`shouldRespondWith` | ||
[json|[{"id":1}]|] | ||
|
||
allowed :: SpecWith ((), Application) | ||
allowed = describe "tx-allow-override = true" $ do | ||
describe "without Prefer tx" $ do | ||
-- TODO: Change this to default to rollback for whole test-suite | ||
preferDefault `shouldRespondToReads` withoutPreferenceApplied | ||
preferDefault `shouldPersistMutations` withoutPreferenceApplied | ||
|
||
describe "Prefer tx=commit" $ do | ||
preferCommit `shouldRespondToReads` withPreferenceCommitApplied | ||
preferCommit `shouldPersistMutations` withPreferenceCommitApplied | ||
|
||
describe "Prefer tx=rollback" $ do | ||
preferRollback `shouldRespondToReads` withPreferenceRollbackApplied | ||
preferRollback `shouldNotPersistMutations` withPreferenceRollbackApplied | ||
|
||
disallowed :: SpecWith ((), Application) | ||
disallowed = describe "tx-rollback-all = false, tx-allow-override = false" $ do | ||
describe "without Prefer tx" $ do | ||
preferDefault `shouldRespondToReads` withoutPreferenceApplied | ||
preferDefault `shouldPersistMutations` withoutPreferenceApplied | ||
|
||
describe "Prefer tx=commit" $ do | ||
preferCommit `shouldRespondToReads` withoutPreferenceApplied | ||
preferCommit `shouldPersistMutations` withoutPreferenceApplied | ||
|
||
describe "Prefer tx=rollback" $ do | ||
preferRollback `shouldRespondToReads` withoutPreferenceApplied | ||
preferRollback `shouldPersistMutations` withoutPreferenceApplied | ||
|
||
|
||
forced :: SpecWith ((), Application) | ||
forced = describe "tx-rollback-all = true, tx-allow-override = false" $ do | ||
describe "without Prefer tx" $ do | ||
preferDefault `shouldRespondToReads` withoutPreferenceApplied | ||
preferDefault `shouldNotPersistMutations` withoutPreferenceApplied | ||
|
||
describe "Prefer tx=commit" $ do | ||
preferCommit `shouldRespondToReads` withoutPreferenceApplied | ||
preferCommit `shouldNotPersistMutations` withoutPreferenceApplied | ||
|
||
describe "Prefer tx=rollback" $ do | ||
preferRollback `shouldRespondToReads` withoutPreferenceApplied | ||
preferRollback `shouldNotPersistMutations` withoutPreferenceApplied | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about mapeResponseHeaders. Maybe that can be used for the
Prefer: return
as well. That would solve #740.