fakeweb-matcher 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -19,12 +19,17 @@ h2. Usage
19
19
 
20
20
  <pre><code>FakeWeb.should have_requested(:get, 'http://example.com')
21
21
  FakeWeb.should have_requested(:any, 'http://example.com')
22
+ FakeWeb.should have_requested(:post, /http:\/\/example.com\/)
22
23
  FakeWeb.should_not have_requested(:put, 'http://example.com')</code></pre>
23
24
 
24
25
  h2. Contribution
25
26
 
26
27
  Unsurprisingly, this library is tested using RSpec, and relies upon FakeWeb. It also uses "YARD":http://yard.soen.ca/ for documentation, so if you're submitting patches (which are most definitely welcome!) please use YARD syntax and have valid specs.
27
28
 
29
+ h2. Contributors
30
+
31
+ * "Thilo Utke":http://github.com/thilo (Regex URL matching)
32
+
28
33
  h2. Copyright
29
34
 
30
35
  Copyright (c) 2009 Pat Allan, released under an MIT Licence
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
- :minor: 0
4
- :patch: 1
3
+ :minor: 1
4
+ :patch: 0
@@ -8,7 +8,7 @@ module FakeWebMatcher
8
8
  # Create a new matcher.
9
9
  #
10
10
  # @param [Symbol] method The HTTP method. Defaults to :any if not supplied.
11
- # @param [String] uri The URI to check for
11
+ # @param [String, Regexp] uri The URI to check for
12
12
  #
13
13
  def initialize(*args)
14
14
  @method, @url = args_split(*args)
@@ -23,7 +23,7 @@ module FakeWebMatcher
23
23
  def matches?(fakeweb)
24
24
  !FakeWeb::Registry.instance.requests.detect { |req|
25
25
  method, url = args_split(*req)
26
- match_method(method) && url == @url
26
+ match_method(method) && match_url(url)
27
27
  }.nil?
28
28
  end
29
29
 
@@ -32,11 +32,7 @@ module FakeWebMatcher
32
32
  # @return [String] failure message
33
33
  #
34
34
  def failure_message
35
- if @method == :any
36
- "The URL #{@url} was not requested."
37
- else
38
- "The URL #{@url} was not requested using #{formatted_method}."
39
- end
35
+ regex?(@url) ? regex_failure_message : url_failure_message
40
36
  end
41
37
 
42
38
  # Failure message if the URI should not have been requested.
@@ -44,15 +40,31 @@ module FakeWebMatcher
44
40
  # @return [String] failure message
45
41
  #
46
42
  def negative_failure_message
47
- if @method == :any
48
- "The URL #{@url} was requested and should not have been."
49
- else
50
- "The URL #{@url} was requested using #{formatted_method} and should not have been."
51
- end
43
+ regex?(@url) ? regex_negative_failure_message : url_negative_failure_message
52
44
  end
53
45
 
54
46
  private
55
47
 
48
+ def regex_negative_failure_message
49
+ "A URL that matches #{@url.inspect} was requested#{failure_message_method} and should not have been."
50
+ end
51
+
52
+ def url_negative_failure_message
53
+ "The URL #{@url} was requested#{failure_message_method} and should not have been."
54
+ end
55
+
56
+ def regex_failure_message
57
+ "A URL that matches #{@url.inspect} was not requested#{failure_message_method}."
58
+ end
59
+
60
+ def url_failure_message
61
+ "The URL #{@url} was not requested#{failure_message_method}."
62
+ end
63
+
64
+ def failure_message_method
65
+ " using #{formatted_method}" unless @method == :any
66
+ end
67
+
56
68
  # Compares methods, or ignores if either side of the comparison is :any.
57
69
  #
58
70
  # @param [Symbol] method HTTP method
@@ -62,6 +74,17 @@ module FakeWebMatcher
62
74
  @method == :any || method == :any || method == @method
63
75
  end
64
76
 
77
+ # Compares the url either by match it agains a regular expression or
78
+ # by simple comparison
79
+ #
80
+ # @param [String] url the called URI
81
+ # @return [Boolean] true if exprexted URI and called URI match.
82
+ #
83
+ def match_url(url)
84
+ regex?(@url) ? @url.match(url.to_s) : @url == url
85
+ end
86
+
87
+
65
88
  # Expected method formatted to be an uppercase string. Example: :get becomes
66
89
  # "GET".
67
90
  #
@@ -75,20 +98,24 @@ module FakeWebMatcher
75
98
  # string, is required, but the method is not (will default to :any).
76
99
  #
77
100
  # @param [Array] args
78
- # @return [Array] Two items: method and URI instance
101
+ # @return [Array] Two items: method and URI instance or regular expression
79
102
  #
80
103
  def args_split(*args)
81
104
  method = :any
82
105
  uri = nil
83
106
 
84
107
  case args.length
85
- when 1 then uri = URI.parse(args[0])
86
- when 2 then method, uri = args[0], URI.parse(args[1])
108
+ when 1 then uri = args[0]
109
+ when 2 then method, uri = args[0], args[1]
87
110
  else
88
111
  raise ArgumentError.new("wrong number of arguments")
89
112
  end
90
-
113
+ uri = URI.parse(uri) unless regex?(uri)
91
114
  return method, uri
92
115
  end
116
+
117
+ def regex?(object)
118
+ object.is_a?(Regexp)
119
+ end
93
120
  end
94
121
  end
@@ -53,6 +53,19 @@ describe FakeWebMatcher::RequestMatcher do
53
53
  matcher = FakeWebMatcher::RequestMatcher.new(:post, 'http://domain.com')
54
54
  matcher.matches?(FakeWeb).should be_false
55
55
  end
56
+
57
+ describe "matching url is regular expression" do
58
+ it "should return true if expression matches" do
59
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, /example/)
60
+ matcher.matches?(FakeWeb).should be_true
61
+ end
62
+
63
+ it "should return false if don't match" do
64
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, /domain/)
65
+ matcher.matches?(FakeWeb).should be_false
66
+ end
67
+
68
+ end
56
69
  end
57
70
 
58
71
  describe '#failure_message' do
@@ -67,6 +80,18 @@ describe FakeWebMatcher::RequestMatcher do
67
80
  matcher.failure_message.
68
81
  should == 'The URL http://example.com was not requested.'
69
82
  end
83
+
84
+ it "should mention failing match" do
85
+ matcher = FakeWebMatcher::RequestMatcher.new(/example.com/)
86
+ matcher.failure_message.
87
+ should == 'A URL that matches /example.com/ was not requested.'
88
+ end
89
+
90
+ it "should mention failing match with method if set" do
91
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, /example.com/)
92
+ matcher.failure_message.
93
+ should == 'A URL that matches /example.com/ was not requested using GET.'
94
+ end
70
95
  end
71
96
 
72
97
  describe '#negative_failure_message' do
@@ -81,5 +106,18 @@ describe FakeWebMatcher::RequestMatcher do
81
106
  matcher.negative_failure_message.
82
107
  should == 'The URL http://example.com was requested and should not have been.'
83
108
  end
109
+
110
+ it "should mention unexpected match" do
111
+ matcher = FakeWebMatcher::RequestMatcher.new(/example.com/)
112
+ matcher.negative_failure_message.
113
+ should == 'A URL that matches /example.com/ was requested and should not have been.'
114
+ end
115
+
116
+ it "should mention unexpected match with method if set" do
117
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, /example.com/)
118
+ matcher.negative_failure_message.
119
+ should == 'A URL that matches /example.com/ was requested using GET and should not have been.'
120
+ end
121
+
84
122
  end
85
123
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakeweb-matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-29 00:00:00 +02:00
12
+ date: 2009-12-06 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -77,7 +77,7 @@ requirements: []
77
77
  rubyforge_project:
78
78
  rubygems_version: 1.3.5
79
79
  signing_key:
80
- specification_version: 2
80
+ specification_version: 3
81
81
  summary: RSpec matcher for the FakeWeb library
82
82
  test_files:
83
83
  - spec/lib/fake_web_matcher/extension_spec.rb