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 Mitwicki

Hi Robert,

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] File: README — Documentation for rspec-mocks (3.12.6)
[2] GitHub - bblimke/webmock: Library for stubbing and setting expectations on HTTP requests in Ruby.

···

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

On Wednesday, 24 October 2012 at 7:59 PM, Robert Mitwicki wrote:

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 Mitwicki

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

···

On Wednesday, 24 October 2012 at 9:35 PM, Arlen Cuss wrote:

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] File: README — Documentation for rspec-mocks (3.12.6)
[2] GitHub - bblimke/webmock: Library for stubbing and setting expectations on HTTP requests in Ruby.

On Wednesday, 24 October 2012 at 7:59 PM, Robert Mitwicki wrote:

> 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 Mitwicki