webmock 1.20.2 → 1.20.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.20.3
4
+
5
+ * `with` method raises error if provided without options hash and without block
6
+
7
+ * `with` and `to_return` raise an error if invoked with invalid keys in options hash.
8
+
3
9
  ## 1.20.2
4
10
 
5
11
  * WebMock provides a helpful error message if an incompatible object is given as response body.
data/Gemfile CHANGED
@@ -5,14 +5,13 @@ if ENV["EM_HTTP_REQUEST_0_X"]
5
5
  gem 'em-http-request', '~> 0.3.0'
6
6
  end
7
7
 
8
-
9
8
  group :development do
10
9
  gem 'rake'
11
10
  end
12
11
 
13
12
  group :test do
14
13
  gem 'rack'
15
- gem 'minitest_tu_shim'
14
+ gem 'minitest_tu_shim', '1.3.2'
16
15
  end
17
16
 
18
17
  platforms :jruby do
data/README.md CHANGED
@@ -546,6 +546,8 @@ WebMock.should have_requested(:get, "www.example.com").
546
546
  WebMock.should_not have_requested(:get, "www.something.com")
547
547
 
548
548
  WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "abc" }
549
+ # Note that the block with `do ... end` instead of curly brackets won't work!
550
+ # Why? See this comment https://github.com/bblimke/webmock/issues/174#issuecomment-34908908
549
551
 
550
552
  WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
551
553
 
@@ -16,6 +16,7 @@ require 'webmock/util/hash_counter'
16
16
  require 'webmock/util/hash_keys_stringifier'
17
17
  require 'webmock/util/json'
18
18
  require 'webmock/util/version_checker'
19
+ require 'webmock/util/hash_validator'
19
20
 
20
21
  require 'webmock/matchers/hash_including_matcher'
21
22
 
@@ -19,7 +19,7 @@ module WebMock
19
19
 
20
20
  def assert_requested(*args, &block)
21
21
  if not args[0].is_a?(WebMock::RequestStub)
22
- args = convert_uri_method_and_options_to_request_and_options(*args, &block)
22
+ args = convert_uri_method_and_options_to_request_and_options(args[0], args[1], args[2], &block)
23
23
  elsif block
24
24
  raise ArgumentError, "assert_requested with a stub object, doesn't accept blocks"
25
25
  end
@@ -28,7 +28,7 @@ module WebMock
28
28
 
29
29
  def assert_not_requested(*args, &block)
30
30
  if not args[0].is_a?(WebMock::RequestStub)
31
- args = convert_uri_method_and_options_to_request_and_options(*args, &block)
31
+ args = convert_uri_method_and_options_to_request_and_options(args[0], args[1], args[2], &block)
32
32
  elsif block
33
33
  raise ArgumentError, "assert_not_requested with a stub object, doesn't accept blocks"
34
34
  end
@@ -59,9 +59,13 @@ module WebMock
59
59
 
60
60
  private
61
61
 
62
- def convert_uri_method_and_options_to_request_and_options(*args, &block)
63
- request = WebMock::RequestPattern.new(*args).with(&block)
64
- [request, args[2] || {}]
62
+ def convert_uri_method_and_options_to_request_and_options(method, uri, options, &block)
63
+ options ||= {}
64
+ options_for_pattern = options.dup
65
+ [:times, :at_least_times, :at_most_times].each { |key| options_for_pattern.delete(key) }
66
+ request = WebMock::RequestPattern.new(method, uri, options_for_pattern)
67
+ request = request.with(&block) if block
68
+ [request, options]
65
69
  end
66
70
 
67
71
  def assert_request_requested(request, options = {})
@@ -20,6 +20,7 @@ module WebMock
20
20
  end
21
21
 
22
22
  def with(options = {}, &block)
23
+ raise ArgumentError.new('#with method invoked with no arguments. Either options hash or block must be specified.') if options.empty? && !block_given?
23
24
  assign_options(options)
24
25
  @with_block = block
25
26
  self
@@ -49,6 +50,7 @@ module WebMock
49
50
 
50
51
  def assign_options(options)
51
52
  options = WebMock::Util::HashKeysStringifier.stringify_keys!(options, :deep => true)
53
+ HashValidator.new(options).validate_keys('body', 'headers', 'query')
52
54
  @body_pattern = BodyPattern.new(options['body']) if options.has_key?('body')
53
55
  @headers_pattern = HeadersPattern.new(options['headers']) if options.has_key?('headers')
54
56
  @uri_pattern.add_query_params(options['query']) if options.has_key?('query')
@@ -76,11 +76,13 @@ module WebMock
76
76
  end
77
77
 
78
78
  def options=(options)
79
- self.headers = options[:headers]
80
- self.status = options[:status]
81
- self.body = options[:body]
82
- self.exception = options[:exception]
83
- @should_timeout = options[:should_timeout]
79
+ options = WebMock::Util::HashKeysStringifier.stringify_keys!(options)
80
+ HashValidator.new(options).validate_keys('headers', 'status', 'body', 'exception', 'should_timeout')
81
+ self.headers = options['headers']
82
+ self.status = options['status']
83
+ self.body = options['body']
84
+ self.exception = options['exception']
85
+ @should_timeout = options['should_timeout']
84
86
  end
85
87
 
86
88
  def evaluate(request_signature)
@@ -0,0 +1,17 @@
1
+ module WebMock
2
+ class HashValidator
3
+ def initialize(hash)
4
+ @hash = hash
5
+ end
6
+
7
+ #This code is based on https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/keys.rb
8
+ def validate_keys(*valid_keys)
9
+ valid_keys.flatten!
10
+ @hash.each_key do |k|
11
+ unless valid_keys.include?(k)
12
+ raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.20.2' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.20.3' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -373,7 +373,7 @@ unless RUBY_PLATFORM =~ /java/
373
373
  end
374
374
 
375
375
  it "should work with several body arguments for post using the class method" do
376
- stub_request(:post, "www.example.com").with(:user => {:first_name=>'Bartosz', :last_name=>'Blimke'})
376
+ stub_request(:post, "www.example.com").with(:body => {:user => {:first_name=>'Bartosz', :last_name=>'Blimke'}})
377
377
  c = Curl::Easy.http_post "http://www.example.com", 'user[first_name]=Bartosz', 'user[last_name]=Blimke'
378
378
  c.response_code.should == 200
379
379
  end
@@ -22,7 +22,7 @@ unless RUBY_PLATFORM =~ /java/
22
22
 
23
23
  describe "when params are used" do
24
24
  it "should take into account params for POST request" do
25
- stub_request(:post, "www.example.com/?hello=world").with(:params => {:hello => 'world'})
25
+ stub_request(:post, "www.example.com/?hello=world").with(:query => {:hello => 'world'})
26
26
  request = Typhoeus::Request.new("http://www.example.com", :method => :post, :params => {:hello => 'world'})
27
27
  hydra.queue(request)
28
28
  hydra.run
@@ -48,6 +48,13 @@ describe WebMock::RequestPattern do
48
48
  @request_pattern.to_s.should ==WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A' => 'a'}).to_s
49
49
  end
50
50
 
51
+ it "should raise an error if options passed to `with` are invalid" do
52
+ expect { @request_pattern.with(:foo => "bar") }.to raise_error('Unknown key: "foo". Valid keys are: "body", "headers", "query"')
53
+ end
54
+
55
+ it "should raise an error if neither options or block is provided" do
56
+ expect { @request_pattern.with() }.to raise_error('#with method invoked with no arguments. Either options hash or block must be specified.')
57
+ end
51
58
  end
52
59
 
53
60
 
@@ -26,6 +26,10 @@ describe WebMock::Response do
26
26
  @response = WebMock::Response.new(:headers => {'A' => 'a'})
27
27
  end
28
28
 
29
+ it "should raise an error when initialized with unknown option" do
30
+ expect { WebMock::Response.new(:foo => "bar") }.to raise_error('Unknown key: "foo". Valid keys are: "headers", "status", "body", "exception", "should_timeout"')
31
+ end
32
+
29
33
  it "should report normalized headers" do
30
34
  WebMock::Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
31
35
  @response = WebMock::Response.new(:headers => {'A' => 'a'})
@@ -33,7 +37,6 @@ describe WebMock::Response do
33
37
  end
34
38
 
35
39
  describe "status" do
36
-
37
40
  it "should have 200 code and empty message by default" do
38
41
  @response.status.should == [200, ""]
39
42
  end
@@ -47,7 +50,6 @@ describe WebMock::Response do
47
50
  @response = WebMock::Response.new(:status => [500, "Internal Server Error"])
48
51
  @response.status.should == [500, "Internal Server Error"]
49
52
  end
50
-
51
53
  end
52
54
 
53
55
  describe "raising error" 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: 67
4
+ hash: 65
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 20
9
- - 2
10
- version: 1.20.2
9
+ - 3
10
+ version: 1.20.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bartosz Blimke
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-11-02 00:00:00 +01:00
18
+ date: 2014-11-05 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -273,6 +273,7 @@ files:
273
273
  - lib/webmock/test_unit.rb
274
274
  - lib/webmock/util/hash_counter.rb
275
275
  - lib/webmock/util/hash_keys_stringifier.rb
276
+ - lib/webmock/util/hash_validator.rb
276
277
  - lib/webmock/util/headers.rb
277
278
  - lib/webmock/util/json.rb
278
279
  - lib/webmock/util/query_mapper.rb