Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post always returns 415 status #51

Open
jinilover opened this issue Oct 3, 2018 · 3 comments
Open

post always returns 415 status #51

jinilover opened this issue Oct 3, 2018 · 3 comments

Comments

@jinilover
Copy link

Sorry that I am pretty new to hspec-wai and couldn't find the answer to this problem. Therefore I posted a question here.
I have a POST test case as follows:

it "post /users sample user responds with success msg" $
  post "/users" postBody `shouldRespondWith` "wai"
where postBody = [json|{"name":"wai"}|]

It returns status_code 415, media-type not supported. I tried other ways such as:
post "/users" "" `shouldRespondWith` "wai" although this will fail, I expect it will return error message like 400, not enough input. But still, it return 415
Alternatively

post "/users" (fromValue $ object [ "name" .= "wai" ]) `shouldRespondWith` "wai"

But still returns 415.
In hspec-wai/Wai.hs, I found the post function is post path = request methodPost path [], the headers is an empty list.
Therefore I changed the test case by using request directly

request methodPost "/users" postHeader postBody `shouldRespondWith` "wai"
where postBody = [json|{"name":"wai"}|]
           postHeader = [("Content-Type", "application/json")]

It works. It seems the post func that passes empty headers doesn't work. I need to specify the content-type in the headers. Did I do something wrong?

@diegospd
Copy link

I also had the same issue. There's no way to set correct headers for post.
Redefining post to send your headers works fine

import Test.Hspec.Wai hiding(post)
import Network.HTTP.Types(methodPost)
post path = request methodPost path headers
    where headers =  [("Content-Type", "application/json")]

@jinilover
Copy link
Author

thanks @diegospd

@ynishi
Copy link

ynishi commented Mar 18, 2019

I think API test with json is common.

How about add postJson like postHtmlForm?
https://github.com/hspec/hspec-wai/blob/master/src/Test/Hspec/Wai.hs#L139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants