Skip to content

Commit

Permalink
Witness capturing of query strings in test output
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbiehl committed Sep 21, 2023
1 parent 37c8c67 commit cc6f64a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 10 deletions.
30 changes: 20 additions & 10 deletions test/Scarf/Gateway/Golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Yaml (decodeFileThrow)
import Network.Wai
( Request
( requestBody,
( rawQueryString,
requestBody,
requestHeaderHost,
requestHeaderReferer,
requestHeaderUserAgent,
Expand Down Expand Up @@ -93,7 +94,9 @@ data Input = Input

-- | Test output value.
data Output = Output
{ -- | Response status code
{ -- | Query string captured from the request
outputQueryString :: Text,
-- | Response status code
outputStatus :: Int,
-- | Response HTTP headers
outputHeaders :: [(Text, Text)],
Expand Down Expand Up @@ -160,12 +163,14 @@ goldenTests testsDirectory = do
(newGatewayConfig inputManifest)
{ gatewayReportRequest = \_span request responseStatus capture -> do
putMVar chan $
captureRequest
(read "2022-01-11 08:34:00.914835 UTC")
requestId
request
responseStatus
capture
( request,
captureRequest
(read "2022-01-11 08:34:00.914835 UTC")
requestId
request
responseStatus
capture
)
}
headers =
fmap (bimap (CaseInsensitive.mk . encodeUtf8) encodeUtf8) inputHeaders
Expand All @@ -191,7 +196,7 @@ goldenTests testsDirectory = do
writeIORef bodyRef ByteString.empty
pure result
}
capture <- takeMVar chan
(request, capture) <- takeMVar chan

-- We want to have clear and deterministic diffs. For that we have to run the produced
-- JSON through aeson-pretty.
Expand All @@ -201,7 +206,9 @@ goldenTests testsDirectory = do
decode $
encode $
Output
{ outputStatus =
{ outputQueryString =
decodeUtf8 (rawQueryString request),
outputStatus =
fromEnum simpleStatus,
outputHeaders =
fmap (bimap (decodeUtf8 . CaseInsensitive.original) decodeUtf8) simpleHeaders,
Expand Down Expand Up @@ -264,3 +271,6 @@ instance ToJSON Output where
( stringUtf8 $ show outputCapture
)
)
<> if outputQueryString /= ""
then pair "query" (text outputQueryString)
else mempty
1 change: 1 addition & 0 deletions test/golden/file-package-with-variables-1.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"headers": {
"Location": "https://datausa.io/api/data?drilldowns=Nation&measures=Population"
},
"query": "?drilldowns=Nation&measures=Population",
"status": 302
}
1 change: 1 addition & 0 deletions test/golden/file-package-with-variables-2.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"headers": {
"Location": "https://datausa.io/api/data?drilldowns=Nation&measures=Population"
},
"query": "?measures=Population",
"status": 302
}
1 change: 1 addition & 0 deletions test/golden/file-package-with-variables-3.output.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"capture": "",
"headers": {},
"query": "?measures=Population&drilldowns=Nation",
"status": 404
}
1 change: 1 addition & 0 deletions test/golden/file-package-without-variable-2.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"headers": {
"Location": "https://datausa.io/api/data"
},
"query": "?drilldowns=Nation&measures=Population",
"status": 302
}
1 change: 1 addition & 0 deletions test/golden/file-package-without-variable.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"headers": {
"Location": "https://datausa.io/api/data"
},
"query": "?drilldowns=Nation&measures=Population",
"status": 302
}
1 change: 1 addition & 0 deletions test/golden/file-package-without-varible-1.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"headers": {
"Location": "https://datausa.io/api/data?drilldowns=Nation&measures=Population"
},
"query": "?drilldowns=Nation&measures=Population",
"status": 302
}
1 change: 1 addition & 0 deletions test/golden/file-package-without-varible-2.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"headers": {
"Location": "https://datausa.io/api/data?drilldowns=Nation&measures=Population"
},
"query": "?drilldowns=Nation&measures=Population",
"status": 302
}
1 change: 1 addition & 0 deletions test/golden/pixel.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"Content-Length": "24",
"Content-Type": "image/png"
},
"query": "?x-pxid=12345",
"status": 200
}

0 comments on commit cc6f64a

Please sign in to comment.