diff --git a/lib/webmock/http_lib_adapters/async_http_client_adapter.rb b/lib/webmock/http_lib_adapters/async_http_client_adapter.rb index db30ab93..b63cd474 100644 --- a/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +++ b/lib/webmock/http_lib_adapters/async_http_client_adapter.rb @@ -203,8 +203,18 @@ def webmock_responses @webmock_responses ||= {} end + def strip_header?(key:, value:) + # WebMock's internal processing will not handle the body + # correctly if the header indicates that it is chunked, unless + # we also create all the chunks. + # It's far easier just to remove the header. + key =~ /transfer-encoding/i && value =~/chunked/i + end + def build_response(webmock_response) headers = (webmock_response.headers || {}).each_with_object([]) do |(k, value), o| + next if strip_header?(key: k, value: value) + Array(value).each do |v| o.push [k, v] end diff --git a/spec/acceptance/async_http_client/async_http_client_spec.rb b/spec/acceptance/async_http_client/async_http_client_spec.rb index 6196657b..8c930d32 100644 --- a/spec/acceptance/async_http_client/async_http_client_spec.rb +++ b/spec/acceptance/async_http_client/async_http_client_spec.rb @@ -130,6 +130,11 @@ ) end + it 'works with responses that use chunked transfer encoding' do + stub_request(:get, "www.example.com").to_return(body: "abc", headers: { 'Transfer-Encoding' => 'chunked' }) + expect(http_request(:get, "http://www.example.com").body).to eq("abc") + end + it 'works with to_timeout' do stub_request(:get, 'http://www.example.com').to_timeout expect { make_request(:get, 'http://www.example.com') }.to raise_error Async::TimeoutError