Skip to content

Commit

Permalink
Merge pull request #121 from NoRedInk/foxen/fxn-3612-allow-http-test-…
Browse files Browse the repository at this point in the history
…helpers-to-work-with-any-error-type

allow `Http.Mock` helpers to work with any error type, not just the built-in one
  • Loading branch information
jali-clarke authored Nov 20, 2024
2 parents 99364b4 + a5f6639 commit d931837
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions nri-http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.5.0.2

- Loosen types for `Http.Mock` helpers to allow for arbitrary error types

# 0.5.0.1

- Drop support for `aeson-1.x`
Expand Down
2 changes: 1 addition & 1 deletion nri-http/nri-http.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: nri-http
version: 0.5.0.1
version: 0.5.0.2
synopsis: Make Elm style HTTP requests
description: Please see the README at <https://github.com/NoRedInk/haskell-libraries/tree/trunk/nri-http#readme>.
category: Web
Expand Down
2 changes: 1 addition & 1 deletion nri-http/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: nri-http
synopsis: Make Elm style HTTP requests
description: Please see the README at <https://github.com/NoRedInk/haskell-libraries/tree/trunk/nri-http#readme>.
author: NoRedInk
version: 0.5.0.1
version: 0.5.0.2
maintainer: [email protected]
copyright: 2024 NoRedInk Corp.
github: NoRedInk/haskell-libraries/nri-http
Expand Down
8 changes: 4 additions & 4 deletions nri-http/src/Http/Mock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ stub responders stubbedTestBody = do
-- submitted inside a 'stub' function.
--
-- This will return 'Nothing' if the body cannot be parsed as UTF8 text.
getTextBody :: Internal.Request expect -> Maybe Text
getTextBody :: Internal.Request' e expect -> Maybe Text
getTextBody req =
Data.Text.Encoding.decodeUtf8' (getBytesBody req)
|> eitherToMaybe
Expand All @@ -106,15 +106,15 @@ getTextBody req =
-- submitted inside a 'stub' function.
--
-- This will return an error if parsing the JSON body fails.
getJsonBody :: Aeson.FromJSON a => Internal.Request expect -> Result Text a
getJsonBody :: Aeson.FromJSON a => Internal.Request' e expect -> Result Text a
getJsonBody req =
case Aeson.eitherDecodeStrict (getBytesBody req) of
Prelude.Left err -> Err (Text.fromList err)
Prelude.Right decoded -> Ok decoded

-- | Read the body of the request as bytes. Useful to check what data got
-- submitted inside a 'stub' function.
getBytesBody :: Internal.Request expect -> ByteString
getBytesBody :: Internal.Request' e expect -> ByteString
getBytesBody req =
Internal.body req
|> Internal.bodyContents
Expand All @@ -125,7 +125,7 @@ getBytesBody req =
--
-- This will return 'Nothing' if no header with that name was set on the
-- request.
getHeader :: Text -> Internal.Request expect -> Maybe Text
getHeader :: Text -> Internal.Request' e expect -> Maybe Text
getHeader name req =
Internal.headers req
|> List.map Internal.unHeader
Expand Down

0 comments on commit d931837

Please sign in to comment.