chrisk/fakeweb

Make it easier to access submitted data

betesh opened this issue · 1 comments

Right now, if I want to verify that I'm POST'ing the right data, I need to do something like this:

FakeWeb.register_uri(:post, my_url, body: 'i-dont-care-about-the-response-only-the-request')
do_something_that_hits_my_url
request_data = CGI.parse FakeWeb.last_request.body_stream.instance_variable_get('@stream').string
expect(request_data).to ...

Line 3 of this example should be encapsulated. For instance, it could be exposed as FakeWeb.last_request.form_data

I can confirm this is necessary when using FakeWeb to stub requests from the RestClient gem (2.0.0+).

Without inspecting this IO stream data, there's no way to see what was sent. Since FakeWeb.last_request.body will be nil in the common case of sending a String payload with RestClient::Request.execute(...).

It is due to this code in RestClient::Request creating a RestClient::Payload::Base (or similar depending on the type of body data) and attaching it to the Net::HTTP request body_stream (rather than body).

This was documented here as a performance improvement with the release of RestClient v2.0.0

Add actual support for streaming request payloads. Previously rest-client would call .to_s even on RestClient::Payload::Streamed objects. Instead, treat any object that responds to .read as a streaming payload and pass it through to .body_stream= on the Net:HTTP object. This massively reduces the memory required for large file uploads.

This isn't an issue with Net::HTTP unless you're setting body_stream directly on the request.