Multiple Stub Requests with webmock / rspec

I tried for a while to mute multiuser requests using webmock and did not find a satisfactory solution.

Ideally, I would like to stub the request as follows:

stub_request(:post, 'http://test.api.com').with(:body => { :file1 => File.new('filepath1'), file2 => File.new('filepath2') }) 

However, this does not seem to work, and RSpec complains that the request was not muted. Printed request printed:

 stub_request(:post, "http://test.api.com"). with(:body => "--785340\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"filepath1\"\r\nContent-Type: text/plain\r\n\r\nhello\r\n--785340\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"filepath2\"\r\nContent-Type: text/plain\r\n\r\nhello2\r\n--785340\r\n", :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'664', 'Content-Type'=>'multipart/form-data; boundary=785340', 'User-Agent'=>'Ruby'}). to_return(:status => 200, :body => "", :headers => {}) 

Of course, I cannot follow this sentence because borders are generated dynamically. Any idea how I could properly drown out these requests?

Thanks! Bruno

+4
source share
3 answers

WebMock does not currently support multiprocessor requests. Mark the author’s comment here for more information: https://github.com/vcr/vcr/issues/295#issuecomment-20181472

I suggest you consider one of the following routes:

  • non-matching stubbing to post-multipart
  • wrapping a request in a method with file path arguments and setting a finer-grained wait for this method
  • using a VCR for mocking external requests in integration tests
+2
source

Late, but I will leave an answer for future crowded and googlers.

I had the same problem and I used Rack :: Multipart :: Parser along with Webmock as work. The quick and dirty code should look something like this (warning: uses activesupport extensions):

 stub_request(:post, 'sample.com').with do |req| env = req.headers.transform_keys { |key| key.underscore.upcase } .merge('rack.input' => StringIO.new(req.body)) parsed_request = Rack::Multipart::Parser.new(env).parse # Expectations: assert_equal parsed_request["file1"][:tempfile].read, "hello world" end 
+2
source

The following is a workaround using WebMock with a regular expression to match multipart / form-data requests, especially useful for testing image loading:

 stub_request(:post, 'sample.com').with do |req| # Test the headers. assert_equal req.headers['Accept'], 'application/json' assert_equal req.headers['Accept-Encoding'], 'gzip, deflate' assert_equal req.headers['Content-Length'], 796588 assert_match %r{\Amultipart/form-data}, req.headers['Content-Type'] assert_equal req.headers['User-Agent'], 'Ruby' # Test the body. This will exclude the image blob data which follow these lines in the body req.body.lines[1].match('Content-Disposition: form-data; name="FormParameterNameForImage"; filename="image_filename.jpeg"').size >= 1 req.body.lines[2].match('Content-Type: img/jpeg').size >= 1 end 

Testing only headers could also be done using the usual WebMock method using stub_request(:post, 'sample.com').with(headers: {'Accept' => 'application/json}) and simply not include the with body specification sentence.

0
source

Source: https://habr.com/ru/post/1468873/


All Articles