Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Split RequestRegistry into RequestRegistry and StubRegistry as they h…

…ave separate responsibilities
  • Loading branch information...
commit ace7156468e2dea740d3d7939d2932dd970255eb 1 parent 2595197
@bblimke authored
View
1  lib/webmock.rb
@@ -31,5 +31,6 @@
require 'webmock/config'
require 'webmock/callback_registry'
require 'webmock/request_registry'
+require 'webmock/stub_registry'
require 'webmock/api'
require 'webmock/webmock'
View
2  lib/webmock/api.rb
@@ -3,7 +3,7 @@ module API
extend self
def stub_request(method, uri)
- WebMock::RequestRegistry.instance.register_request_stub(WebMock::RequestStub.new(method, uri))
+ WebMock::StubRegistry.instance.register_request_stub(WebMock::RequestStub.new(method, uri))
end
alias_method :stub_http_request, :stub_request
View
4 lib/webmock/http_lib_adapters/curb.rb
@@ -6,8 +6,8 @@ def curb_or_webmock
request_signature = build_request_signature
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
- if WebMock::RequestRegistry.instance.registered_request?(request_signature)
- webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
+ if WebMock::StubRegistry.instance.registered_request?(request_signature)
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
build_curb_response(webmock_response)
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :curb}, request_signature, webmock_response)
View
4 lib/webmock/http_lib_adapters/em_http_request.rb
@@ -32,8 +32,8 @@ def send_request_with_webmock(&block)
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
- if WebMock::RequestRegistry.instance.registered_request?(request_signature)
- webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
+ if WebMock::StubRegistry.instance.registered_request?(request_signature)
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :em_http_request}, request_signature, webmock_response)
client = WebMockFakeHttpClient.new(nil)
View
6 lib/webmock/http_lib_adapters/httpclient.rb
@@ -15,8 +15,8 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
- if WebMock::RequestRegistry.instance.registered_request?(request_signature)
- webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
+ if WebMock::StubRegistry.instance.registered_request?(request_signature)
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
response = build_httpclient_response(webmock_response, stream, &block)
res = conn.push(response)
WebMock::CallbackRegistry.invoke_callbacks(
@@ -46,7 +46,7 @@ def do_request_async_with_webmock(method, uri, query, body, extheader)
req = create_request(method, uri, query, body, extheader)
request_signature = build_request_signature(req)
- if WebMock::RequestRegistry.instance.registered_request?(request_signature) ||
+ if WebMock::StubRegistry.instance.registered_request?(request_signature) ||
WebMock.net_connect_allowed?(request_signature.uri)
do_request_async_without_webmock(method, uri, query, body, extheader)
else
View
4 lib/webmock/http_lib_adapters/net_http.rb
@@ -51,9 +51,9 @@ def request_with_webmock(request, body = nil, &block)
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
- if WebMock::RequestRegistry.instance.registered_request?(request_signature)
+ if WebMock::StubRegistry.instance.registered_request?(request_signature)
@socket = Net::HTTP.socket_type.new
- webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :net_http}, request_signature, webmock_response)
build_net_http_response(webmock_response, &block)
View
4 lib/webmock/http_lib_adapters/patron.rb
@@ -8,8 +8,8 @@ def handle_request_with_webmock(req)
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
- if WebMock::RequestRegistry.instance.registered_request?(request_signature)
- webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
+ if WebMock::StubRegistry.instance.registered_request?(request_signature)
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
handle_file_name(req, webmock_response)
res = build_patron_response(webmock_response)
WebMock::CallbackRegistry.invoke_callbacks(
View
29 lib/webmock/request_registry.rb
@@ -3,48 +3,21 @@ module WebMock
class RequestRegistry
include Singleton
- attr_accessor :request_stubs, :requested_signatures
+ attr_accessor :requested_signatures
def initialize
reset!
end
def reset!
- self.request_stubs = []
self.requested_signatures = Util::HashCounter.new
end
- def register_request_stub(stub)
- request_stubs.insert(0, stub)
- stub
- end
-
- def registered_request?(request_signature)
- request_stub_for(request_signature)
- end
-
- def response_for_request(request_signature)
- stub = request_stub_for(request_signature)
- stub ? evaluate_response_for_request(stub.response, request_signature) : nil
- end
-
def times_executed(request_pattern)
self.requested_signatures.hash.select { |request_signature, times_executed|
request_pattern.matches?(request_signature)
}.inject(0) {|sum, (_, times_executed)| sum + times_executed }
end
- private
-
- def request_stub_for(request_signature)
- request_stubs.detect { |registered_request_stub|
- registered_request_stub.request_pattern.matches?(request_signature)
- }
- end
-
- def evaluate_response_for_request(response, request_signature)
- response.evaluate(request_signature)
- end
-
end
end
View
43 lib/webmock/stub_registry.rb
@@ -0,0 +1,43 @@
+module WebMock
+
+ class StubRegistry
+ include Singleton
+
+ attr_accessor :request_stubs
+
+ def initialize
+ reset!
+ end
+
+ def reset!
+ self.request_stubs = []
+ end
+
+ def register_request_stub(stub)
+ request_stubs.insert(0, stub)
+ stub
+ end
+
+ def registered_request?(request_signature)
+ request_stub_for(request_signature)
+ end
+
+ def response_for_request(request_signature)
+ stub = request_stub_for(request_signature)
+ stub ? evaluate_response_for_request(stub.response, request_signature) : nil
+ end
+
+ private
+
+ def request_stub_for(request_signature)
+ request_stubs.detect { |registered_request_stub|
+ registered_request_stub.request_pattern.matches?(request_signature)
+ }
+ end
+
+ def evaluate_response_for_request(response, request_signature)
+ response.evaluate(request_signature)
+ end
+
+ end
+end
View
5 lib/webmock/webmock.rb
@@ -50,6 +50,7 @@ def self.net_connect_allowed?(uri = nil)
def self.reset!
WebMock::RequestRegistry.instance.reset!
+ WebMock::StubRegistry.instance.reset!
end
def self.reset_webmock
@@ -62,11 +63,11 @@ def self.reset_callbacks
end
def self.after_request(options={}, &block)
- CallbackRegistry.add_callback(options, block)
+ WebMock::CallbackRegistry.add_callback(options, block)
end
def self.registered_request?(request_signature)
- RequestRegistry.instance.registered_request?(request_signature)
+ WebMock::StubRegistry.instance.registered_request?(request_signature)
end
%w(
View
2  spec/curb_spec.rb
@@ -21,7 +21,7 @@
describe "Curb features" do
before(:each) do
WebMock.disable_net_connect!
- WebMock::RequestRegistry.instance.reset!
+ WebMock.reset!
end
describe "callbacks" do
View
78 spec/request_registry_spec.rb
@@ -6,21 +6,13 @@
WebMock::RequestRegistry.instance.reset!
@request_pattern = WebMock::RequestPattern.new(:get, "www.example.com")
@request_signature = WebMock::RequestSignature.new(:get, "www.example.com")
- @request_stub = WebMock::RequestStub.new(:get, "www.example.com")
end
describe "reset!" do
before(:each) do
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub)
WebMock::RequestRegistry.instance.requested_signatures.put(@request_signature)
end
- it "should clean request stubs" do
- WebMock::RequestRegistry.instance.registered_request?(@request_signature).should == @request_stub
- WebMock::RequestRegistry.instance.reset!
- WebMock::RequestRegistry.instance.registered_request?(@request_signature).should == nil
- end
-
it "should clean list of executed requests" do
WebMock::RequestRegistry.instance.times_executed(@request_pattern).should == 1
WebMock::RequestRegistry.instance.reset!
@@ -29,76 +21,8 @@
end
- describe "registering and reporting registered requests" do
-
- it "should return registered stub" do
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub).should == @request_stub
- end
-
- it "should report if request stub is not registered" do
- WebMock::RequestRegistry.instance.registered_request?(@request_signature).should == nil
- end
-
- it "should register and report registered stib" do
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub)
- WebMock::RequestRegistry.instance.registered_request?(@request_signature).should == @request_stub
- end
-
-
- end
-
- describe "response for request" do
-
- it "should report registered evaluated response for request pattern" do
- @request_stub.to_return(:body => "abc")
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub)
- WebMock::RequestRegistry.instance.response_for_request(@request_signature).
- should == WebMock::Response.new(:body => "abc")
- end
-
- it "should report evaluated response" do
- @request_stub.to_return {|request| {:body => request.method.to_s} }
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub)
- response1 = WebMock::RequestRegistry.instance.response_for_request(@request_signature)
- response1.should == WebMock::Response.new(:body => "get")
- end
-
- it "should report clone of theresponse" do
- @request_stub.to_return {|request| {:body => request.method.to_s} }
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub)
- response1 = WebMock::RequestRegistry.instance.response_for_request(@request_signature)
- response2 = WebMock::RequestRegistry.instance.response_for_request(@request_signature)
- response1.should_not be(response2)
- end
-
- it "should report nothing if no response for request is registered" do
- WebMock::RequestRegistry.instance.response_for_request(@request_signature).should == nil
- end
-
- it "should always return last registered matching response" do
- @request_stub1 = WebMock::RequestStub.new(:get, "www.example.com")
- @request_stub1.to_return(:body => "abc")
- @request_stub2 = WebMock::RequestStub.new(:get, "www.example.com")
- @request_stub2.to_return(:body => "def")
- @request_stub3 = WebMock::RequestStub.new(:get, "www.example.org")
- @request_stub3.to_return(:body => "ghj")
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub1)
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub2)
- WebMock::RequestRegistry.instance.register_request_stub(@request_stub3)
- WebMock::RequestRegistry.instance.response_for_request(@request_signature).
- should == WebMock::Response.new(:body => "def")
- end
-
- end
-
describe "times executed" do
- def times_executed(request_pattern)
- self.requested.hash.select { |executed_request_pattern, times_executed|
- executed_request_pattern.match(request_pattern)
- }.inject(0) {|sum, (_, times_executed)| sum =+ times_executed }
- end
-
before(:each) do
@request_stub1 = WebMock::RequestStub.new(:get, "www.example.com")
@request_stub2 = WebMock::RequestStub.new(:get, "www.example.net")
@@ -117,7 +41,7 @@ def times_executed(request_pattern)
end
it "should report number of times all matching pattern were requested" do
- WebMock::RequestRegistry.instance.times_executed(WebMock::RequestPattern.new(:get, /.*example.*/)).should == 3
+ WebMock::RequestRegistry.instance.times_executed(WebMock::RequestPattern.new(:get, /.*example.*/)).should == 3
end
View
86 spec/stub_registry_spec.rb
@@ -0,0 +1,86 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe WebMock::StubRegistry do
+
+ before(:each) do
+ WebMock::StubRegistry.instance.reset!
+ @request_pattern = WebMock::RequestPattern.new(:get, "www.example.com")
+ @request_signature = WebMock::RequestSignature.new(:get, "www.example.com")
+ @request_stub = WebMock::RequestStub.new(:get, "www.example.com")
+ end
+
+ describe "reset!" do
+ before(:each) do
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
+ end
+
+ it "should clean request stubs" do
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == @request_stub
+ WebMock::StubRegistry.instance.reset!
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == nil
+ end
+ end
+
+ describe "registering and reporting registered requests" do
+
+ it "should return registered stub" do
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub).should == @request_stub
+ end
+
+ it "should report if request stub is not registered" do
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == nil
+ end
+
+ it "should register and report registered stib" do
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == @request_stub
+ end
+
+
+ end
+
+ describe "response for request" do
+
+ it "should report registered evaluated response for request pattern" do
+ @request_stub.to_return(:body => "abc")
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
+ WebMock::StubRegistry.instance.response_for_request(@request_signature).
+ should == WebMock::Response.new(:body => "abc")
+ end
+
+ it "should report evaluated response" do
+ @request_stub.to_return {|request| {:body => request.method.to_s} }
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
+ response1 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
+ response1.should == WebMock::Response.new(:body => "get")
+ end
+
+ it "should report clone of theresponse" do
+ @request_stub.to_return {|request| {:body => request.method.to_s} }
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
+ response1 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
+ response2 = WebMock::StubRegistry.instance.response_for_request(@request_signature)
+ response1.should_not be(response2)
+ end
+
+ it "should report nothing if no response for request is registered" do
+ WebMock::StubRegistry.instance.response_for_request(@request_signature).should == nil
+ end
+
+ it "should always return last registered matching response" do
+ @request_stub1 = WebMock::RequestStub.new(:get, "www.example.com")
+ @request_stub1.to_return(:body => "abc")
+ @request_stub2 = WebMock::RequestStub.new(:get, "www.example.com")
+ @request_stub2.to_return(:body => "def")
+ @request_stub3 = WebMock::RequestStub.new(:get, "www.example.org")
+ @request_stub3.to_return(:body => "ghj")
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub1)
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub2)
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub3)
+ WebMock::StubRegistry.instance.response_for_request(@request_signature).
+ should == WebMock::Response.new(:body => "def")
+ end
+
+ end
+
+end
View
2  spec/webmock_shared.rb
@@ -21,7 +21,7 @@ class MyException < StandardError; end;
shared_examples_for "WebMock" do
before(:each) do
WebMock.disable_net_connect!
- WebMock::RequestRegistry.instance.reset!
+ WebMock.reset!
end
describe "when web connect" do
Please sign in to comment.
Something went wrong with that request. Please try again.