Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Add support for :expects option in Excon adapter #248

merged 1 commit into from

3 participants


Excon provides :expects option allowing to set expected HTTP status on response, if the expectation is not met - exception is raised. Since excon adapter redefines request_kernel this functionality is lost


(Just wrote this exact patch before checking to see if anyone else had already done it.)

Excon's :expects can be fairly critical to program flow in that it allows the standard logical path to run normally and any unexpected variations to be raised as errors so they can be handled elsewhere, e.g.

begin"/oauth/authorizations", expects: 201)
  session["token"] ="/oauth/tokens", expects: 201)
rescue Excon::Errors::Forbidden
  redirect to("/login")

Webmock dropping this functionality threw me through a bit of a loop before I realized what was going on. Any thoughts on this pull?

@bblimke bblimke merged commit 4ce654b into bblimke:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2013
  1. Add support for :expects option in Excon adapter

    Evgeniy Dolzhenko authored
This page is out of date. Refresh to see the latest.
9 lib/webmock/http_lib_adapters/excon_adapter.rb
@@ -87,7 +87,14 @@ def request_kernel(params, &block)
if mock_response = WebMock::StubRegistry.instance.response_for_request(mock_request)
ExconAdapter.perform_callbacks(mock_request, mock_response, :real_request => false)
- ExconAdapter.real_response(mock_response)
+ response = ExconAdapter.real_response(mock_response)
+ if params.has_key?(:expects) && ![*params[:expects]].include?(response.status)
+ raise(Excon::Errors.status_error(params, response))
+ else
+ response
+ end
elsif WebMock.net_connect_allowed?(mock_request.uri)
real_response = super
ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response), :real_request => true)
6 spec/acceptance/excon/excon_spec.rb
@@ -11,6 +11,11 @@
Excon.get('', :path => "resource/", :query => {:a => 1, :b => 2}).body.should == "abc"
+ it 'should support Excon :expects options' do
+ stub_request(:get, "").to_return(:body => 'a')
+ lambda { Excon.get('', :expects => 204) }.should raise_error(Excon::Errors::OK)
+ end
let(:file) { }
let(:file_contents) { }
@@ -27,4 +32,3 @@
yielded_request_body.should eq(file_contents)
Something went wrong with that request. Please try again.