webmock 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.8.1
2
+ * Fixed HTTPClient adapter compatibility with Ruby 1.8.6 (reported by Piotr Usewicz)
3
+ * Net:HTTP adapter now handles request body assigned as Net::HTTP::Post#body attribute (fixed by Mack Earnhardt)
4
+ * Fixed issue where requests were not matching stubs with Accept header set.(reported by Piotr Usewicz)
5
+ * Fixed compatibility with Ruby 1.9.1, 1.9.2 and JRuby 1.3.1 (reported by Diego E. “Flameeyes” Pettenò)
6
+ * Fixed issue with response body declared as IO object and multiple requests (reported by Niels Meersschaert)
7
+ * Fixed "undefined method `assertion_failure'" error (reported by Nick Plante)
8
+
1
9
  == 0.8.0
2
10
 
3
11
  * Support for HTTPClient (sync and async requests)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.0
1
+ 0.8.1
@@ -14,7 +14,6 @@ Spec::Runner.configure { |config|
14
14
  }
15
15
 
16
16
  module WebMock
17
- private
18
17
  def assertion_failure(message)
19
18
  raise Spec::Expectations::ExpectationNotMetError.new(message)
20
19
  end
@@ -16,9 +16,9 @@ class Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  module WebMock
19
- private
19
+ AssertionFailedError = Test::Unit::AssertionFailedError rescue MiniTest::Assertion # ruby1.9 compat
20
20
  def assertion_failure(message)
21
- raise Test::Unit::AssertionFailedError.new(message)
21
+ raise AssertionFailedError.new(message)
22
22
  end
23
23
  end
24
24
 
@@ -27,7 +27,7 @@ if defined?(HTTPClient)
27
27
  end
28
28
  else
29
29
  message = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
30
- assertion_failure(message)
30
+ WebMock.assertion_failure(message)
31
31
  end
32
32
  end
33
33
 
@@ -39,7 +39,7 @@ if defined?(HTTPClient)
39
39
  do_request_async_without_webmock(method, uri, query, body, extheader)
40
40
  else
41
41
  message = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
42
- assertion_failure(message)
42
+ WebMock.assertion_failure(message)
43
43
  end
44
44
  end
45
45
 
@@ -76,7 +76,7 @@ if defined?(HTTPClient)
76
76
  auth = www_auth.basic_auth
77
77
  auth.challenge(req.header.request_uri, nil)
78
78
 
79
- headers = Hash[req.header.all]
79
+ headers = Hash[*req.header.all.flatten]
80
80
 
81
81
  if (auth_cred = auth.get(req)) && auth.scheme == 'Basic'
82
82
  userinfo = WebMock::Util::Headers.decode_userinfo_from_header(auth_cred)
@@ -69,10 +69,14 @@ module Net #:nodoc: all
69
69
  method = request.method.downcase.to_sym
70
70
 
71
71
  headers = Hash[*request.to_hash.map {|k,v| [k, v.flatten]}.flatten]
72
- headers.reject! {|k,v| k =~ /[Aa]ccept/ && v = '*/*'} #removing header added by Net::HTTP
72
+
73
+ remove_default_net_http_headers!(headers)
74
+
73
75
  headers.reject! {|k,v| k =~ /[Aa]uthorization/ && v =~ /^Basic / } #we added it to url userinfo
74
76
 
75
- request_signature = WebMock::RequestSignature.new(method, uri, :body => body, :headers => headers)
77
+ request.set_body_internal body
78
+
79
+ request_signature = WebMock::RequestSignature.new(method, uri, :body => request.body, :headers => headers)
76
80
 
77
81
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
78
82
 
@@ -85,7 +89,7 @@ module Net #:nodoc: all
85
89
  request_without_webmock(request, body, &block)
86
90
  else
87
91
  message = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
88
- assertion_failure(message)
92
+ WebMock.assertion_failure(message)
89
93
  end
90
94
  end
91
95
  alias_method :request_without_webmock, :request
@@ -117,6 +121,16 @@ module Net #:nodoc: all
117
121
 
118
122
  response
119
123
  end
124
+
125
+ def remove_default_net_http_headers!(headers)
126
+ default_request = Net::HTTPGenericRequest.new('','','','/')
127
+ default_net_http_headers = Hash[*default_request.to_hash.map {|k,v|
128
+ [k, v.flatten]
129
+ }.flatten]
130
+ headers.reject! {|k,v| default_request[k] == v}
131
+ headers
132
+ end
133
+
120
134
  end
121
135
 
122
136
  end
@@ -13,12 +13,8 @@ module WebMock
13
13
 
14
14
  def body
15
15
  return '' unless @options.has_key?(:body)
16
- case @options[:body]
17
- when IO
18
- @options[:body].read
19
- when String
20
- @options[:body]
21
- end
16
+ stringify_body!
17
+ @options[:body]
22
18
  end
23
19
 
24
20
  def status
@@ -30,6 +26,7 @@ module WebMock
30
26
  end
31
27
 
32
28
  def dup
29
+ stringify_body!
33
30
  dup_response = super
34
31
  dup_response.options = options.dup
35
32
  dup_response
@@ -39,5 +36,11 @@ module WebMock
39
36
  options == other.options
40
37
  end
41
38
 
39
+ def stringify_body!
40
+ if @options[:body].is_a?(IO)
41
+ @options[:body] = @options[:body].read
42
+ end
43
+ end
44
+
42
45
  end
43
46
  end
@@ -16,7 +16,7 @@ module WebMock
16
16
  return uri if uri.is_a?(Regexp)
17
17
  uri = 'http://' + uri unless uri.match('^https?://') if uri.is_a?(String)
18
18
  normalized_uri = Addressable::URI.heuristic_parse(uri)
19
- normalized_uri.query_values = normalized_uri.query_values if normalized_uri.query_values
19
+ normalized_uri.query_values = Hash[*normalized_uri.query_values.sort.flatten] if normalized_uri.query_values
20
20
  normalized_uri.normalize!
21
21
  normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
22
22
  normalized_uri
@@ -48,8 +48,6 @@ module WebMock
48
48
  WebMock::RequestRegistry.instance.reset_webmock
49
49
  end
50
50
 
51
- private
52
-
53
51
  def assertion_failure(message)
54
52
  raise message
55
53
  end
@@ -19,7 +19,7 @@ module HTTPClientSpecHelper
19
19
  end
20
20
  OpenStruct.new({
21
21
  :body => HTTPClientSpecHelper.async_mode ? response.content.read : response.content,
22
- :headers => Hash[response.header.all],
22
+ :headers => Hash[*response.header.all.flatten],
23
23
  :status => response.code.to_s })
24
24
  end
25
25
 
@@ -13,7 +13,7 @@ describe "Webmock with Net:HTTP" do
13
13
  stub_http_request(:get, "www.example.com").to_return(:body => "abc"*100000)
14
14
  Net::HTTP.start("www.example.com") { |query| query.get("/") }.body.should == "abc"*100000
15
15
  end
16
-
16
+
17
17
  it "should yield block on response" do
18
18
  stub_http_request(:get, "www.example.com").to_return(:body => "abc")
19
19
  response_body = ""
@@ -22,4 +22,21 @@ describe "Webmock with Net:HTTP" do
22
22
  end
23
23
  response_body.should == "abc"
24
24
  end
25
+
26
+ it "should handle Net::HTTP::Post#body" do
27
+ stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
28
+ req = Net::HTTP::Post.new("/")
29
+ req.body = "my_params"
30
+ Net::HTTP.start("www.example.com") { |http| http.request(req)}.body.should == "abc"
31
+ end
32
+
33
+ it "should behave like Net::HTTP and raise error if both request body and body argument are set" do
34
+ stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
35
+ req = Net::HTTP::Post.new("/")
36
+ req.body = "my_params"
37
+ lambda {
38
+ Net::HTTP.start("www.example.com") { |http| http.request(req, "my_params")}
39
+ }.should raise_error("both of body argument and HTTPRequest#body set")
40
+ end
41
+
25
42
  end
@@ -46,8 +46,9 @@ module NetHTTPSpecHelper
46
46
  # Request/response handling
47
47
  request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
48
48
  socket.should_receive(:write).with(/#{request_parts[0]}.*#{request_parts[1]}.*/m).and_return(100)
49
-
50
- socket.should_receive(:sysread).once.and_return("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}")
51
- socket.should_receive(:sysread).any_number_of_times.and_raise(EOFError)
49
+
50
+ read_method = RUBY_VERSION >= "1.9.2" ? :read_nonblock : :sysread
51
+ 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]}")
52
+ socket.should_receive(read_method).any_number_of_times.and_raise(EOFError)
52
53
  end
53
54
  end
@@ -11,7 +11,7 @@ describe "loading other Net::HTTP based libraries" do
11
11
  load_path_opts = vendor_dirs.unshift(webmock_dir).map { |dir| "-I#{dir}" }.join(" ")
12
12
 
13
13
  # TODO: use the same Ruby executable that this test was invoked with
14
- `ruby #{load_path_opts} -e "#{requires}; #{additional_code}" 2>&1`
14
+ `ruby #{load_path_opts} -e "#{requires}; #{additional_code}" 2>&1 | cat`
15
15
  end
16
16
 
17
17
  it "should requiring samuel before webmock prints warning" do
@@ -60,6 +60,12 @@ describe Response do
60
60
  @response.body.should == File.new(__FILE__).read
61
61
  end
62
62
 
63
+ it "should report many times content of a IO object if provided" do
64
+ @response = Response.new(:body => File.new(__FILE__))
65
+ @response.body.should == File.new(__FILE__).read
66
+ @response.body.should == File.new(__FILE__).read
67
+ end
68
+
63
69
  end
64
70
 
65
71
  end
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  unless defined? SAMPLE_HEADERS
4
- SAMPLE_HEADERS = { "Content-Length" => "8888" }
4
+ SAMPLE_HEADERS = { "Content-Length" => "8888", "Accept" => "application/json" }
5
5
  ESCAPED_PARAMS = "x=ab%2Bc&z=%27Stop%21%27%20said%20Fred"
6
6
  NOT_ESCAPED_PARAMS = "z='Stop!' said Fred&x=ab c"
7
7
  end
@@ -136,7 +136,6 @@ describe "WebMock", :shared => true do
136
136
  :headers => SAMPLE_HEADERS).status.should == "200"
137
137
  end
138
138
 
139
-
140
139
  it "should not match requests if headers are different" do
141
140
  stub_http_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS)
142
141
 
@@ -146,6 +145,16 @@ describe "WebMock", :shared => true do
146
145
  :headers => { 'Content-Length' => '9999'})
147
146
  }.should fail_with(%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers {'Content-Length'=>'9999'}))
148
147
  end
148
+
149
+ it "should not match if accept header is different" do
150
+ stub_http_request(:get, "www.example.com").
151
+ with(:headers => { 'Accept' => 'application/json'})
152
+ lambda {
153
+ http_request(
154
+ :get, "http://www.example.com/",
155
+ :headers => { 'Accept' => 'application/xml'})
156
+ }.should fail_with(%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers {'Accept'=>'application/xml'}))
157
+ end
149
158
  end
150
159
 
151
160
  describe "with basic authentication" do
@@ -211,6 +220,17 @@ describe "WebMock", :shared => true do
211
220
  http_request(:get, "http://www.example.com/").status.should == "500"
212
221
  end
213
222
 
223
+ it "should return body declared as IO" do
224
+ stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
225
+ http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
226
+ end
227
+
228
+ it "should return body declared as IO if requested many times" do
229
+ stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
230
+ 2.times do
231
+ http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
232
+ end
233
+ end
214
234
 
215
235
  describe "dynamic responses" do
216
236
 
@@ -8,6 +8,6 @@ require 'test/unit'
8
8
  include WebMock
9
9
 
10
10
  def assert_fail(message, &block)
11
- e = assert_raise(Test::Unit::AssertionFailedError, &block)
11
+ e = assert_raise(AssertionFailedError, &block)
12
12
  assert_equal(message, e.message)
13
13
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{webmock}
8
- s.version = "0.8.0"
8
+ s.version = "0.8.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bartosz Blimke"]
12
- s.date = %q{2009-12-11}
12
+ s.date = %q{2010-01-14}
13
13
  s.description = %q{WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.}
14
14
  s.email = %q{bartosz.blimke@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-11 00:00:00 +00:00
12
+ date: 2010-01-14 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency