Skip to content

Commit

Permalink
spec: test Server-Timing metrics presence
Browse files Browse the repository at this point in the history
  • Loading branch information
develop7 committed Oct 5, 2023
1 parent 999fe5d commit 4d12745
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 6 additions & 10 deletions test/spec/Feature/Query/ServerTimingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ spec =
`shouldRespondWith`
[json|[{"id":6,"name":"Oscorp","referee":3,"auditor":4,"manager_id":6}]|]
{ matchStatus = 200
, matchHeaders = [ matchContentTypeJson
, matchHeaderPresent "Server-Timing"]
, matchHeaders = matchContentTypeJson : map matchServerTimingHasTiming ["jwt", "plan", "query", "render"]
}

it "works with post request" $
Expand All @@ -33,8 +32,7 @@ spec =
`shouldRespondWith`
[json|[{"id":7,"name":"John","referee":null,"auditor":null,"manager_id":6}]|]
{ matchStatus = 201
, matchHeaders = [ matchContentTypeJson
, matchHeaderPresent "Server-Timing"]
, matchHeaders = matchContentTypeJson : map matchServerTimingHasTiming ["jwt", "plan", "query", "render"]
}

it "works with patch request" $
Expand All @@ -43,8 +41,7 @@ spec =
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, matchHeaderPresent "Server-Timing" ]
, matchHeaders = matchHeaderAbsent hContentType : map matchServerTimingHasTiming ["jwt", "plan", "query", "render"]
}

it "works with put request" $
Expand All @@ -54,7 +51,7 @@ spec =
`shouldRespondWith`
[json| [ { "name": "Go", "rank": 19 } ]|]
{ matchStatus = 200
, matchHeaders = [ matchHeaderPresent "Server-Timing" ]
, matchHeaders = map matchServerTimingHasTiming ["jwt", "plan", "query", "render"]
}

it "works with delete request" $
Expand All @@ -64,8 +61,7 @@ spec =
`shouldRespondWith`
""
{ matchStatus = 204
, matchHeaders = [ matchHeaderAbsent hContentType
, matchHeaderPresent "Server-Timing" ]
, matchHeaders = matchHeaderAbsent hContentType : map matchServerTimingHasTiming ["jwt", "plan", "query", "render"]
}

it "works with rpc call" $
Expand All @@ -75,5 +71,5 @@ spec =
`shouldRespondWith`
[json|{"x": 1, "y": 2}|]
{ matchStatus = 200
, matchHeaders = [ matchHeaderPresent "Server-Timing" ]
, matchHeaders = map matchServerTimingHasTiming ["jwt", "plan", "query", "render"]
}
10 changes: 10 additions & 0 deletions test/spec/SpecHelper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import PostgREST.MediaType (MediaType (..))
import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..))
import Protolude hiding (get, toS)
import Protolude.Conv (toS)
import Data.String (String)

matchContentTypeJson :: MatchHeader
matchContentTypeJson = "Content-Type" <:> "application/json; charset=utf-8"
Expand Down Expand Up @@ -64,6 +65,15 @@ matchHeaderPresent name = MatchHeader $ \headers _body ->
Just _ -> Nothing
Nothing -> Just $ "missing header: " <> toS (original name) <> "\n"

-- | Matches Server-Timing header has a well-formed metric with the given name
matchServerTimingHasTiming :: String -> MatchHeader
matchServerTimingHasTiming metric = MatchHeader $ \headers _body ->
case lookup "Server-Timing" headers of
Just hdr -> if hdr =~ (metric <> ";dur=[[:digit:]]+.[[:digit:]]+")
then Nothing
else Just $ "missing metric: " <> metric <> "\n"
Nothing -> Just "missing Server-Timing header\n"

validateOpenApiResponse :: [Header] -> WaiSession () ()
validateOpenApiResponse headers = do
r <- request methodGet "/" headers ""
Expand Down

0 comments on commit 4d12745

Please sign in to comment.