Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

1.0 release

  • Loading branch information...
commit f7f190111961af4caa7d3658b0dcee8d0f29f175 1 parent 6d09fe3
Pat Allan authored
5 .document
View
@@ -1,5 +0,0 @@
-README.rdoc
-lib/**/*.rb
-bin/*
-features/**/*.feature
-LICENSE
3  .gitignore
View
@@ -3,3 +3,6 @@
coverage
rdoc
pkg
+coverage
+doc
+.yardoc
7 README.rdoc
View
@@ -1,7 +0,0 @@
-= fakeweb-matcher
-
-Description goes here.
-
-== Copyright
-
-Copyright (c) 2009 Pat Allan. See LICENSE for details.
30 README.textile
View
@@ -0,0 +1,30 @@
+h1. FakeWeb Matcher
+
+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.
+
+h2. Installing
+
+First, install the gem
+<pre><code>gem install freelancing-god-fakeweb-matcher --source http://gems.github.com</code></pre>
+
+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, to look something like this:
+
+<pre><code>require 'spec'
+require 'fakeweb'
+require 'fakeweb_matcher'</code></pre>
+
+This ensures that the matcher is automatically loaded into RSpec for you.
+
+h2. Usage
+
+<pre><code>FakeWeb.should have_requested(:get, 'http://example.com')
+FakeWeb.should have_requested(:any, 'http://example.com')
+FakeWeb.should_not have_requested(:put, 'http://example.com')</code></pre>
+
+h2. Contribution
+
+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.
+
+h2. Copyright
+
+Copyright (c) 2009 Pat Allan, released under an MIT Licence
48 Rakefile
View
@@ -1,48 +1,4 @@
-require 'rubygems'
-require 'rake'
-
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "fakeweb-matcher"
- gem.summary = %Q{TODO}
- gem.email = "pat@freelancing-gods.com"
- gem.homepage = "http://github.com/freelancing-god/fakeweb-matcher"
- gem.authors = ["Pat Allan"]
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
- end
-
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
-end
-
-require 'spec/rake/spectask'
-Spec::Rake::SpecTask.new(:spec) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.spec_files = FileList['spec/**/*_spec.rb']
-end
-
-Spec::Rake::SpecTask.new(:rcov) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.pattern = 'spec/**/*_spec.rb'
- spec.rcov = true
-end
-
+require 'tasks/distribution'
+require 'tasks/testing'
task :default => :spec
-
-require 'rake/rdoctask'
-Rake::RDocTask.new do |rdoc|
- if File.exist?('VERSION.yml')
- config = YAML.load(File.read('VERSION.yml'))
- version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
- else
- version = ""
- end
-
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "fakeweb-matcher #{version}"
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
-
4 VERSION.yml
View
@@ -0,0 +1,4 @@
+---
+:major: 1
+:minor: 0
+:patch: 0
55 fakeweb-matcher.gemspec
View
@@ -0,0 +1,55 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{fakeweb-matcher}
+ s.version = "1.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Pat Allan"]
+ s.date = %q{2009-07-29}
+ s.email = %q{pat@freelancing-gods.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.textile"
+ ]
+ s.files = [
+ "LICENSE",
+ "README.textile",
+ "Rakefile",
+ "VERSION.yml",
+ "lib/fake_web_matcher.rb",
+ "lib/fake_web_matcher/extension.rb",
+ "lib/fake_web_matcher/matchers.rb",
+ "lib/fake_web_matcher/request_matcher.rb",
+ "lib/fakeweb_matcher.rb"
+ ]
+ s.has_rdoc = true
+ s.homepage = %q{http://github.com/freelancing-god/fakeweb-matcher}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.1}
+ s.summary = %q{RSpec matcher for the FakeWeb library}
+ s.test_files = [
+ "spec/lib/fake_web_matcher/extension_spec.rb",
+ "spec/lib/fake_web_matcher/matchers_spec.rb",
+ "spec/lib/fake_web_matcher/request_matcher_spec.rb",
+ "spec/lib/fake_web_matcher_spec.rb",
+ "spec/spec_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<fakeweb>, [">= 1.2.5"])
+ s.add_runtime_dependency(%q<rspec>, [">= 1.2.0"])
+ else
+ s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
+ s.add_dependency(%q<rspec>, [">= 1.2.0"])
+ end
+ else
+ s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
+ s.add_dependency(%q<rspec>, [">= 1.2.0"])
+ end
+end
37 lib/fake_web_matcher.rb
View
@@ -0,0 +1,37 @@
+require 'fake_web_matcher/extension'
+require 'fake_web_matcher/matchers'
+require 'fake_web_matcher/request_matcher'
+
+# 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.
+#
+# The matcher is automatically included into RSpec's set, and can be used as
+# follows:
+#
+# @example
+# FakeWeb.should have_requested(:get, 'http://example.com')
+# FakeWeb.should have_requested(:any, 'http://example.com')
+# FakeWeb.should_not have_requested(:put, 'http://example.com')
+#
+# @see FakeWebMatcher::Matchers
+# @see http://fakeweb.rubyforge.org
+# @author Pat Allan
+#
+module FakeWebMatcher
+ #
+end
+
+FakeWeb::Registry.class_eval do
+ # Don't like doing this, but need some way to track the requests
+ include FakeWebMatcher::Extension
+end
+
+Spec::Runner.configure { |config|
+ # Adding the custom matcher to the default set
+ config.include FakeWebMatcher::Matchers
+
+ # Ensuring the request list gets cleared after each spec
+ config.before :each do
+ FakeWeb::Registry.instance.clear_requests
+ end
+}
43 lib/fake_web_matcher/extension.rb
View
@@ -0,0 +1,43 @@
+module FakeWebMatcher
+ # Extension for FakeWeb::Registry, to track requests made for given URIs. The
+ # code that includes this into FakeWeb is in the base FakeWebMatcher module.
+ #
+ # @see http://fakeweb.rubyforge.org
+ #
+ module Extension
+ def self.included(base)
+ base.class_eval do
+ # Keep the original response_for method
+ alias_method :response_without_request_tracking, :response_for
+
+ # Overwrites the existing FakeWeb::Registry#response method, to ensure
+ # requests are tracked. Returns the usual stubbed response.
+ #
+ # @param [Symbol] method HTTP method
+ # @param [String] uri URI requested
+ # @param [Proc] block The block passed into Net::HTTP requests
+ # @return [String] The stubbed page response
+ #
+ def response_for(method, uri, &block)
+ requests << [method, uri]
+ response_without_request_tracking(method, uri, &block)
+ end
+ end
+ end
+
+ # A list of the requests, kept as an array of arrays, where each child array
+ # has two values - the method and the URI.
+ #
+ # @return [Array] Recorded requests
+ #
+ def requests
+ @requests ||= []
+ end
+
+ # Clears the stored request list
+ #
+ def clear_requests
+ requests.clear
+ end
+ end
+end
22 lib/fake_web_matcher/matchers.rb
View
@@ -0,0 +1,22 @@
+# 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.
+#
+# @see FakeWebMatcher::Matchers
+# @see http://fakeweb.rubyforge.org
+# @author Pat Allan
+#
+module FakeWebMatcher
+ # Custom matcher holder for RSpec
+ #
+ module Matchers
+ # Returns a new matcher instance.
+ #
+ # @param [Symbol] method The HTTP method
+ # @param [String] uri The URI to check for
+ # @return [FakeWebMatcher::RequestMatcher]
+ #
+ def have_requested(method, uri)
+ FakeWebMatcher::RequestMatcher.new(method, uri)
+ end
+ end
+end
94 lib/fake_web_matcher/request_matcher.rb
View
@@ -0,0 +1,94 @@
+module FakeWebMatcher
+ # Matcher class, following RSpec's expectations. Used to confirm whether a
+ # request has been made on a given method and URI.
+ #
+ class RequestMatcher
+ attr_reader :url, :method
+
+ # Create a new matcher.
+ #
+ # @param [Symbol] method The HTTP method
+ # @param [String] uri The URI to check for
+ #
+ def initialize(method, url)
+ @method, @url = method, url
+ end
+
+ # Indication of whether there's a match on the URI from given requests.
+ #
+ # @param [Module] FakeWeb Module, necessary for RSpec, although not
+ # required internally.
+ # @return [Boolean] true if the URI was requested, otherwise false.
+ #
+ def matches?(fakeweb)
+ !FakeWeb::Registry.instance.requests.detect { |req|
+ method, url = args_split(*req)
+ match_method(method) && url == @url
+ }.nil?
+ end
+
+ # Failure message if the URI should have been requested.
+ #
+ # @return [String] failure message
+ #
+ def failure_message
+ if @method == :any
+ "The URL #{@url} was not requested."
+ else
+ "The URL #{@url} was not requested using #{formatted_method}."
+ end
+ end
+
+ # Failure message if the URI should not have been requested.
+ #
+ # @return [String] failure message
+ #
+ def negative_failure_message
+ if @method == :any
+ "The URL #{@url} was requested and should not have been."
+ else
+ "The URL #{@url} was requested using #{formatted_method} and should not have been."
+ end
+ end
+
+ private
+
+ # Compares methods, or ignores if either side of the comparison is :any.
+ #
+ # @param [Symbol] method HTTP method
+ # @return [Boolean] true if methods match or either is :any.
+ #
+ def match_method(method)
+ @method == :any || method == :any || method == @method
+ end
+
+ # Expected method formatted to be an uppercase string. Example: :get becomes
+ # "GET".
+ #
+ # @return [String] uppercase method
+ #
+ def formatted_method
+ @method.to_s.upcase
+ end
+
+ # Interprets given arguments to a method and URI instance. The URI, as a
+ # string, is required, but the method is not (will default to :any).
+ #
+ # @param [Array] args
+ # @return [Array] Two items: method and URI instance
+ #
+ def args_split(*args)
+ method = :any
+ uri = nil
+
+ case args.length
+ when 1 then uri = URI.parse(args[0])
+ when 2 then method, uri = args[0], URI.parse(args[1])
+ else
+ raise ArgumentError.new("wrong number of arguments")
+ end
+
+ return method, uri
+ end
+ end
+end
0  lib/fakeweb-matcher.rb
View
No changes.
1  lib/fakeweb_matcher.rb
View
@@ -0,0 +1 @@
+require 'fake_web_matcher'
7 spec/fakeweb-matcher_spec.rb
View
@@ -1,7 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-
-describe "FakewebMatcher" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
- end
-end
42 spec/lib/fake_web_matcher/extension_spec.rb
View
@@ -0,0 +1,42 @@
+require 'spec/spec_helper'
+
+describe FakeWebMatcher::Extension do
+ it "should be included into the FakeWeb::Registry class" do
+ FakeWeb::Registry.included_modules.should include(FakeWebMatcher::Extension)
+ end
+
+ describe '#requests' do
+ it "should return an empty Array by default" do
+ FakeWeb::Registry.instance.requests.should == []
+ end
+ end
+
+ describe '#clear_requests' do
+ it "should clear the requests array" do
+ registry = FakeWeb::Registry.instance
+ registry.requests << :something
+ registry.requests.should == [:something]
+
+ registry.clear_requests
+ registry.requests.should == []
+ end
+ end
+
+ describe '#response_for' do
+ before :each do
+ @registry = FakeWeb::Registry.instance
+ end
+
+ it "should track request" do
+ @registry.response_for(:any, 'http://uri.com')
+
+ @registry.requests.should == [[:any, 'http://uri.com']]
+ end
+
+ it "should return the underlying response from response_without_request_tracking" do
+ @registry.stub!(:response_without_request_tracking => :response)
+
+ @registry.response_for(:any, 'http://uri.com').should == :response
+ end
+ end
+end
35 spec/lib/fake_web_matcher/matchers_spec.rb
View
@@ -0,0 +1,35 @@
+require 'spec/spec_helper'
+
+describe FakeWebMatcher::Matchers do
+ describe '#have_requested' do
+ before :each do
+ class Matchbox
+ include FakeWebMatcher::Matchers
+ end
+
+ @matcher = Matchbox.new.have_requested(:put, 'http://url.com')
+ end
+
+ it "should return an instance of RequestMatcher" do
+ @matcher.should be_a(FakeWebMatcher::RequestMatcher)
+ end
+
+ it "should set the url and method using the matcher arguments" do
+ @matcher.url.to_s.should == 'http://url.com'
+ @matcher.method.should == :put
+ end
+ end
+
+ it "should pass if the request has been made" do
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
+ open('http://example.com/')
+
+ FakeWeb.should have_requested(:get, 'http://example.com')
+ end
+
+ it "should pass if the request has not been made" do
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
+
+ FakeWeb.should_not have_requested(:get, 'http://example.com')
+ end
+end
85 spec/lib/fake_web_matcher/request_matcher_spec.rb
View
@@ -0,0 +1,85 @@
+require 'spec/spec_helper'
+
+describe FakeWebMatcher::RequestMatcher do
+ describe '#initialize' do
+ it "should set the url if no method is supplied" do
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
+ matcher.url.to_s.should == 'http://example.com'
+ end
+
+ it "set the url if a method is explicitly supplied" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
+ matcher.url.to_s.should == 'http://example.com'
+ end
+
+ it "should set the method to any if not supplied" do
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
+ matcher.method.should == :any
+ end
+
+ it "set the method if explicitly supplied" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
+ matcher.method.should == :get
+ end
+ end
+
+ describe '#matches?' do
+ before :each do
+ FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
+ open('http://example.com/')
+ end
+
+ it "should return true if same url and any method" do
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
+ matcher.matches?(FakeWeb).should be_true
+ end
+
+ it "should return true if same url and same explicit method" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
+ matcher.matches?(FakeWeb).should be_true
+ end
+
+ it "should return false if same url and different explicit method" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:post, 'http://example.com')
+ matcher.matches?(FakeWeb).should be_false
+ end
+
+ it "should return false if different url and same method" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://domain.com')
+ matcher.matches?(FakeWeb).should be_false
+ end
+
+ it "should return false if different url and different explicit method" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:post, 'http://domain.com')
+ matcher.matches?(FakeWeb).should be_false
+ end
+ end
+
+ describe '#failure_message' do
+ it "should mention the method if explicitly set" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
+ matcher.failure_message.
+ should == 'The URL http://example.com was not requested using GET.'
+ end
+
+ it "should not mention the method if not explicitly set" do
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
+ matcher.failure_message.
+ should == 'The URL http://example.com was not requested.'
+ end
+ end
+
+ describe '#negative_failure_message' do
+ it "should mention the method if explicitly set" do
+ matcher = FakeWebMatcher::RequestMatcher.new(:get, 'http://example.com')
+ matcher.negative_failure_message.
+ should == 'The URL http://example.com was requested using GET and should not have been.'
+ end
+
+ it "should not mention the method if not explicitly set" do
+ matcher = FakeWebMatcher::RequestMatcher.new('http://example.com')
+ matcher.negative_failure_message.
+ should == 'The URL http://example.com was requested and should not have been.'
+ end
+ end
+end
5 spec/lib/fake_web_matcher_spec.rb
View
@@ -0,0 +1,5 @@
+require 'spec/spec_helper'
+
+describe FakeWebMatcher do
+ #
+end
12 spec/spec_helper.rb
View
@@ -1,9 +1,11 @@
-require 'spec'
+$:.unshift File.dirname(__FILE__) + '/../lib'
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-require 'fakeweb-matcher'
+require 'rubygems'
+require 'open-uri'
+require 'spec'
+require 'fake_web'
+require 'fake_web_matcher'
Spec::Runner.configure do |config|
-
+ #
end
28 tasks/distribution.rb
View
@@ -0,0 +1,28 @@
+require 'yard'
+require 'jeweler'
+
+desc 'Generate documentation'
+YARD::Rake::YardocTask.new
+
+task :rdoc => :yardoc
+
+Jeweler::Tasks.new do |gem|
+ gem.name = "fakeweb-matcher"
+ gem.summary = "RSpec matcher for the FakeWeb library"
+ gem.homepage = "http://github.com/freelancing-god/fakeweb-matcher"
+ gem.author = "Pat Allan"
+ gem.email = "pat@freelancing-gods.com"
+
+ gem.files = FileList[
+ 'lib/**/*.rb',
+ 'LICENSE',
+ 'Rakefile',
+ 'README.textile',
+ 'tasks',
+ 'VERSION.yml'
+ ]
+ gem.test_files = FileList['spec/**/*.rb']
+
+ gem.add_dependency 'fakeweb', '>= 1.2.5'
+ gem.add_dependency 'rspec', '>= 1.2.0'
+end
14 tasks/testing.rb
View
@@ -0,0 +1,14 @@
+require 'spec/rake/spectask'
+
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+ t.spec_opts << "-c"
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |t|
+ t.pattern = 'spec/**/*_spec.rb'
+ t.spec_opts << "-c"
+
+ t.rcov_opts = ['--exclude', 'spec', '--exclude', 'gems']
+ t.rcov = true
+end
Please sign in to comment.
Something went wrong with that request. Please try again.