How can I set body for Net::HTTPResponse

Hi,

I tried to setup body for Net::HTTPResponse in my test code.
But I can’t achieve that without weird patch.
I try the same on ruby 1.9.3 and 1.8.7 but behavior is the same.

require ‘net/http.rb’
response = Net::HTTPResponse.new(‘1.0’, “200”, “OK”)
response.body = “test”
p response.body # => nil

working example

require ‘net/http.rb’
response = Net::HTTPResponse.new(‘1.0’, “200”, “OK”)
response.body
response.body = “test”
p response.body # => test

Why I need first use response.body to make it work?
What I do wrong here?
Is there any easier way to create response with body?

Thanks a lot
Robert M.

Hi Robert,

From my reading of net/http/response.rb, there’s not going to be a
straightforward way for you to do this.

If you’re testing with something like RSpec, consider using a double[1]
or even WebMock[2] to do this for you.

Cheers,

Arlen

[1] http://rubydoc.info/gems/rspec-mocks/frames
[2] https://github.com/bblimke/webmock

(And silly me, you can use WebMock without RSpec. That may be the best
way forward?)