webmock 1.20.1 → 1.20.2

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.
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.20.2
4
+
5
+ * WebMock provides a helpful error message if an incompatible object is given as response body.
6
+
7
+ Thanks to [Mark Lorenz](https://github.com/dapplebeforedawn)
8
+
3
9
  ## 1.20.1
4
10
 
5
11
  * `assert_requested` and `assert_not_requested` accept `at_least_times` and `at_most_times` options
data/README.md CHANGED
@@ -35,6 +35,7 @@ Supported Ruby Interpreters
35
35
  * MRI 1.9.2
36
36
  * MRI 1.9.3
37
37
  * MRI 2.0.0
38
+ * MRI 2.1
38
39
  * REE 1.8.7
39
40
  * JRuby
40
41
  * Rubinius
@@ -931,6 +932,7 @@ People who submitted patches and new features or suggested improvements. Many th
931
932
  * Tasos Stathopoulos
932
933
  * Dan Buettner
933
934
  * Sven Riedel
935
+ * Mark Lorenz
934
936
 
935
937
  For a full list of contributors you can visit the
936
938
  [contributors](https://github.com/bblimke/webmock/contributors) page.
@@ -43,6 +43,7 @@ module WebMock
43
43
 
44
44
  def body=(body)
45
45
  @body = body
46
+ assert_valid_body!
46
47
  stringify_body!
47
48
  end
48
49
 
@@ -109,6 +110,13 @@ module WebMock
109
110
  end
110
111
  end
111
112
 
113
+ def assert_valid_body!
114
+ valid_types = [Proc, IO, Pathname, String, Array]
115
+ return if @body.nil?
116
+ return if valid_types.any? { |c| @body.is_a?(c) }
117
+ raise InvalidBody, "must be one of: #{valid_types}. '#{@body.class}' given"
118
+ end
119
+
112
120
  def read_raw_response(raw_response)
113
121
  if raw_response.is_a?(IO)
114
122
  string = raw_response.read
@@ -129,6 +137,8 @@ module WebMock
129
137
  options
130
138
  end
131
139
 
140
+ InvalidBody = Class.new(StandardError)
141
+
132
142
  end
133
143
 
134
144
  class DynamicResponse < Response
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.20.1' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.20.2' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -400,7 +400,7 @@ describe WebMock::RequestPattern do
400
400
  :headers => {:content_type => 'application/json'}, :body => "foo bar"))
401
401
  end
402
402
 
403
- it "shound not match if request body is different" do
403
+ it "should not match if request body is different" do
404
404
  WebMock::RequestPattern.new(:post, 'www.example.com', :body => {:a => 1, :b => 2}).
405
405
  should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
406
406
  :headers => {:content_type => 'application/json'}, :body => "{\"a\":1,\"c\":null}"))
@@ -124,6 +124,15 @@ describe WebMock::Response do
124
124
  @response = WebMock::Response.new(:body => Pathname.new(__FILE__))
125
125
  @response.body.should == File.read(__FILE__)
126
126
  end
127
+
128
+ # Users of webmock commonly make the mistake of stubbing the response
129
+ # body to return a hash, to prevent this:
130
+ #
131
+ it "should error if not given one of the allowed types" do
132
+ lambda { WebMock::Response.new(:body => Hash.new) }.should \
133
+ raise_error(WebMock::Response::InvalidBody)
134
+ end
135
+
127
136
  end
128
137
 
129
138
  describe "from raw response" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- hash: 69
4
+ hash: 67
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 20
9
- - 1
10
- version: 1.20.1
9
+ - 2
10
+ version: 1.20.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bartosz Blimke