Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for :expects option in Excon adapter #248

Merged
merged 1 commit into from

3 participants

@dolzenko

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 https://github.com/geemus/excon/blob/master/lib/excon/connection.rb#L307

@brandur

(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
  conn.post("/oauth/authorizations", expects: 201)
  session["token"] = conn.post("/oauth/tokens", expects: 201)
rescue Excon::Errors::Forbidden
  redirect to("/login")
end

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.
View
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)
View
6 spec/acceptance/excon/excon_spec.rb
@@ -11,6 +11,11 @@
Excon.get('http://example.com', :path => "resource/", :query => {:a => 1, :b => 2}).body.should == "abc"
end
+ it 'should support Excon :expects options' do
+ stub_request(:get, "http://example.com/").to_return(:body => 'a')
+ lambda { Excon.get('http://example.com', :expects => 204) }.should raise_error(Excon::Errors::OK)
+ end
+
let(:file) { File.new(__FILE__) }
let(:file_contents) { File.new(__FILE__).read }
@@ -27,4 +32,3 @@
yielded_request_body.should eq(file_contents)
end
end
-
Something went wrong with that request. Please try again.