From 33757e537bd6fccd15dcaa13c0bb861caec33d89 Mon Sep 17 00:00:00 2001 From: calebmer Date: Thu, 21 Jan 2016 17:57:30 -0500 Subject: [PATCH] Fix options on non existant tables, closes #442 --- CHANGELOG.md | 1 + src/PostgREST/App.hs | 21 ++++++++++++--------- test/Feature/StructureSpec.hs | 3 +++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcfdc8eef..a266e9945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Reject non-POSTs to rpc endpoints - @begriffs +- Throw an error for OPTIONS on nonexistent tables - @calebmer ## [0.3.0.3] - 2016-01-08 diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 60e56aff8..530924809 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -11,7 +11,7 @@ import Control.Arrow ((***)) import Control.Monad (join) import Data.Bifunctor (first) import Data.List (find, sortBy, delete) -import Data.Maybe (fromMaybe, fromJust, mapMaybe) +import Data.Maybe (isJust, fromMaybe, fromJust, mapMaybe) import Data.Ord (comparing) import Data.Ranged.Ranges (emptyRange) import Data.String.Conversions (cs) @@ -146,14 +146,17 @@ app dbStructure conf reqBody req = then notFound else responseLBS status204 [("Content-Range", "*/"<> cs (show queryTotal))] "" - (ActionInfo, TargetIdent (QualifiedIdentifier tSchema tTable), Nothing) -> do - let cols = filter (filterCol tSchema tTable) $ dbColumns dbStructure - pkeys = map pkName $ filter (filterPk tSchema tTable) allPrKeys - body = encode (TableOptions cols pkeys) - filterCol :: Schema -> TableName -> Column -> Bool - filterCol sc tb Column{colTable=Table{tableSchema=s, tableName=t}} = s==sc && t==tb - filterCol _ _ _ = False - return $ responseLBS status200 [jsonH, allOrigins] $ cs body + (ActionInfo, TargetIdent (QualifiedIdentifier tSchema tTable), Nothing) -> + if isJust $ find (\t -> tableName t == tTable && tableSchema t == tSchema) (dbTables dbStructure) + then let cols = filter (filterCol tSchema tTable) $ dbColumns dbStructure + pkeys = map pkName $ filter (filterPk tSchema tTable) allPrKeys + body = encode (TableOptions cols pkeys) + filterCol :: Schema -> TableName -> Column -> Bool + filterCol sc tb (Column{colTable=Table{tableSchema=s, tableName=t}}) = s==sc && t==tb + filterCol _ _ _ = False in + return $ responseLBS status200 [jsonH, allOrigins] $ cs body + else + return notFound (ActionInvoke, TargetProc qi, Just (PayloadJSON (UniformObjects payload))) -> do diff --git a/test/Feature/StructureSpec.hs b/test/Feature/StructureSpec.hs index 6fe0a80e4..a441f00c9 100644 --- a/test/Feature/StructureSpec.hs +++ b/test/Feature/StructureSpec.hs @@ -320,3 +320,6 @@ spec struct c = around (withApp cfgDefault struct c) $ do ] } |] + + it "errors for non existant tables" $ + request methodOptions "/dne" [] "" `shouldRespondWith` 404