fakeweb-matcher 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Pat Allan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,30 @@
1
+ h1. FakeWeb Matcher
2
+
3
+ An RSpec matcher for the Fakeweb HTTP stubbing library, allowing you to use RSpec syntax to check if requests to particular URIs have been made.
4
+
5
+ h2. Installing
6
+
7
+ First, install the gem
8
+ <pre><code>gem install fakeweb-matcher --source http://gemcutter.org</code></pre>
9
+
10
+ Then, in your @spec/spec_helper.rb@ file, you'll need to require the library _after_ you have required "FakeWeb":http://fakeweb.rubyforge.org and "RSpec":http://rspec.info. It should end up looking something like this:
11
+
12
+ <pre><code>require 'spec'
13
+ require 'fakeweb'
14
+ require 'fakeweb_matcher'</code></pre>
15
+
16
+ This ensures that the matcher is automatically loaded into RSpec for you.
17
+
18
+ h2. Usage
19
+
20
+ <pre><code>FakeWeb.should have_requested(:get, 'http://example.com')
21
+ FakeWeb.should have_requested(:any, 'http://example.com')
22
+ FakeWeb.should_not have_requested(:put, 'http://example.com')</code></pre>
23
+
24
+ h2. Contribution
25
+
26
+ 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
+ h2. Copyright
29
+
30
+ Copyright (c) 2009 Pat Allan, released under an MIT Licence
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'tasks/distribution'
2
+ require 'tasks/testing'
3
+
4
+ task :default => :spec
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 0
4
+ :patch: 1
@@ -0,0 +1,43 @@
1
+ module FakeWebMatcher
2
+ # Extension for FakeWeb::Registry, to track requests made for given URIs. The
3
+ # code that includes this into FakeWeb is in the base FakeWebMatcher module.
4
+ #
5
+ # @see http://fakeweb.rubyforge.org
6
+ #
7
+ module Extension
8
+ def self.included(base)
9
+ base.class_eval do
10
+ # Keep the original response_for method
11
+ alias_method :response_without_request_tracking, :response_for
12
+
13
+ # Overwrites the existing FakeWeb::Registry#response method, to ensure
14
+ # requests are tracked. Returns the usual stubbed response.
15
+ #
16
+ # @param [Symbol] method HTTP method
17
+ # @param [String] uri URI requested
18
+ # @param [Proc] block The block passed into Net::HTTP requests
19
+ # @return [String] The stubbed page response
20
+ #
21
+ def response_for(method, uri, &block)
22
+ requests << [method, uri]
23
+ response_without_request_tracking(method, uri, &block)
24
+ end
25
+ end
26
+ end
27
+
28
+ # A list of the requests, kept as an array of arrays, where each child array
29
+ # has two values - the method and the URI.
30
+ #
31
+ # @return [Array] Recorded requests
32
+ #
33
+ def requests
34
+ @requests ||= []
35
+ end
36
+
37
+ # Clears the stored request list
38
+ #
39
+ def clear_requests
40
+ requests.clear
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ # An RSpec matcher for the Fakeweb HTTP stubbing library, allowing you to use
2
+ # RSpec syntax to check if requests to particular URIs have been made.
3
+ #
4
+ # @see FakeWebMatcher::Matchers
5
+ # @see http://fakeweb.rubyforge.org
6
+ # @author Pat Allan
7
+ #
8
+ module FakeWebMatcher
9
+ # Custom matcher holder for RSpec
10
+ #
11
+ module Matchers
12
+ # Returns a new matcher instance.
13
+ #
14
+ # @param [Symbol] method The HTTP method
15
+ # @param [String] uri The URI to check for
16
+ # @return [FakeWebMatcher::RequestMatcher]
17
+ #
18
+ def have_requested(method, uri)
19
+ FakeWebMatcher::RequestMatcher.new(method, uri)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,94 @@
1
+ module FakeWebMatcher
2
+ # Matcher class, following RSpec's expectations. Used to confirm whether a
3
+ # request has been made on a given method and URI.
4
+ #
5
+ class RequestMatcher
6
+ attr_reader :url, :method
7
+
8
+ # Create a new matcher.
9
+ #
10
+ # @param [Symbol] method The HTTP method. Defaults to :any if not supplied.
11
+ # @param [String] uri The URI to check for
12
+ #
13
+ def initialize(*args)
14
+ @method, @url = args_split(*args)
15
+ end
16
+
17
+ # Indication of whether there's a match on the URI from given requests.
18
+ #
19
+ # @param [Module] FakeWeb Module, necessary for RSpec, although not
20
+ # required internally.
21
+ # @return [Boolean] true if the URI was requested, otherwise false.
22
+ #
23
+ def matches?(fakeweb)
24
+ !FakeWeb::Registry.instance.requests.detect { |req|
25
+ method, url = args_split(*req)
26
+ match_method(method) && url == @url
27
+ }.nil?
28
+ end
29
+
30
+ # Failure message if the URI should have been requested.
31
+ #
32
+ # @return [String] failure message
33
+ #
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
40
+ end
41
+
42
+ # Failure message if the URI should not have been requested.
43
+ #
44
+ # @return [String] failure message
45
+ #
46
+ 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
52
+ end
53
+
54
+ private
55
+
56
+ # Compares methods, or ignores if either side of the comparison is :any.
57
+ #
58
+ # @param [Symbol] method HTTP method
59
+ # @return [Boolean] true if methods match or either is :any.
60
+ #
61
+ def match_method(method)
62
+ @method == :any || method == :any || method == @method
63
+ end
64
+
65
+ # Expected method formatted to be an uppercase string. Example: :get becomes
66
+ # "GET".
67
+ #
68
+ # @return [String] uppercase method
69
+ #
70
+ def formatted_method
71
+ @method.to_s.upcase
72
+ end
73
+
74
+ # Interprets given arguments to a method and URI instance. The URI, as a
75
+ # string, is required, but the method is not (will default to :any).
76
+ #
77
+ # @param [Array] args
78
+ # @return [Array] Two items: method and URI instance
79
+ #
80
+ def args_split(*args)
81
+ method = :any
82
+ uri = nil
83
+
84
+ case args.length
85
+ when 1 then uri = URI.parse(args[0])
86
+ when 2 then method, uri = args[0], URI.parse(args[1])
87
+ else
88
+ raise ArgumentError.new("wrong number of arguments")
89
+ end
90
+
91
+ return method, uri
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,37 @@
1
+ require 'fake_web_matcher/extension'
2
+ require 'fake_web_matcher/matchers'
3
+ require 'fake_web_matcher/request_matcher'
4
+
5
+ # An RSpec matcher for the Fakeweb HTTP stubbing library, allowing you to use
6
+ # RSpec syntax to check if requests to particular URIs have been made.
7
+ #
8
+ # The matcher is automatically included into RSpec's set, and can be used as
9
+ # follows:
10
+ #
11
+ # @example
12
+ # FakeWeb.should have_requested(:get, 'http://example.com')
13
+ # FakeWeb.should have_requested(:any, 'http://example.com')
14
+ # FakeWeb.should_not have_requested(:put, 'http://example.com')
15
+ #
16
+ # @see FakeWebMatcher::Matchers
17
+ # @see http://fakeweb.rubyforge.org
18
+ # @author Pat Allan
19
+ #
20
+ module FakeWebMatcher
21
+ #
22
+ end
23
+
24
+ FakeWeb::Registry.class_eval do
25
+ # Don't like doing this, but need some way to track the requests
26
+ include FakeWebMatcher::Extension
27
+ end
28
+
29
+ Spec::Runner.configure { |config|
30
+ # Adding the custom matcher to the default set
31
+ config.include FakeWebMatcher::Matchers
32
+
33
+ # Ensuring the request list gets cleared after each spec
34
+ config.before :each do
35
+ FakeWeb::Registry.instance.clear_requests
36
+ end
37
+ }
@@ -0,0 +1 @@
1
+ require 'fake_web_matcher'
@@ -0,0 +1,42 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe FakeWebMatcher::Extension do
4
+ it "should be included into the FakeWeb::Registry class" do
5
+ FakeWeb::Registry.included_modules.should include(FakeWebMatcher::Extension)
6
+ end
7
+
8
+ describe '#requests' do
9
+ it "should return an empty Array by default" do
10
+ FakeWeb::Registry.instance.requests.should == []
11
+ end
12
+ end
13
+
14
+ describe '#clear_requests' do
15
+ it "should clear the requests array" do
16
+ registry = FakeWeb::Registry.instance
17
+ registry.requests << :something
18
+ registry.requests.should == [:something]
19
+
20
+ registry.clear_requests
21
+ registry.requests.should == []
22
+ end
23
+ end
24
+
25
+ describe '#response_for' do
26
+ before :each do
27
+ @registry = FakeWeb::Registry.instance
28
+ end
29
+
30
+ it "should track request" do
31
+ @registry.response_for(:any, 'http://uri.com')
32
+
33
+ @registry.requests.should == [[:any, 'http://uri.com']]
34
+ end
35
+
36
+ it "should return the underlying response from response_without_request_tracking" do
37
+ @registry.stub!(:response_without_request_tracking => :response)
38
+
39
+ @registry.response_for(:any, 'http://uri.com').should == :response
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe FakeWebMatcher::Matchers do
4
+ describe '#have_requested' do
5
+ before :each do
6
+ class Matchbox
7
+ include FakeWebMatcher::Matchers
8
+ end
9
+
10
+ @matcher = Matchbox.new.have_requested(:put, 'http://url.com')
11
+ end
12
+
13
+ it "should return an instance of RequestMatcher" do
14
+ @matcher.should be_a(FakeWebMatcher::RequestMatcher)
15
+ end
16
+
17
+ it "should set the url and method using the matcher arguments" do
18
+ @matcher.url.to_s.should == 'http://url.com'
19
+ @matcher.method.should == :put
20
+ end
21
+ end
22
+
23
+ it "should pass if the request has been made" do
24
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
25
+ open('http://example.com/')
26
+
27
+ FakeWeb.should have_requested(:get, 'http://example.com')
28
+ end
29
+
30
+ it "should pass if the request has not been made" do
31
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
32
+
33
+ FakeWeb.should_not have_requested(:get, 'http://example.com')
34
+ end
35
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe FakeWebMatcher::RequestMatcher do
4
+ describe '#initialize' do
5
+ it "should set the url if no method is supplied" do
6
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
7
+ matcher.url.to_s.should == 'http://example.com'
8
+ end
9
+
10
+ it "set the url if a method is explicitly supplied" do
11
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
12
+ matcher.url.to_s.should == 'http://example.com'
13
+ end
14
+
15
+ it "should set the method to any if not supplied" do
16
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
17
+ matcher.method.should == :any
18
+ end
19
+
20
+ it "set the method if explicitly supplied" do
21
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
22
+ matcher.method.should == :get
23
+ end
24
+ end
25
+
26
+ describe '#matches?' do
27
+ before :each do
28
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
29
+ open('http://example.com/')
30
+ end
31
+
32
+ it "should return true if same url and any method" do
33
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
34
+ matcher.matches?(FakeWeb).should be_true
35
+ end
36
+
37
+ it "should return true if same url and same explicit method" do
38
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
39
+ matcher.matches?(FakeWeb).should be_true
40
+ end
41
+
42
+ it "should return false if same url and different explicit method" do
43
+ matcher = FakeWebMatcher::RequestMatcher.new(:post, 'http://example.com')
44
+ matcher.matches?(FakeWeb).should be_false
45
+ end
46
+
47
+ it "should return false if different url and same method" do
48
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://domain.com')
49
+ matcher.matches?(FakeWeb).should be_false
50
+ end
51
+
52
+ it "should return false if different url and different explicit method" do
53
+ matcher = FakeWebMatcher::RequestMatcher.new(:post, 'http://domain.com')
54
+ matcher.matches?(FakeWeb).should be_false
55
+ end
56
+ end
57
+
58
+ describe '#failure_message' do
59
+ it "should mention the method if explicitly set" do
60
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
61
+ matcher.failure_message.
62
+ should == 'The URL http://example.com was not requested using GET.'
63
+ end
64
+
65
+ it "should not mention the method if not explicitly set" do
66
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
67
+ matcher.failure_message.
68
+ should == 'The URL http://example.com was not requested.'
69
+ end
70
+ end
71
+
72
+ describe '#negative_failure_message' do
73
+ it "should mention the method if explicitly set" do
74
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
75
+ matcher.negative_failure_message.
76
+ should == 'The URL http://example.com was requested using GET and should not have been.'
77
+ end
78
+
79
+ it "should not mention the method if not explicitly set" do
80
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
81
+ matcher.negative_failure_message.
82
+ should == 'The URL http://example.com was requested and should not have been.'
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe FakeWebMatcher do
4
+ #
5
+ end
@@ -0,0 +1,11 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'rubygems'
4
+ require 'open-uri'
5
+ require 'spec'
6
+ require 'fake_web'
7
+ require 'fake_web_matcher'
8
+
9
+ Spec::Runner.configure do |config|
10
+ #
11
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fakeweb-matcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-29 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fakeweb
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.0
34
+ version:
35
+ description:
36
+ email: pat@freelancing-gods.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.textile
44
+ files:
45
+ - LICENSE
46
+ - README.textile
47
+ - Rakefile
48
+ - VERSION.yml
49
+ - lib/fake_web_matcher.rb
50
+ - lib/fake_web_matcher/extension.rb
51
+ - lib/fake_web_matcher/matchers.rb
52
+ - lib/fake_web_matcher/request_matcher.rb
53
+ - lib/fakeweb_matcher.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/freelancing-god/fakeweb-matcher
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.5
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: RSpec matcher for the FakeWeb library
82
+ test_files:
83
+ - spec/lib/fake_web_matcher/extension_spec.rb
84
+ - spec/lib/fake_web_matcher/matchers_spec.rb
85
+ - spec/lib/fake_web_matcher/request_matcher_spec.rb
86
+ - spec/lib/fake_web_matcher_spec.rb
87
+ - spec/spec_helper.rb