hspec / hspec-wai

Helpers to test WAI applications with Hspec
MIT License
64 stars 25 forks source link

Json matcher doesn't accept correct header #46

Closed Philonous closed 6 years ago

Philonous commented 6 years ago

I'm trying to test a REST api written in servant. To that end I have a test like this:

get "/users" `shouldRespondWith` [json|[{"name":"bob"}]|]

That test fails with

  1) GET /users responds with [User]
       missing header:
         Content-Type: application/json
       the actual headers were:
         Content-Type: application/json;charset=utf-8

The actual header is compatible with the required one, so this shouldn't fail.

sol commented 6 years ago

@Philonous As I understand it application/json; charset=utf-8 is non-standard. See also https://github.com/hspec/hspec-wai/pull/40 and elsewhere on the Internet.

sol commented 6 years ago

@Philonous if you want to test your current behavior, you can do this with:

get "/users" `shouldRespondWith` [json|[{"name":"bob"}]|] {matchHeaders = ["Content-Type" <:> "application/json; charset=utf-8"]}
sol commented 6 years ago

@jkarni any opinion on this. Have you had a discussion on what the right thing for servant is before?

Philonous commented 6 years ago

Servant seems to implement it on purpose to appease other clients (see https://github.com/haskell-servant/servant/issues/849).

Also, the JSON rfc remarks that although it isn't defined, it should be ignored by compliant recipients:

Note: No "charset" parameter is defined for this registration. Adding one really has no effect on compliant recipients.

So I think this should be fixed here and have implemented a patch: https://github.com/hspec/hspec-wai/pull/47

sol commented 6 years ago

I added a lengthy source code comment in https://github.com/hspec/hspec-wai/commit/791dcbb48dff41735b53758d988b8754ad7dc49b that elaborates my view on this matter.

sol commented 6 years ago

Fixed.

Philonous commented 6 years ago

Thanks :)