Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: implement body streaming for Net::HTTP #1051

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 3.23.1 (future)

* Add support for streaming responses in Net::HTTP adapter

# 3.23.0

* Fixed HTTP.rb adapter to support streaming real responses when WebMock is enabled.
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ stub_request(:any, 'www.example.net').to_timeout
RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout
```

### Streaming response

Net::HTTP supports streaming responses. WebMock can simulate this by specifying an Array for body.
This is only implemented for Net::HTTP at the moment.

```ruby
stub_http_request(:get, "www.example.com").to_return(body: ["a", "b", "c"])

Net::HTTP.start('www.example.com') do |http|
req = Net::HTTP::Get.new('/')
http.request(req) do |res|
res.read_body do |segment|
puts segment
end
end
end # ===> "a\nb\nc\n"
```

### Multiple responses for repeated requests

```ruby
Expand Down
6 changes: 5 additions & 1 deletion lib/webmock/http_lib_adapters/net_http_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def read_body(dest = nil, &block)
return nil if @body.nil?

dest ||= ::Net::ReadAdapter.new(block)
dest << @body.dup
if @body.is_a?(Array)
@body.each { |part| dest << part.dup }
else
dest << @body.dup
end
@body = dest
ensure
# allow subsequent calls to #read_body to proceed as normal, without our hack...
Expand Down
15 changes: 15 additions & 0 deletions spec/acceptance/net_http/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ class TestMarshalingInWebMockNetHTTP
expect(response_body).to eq("abc")
end

it "should support streaming a response body", focus: true do
stub_http_request(:get, "www.example.com").to_return(body: ["a", "b", "c"])
response_body = +""

http_request(:get, "http://www.example.com/") do |response|
i = 0
response.read_body do |fragment|
response_body << "#{fragment}#{i}"
i += 1
end
end

expect(response_body).to eq("a0b1c2")
end

it "should handle Net::HTTP::Post#body" do
stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
req = Net::HTTP::Post.new("/")
Expand Down