webmock 1.3.5 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,8 +30,8 @@ if defined?(EventMachine::HttpRequest)
30
30
 
31
31
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
32
32
 
33
- if WebMock.registered_request?(request_signature)
34
- webmock_response = WebMock.response_for_request(request_signature)
33
+ if WebMock::RequestRegistry.instance.registered_request?(request_signature)
34
+ webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
35
35
  WebMock::CallbackRegistry.invoke_callbacks(
36
36
  {:lib => :em_http_request}, request_signature, webmock_response)
37
37
  client = WebMockFakeHttpClient.new(nil)
@@ -15,8 +15,8 @@ if defined?(::HTTPClient)
15
15
 
16
16
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
17
17
 
18
- if WebMock.registered_request?(request_signature)
19
- webmock_response = WebMock.response_for_request(request_signature)
18
+ if WebMock::RequestRegistry.instance.registered_request?(request_signature)
19
+ webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
20
20
  response = build_httpclient_response(webmock_response, stream, &block)
21
21
  res = conn.push(response)
22
22
  WebMock::CallbackRegistry.invoke_callbacks(
@@ -46,7 +46,8 @@ if defined?(::HTTPClient)
46
46
  req = create_request(method, uri, query, body, extheader)
47
47
  request_signature = build_request_signature(req)
48
48
 
49
- if WebMock.registered_request?(request_signature) || WebMock.net_connect_allowed?(request_signature.uri)
49
+ if WebMock::RequestRegistry.instance.registered_request?(request_signature) ||
50
+ WebMock.net_connect_allowed?(request_signature.uri)
50
51
  do_request_async_without_webmock(method, uri, query, body, extheader)
51
52
  else
52
53
  raise WebMock::NetConnectNotAllowedError.new(request_signature)
@@ -51,9 +51,9 @@ module Net #:nodoc: all
51
51
 
52
52
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
53
53
 
54
- if WebMock.registered_request?(request_signature)
54
+ if WebMock::RequestRegistry.instance.registered_request?(request_signature)
55
55
  @socket = Net::HTTP.socket_type.new
56
- webmock_response = WebMock.response_for_request(request_signature)
56
+ webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
57
57
  WebMock::CallbackRegistry.invoke_callbacks(
58
58
  {:lib => :net_http}, request_signature, webmock_response)
59
59
  build_net_http_response(webmock_response, &block)
@@ -14,36 +14,18 @@
14
14
  module WebMock
15
15
  module Net
16
16
  module HTTPResponse
17
- def self.extended(object)
18
- body_object = object.instance_variable_get(:@body)
19
- object.instance_variable_set(:@__orig_body__,
20
- case body_object
21
- when String then body_object
22
- when nil then nil
23
- else raise ArgumentError.new("Unexpected body object: #{body_object}")
24
- end
25
- )
26
- end
27
-
28
17
  def read_body(dest = nil, &block)
29
- if @__orig_body__
30
- if dest && block
31
- raise ArgumentError.new("both arg and block given for HTTP method")
32
- elsif dest
33
- dest << @__orig_body__
34
- elsif block
35
- @body = ::Net::ReadAdapter.new(block)
36
- @body << @__orig_body__
37
- @body
38
- else
39
- @body = @__orig_body__
40
- end
41
- else
42
- super
43
- end
18
+ return super if @__read_body_previously_called
19
+ return @body if dest.nil? && block.nil?
20
+ raise ArgumentError.new("both arg and block given for HTTP method") if dest && block
21
+ return nil if @body.nil?
22
+
23
+ dest ||= ::Net::ReadAdapter.new(block)
24
+ dest << @body
25
+ @body = dest
44
26
  ensure
45
27
  # allow subsequent calls to #read_body to proceed as normal, without our hack...
46
- @__orig_body__ = nil
28
+ @__read_body_previously_called = true
47
29
  end
48
30
  end
49
31
  end
@@ -8,8 +8,8 @@ if defined?(Patron)
8
8
 
9
9
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
10
10
 
11
- if WebMock.registered_request?(request_signature)
12
- webmock_response = WebMock.response_for_request(request_signature)
11
+ if WebMock::RequestRegistry.instance.registered_request?(request_signature)
12
+ webmock_response = WebMock::RequestRegistry.instance.response_for_request(request_signature)
13
13
  handle_file_name(req, webmock_response)
14
14
  res = build_patron_response(webmock_response)
15
15
  WebMock::CallbackRegistry.invoke_callbacks(
@@ -66,6 +66,11 @@ module WebMock
66
66
  Addressable::URI.encode_component(userinfo, Addressable::URI::CharacterClasses::USERINFO)
67
67
  end
68
68
 
69
+ def self.is_uri_localhost?(uri)
70
+ uri.is_a?(Addressable::URI) &&
71
+ %w(localhost 127.0.0.1 0.0.0.0).include?(uri.host)
72
+ end
73
+
69
74
  private
70
75
 
71
76
  def self.sort_query_values(query_values)
@@ -1,76 +1,82 @@
1
1
  module WebMock
2
- extend self
3
2
 
4
- def self.version
5
- open(File.join(File.dirname(__FILE__), '../../VERSION')) { |f|
6
- f.read.strip
7
- }
8
- end
9
-
10
- def stub_request(method, uri)
11
- RequestRegistry.instance.register_request_stub(RequestStub.new(method, uri))
3
+ def self.included(clazz)
4
+ WebMock::Deprecation.warning("include WebMock is deprecated. Please include WebMock::API instead")
5
+ if clazz.instance_methods.map(&:to_s).include?('request')
6
+ warn "WebMock#request was not included in #{clazz} to avoid name collision"
7
+ else
8
+ clazz.class_eval do
9
+ def request(method, uri)
10
+ WebMock::Deprecation.warning("WebMock#request is deprecated. Please use WebMock::API#a_request method instead")
11
+ WebMock.a_request(method, uri)
12
+ end
13
+ end
14
+ end
12
15
  end
13
16
 
14
- alias_method :stub_http_request, :stub_request
15
-
16
- def request(method, uri)
17
- RequestPattern.new(method, uri)
18
- end
17
+ include WebMock::API
18
+ extend WebMock::API
19
19
 
20
- def assert_requested(method, uri, options = {}, &block)
21
- expected_times_executed = options.delete(:times) || 1
22
- request = RequestPattern.new(method, uri, options).with(&block)
23
- verifier = RequestExecutionVerifier.new(request, expected_times_executed)
24
- assertion_failure(verifier.failure_message) unless verifier.matches?
20
+ class << self
21
+ alias :request :a_request
25
22
  end
26
23
 
27
- def assert_not_requested(method, uri, options = {}, &block)
28
- request = RequestPattern.new(method, uri, options).with(&block)
29
- verifier = RequestExecutionVerifier.new(request, options.delete(:times))
30
- assertion_failure(verifier.negative_failure_message) unless verifier.does_not_match?
24
+ def self.version
25
+ open(File.join(File.dirname(__FILE__), '../../VERSION')) { |f|
26
+ f.read.strip
27
+ }
31
28
  end
32
29
 
33
- def allow_net_connect!
30
+ def self.allow_net_connect!
34
31
  Config.instance.allow_net_connect = true
35
32
  end
36
33
 
37
- def disable_net_connect!(options = {})
34
+ def self.disable_net_connect!(options = {})
38
35
  Config.instance.allow_net_connect = false
39
36
  Config.instance.allow_localhost = options[:allow_localhost]
40
37
  Config.instance.allow = options[:allow]
41
38
  end
42
39
 
43
- def net_connect_allowed?(uri = nil)
40
+ def self.net_connect_allowed?(uri = nil)
44
41
  if uri.is_a?(String)
45
42
  uri = WebMock::Util::URI.normalize_uri(uri)
46
43
  end
47
44
  Config.instance.allow_net_connect ||
48
- (Config.instance.allow_localhost && uri.is_a?(Addressable::URI) && (uri.host == 'localhost' || uri.host == '127.0.0.1')) ||
45
+ (Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri)) ||
49
46
  Config.instance.allow && Config.instance.allow.include?(uri.host)
50
47
  end
51
48
 
52
- def registered_request?(request_signature)
53
- RequestRegistry.instance.registered_request?(request_signature)
49
+ def self.reset_webmock
50
+ WebMock::RequestRegistry.instance.reset_webmock
54
51
  end
55
52
 
56
- def response_for_request(request_signature, &block)
57
- RequestRegistry.instance.response_for_request(request_signature, &block)
53
+ def self.reset_callbacks
54
+ WebMock::CallbackRegistry.reset
58
55
  end
59
56
 
60
- def reset_webmock
61
- WebMock::RequestRegistry.instance.reset_webmock
57
+ def self.after_request(options={}, &block)
58
+ CallbackRegistry.add_callback(options, block)
62
59
  end
63
60
 
64
- def reset_callbacks
65
- WebMock::CallbackRegistry.reset
61
+ def self.registered_request?(request_signature)
62
+ RequestRegistry.instance.registered_request?(request_signature)
66
63
  end
67
64
 
68
- def assertion_failure(message)
69
- raise message
70
- end
71
-
72
- def after_request(options={}, &block)
73
- CallbackRegistry.add_callback(options, block)
65
+ %w(
66
+ allow_net_connect!
67
+ disable_net_connect!
68
+ net_connect_allowed?
69
+ reset_webmock
70
+ reset_callbacks
71
+ after_request
72
+ registered_request?
73
+ ).each do |method|
74
+ self.class_eval(%Q(
75
+ def #{method}(*args, &block)
76
+ WebMock::Deprecation.warning("WebMock##{method} instance method is deprecated. Please use WebMock.#{method} class method instead")
77
+ WebMock.#{method}(*args, &block)
78
+ end
79
+ ))
74
80
  end
75
81
 
76
82
  end
@@ -0,0 +1,173 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'webmock_spec'
3
+
4
+ unless RUBY_PLATFORM =~ /java/
5
+ require 'curb_spec_helper'
6
+
7
+ describe "Curb", :shared => true do
8
+ include CurbSpecHelper
9
+
10
+ it_should_behave_like "WebMock"
11
+
12
+ describe "when doing PUTs" do
13
+ it "should stub them" do
14
+ stub_http_request(:put, "www.example.com").with(:body => "01234")
15
+ http_request(:put, "http://www.example.com", :body => "01234").
16
+ status.should == "200"
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "Curb features" do
22
+ before(:each) do
23
+ WebMock.disable_net_connect!
24
+ WebMock::RequestRegistry.instance.reset_webmock
25
+ end
26
+
27
+ describe "callbacks" do
28
+ before(:each) do
29
+ @curl = Curl::Easy.new
30
+ @curl.url = "http://example.com"
31
+ end
32
+
33
+ it "should call on_success with 2xx response" do
34
+ body = "on_success fired"
35
+ stub_request(:any, "example.com").to_return(:body => body)
36
+
37
+ test = nil
38
+ @curl.on_success do |c|
39
+ test = c.body_str
40
+ end
41
+ @curl.http_get
42
+ test.should == body
43
+ end
44
+
45
+ it "should call on_failure with 5xx response" do
46
+ response_code = 599
47
+ stub_request(:any, "example.com").
48
+ to_return(:status => [response_code, "Server On Fire"])
49
+
50
+ test = nil
51
+ @curl.on_failure do |c, code|
52
+ test = code
53
+ end
54
+ @curl.http_get
55
+ test.should == response_code
56
+ end
57
+
58
+ it "should call on_body when response body is read" do
59
+ body = "on_body fired"
60
+ stub_request(:any, "example.com").
61
+ to_return(:body => body)
62
+
63
+ test = nil
64
+ @curl.on_body do |data|
65
+ test = data
66
+ end
67
+ @curl.http_get
68
+ test.should == body
69
+ end
70
+
71
+ it "should call on_header when response headers are read" do
72
+ stub_request(:any, "example.com").
73
+ to_return(:headers => {:one => 1})
74
+
75
+ test = nil
76
+ @curl.on_header do |data|
77
+ test = data
78
+ end
79
+ @curl.http_get
80
+ test.should match /One: 1/
81
+ end
82
+
83
+ it "should call on_complete when request is complete" do
84
+ body = "on_complete fired"
85
+ stub_request(:any, "example.com").to_return(:body => body)
86
+
87
+ test = nil
88
+ @curl.on_complete do |curl|
89
+ test = curl.body_str
90
+ end
91
+ @curl.http_get
92
+ test.should == body
93
+ end
94
+
95
+ it "should call on_progress when portion of response body is read" do
96
+ stub_request(:any, "example.com").to_return(:body => "01234")
97
+
98
+ test = nil
99
+ @curl.on_progress do |*args|
100
+ args.length.should == 4
101
+ args.each {|arg| arg.is_a?(Float).should == true }
102
+ test = true
103
+ end
104
+ @curl.http_get
105
+ test.should == true
106
+ end
107
+
108
+ it "should call callbacks in correct order on successful request" do
109
+ stub_request(:any, "example.com")
110
+ order = []
111
+ @curl.on_success {|*args| order << :on_success }
112
+ @curl.on_failure {|*args| order << :on_failure }
113
+ @curl.on_header {|*args| order << :on_header }
114
+ @curl.on_body {|*args| order << :on_body }
115
+ @curl.on_complete {|*args| order << :on_complete }
116
+ @curl.on_progress {|*args| order << :on_progress }
117
+ @curl.http_get
118
+
119
+ order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_success]
120
+ end
121
+
122
+ it "should call callbacks in correct order on successful request" do
123
+ stub_request(:any, "example.com").to_return(:status => [500, ""])
124
+ order = []
125
+ @curl.on_success {|*args| order << :on_success }
126
+ @curl.on_failure {|*args| order << :on_failure }
127
+ @curl.on_header {|*args| order << :on_header }
128
+ @curl.on_body {|*args| order << :on_body }
129
+ @curl.on_complete {|*args| order << :on_complete }
130
+ @curl.on_progress {|*args| order << :on_progress }
131
+ @curl.http_get
132
+
133
+ order.should == [:on_progress,:on_header,:on_body,:on_complete,:on_failure]
134
+ end
135
+ end
136
+ end
137
+
138
+ describe "Webmock with Curb" do
139
+ describe "using #http for requests" do
140
+ it_should_behave_like "Curb"
141
+ include CurbSpecHelper::DynamicHttp
142
+
143
+ it "should work with uppercase arguments" do
144
+ stub_request(:get, "www.example.com").to_return(:body => "abc")
145
+
146
+ c = Curl::Easy.new
147
+ c.url = "http://www.example.com"
148
+ c.http(:GET)
149
+ c.body_str.should == "abc"
150
+ end
151
+ end
152
+
153
+ describe "using #http_* methods for requests" do
154
+ it_should_behave_like "Curb"
155
+ include CurbSpecHelper::NamedHttp
156
+ end
157
+
158
+ describe "using #perform for requests" do
159
+ it_should_behave_like "Curb"
160
+ include CurbSpecHelper::Perform
161
+ end
162
+
163
+ describe "using .http_* methods for requests" do
164
+ it_should_behave_like "Curb"
165
+ include CurbSpecHelper::ClassNamedHttp
166
+ end
167
+
168
+ describe "using .perform for requests" do
169
+ it_should_behave_like "Curb"
170
+ include CurbSpecHelper::ClassPerform
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,137 @@
1
+ module CurbSpecHelper
2
+ def http_request(method, uri, options = {}, &block)
3
+ uri = Addressable::URI.heuristic_parse(uri)
4
+ body = options[:body]
5
+
6
+ curl = curb_http_request(uri, method, body, options)
7
+
8
+ status, response_headers = Curl::Easy::WebmockHelper.parse_header_string(curl.header_str)
9
+
10
+ OpenStruct.new(
11
+ :body => curl.body_str,
12
+ :headers => WebMock::Util::Headers.normalize_headers(response_headers),
13
+ :status => curl.response_code.to_s,
14
+ :message => status
15
+ )
16
+ end
17
+
18
+ def setup_request(uri, curl, options={})
19
+ curl ||= Curl::Easy.new
20
+ curl.url = uri.omit(:userinfo).to_s
21
+ curl.username = uri.user
22
+ curl.password = uri.password
23
+ curl.timeout = 10
24
+
25
+ if headers = options[:headers]
26
+ headers.each {|k,v| curl.headers[k] = v }
27
+ end
28
+
29
+ curl
30
+ end
31
+
32
+ def default_client_request_headers(request_method = nil, has_body = false)
33
+ nil
34
+ end
35
+
36
+ def client_timeout_exception_class
37
+ Curl::Err::TimeoutError
38
+ end
39
+
40
+ def connection_refused_exception_class
41
+ Curl::Err::ConnectionFailedError
42
+ end
43
+
44
+ def setup_expectations_for_real_request(options = {})
45
+ end
46
+
47
+ def http_library
48
+ :curb
49
+ end
50
+
51
+ module DynamicHttp
52
+ def curb_http_request(uri, method, body, options)
53
+ curl = setup_request(uri, nil, options)
54
+
55
+ case method
56
+ when :post
57
+ curl.post_body = body
58
+ when :put
59
+ curl.put_data = body
60
+ end
61
+
62
+ curl.http(method)
63
+ curl
64
+ end
65
+ end
66
+
67
+ module NamedHttp
68
+ def curb_http_request(uri, method, body, options)
69
+ curl = setup_request(uri, nil, options)
70
+
71
+ case method
72
+ when :put, :post
73
+ curl.send( "http_#{method}", body )
74
+ else
75
+ curl.send( "http_#{method}" )
76
+ end
77
+ curl
78
+ end
79
+ end
80
+
81
+ module Perform
82
+ def curb_http_request(uri, method, body, options)
83
+ curl = setup_request(uri, nil, options)
84
+
85
+ case method
86
+ when :post
87
+ curl.post_body = body
88
+ when :put
89
+ curl.put_data = body
90
+ when :head
91
+ curl.head = true
92
+ when :delete
93
+ curl.delete = true
94
+ end
95
+
96
+ curl.perform
97
+ curl
98
+ end
99
+ end
100
+
101
+ module ClassNamedHttp
102
+ def curb_http_request(uri, method, body, options)
103
+ args = ["http_#{method}", uri]
104
+ args << body if method == :post || method == :put
105
+
106
+ c = Curl::Easy.send(*args) do |curl|
107
+ setup_request(uri, curl, options)
108
+ end
109
+
110
+ c
111
+ end
112
+ end
113
+
114
+ module ClassPerform
115
+ def curb_http_request(uri, method, body, options)
116
+ args = ["http_#{method}", uri]
117
+ args << body if method == :post || method == :put
118
+
119
+ c = Curl::Easy.send(*args) do |curl|
120
+ setup_request(uri, curl, options)
121
+
122
+ case method
123
+ when :post
124
+ curl.post_body = body
125
+ when :put
126
+ curl.put_data = body
127
+ when :head
128
+ curl.head = true
129
+ when :delete
130
+ curl.delete = true
131
+ end
132
+ end
133
+
134
+ c
135
+ end
136
+ end
137
+ end