Skip to content

Commit

Permalink
Fix content type & override (#79)
Browse files Browse the repository at this point in the history
* fix content type & override

* update example spec with content type override example
  • Loading branch information
aviaviavi authored Dec 3, 2022
1 parent 16fd9db commit 52de9e6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
11 changes: 11 additions & 0 deletions examples/example-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,14 @@ cases:
- "Hello: world"
-
value: "Value-With-Key-We-Dont-Care-About"

- name: We use a json content type by default
url: http://your-url.com/other/path
requestMethod: GET
expectStatus: 200

- name: Override the content type
url: http://your-url.com/other/path
requestMethod: GET
headers: "Content-Type: an overridden content type"
expectStatus: 200
29 changes: 24 additions & 5 deletions src/Testing/CurlRunnings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,35 @@ appendQueryParameters newParams r = setQueryString (existing ++ newQuery) r wher
existing = NT.parseQuery $ queryString r
newQuery = NT.simpleQueryToQuery $ fmap (\(KeyValuePair k v) -> (T.encodeUtf8 . A.toText $ k, T.encodeUtf8 v)) newParams

resetContentTypeIfOverridden :: Request -> Request-> Request
resetContentTypeIfOverridden old new =
if (not $ null (getRequestHeader "Content-Type" old))
then (setRequestHeader
"Content-Type"
(getRequestHeader "Content-Type" old)
new)
else new


-- | Sets the payload based on the provided payload type. If a custom
-- "Content-Type" header is provided, it will be preserved
setPayload :: Maybe Payload -> Request -> Request
-- TODO - for backwards compatability, empty requests will set an empty json
-- payload. Given that we support multiple content types, this funtionality
-- isn't exactly correct anymore. This behavior should be considered
-- deprecated and will be updated with the next major version release of
-- curl-runnings.
setPayload Nothing = setRequestBodyJSON emptyObject
setPayload (Just (JSON v)) = setRequestBodyJSON v
setPayload (Just (URLEncoded (KeyValuePairs xs))) = setRequestBodyURLEncoded $ kvpairs xs where
kvpairs = fmap (\(KeyValuePair k v) -> (T.encodeUtf8 . A.toText $ k, T.encodeUtf8 v))
setPayload Nothing req =
resetContentTypeIfOverridden req . setRequestBodyJSON emptyObject $ req
setPayload (Just (JSON v)) req =
resetContentTypeIfOverridden req . (setRequestBodyJSON v) $ req
setPayload (Just (URLEncoded (KeyValuePairs xs))) req =
resetContentTypeIfOverridden req . (setRequestBodyURLEncoded $ kvpairs xs) $
req
where
kvpairs =
fmap
(\(KeyValuePair k v) -> (T.encodeUtf8 . A.toText $ k, T.encodeUtf8 v))

-- | Run a single test case, and returns the result. IO is needed here since this method is responsible
-- for actually curling the test case endpoint and parsing the result.
Expand All @@ -140,8 +159,8 @@ runCase state@(CurlRunningsState _ _ _ tlsCheckType) curlCase = do
manager <- newManager noVerifyTlsManagerSettings

let !request =
setRequestHeaders (toHTTPHeaders interpolatedHeaders) .
setPayload interpolatedData .
setRequestHeaders (toHTTPHeaders interpolatedHeaders) .
appendQueryParameters interpolatedQueryParams .
(if tlsCheckType == DoTLSCheck then id else (setRequestManager manager)) $
initReq { method = B8S.pack . show $ requestMethod curlCase
Expand Down

0 comments on commit 52de9e6

Please sign in to comment.