Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Stubs for Typhoeus::Request#on_body #349

Merged
merged 4 commits into from

2 participants

Matt Burke Bartosz Blimke
Matt Burke

Typhoeus 0.6.7 includes support for streaming. This branch adds support for mocking streamed typhoeus requests. For example, it makes it possible to use wemock to do the stubbing for this code:

request = Typhoeus::Request.new('https://github.com')
request.on_body { |chunk| p chunk }
request.run

cc typhoeus/typhoeus#339

Bartosz Blimke
Owner

Cheers @spraints It looks good.

Bartosz Blimke bblimke merged commit e89d0ec into from
Matt Burke spraints deleted the branch
Matt Burke spraints restored the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7 lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb
View
@@ -143,6 +143,13 @@ def self.request_hash(request_signature)
if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature)
# ::WebMock::HttpLibAdapters::TyphoeusAdapter.stub_typhoeus(request_signature, webmock_response, self)
response = ::WebMock::HttpLibAdapters::TyphoeusAdapter.generate_typhoeus_response(request_signature, webmock_response)
+ if request.respond_to?(:on_headers)
+ request.execute_headers_callbacks(response)
+ end
+ if request.respond_to?(:streaming?) && request.streaming?
+ response.options[:response_body] = ""
+ request.on_body.each { |callback| callback.call(webmock_response.body, response) }
+ end
request.finish(response)
webmock_response.raise_error_if_any
res = false
33 spec/acceptance/typhoeus/typhoeus_hydra_spec.rb
View
@@ -78,6 +78,39 @@
hydra.run
test.should == response_code
end
+
+ it "should call on_body with 2xx response" do
+ body = "on_body fired"
+ stub_request(:any, "example.com").to_return(:body => body)
+
+ test_body = nil
+ test_complete = nil
+ pending("This test requires a newer version of Typhoeus") unless @request.respond_to?(:on_body)
+ @request.on_body do |body, response|
+ test_body = body
+ end
+ @request.on_complete do |response|
+ test_complete = response.body
+ end
+ hydra.queue @request
+ hydra.run
+ test_body.should == body
+ test_complete.should == ""
+ end
+
+ it "should call on_headers with 2xx response" do
+ body = "on_headers fired"
+ stub_request(:any, "example.com").to_return(:body => body, :headers => {'X-Test' => '1'})
+
+ test_headers = nil
+ pending("This test requires a newer version of Typhoeus") unless @request.respond_to?(:on_headers)
+ @request.on_headers do |response|
+ test_headers = response.headers
+ end
+ hydra.queue @request
+ hydra.run
+ test_headers.should include('X-Test' => '1')
+ end
end
end
end
Something went wrong with that request. Please try again.