Skip to content

Commit

Permalink
Split enormous "webmock" shared examples group into separate groups i…
Browse files Browse the repository at this point in the history
…n separate files. Some of them are still enormous.
  • Loading branch information
bblimke committed Aug 28, 2011
1 parent b9fe928 commit 850c8f7
Show file tree
Hide file tree
Showing 8 changed files with 1,607 additions and 1,653 deletions.
141 changes: 141 additions & 0 deletions spec/acceptance/shared/allowing_and_disabling_net_connect.rb
@@ -0,0 +1,141 @@
shared_examples_for "allowing and disabling net connect" do

describe "is allowed", :net_connect => true do
before(:each) do
WebMock.allow_net_connect!
end

it "should make a real web request if request is not stubbed" do
http_request(:get, webmock_server_url).status.should == "200"
end

it "should make a real https request if request is not stubbed" do
unless http_library == :httpclient
http_request(:get, "https://www.paypal.com/uk/cgi-bin/webscr").
body.should =~ /.*paypal.*/
end
end

it "should return stubbed response if request was stubbed" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
http_request(:get, "http://www.example.com/").body.should == "abc"
end
end

describe "is not allowed" do
before(:each) do
WebMock.disable_net_connect!
end

it "should return stubbed response if request was stubbed" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
http_request(:get, "http://www.example.com/").body.should == "abc"
end

it "should return stubbed response if request with path was stubbed" do
stub_http_request(:get, "www.example.com/hello_world").to_return(:body => "abc")
http_request(:get, "http://www.example.com/hello_world").body.should == "abc"
end

it "should raise exception if request was not stubbed" do
lambda {
http_request(:get, "http://www.example.com/")
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end
end

describe "is not allowed with exception for localhost" do
before(:each) do
WebMock.disable_net_connect!(:allow_localhost => true)
end

it "should return stubbed response if request was stubbed" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
http_request(:get, "http://www.example.com/").body.should == "abc"
end

it "should raise exception if request was not stubbed" do
lambda {
http_request(:get, "http://www.example.com/")
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end

it "should allow a real request to localhost" do
lambda {
http_request(:get, "http://localhost:12345/")
}.should raise_error(connection_refused_exception_class)
end

it "should allow a real request to 127.0.0.1" do
lambda {
http_request(:get, "http://127.0.0.1:12345/")
}.should raise_error(connection_refused_exception_class)
end

it "should allow a real request to 0.0.0.0" do
lambda {
http_request(:get, "http://0.0.0.0:12345/")
}.should raise_error(connection_refused_exception_class)
end
end

describe "is not allowed with exception for allowed domains" do
let(:host_with_port){ WebMockServer.instance.host_with_port }

before(:each) do
WebMock.disable_net_connect!(:allow => ["www.example.org", host_with_port])
end

context "when the host is not allowed" do
it "should return stubbed response if request was stubbed" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
http_request(:get, "http://www.example.com/").body.should == "abc"
end

it "should raise exception if request was not stubbed" do
lambda {
http_request(:get, "http://www.example.com/")
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end
end

context "when the host with port is not allowed" do
it "should return stubbed response if request was stubbed" do
stub_http_request(:get, "http://localhost:2345").to_return(:body => "abc")
http_request(:get, "http://localhost:2345/").body.should == "abc"
end

it "should raise exception if request was not stubbed" do
lambda {
http_request(:get, "http://localhost:2345/")
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://localhost:2345/))
end
end

context "when the host is allowed" do
it "should raise exception if request was not stubbed" do
lambda {
http_request(:get, "http://www.example.com/")
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
end

it "should allow a real request to allowed host", :net_connect => true do
http_request(:get, "http://www.example.org/").status.should == "302"
end
end

context "when the host with port is allowed" do
it "should allow a real request to allowed host", :net_connect => true do
http_request(:get, "http://#{host_with_port}/").status.should == "200"
end
end

context "when the host is allowed but not port" do
it "should allow a real request to allowed host", :net_connect => true do
lambda {
http_request(:get, "http://localhost:123/")
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://localhost:123/))
end
end
end
end
130 changes: 130 additions & 0 deletions spec/acceptance/shared/callbacks.rb
@@ -0,0 +1,130 @@
shared_examples_for "callbacks" do
describe "after_request" do
before(:each) do
WebMock.reset_callbacks
stub_request(:get, "http://www.example.com")
end

it "should not invoke callback unless request is made" do
WebMock.after_request {
@called = true
}
@called.should == nil
end

it "should invoke a callback after request is made" do
WebMock.after_request {
@called = true
}
http_request(:get, "http://www.example.com/")
@called.should == true
end

it "should not invoke a callback if specific http library should be ignored" do
WebMock.after_request(:except => [http_library()]) {
@called = true
}
http_request(:get, "http://www.example.com/")
@called.should == nil
end

it "should invoke a callback even if other http libraries should be ignored" do
WebMock.after_request(:except => [:other_lib]) {
@called = true
}
http_request(:get, "http://www.example.com/")
@called.should == true
end

it "should pass request signature to the callback" do
WebMock.after_request(:except => [:other_lib]) do |request_signature, _|
@request_signature = request_signature
end
http_request(:get, "http://www.example.com/")
@request_signature.uri.to_s.should == "http://www.example.com:80/"
end

describe "passing response to callback" do
describe "for stubbed requests" do
before(:each) do
stub_request(:get, "http://www.example.com").
to_return(
:status => ["200", "hello"],
:headers => {'Content-Length' => '666', 'Hello' => 'World'},
:body => "foo bar"
)
WebMock.after_request(:except => [:other_lib]) do |_, response|
@response = response
end
http_request(:get, "http://www.example.com/")
end

it "should pass response with status and message" do
@response.status.should == ["200", "hello"]
end

it "should pass response with headers" do
@response.headers.should == {
'Content-Length' => '666',
'Hello' => 'World'
}
end

it "should pass response with body" do
@response.body.should == "foo bar"
end
end

describe "for real requests", :net_connect => true do
before(:each) do
WebMock.reset!
WebMock.allow_net_connect!
WebMock.after_request(:except => [:other_lib]) do |_, response|
@response = response
end
http_request(:get, "http://www.example.com/")
end

it "should pass response with status and message" do
# not supported by em-http-request, it always returns "unknown" for http_reason
unless http_library == :em_http_request
@response.status[0].should == 302
@response.status[1].should == "Found"
end
end

it "should pass response with headers" do
@response.headers["Content-Length"].should == "0"
end

it "should pass response with body" do
@response.body.size.should == 0
end
end
end

it "should invoke multiple callbacks in order of their declarations" do
WebMock.after_request { @called = 1 }
WebMock.after_request { @called += 1 }
http_request(:get, "http://www.example.com/")
@called.should == 2
end

it "should invoke callbacks only for real requests if requested", :net_connect => true do
WebMock.after_request(:real_requests_only => true) { @called = true }
http_request(:get, "http://www.example.com/")
@called.should == nil
WebMock.allow_net_connect!
http_request(:get, "http://www.example.net/")
@called.should == true
end

it "should clear all declared callbacks on reset callbacks" do
WebMock.after_request { @called = 1 }
WebMock.reset_callbacks
stub_request(:get, "http://www.example.com/")
http_request(:get, "http://www.example.com/")
@called.should == nil
end
end
end
3 changes: 0 additions & 3 deletions spec/acceptance/shared/enabling_and_disabling_webmock.rb
@@ -1,6 +1,3 @@
require 'spec_helper'


shared_examples_for "enabled and disabled webmock" do
describe "enabling and disabling webmock" do
describe "when webmock is disabled" do
Expand Down
13 changes: 13 additions & 0 deletions spec/acceptance/shared/precedence_of_stubs.rb
@@ -0,0 +1,13 @@
shared_examples_for "precedence of stubs" do
it "should use the last declared matching request stub" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
stub_http_request(:get, "www.example.com").to_return(:body => "def")
http_request(:get, "http://www.example.com/").body.should == "def"
end

it "should not be affected by the type of uri or request method" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
stub_http_request(:any, /.*example.*/).to_return(:body => "def")
http_request(:get, "http://www.example.com/").body.should == "def"
end
end

0 comments on commit 850c8f7

Please sign in to comment.