Skip to content

Commit

Permalink
Fix manticore streaming mode when WebMock is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kovyrin committed Jan 28, 2021
1 parent d19b472 commit 4ec1f2d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/webmock/http_lib_adapters/manticore_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,15 @@ def generate_manticore_response(webmock_response)
def generate_webmock_response(manticore_response)
webmock_response = WebMock::Response.new
webmock_response.status = [manticore_response.code, manticore_response.message]
webmock_response.body = manticore_response.body
webmock_response.headers = manticore_response.headers

# The attempt to read the body could fail if manticore is used in a streaming mode
webmock_response.body = begin
manticore_response.body
rescue ::Manticore::StreamClosedException
nil
end

webmock_response
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/acceptance/manticore/manticore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@
expect(failure_handler).to have_received(:call)
end
end

context 'when used in a streaming mode' do
let(:webmock_server_url) {"http://#{WebMockServer.instance.host_with_port}/"}
let(:result_chunks) { [] }

def manticore_streaming_get
Manticore.get(webmock_server_url).tap do |req|
req.on_success do |response|
response.body do |chunk|
result_chunks << chunk
end
end
end
end

context 'when connections are allowed' do
it 'works' do
WebMock.allow_net_connect!
expect { manticore_streaming_get.call }.to_not raise_error
expect(result_chunks).to_not be_empty
end
end

context 'when stubbed' do
it 'works' do
stub_body = 'hello!'
stub_request(:get, webmock_server_url).to_return(body: stub_body)
expect { manticore_streaming_get.call }.to_not raise_error
expect(result_chunks).to eq [stub_body]
end
end
end
end
end
end

0 comments on commit 4ec1f2d

Please sign in to comment.