Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial version of callbacks behaviour. No unit spec for callback reg…

…istry.
  • Loading branch information...
commit 9b30876eb18a03fd4317e5d9dc0e1d9453e97e61 1 parent f3820ef
Bartosz Blimke authored
View
1  lib/webmock.rb
@@ -20,5 +20,6 @@
require 'webmock/request_execution_verifier'
require 'webmock/config'
+require 'webmock/callback_registry'
require 'webmock/request_registry'
require 'webmock/webmock'
View
31 lib/webmock/callback_registry.rb
@@ -0,0 +1,31 @@
+module WebMock
+ class CallbackRegistry
+ @@callbacks = []
+
+ def self.add_callback(options, block)
+ @@callbacks << {:options => options, :block => block}
+ end
+
+ def self.callbacks
+ @@callbacks
+ end
+
+ def self.invoke_callbacks(options, request_signature)
+ return if @@callbacks.empty?
+ CallbackRegistry.callbacks.each do |callback|
+ except = callback[:options][:except]
+ real_only = callback[:options][:real_requests_only]
+ unless except && except.include?(options[:lib])
+ if !real_only || options[:real_request]
+ callback[:block].call(request_signature)
+ end
+ end
+ end
+ end
+
+ def self.reset
+ @@callbacks = []
+ end
+
+ end
+end
View
10 lib/webmock/http_lib_adapters/httpclient.rb
@@ -18,13 +18,19 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
if WebMock.registered_request?(request_signature)
webmock_response = WebMock.response_for_request(request_signature)
response = build_httpclient_response(webmock_response, stream, &block)
- conn.push(response)
+ res = conn.push(response)
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :http_client}, request_signature)
+ res
elsif WebMock.net_connect_allowed?(request_signature.uri)
- if stream
+ res = if stream
do_get_stream_without_webmock(req, proxy, conn, &block)
else
do_get_block_without_webmock(req, proxy, conn, &block)
end
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :http_client, :real_request => true}, request_signature)
+ res
else
raise NetConnectNotAllowedError.new(request_signature)
end
View
7 lib/webmock/http_lib_adapters/net_http.rb
@@ -60,10 +60,15 @@ def request_with_webmock(request, body = nil, &block)
if WebMock.registered_request?(request_signature)
@socket = Net::HTTP.socket_type.new
webmock_response = WebMock.response_for_request(request_signature)
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :net_http}, request_signature)
build_net_http_response(webmock_response, &block)
elsif WebMock.net_connect_allowed?(request_signature.uri)
connect_without_webmock
- request_without_webmock(request, nil, &block)
+ response = request_without_webmock(request, nil, &block)
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :net_http, :real_request => true}, request_signature)
+ response
else
raise WebMock::NetConnectNotAllowedError.new(request_signature)
end
View
10 lib/webmock/http_lib_adapters/patron.rb
@@ -11,9 +11,15 @@ def handle_request_with_webmock(req)
if WebMock.registered_request?(request_signature)
webmock_response = WebMock.response_for_request(request_signature)
handle_file_name(req, webmock_response)
- build_patron_response(webmock_response)
+ res = build_patron_response(webmock_response)
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :patron}, request_signature)
+ res
elsif WebMock.net_connect_allowed?(request_signature.uri)
- handle_request_without_webmock(req)
+ res = handle_request_without_webmock(req)
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :patron, :real_request => true}, request_signature)
+ res
else
raise WebMock::NetConnectNotAllowedError.new(request_signature)
end
View
5 lib/webmock/webmock.rb
@@ -57,10 +57,15 @@ def response_for_request(request_signature, &block)
def reset_webmock
WebMock::RequestRegistry.instance.reset_webmock
+ WebMock::CallbackRegistry.reset
end
def assertion_failure(message)
raise message
end
+
+ def after_request(options={}, &block)
+ CallbackRegistry.add_callback(options, block)
+ end
end
View
4 spec/httpclient_spec_helper.rb
@@ -75,5 +75,9 @@ def setup_expectations_for_real_request(options = {})
socket.should_receive(:readpartial).any_number_of_times.and_raise(EOFError)
end
+
+ def http_library
+ :http_client
+ end
end
View
4 spec/net_http_spec_helper.rb
@@ -79,4 +79,8 @@ def setup_expectations_for_real_request(options = {})
socket.should_receive(read_method).once.and_return("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}")
socket.should_receive(read_method).any_number_of_times.and_raise(EOFError)
end
+
+ def http_library
+ :net_http
+ end
end
View
4 spec/patron_spec_helper.rb
@@ -47,4 +47,8 @@ def setup_expectations_for_real_request(options = {})
#TODO
end
+ def http_library
+ :patron
+ end
+
end
View
75 spec/webmock_spec.rb
@@ -1137,4 +1137,79 @@ def call(request)
end
+
+ describe "callbacks" do
+
+ describe "after_request" do
+ before(:each) do
+ 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
+
+ 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" 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" do
+ WebMock.after_request { @called = 1 }
+ WebMock.reset_webmock
+ stub_request(:get, "http://www.example.com")
+ http_request(:get, "http://www.example.com/")
+ @called.should == nil
+ end
+
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.