Include allow header in options response (#628)

This commit is contained in:
Joe Nelson
2016-06-08 23:12:29 -07:00
parent e272c2ed08
commit e315dbc91e
3 changed files with 29 additions and 11 deletions
+1
View File
@@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Ability to order embedded entities - @ruslantalpa - Ability to order embedded entities - @ruslantalpa
- Ability to paginate using &limit and &offset parameters - @ruslantalpa - Ability to paginate using &limit and &offset parameters - @ruslantalpa
- Ability to apply limits to embedded entities and enforce --max-rows on all levels - @ruslantalpa, @begriffs - Ability to apply limits to embedded entities and enforce --max-rows on all levels - @ruslantalpa, @begriffs
- Add allow response header in OPTIONS - @begriffs
### Fixed ### Fixed
- Return 401 or 403 for access denied rather than 404 - @begriffs - Return 401 or 403 for access denied rather than 404 - @begriffs
+13 -11
View File
@@ -11,7 +11,7 @@ import Data.Bifunctor (first)
import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Char8 as BS
import Data.IORef (IORef, readIORef) import Data.IORef (IORef, readIORef)
import Data.List (find, delete) import Data.List (find, delete)
import Data.Maybe (isJust, fromMaybe, fromJust, mapMaybe) import Data.Maybe (fromMaybe, fromJust, mapMaybe)
import Data.Ranged.Ranges (emptyRange) import Data.Ranged.Ranges (emptyRange)
import Data.String.Conversions (cs) import Data.String.Conversions (cs)
import Data.Text (Text, replace, strip) import Data.Text (Text, replace, strip)
@@ -173,16 +173,18 @@ app dbStructure conf apiRequest =
else responseLBS status204 [r] "" else responseLBS status204 [r] ""
(ActionInfo, TargetIdent (QualifiedIdentifier tSchema tTable), Nothing) -> (ActionInfo, TargetIdent (QualifiedIdentifier tSchema tTable), Nothing) ->
if isJust $ find (\t -> tableName t == tTable && tableSchema t == tSchema) (dbTables dbStructure) let mTable = find (\t -> tableName t == tTable && tableSchema t == tSchema) (dbTables dbStructure) in
then let cols = filter (filterCol tSchema tTable) $ dbColumns dbStructure case mTable of
pkeys = map pkName $ filter (filterPk tSchema tTable) allPrKeys Nothing -> return notFound
body = encode (TableOptions cols pkeys) Just table ->
filterCol :: Schema -> TableName -> Column -> Bool let cols = filter (filterCol tSchema tTable) $ dbColumns dbStructure
filterCol sc tb Column{colTable=Table{tableSchema=s, tableName=t}} = s==sc && t==tb pkeys = map pkName $ filter (filterPk tSchema tTable) allPrKeys
filterCol _ _ _ = False in body = encode (TableOptions cols pkeys)
return $ responseLBS status200 [jsonH, allOrigins] $ cs body filterCol :: Schema -> TableName -> Column -> Bool
else filterCol sc tb Column{colTable=Table{tableSchema=s, tableName=t}} = s==sc && t==tb
return notFound filterCol _ _ _ = False
acceptH = (hAllow, if tableInsertable table then "GET,POST,PATCH,DELETE" else "GET") in
return $ responseLBS status200 [jsonH, allOrigins, acceptH] $ cs body
(ActionInvoke, TargetProc qi, (ActionInvoke, TargetProc qi,
Just (PayloadJSON (UniformObjects payload))) -> do Just (PayloadJSON (UniformObjects payload))) -> do
+15
View File
@@ -8,6 +8,7 @@ import SpecHelper
import Network.HTTP.Types import Network.HTTP.Types
import Network.Wai (Application) import Network.Wai (Application)
import Network.Wai.Test (SResponse(simpleHeaders))
spec :: SpecWith Application spec :: SpecWith Application
spec = do spec = do
@@ -382,3 +383,17 @@ spec = do
it "errors for non existant tables" $ it "errors for non existant tables" $
request methodOptions "/dne" [] "" `shouldRespondWith` 404 request methodOptions "/dne" [] "" `shouldRespondWith` 404
describe "Allow header" $ do
it "includes read/write verbs for writeable table" $ do
r <- request methodOptions "/items" [] ""
liftIO $
simpleHeaders r `shouldSatisfy`
matchHeader "Allow" "GET,POST,PATCH,DELETE"
it "includes read verbs for read-only table" $ do
r <- request methodOptions "/has_count_column" [] ""
liftIO $
simpleHeaders r `shouldSatisfy`
matchHeader "Allow" "GET"