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

Fix FrozenError in Typhoeus streaming response body #1078

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

patrickjaberg
Copy link

When stubbing a response for the Typhoeus adapter, and the Typhoeus request has an on_body callback, a FrozenError exception is raised when attempting to concatenate the current chunk of the response to the existing response body (i.e. response.body << chunk).

FWIW, my use case for this is to abort a request as early as possible when the response body exceeds a given size, specifically when the response doesn't have a Content-Length header.

The example below illustrates the issue:

require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  gem "typhoeus", "1.4.1"
  gem "webmock", "3.24.0"
end

WebMock.enable!
WebMock.stub_request(:get, "https://example.com").to_return(status: "200", body: "body")

request = Typhoeus::Request.new("https://example.com")

request.on_body do |chunk, response|
  response.body << chunk
end

request.run

This change initializes the Typhoeus response body to a non-frozen, mutable string when using the on_body callback.

When stubbing a response for the Typhoeus adapter, and the Typhoeus
request has an `on_body` callback, a `FrozenError` exception is raised
when attempting to concatenate the current chunk of the response to
the existing response body (i.e. `response.body << chunk`).

FWIW, my use case for this is to abort a request as early as possible
when the response body exceeds a given size, specifically when the
response doesn't have a `Content-Length` header.

The example below illustrates the issue:

```ruby
require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  gem "typhoeus", "1.4.1"
  gem "webmock", "3.24.0"
end

WebMock.enable!
WebMock.stub_request(:get, "https://example.com").to_return(status: "200", body: "body")

request = Typhoeus::Request.new("https://example.com")

request.on_body do |chunk, response|
  response.body << chunk
end

request.run
```

This change initializes the Typhoeus response body to a non-frozen,
mutable string when using the `on_body` callback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant