Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Order of json matters with em-http-request #180

Closed
fltiago opened this Issue · 8 comments

2 participants

@fltiago

Hi,

when I test my application that submit a body in json's format through em-http-request this error happens:

     Failure/Error: a_request(:post, "http://localhost:4000/test.json").
       The request POST http://localhost:4000/test.json with body "{\"user_id\":1444, \"test\":null}" with headers {'Authorization'=>['test', 'test'], 'Content-Type'=>'application/json'} was expected to execute 1 time but it executed 0 times

       The following requests were made:

       POST http://localhost:4000/test.json with body '{"test":null,"user_id":1444}' with headers {'Authorization'=>['test', 'test'], 'Content-Type'=>'application/json'} was made 1 time

Sometimes the json comes in right order and the test pass, otherwise don't pass.

I believe the order of json body should not be important.

@bblimke
Owner

Can you provide the code that defines the expectation?

Have you used a ruby hash to define expected body or a string?

If you use a string then Webmock will try to match this string body exactly as it was declared in expectation.

If you use a hash to define the expected body, then WebMock will do hash matchting and won't care about the order.

@fltiago

Hello,

thanks for answer me, this is the code:

params = { :user_id => @user.id, :test => nil }

a_request(:post, "http://localhost:4000/test.json").with(:body => params.to_json,
          :headers => {'Authorization'=>['test', 'test'],
                       'Content-Type'=>'application/json'}).should have_been_made

@bblimke
Owner

Just do :body => params instead of :body => params.to_json. WebMock will then try to match a hash instead of trying to match the json string exactly.

@fltiago

Thanks for the answer, but not worked

Failure/Error: a_request(:post, "http://localhost:4000/test.json").
       The request POST http://localhost:4000/test.json with body {"user_id"=>1444, "test"=>null} with headers {'Authorization'=>['test', 'test'], 'Content-Type'=>'application/json'} was expected to execute 1 time but it executed 0 times

       The following requests were made:

       POST http://localhost:4000/test.json with body '{"test":null,"user_id":1444}' with headers {'Authorization'=>['test', 'test'], 'Content-Type'=>'application/json'} was made 1 time
@bblimke
Owner

Webmock has acceptance specs, covering these cases, and the pass for all clients, including em-http-request i.e. https://github.com/bblimke/webmock/blob/master/spec/acceptance/shared/request_expectations.rb#L253-259

Are you able to extract a failing spec based on your code? Request + Expectation declaration?

@bblimke
Owner

Can you also provide Ruby and em-http-request versions that you use?

@fltiago

Interisting, I will extract my failling spec and put here.

Ruby 1.8.7
em-http-request 1.0.2

@bblimke
Owner

Is this still an open issue?

@bblimke bblimke closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.