This function checks that a response body is JSON, and parses it into a Haskell value. Having something like this function is pretty essential to using Yesod as a JSON API server, so I think it's a good addition. You can use it to parse a Haskell record directly (usually by adding FromJSON classes to your response types), or parse a Value and pull out individual fields, maybe using something like `aeson-lens` (though probably a testing-specific library would be better). I debated over these things: 1. The name. I was thinking of something like [assert/require/decode/parse]JSON[Response/Body]. I ultimately went with requireJSONResponse: - decode/parse sound like the aeson functions that return Either or Maybe, and I wanted this function to throw an error if it failed - I'm open to using `assertJSONResponse`—it matches the other functions (`assertEq`) better—but I think it reads less like English. - I chose Response over Body because (a) It also checks the content-type header, which is not in the body (b) "Body" felt slightly in-the-weeds of HTTP; I think "response" is more approachable. 2. Should it require the JSON content type? You can definitely have a server that returns JSON without JSON content types, but I think that's a such a bad idea, it's more likely requiring it helps people if they accidentally don't add the header. 3. Should it take a String parameter to add to the error message? This would match `assertEq`, but other functions like `statusIs` don't take a message. Ultimately I went without it, because the messages felt like I was repeating myself: `(comment :: Comment) <- requireJSONResponse "the response has a comment"`
3.7 KiB
ChangeLog for yesod-test
1.6.9
Add requireJSONResponse function #164
1.6.8
Add testModifySite function #1642
1.6.7
Add addBasicAuthHeader function #1632
1.6.6.2
addPostParam will now URL-encode keys and values to prevent corruption
when special characters such as & are used
#1617
1.6.6.1
- Documentation fixes
- Support for network 3
1.6.6
- Add utility functions to modify cookies $1570
1.6.5.1
- Make test suite build with GHC 8.6 #1561
1.6.5
bodyEquals prints out actual body in addition to expected body in failure msg #1525
1.6.4
Add yesodSpecWithSiteGeneratorAndArgument #1485
1.6.3
Add performMethod #1502
1.6.2
- Add byLabel-related functions like byLabelContain #1482
1.6.1
- Fix the build with
base-4.11(GHC 8.4).
1.6.0
- Upgrade to yesod-core 1.6.0
1.5.9.1
- Fixes a Haddock syntax error in 1.5.9 #1473
1.5.9
- Add byLabelExact and related functions #1459
1.5.8
- Added implicit parameter HasCallStack to assertions. #1421
1.5.7
- Add clickOn. #1408
1.5.6
- Add assertNotEq. #1375
1.5.5
- Fix warnings
1.5.4.1
- Compilation fix for GHC 7.8
1.5.4
- yesod-test: add getLocation test helper. #1314
1.5.3
- Added bodyNotContains #1271
1.5.2
- Added assertEq, deprecated assertEqual #1259
1.5.1.1
- Fix
addToken_needing a trailing space and allows multiples spaces in css selector.
1.5.1.0
- Better error provenance for stuff invoking withResponse' #1191
1.5.0.1
- Fixed the
application/x-www-form-urlencodedheader being added to all requests, even those sending a binary POST body #1064- The
application/x-www-form-urlencodedContent-Type header is now only added if key-value POST parameters are added - If no key-values pairs are added, or the request body is set with
setRequestBody, no default Content-Type header is set
- The
1.5
- remove deprecated addNonce functions
- You can now configure testing middleware
Configuring middleware makes it easy to add logging among other things. middleware is applied to the wai app before each test.
If you follow the yesod scaffold, you probably have a
withApp function in TestImport.hs.
This function should now return (foundation, middleware).
id is an acceptable value for middleware.
1.4.4
test helpers for CRSF middleware such as addTokenFromCookie
1.4.3.2
- Add
addTokenFromCookieandaddTokenFromCookieNamedToHeaderNamed, which support the new CSRF token middleware #1058 - Add
getRequestCookies, which returns the cookies from the most recent request #1058
1.4.3.1
- Improved README
1.4.2
Provide Example instance for YesodExample.
1.4.1.1
Upgrade to hspec 2