webmock 1.9.3 → 1.10.0

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.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.10.0
4
+
5
+ * '+' in query params is not treated as space anymore and is encoded as %2B
6
+
7
+ Thanks to [goblin](https://github.com/goblin) for reporting this issue.
8
+
9
+ * added `remove_request_stub` method to the api to allow removing unused stubs i.e.
10
+
11
+ stub_get = stub_request(:get, "www.example.com")
12
+ remove_request_stub(stub_get)
13
+
14
+ * `assert_requested` and `assert_not_requested` raise an error if a stub object is provided together with a block.
15
+
3
16
  ## 1.9.3
4
17
 
5
18
  * Fixed issue with unavailable constant Mutex in Ruby < 1.9
data/lib/webmock/api.rb CHANGED
@@ -21,15 +21,19 @@ module WebMock
21
21
  def assert_requested(*args, &block)
22
22
  if not args[0].is_a?(WebMock::RequestStub)
23
23
  args = convert_uri_method_and_options_to_request_and_options(*args, &block)
24
+ elsif block
25
+ raise ArgumentError, "assert_requested with a stub object, doesn't accept blocks"
24
26
  end
25
- assert_request_requested(*args, &block)
27
+ assert_request_requested(*args)
26
28
  end
27
29
 
28
30
  def assert_not_requested(*args, &block)
29
31
  if not args[0].is_a?(WebMock::RequestStub)
30
32
  args = convert_uri_method_and_options_to_request_and_options(*args, &block)
33
+ elsif block
34
+ raise ArgumentError, "assert_not_requested with a stub object, doesn't accept blocks"
31
35
  end
32
- assert_request_not_requested(*args, &block)
36
+ assert_request_not_requested(*args)
33
37
  end
34
38
 
35
39
  def hash_including(expected)
@@ -42,6 +46,10 @@ module WebMock
42
46
  end
43
47
  end
44
48
 
49
+ def remove_request_stub(stub)
50
+ WebMock::StubRegistry.instance.remove_request_stub(stub)
51
+ end
52
+
45
53
  private
46
54
 
47
55
  def convert_uri_method_and_options_to_request_and_options(*args, &block)
@@ -31,7 +31,7 @@ if defined?(Excon)
31
31
  string << key.to_s << '&'
32
32
  else
33
33
  for value in [*values]
34
- string << key.to_s << '=' << CGI.escape(value.to_s) << '&'
34
+ string << key.to_s << '=' << URI.escape(value.to_s) << '&'
35
35
  end
36
36
  end
37
37
  end
@@ -36,6 +36,12 @@ module WebMock
36
36
  stub
37
37
  end
38
38
 
39
+ def remove_request_stub(stub)
40
+ if not request_stubs.delete(stub)
41
+ raise "Request stub \n\n #{stub.to_s} \n\n is not registered."
42
+ end
43
+ end
44
+
39
45
  def registered_request?(request_signature)
40
46
  request_stub_for(request_signature)
41
47
  end
@@ -66,7 +66,7 @@ module WebMock::Util
66
66
  value = true if value.nil?
67
67
  key = Addressable::URI.unencode_component(key)
68
68
  if value != true
69
- value = Addressable::URI.unencode_component(value.gsub(/\+/, " "))
69
+ value = Addressable::URI.unencode_component(value)
70
70
  end
71
71
  if options[:notation] == :flat
72
72
  if accumulator[key]
@@ -18,6 +18,7 @@ module WebMock
18
18
  normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values)
19
19
  end
20
20
  normalized_uri = normalized_uri.normalize #normalize! is slower
21
+ normalized_uri.query = normalized_uri.query.gsub("+", "%2B") if normalized_uri.query
21
22
  normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
22
23
  hash[uri] = normalized_uri
23
24
  end
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.9.3' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.10.0' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -5,7 +5,7 @@ module ExconSpecHelper
5
5
  def http_request(method, uri, options = {}, &block)
6
6
  Excon.defaults[:ssl_verify_peer] = false
7
7
  uri = Addressable::URI.heuristic_parse(uri)
8
- uri = uri.omit(:userinfo).to_s.gsub(' ', '+')
8
+ uri = uri.omit(:userinfo).to_s.gsub(' ', '%20')
9
9
 
10
10
  options = options.merge(:method => method, :nonblock => false) # Dup and merge
11
11
  response = Excon.new(uri).request(options, &block)
@@ -12,7 +12,7 @@ module PatronSpecHelper
12
12
  sess.timeout = 30
13
13
  sess.max_redirects = 0
14
14
  uri = "#{uri.path}#{uri.query ? '?' : ''}#{uri.query}"
15
- uri.gsub!(' ','+')
15
+ uri.gsub!(' ','%20')
16
16
  response = sess.request(method, uri, options[:headers] || {}, {
17
17
  :data => options[:body]
18
18
  })
@@ -15,6 +15,15 @@ shared_examples_for "stubbing requests" do |*adapter_info|
15
15
  stub_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
16
16
  http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
17
17
  end
18
+
19
+ it "should raise error specifying stubbing instructions with escaped characters in params if there is no matching stub" do
20
+ begin
21
+ http_request(:get, "http://www.example.com/hello/?#{NOT_ESCAPED_PARAMS}")
22
+ rescue WebMock::NetConnectNotAllowedError => e
23
+ e.message.should match /Unregistered request: GET http:\/\/www\.example\.com\/hello\/\?x=ab%20c&z='Stop!'%20said%20Fred%2Bm/m
24
+ e.message.should match /stub_request\(:get, "http:\/\/www\.example\.com\/hello\/\?x=ab%20c&z='Stop!'%20said%20Fred%2Bm"\)/m
25
+ end
26
+ end
18
27
  end
19
28
 
20
29
  describe "based on query params" do
@@ -444,4 +453,18 @@ shared_examples_for "stubbing requests" do |*adapter_info|
444
453
  end
445
454
  end
446
455
  end
456
+
457
+ describe "when request stub was removed" do
458
+ it "should raise an error on request" do
459
+ stub = stub_request(:get, "www.example.com")
460
+
461
+ http_request(:get, "http://www.example.com/")
462
+
463
+ remove_request_stub(stub)
464
+
465
+ lambda {
466
+ http_request(:get, "http://www.example.com/")
467
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
468
+ end
469
+ end
447
470
  end
@@ -10,8 +10,8 @@ require 'acceptance/shared/complex_cross_concern_behaviors'
10
10
 
11
11
  unless defined? SAMPLE_HEADERS
12
12
  SAMPLE_HEADERS = { "Content-Length" => "8888", "Accept" => "application/json" }
13
- ESCAPED_PARAMS = "x=ab%20c&z=%27Stop%21%27%20said%20Fred"
14
- NOT_ESCAPED_PARAMS = "z='Stop!' said Fred&x=ab c"
13
+ ESCAPED_PARAMS = "x=ab%20c&z=%27Stop%21%27%20said%20Fred%2Bm"
14
+ NOT_ESCAPED_PARAMS = "z='Stop!' said Fred+m&x=ab c"
15
15
  end
16
16
 
17
17
  shared_examples "with WebMock" do |*adapter_info|
@@ -9,6 +9,15 @@ describe WebMock::StubRegistry do
9
9
  @request_stub = WebMock::RequestStub.new(:get, "www.example.com")
10
10
  end
11
11
 
12
+ describe "remove_request_stub" do
13
+ it "should remove stub from registry" do
14
+ WebMock::StubRegistry.instance.register_request_stub(@request_stub)
15
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == @request_stub
16
+ WebMock::StubRegistry.instance.remove_request_stub(@request_stub)
17
+ WebMock::StubRegistry.instance.registered_request?(@request_signature).should == nil
18
+ end
19
+ end
20
+
12
21
  describe "reset!" do
13
22
  before(:each) do
14
23
  WebMock::StubRegistry.instance.register_request_stub(@request_stub)
@@ -47,14 +47,14 @@ URIS_WITH_AUTH =
47
47
 
48
48
  URIS_WITH_PATH_AND_PARAMS =
49
49
  [
50
- "www.example.com/my path/?a=my param&b=c",
51
- "www.example.com/my%20path/?a=my%20param&b=c",
52
- "www.example.com:80/my path/?a=my param&b=c",
53
- "www.example.com:80/my%20path/?a=my%20param&b=c",
54
- "http://www.example.com/my path/?a=my param&b=c",
55
- "http://www.example.com/my%20path/?a=my%20param&b=c",
56
- "http://www.example.com:80/my path/?a=my param&b=c",
57
- "http://www.example.com:80/my%20path/?a=my%20param&b=c",
50
+ "www.example.com/my path/?a=my param&b=c+d",
51
+ "www.example.com/my%20path/?a=my%20param&b=c%2Bd",
52
+ "www.example.com:80/my path/?a=my param&b=c+d",
53
+ "www.example.com:80/my%20path/?a=my%20param&b=c%2Bd",
54
+ "http://www.example.com/my path/?a=my param&b=c+d",
55
+ "http://www.example.com/my%20path/?a=my%20param&b=c%2Bd",
56
+ "http://www.example.com:80/my path/?a=my param&b=c+d",
57
+ "http://www.example.com:80/my%20path/?a=my%20param&b=c%2Bd",
58
58
  ].sort
59
59
 
60
60
  URIS_WITH_DIFFERENT_PORT =
@@ -4,4 +4,4 @@ describe "WebMock version" do
4
4
  it "should report version" do
5
5
  WebMock.version.should == WebMock::VERSION
6
6
  end
7
- end
7
+ end
data/test/shared_test.rb CHANGED
@@ -9,6 +9,18 @@ module SharedTest
9
9
  @stub_https = stub_http_request(:any, "https://www.example.com")
10
10
  end
11
11
 
12
+ def test_assert_requested_with_stub_and_block_raises_error
13
+ assert_raise ArgumentError do
14
+ assert_requested(@stub_http) {}
15
+ end
16
+ end
17
+
18
+ def test_assert_not_requested_with_stub_and_block_raises_error
19
+ assert_raise ArgumentError do
20
+ assert_not_requested(@stub_http) {}
21
+ end
22
+ end
23
+
12
24
  def test_error_on_non_stubbed_request
13
25
  default_ruby_headers = (RUBY_VERSION >= "1.9.1") ? "{'Accept'=>'*/*', 'User-Agent'=>'Ruby'}" : "{'Accept'=>'*/*'}"
14
26
  assert_raise_with_message(WebMock::NetConnectNotAllowedError, %r{Real HTTP connections are disabled. Unregistered request: GET http://www.example.net/ with headers}) 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: 53
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 9
9
- - 3
10
- version: 1.9.3
8
+ - 10
9
+ - 0
10
+ version: 1.10.0
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: 2013-02-20 00:00:00 Z
18
+ date: 2013-02-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: addressable