Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Adding support for lazy construction of responses from stubbed requests #275

merged 2 commits into from Mar 29, 2013


None yet
2 participants

ryankinderman commented Feb 24, 2013

See new example doc entries on Typhoeus::Expectation for how this can be used.


  • With the introduction of deferred response construction, the creation of a response for a request stub should be able to make assumptions about the request insofar as it matches the stub. For example, if the stub specifies that the request method is a GET, then the block that constructs the response should be able to assume that it's getting called for a GET request.
  • To reduce the possibility that a response construction block is called with an unmatching request, I've replaced Typhoeus::Expectation.find_by with .response_for, to provide the *::Stubbable subclasses with a single method to call to obtain a stubbed response, if there are any that match the request. This is in an effort to emphasize that an expectation's response is based on the corresponding request matching the stub specification.

i0rek commented Feb 24, 2013

Thank you very much for your work! I'll look into it either today or tomorrow!


i0rek commented Mar 1, 2013

@ryankinderman I finally have time to have a look at your work and I love it! 🚀
What do you think about doing the same like rspec does in here: https://www.relishapp.com/rspec/rspec-mocks/v/2-13/docs/message-expectations/expect-a-message#provide-a-return-value regarding the block syntax?

# or
Typhoeus.stub("www.example.com/widgets") do |request|
  Typhoeus::Response.new(body: request.url)

ryankinderman commented Mar 4, 2013

The latter is fine with me as well. If you prefer that, I can update it.


i0rek commented Mar 4, 2013

That would be awesome!


ryankinderman commented Mar 9, 2013

Okay, the solution now supports:

Typhoeus.stub(...) do |request|
  #... build response ...

I kept the functionality for adding the block to the expectation on Expectation#and_return, since that needs to happen somewhere anyways. Not sure if you want to move that to a separate method that's not part of the public API; if so, let me know, but I personally think it's fine where it is.


i0rek commented Mar 12, 2013


@i0rek i0rek added a commit that referenced this pull request Mar 29, 2013

@i0rek i0rek Merge pull request #275 from ryankinderman/master-stubbed-lazy-response
Adding support for lazy construction of responses from stubbed requests

@i0rek i0rek merged commit c53f026 into typhoeus:master Mar 29, 2013

1 check passed

default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment