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"`
74 lines
2.7 KiB
Plaintext
74 lines
2.7 KiB
Plaintext
name: yesod-test
|
|
version: 1.6.9
|
|
license: MIT
|
|
license-file: LICENSE
|
|
author: Nubis <nubis@woobiz.com.ar>
|
|
maintainer: Michael Snoyman, Greg Weber, Nubis <nubis@woobiz.com.ar>
|
|
synopsis: integration testing for WAI/Yesod Applications
|
|
category: Web, Yesod, Testing
|
|
stability: Experimental
|
|
cabal-version: >= 1.8
|
|
build-type: Simple
|
|
homepage: http://www.yesodweb.com
|
|
description: API docs and the README are available at <http://www.stackage.org/package/yesod-test>
|
|
extra-source-files: README.md, LICENSE, test/main.hs, ChangeLog.md
|
|
|
|
library
|
|
build-depends: HUnit >= 1.2
|
|
, aeson
|
|
, attoparsec >= 0.10
|
|
, base >= 4.3 && < 5
|
|
, blaze-builder
|
|
, blaze-html >= 0.5
|
|
, bytestring >= 0.9
|
|
, case-insensitive >= 0.2
|
|
, conduit
|
|
, containers
|
|
, cookie
|
|
, hspec-core == 2.*
|
|
, html-conduit >= 0.1
|
|
, http-types >= 0.7
|
|
, network >= 2.2
|
|
, memory
|
|
, pretty-show >= 1.6
|
|
, semigroups
|
|
, text
|
|
, time
|
|
, transformers >= 0.2.2
|
|
, wai >= 3.0
|
|
, wai-extra
|
|
, xml-conduit >= 1.0
|
|
, xml-types >= 0.3
|
|
, yesod-core >= 1.6
|
|
|
|
exposed-modules: Yesod.Test
|
|
Yesod.Test.CssQuery
|
|
Yesod.Test.TransversingCSS
|
|
ghc-options: -Wall
|
|
|
|
test-suite test
|
|
type: exitcode-stdio-1.0
|
|
main-is: main.hs
|
|
hs-source-dirs: test
|
|
build-depends: base
|
|
, yesod-test
|
|
, hspec
|
|
, HUnit
|
|
, xml-conduit
|
|
, bytestring
|
|
, containers
|
|
, html-conduit
|
|
, yesod-core
|
|
, yesod-form >= 1.6
|
|
, text
|
|
, wai
|
|
, wai-extra
|
|
, http-types
|
|
, unliftio
|
|
, cookie
|
|
, unliftio-core
|
|
|
|
source-repository head
|
|
type: git
|
|
location: git://github.com/yesodweb/yesod.git
|