Permalink
Browse files

Add some test coverage for response precedence when you register iden…

…tical strings and regexes
  • Loading branch information...
1 parent 81865d9 commit ca720ad1b4c81b22e5f08914284768440659d43c @chrisk chrisk committed Jun 29, 2009
Showing with 47 additions and 2 deletions.
  1. +1 −0 .manifest
  2. +2 −2 fakeweb.gemspec
  3. +44 −0 test/test_precedence.rb
View
@@ -26,6 +26,7 @@ test/test_fake_web.rb
test/test_fake_web_open_uri.rb
test/test_helper.rb
test/test_missing_open_uri.rb
+test/test_precedence.rb
test/test_query_string.rb
test/test_regexes.rb
test/test_response_headers.rb
View
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
s.has_rdoc = true
s.authors = ["Chris Kampmeier", "Blaine Cook"]
s.email = ["chris@kampers.net", "romeda@gmail.com"]
- s.files = %w(CHANGELOG LICENSE.txt README.rdoc Rakefile lib lib/fake_web lib/fake_web.rb lib/fake_web/ext lib/fake_web/ext/net_http.rb lib/fake_web/registry.rb lib/fake_web/responder.rb lib/fake_web/response.rb lib/fake_web/stub_socket.rb lib/fakeweb.rb test test/fixtures test/fixtures/google_response_from_curl test/fixtures/google_response_with_transfer_encoding test/fixtures/google_response_without_transfer_encoding test/fixtures/test_example.txt test/fixtures/test_txt_file test/test_allow_net_connect.rb test/test_deprecations.rb test/test_fake_authentication.rb test/test_fake_web.rb test/test_fake_web_open_uri.rb test/test_helper.rb test/test_missing_open_uri.rb test/test_query_string.rb test/test_regexes.rb test/test_response_headers.rb test/test_trailing_slashes.rb)
- s.test_files = %w(test/fixtures test/fixtures/google_response_from_curl test/fixtures/google_response_with_transfer_encoding test/fixtures/google_response_without_transfer_encoding test/fixtures/test_example.txt test/fixtures/test_txt_file test/test_allow_net_connect.rb test/test_deprecations.rb test/test_fake_authentication.rb test/test_fake_web.rb test/test_fake_web_open_uri.rb test/test_helper.rb test/test_missing_open_uri.rb test/test_query_string.rb test/test_regexes.rb test/test_response_headers.rb test/test_trailing_slashes.rb)
+ s.files = %w(CHANGELOG LICENSE.txt README.rdoc Rakefile lib lib/fake_web lib/fake_web.rb lib/fake_web/ext lib/fake_web/ext/net_http.rb lib/fake_web/registry.rb lib/fake_web/responder.rb lib/fake_web/response.rb lib/fake_web/stub_socket.rb lib/fakeweb.rb test test/fixtures test/fixtures/google_response_from_curl test/fixtures/google_response_with_transfer_encoding test/fixtures/google_response_without_transfer_encoding test/fixtures/test_example.txt test/fixtures/test_txt_file test/test_allow_net_connect.rb test/test_deprecations.rb test/test_fake_authentication.rb test/test_fake_web.rb test/test_fake_web_open_uri.rb test/test_helper.rb test/test_missing_open_uri.rb test/test_precedence.rb test/test_query_string.rb test/test_regexes.rb test/test_response_headers.rb test/test_trailing_slashes.rb)
+ s.test_files = %w(test/fixtures test/fixtures/google_response_from_curl test/fixtures/google_response_with_transfer_encoding test/fixtures/google_response_without_transfer_encoding test/fixtures/test_example.txt test/fixtures/test_txt_file test/test_allow_net_connect.rb test/test_deprecations.rb test/test_fake_authentication.rb test/test_fake_web.rb test/test_fake_web_open_uri.rb test/test_helper.rb test/test_missing_open_uri.rb test/test_precedence.rb test/test_query_string.rb test/test_regexes.rb test/test_response_headers.rb test/test_trailing_slashes.rb)
s.rdoc_options = ["--main", "README.rdoc",
"--title", "FakeWeb API Documentation",
"--charset", "utf-8",
View
@@ -0,0 +1,44 @@
+require File.join(File.dirname(__FILE__), "test_helper")
+
+class TestPrecedence < Test::Unit::TestCase
+
+ def test_matching_get_strings_have_precedence_over_matching_get_regexes
+ FakeWeb.register_uri(:get, "http://example.com/test", :body => "string")
+ FakeWeb.register_uri(:get, %r|http://example\.com/test|, :body => "regex")
+ response = Net::HTTP.start("example.com") { |query| query.get('/test') }
+ assert_equal "string", response.body
+ end
+
+ def test_matching_any_strings_have_precedence_over_matching_any_regexes
+ FakeWeb.register_uri(:any, "http://example.com/test", :body => "string")
+ FakeWeb.register_uri(:any, %r|http://example\.com/test|, :body => "regex")
+ response = Net::HTTP.start("example.com") { |query| query.get('/test') }
+ assert_equal "string", response.body
+ end
+
+ def test_matching_get_strings_have_precedence_over_matching_any_strings
+ FakeWeb.register_uri(:get, "http://example.com/test", :body => "get method")
+ FakeWeb.register_uri(:any, "http://example.com/test", :body => "any method")
+ response = Net::HTTP.start("example.com") { |query| query.get('/test') }
+ assert_equal "get method", response.body
+
+ # registration order should not matter
+ FakeWeb.register_uri(:any, "http://example.com/test2", :body => "any method")
+ FakeWeb.register_uri(:get, "http://example.com/test2", :body => "get method")
+ response = Net::HTTP.start("example.com") { |query| query.get('/test2') }
+ assert_equal "get method", response.body
+ end
+
+ def test_registered_strings_and_uris_are_equivalent_so_second_takes_precedence
+ FakeWeb.register_uri(:get, "http://example.com/test", :body => "string")
+ FakeWeb.register_uri(:get, URI.parse("http://example.com/test"), :body => "uri")
+ response = Net::HTTP.start("example.com") { |query| query.get('/test') }
+ assert_equal "uri", response.body
+
+ FakeWeb.register_uri(:get, URI.parse("http://example.com/test2"), :body => "uri")
+ FakeWeb.register_uri(:get, "http://example.com/test2", :body => "string")
+ response = Net::HTTP.start("example.com") { |query| query.get('/test2') }
+ assert_equal "string", response.body
+ end
+
+end

0 comments on commit ca720ad

Please sign in to comment.