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

hash_including not working with JSON request #816

Open
tejaswinichile opened this issue Apr 17, 2019 · 2 comments
Open

hash_including not working with JSON request #816

tejaswinichile opened this issue Apr 17, 2019 · 2 comments

Comments

@tejaswinichile
Copy link

tejaswinichile commented Apr 17, 2019

headers = {
'Accept'=>'application/json',
'Accept-Encoding'=>'gzip, deflate',
'Authorization'=>'Bearer loremipsum',
'Content-Length'=>'61',
'Content-Type'=>'application/json',
'Host'=>'my_url',
'User-Agent'=>'rest-client/2.0.2 (darwin17.6.0 x86_64) ruby/2.3.8p459'
}

::WebMock.stub_request(:post, url).
with(:body => ::WebMock.hash_including(params.to_json),:headers => headers).
to_return(status: 200, body: "", headers: {})


       stub_request(:post, "my_url").
         with(:body => "{\"name\":\"soraya-jast\",\"is_platform_access_only\":true}",
              :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Authorization'=>'Bearer loremipsum', 'Content-Length'=>'53', 'Content-Type'=>'application/json', 'Host'=> 'my_host', 'User-Agent'=>'rest-client/2.0.2 (darwin17.6.0 x86_64) ruby/2.3.8p459'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:post, "my_url").
         with(:body => hash_including({"{\"is_platform_access_only\":true}"=>#<WebMock::Matchers::AnyArgMatcher:0x00007fea71adbb38>}),
              :headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'Authorization'=>'Bearer loremipsum', 'Content-Length'=>'61', 'Content-Type'=>'application/json', 'Host'=>'my_host', 'User-Agent'=>'rest-client/2.0.2 (darwin17.6.0 x86_64) ruby/2.3.8p459'})```

Where am I doing wrong?
@fcheung
Copy link
Contributor

fcheung commented Jul 29, 2019

You should be passing a hash to hash_including, not json, i.e. hash_including(params). Webmock takes care of parsing request body according to the content type when you use that sort of matcher (see https://github.com/bblimke/webmock/blob/master/lib/webmock/request_pattern.rb#L272 )

@jarl-dk
Copy link

jarl-dk commented Aug 26, 2022

I think this can be closed.

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