Skip to content

Commit

Permalink
Merge pull request #415 from typhoeus/stubbing_headers
Browse files Browse the repository at this point in the history
call on_headers and on_body when stubbed.
  • Loading branch information
hanshasselberg committed Nov 11, 2014
2 parents 04c6f10 + 107ccac commit b548985
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/typhoeus/hydra/stubbable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module Stubbable
# hydra.add(request)
def add(request)
if response = Expectation.response_for(request)
request.execute_headers_callbacks(response)
request.on_body.each{ |callback| callback.call(response.body, response) }
request.finish(response)
else
super
Expand Down
2 changes: 2 additions & 0 deletions lib/typhoeus/request/stubbable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Stubbable
# @return [ Response ] The response.
def run
if response = Expectation.response_for(self)
execute_headers_callbacks(response)
self.on_body.each{ |callback| callback.call(response.body, response) }
finish(response)
else
super
Expand Down
20 changes: 20 additions & 0 deletions spec/typhoeus/hydra/stubbable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@
end

context "when expectation found" do
it "calls on_headers callbacks" do
canary = :not_called
request.on_headers do
canary = :called
end
hydra.add(request)
hydra.run
expect(canary).to eq(:called)
end

it "calls on_body callbacks" do
canary = :not_called
request.on_body do
canary = :called
end
hydra.add(request)
hydra.run
expect(canary).to eq(:called)
end

it "finishes response" do
expect(request).to receive(:finish)
hydra.add(request)
Expand Down
18 changes: 18 additions & 0 deletions spec/typhoeus/request/stubbable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
end

context "when expectation found" do
it "calls on_headers callbacks" do
canary = :not_called
request.on_headers do
canary = :called
end
request.run
expect(canary).to eq(:called)
end

it "calls on_body callbacks" do
canary = :not_called
request.on_body do
canary = :called
end
request.run
expect(canary).to eq(:called)
end

it "finishes request" do
expect(request).to receive(:finish)
request.run
Expand Down

0 comments on commit b548985

Please sign in to comment.