fakeweb 1.2.5 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ /doc
2
+ /rdoc
3
+ /coverage
4
+ /pkg
5
+ /.idea
data/CHANGELOG CHANGED
@@ -1,3 +1,19 @@
1
+ fakeweb (1.2.6)
2
+
3
+ * fix that query params in a regex would have to be sorted for it to ever match
4
+ a request URI [Chris Kampmeier, Ben Hall]
5
+
6
+ * improve regex handling so registration with an explicit port (like
7
+ /example.com:80/) matches a request that uses an implied port
8
+ (like "http://example.com/") [Chris Kampmeier, Dan Dofter]
9
+
10
+ * refactor URI registry to reduce duplication; now about twice as fast at
11
+ handling requests [Chris Kampmeier]
12
+
13
+ * Add FakeWeb::VERSION so you can programmatically determine what version of
14
+ FakeWeb is loaded without using RubyGems [Chris Kampmeier, Chris Wanstrath]
15
+
16
+
1
17
  fakeweb (1.2.5)
2
18
 
3
19
  * fix handling of userinfo strings that contain percent-encoded unsafe
data/Rakefile CHANGED
@@ -1,76 +1,70 @@
1
1
  puts "Using ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
2
2
 
3
3
  require 'rubygems'
4
- require 'rake/gempackagetask'
5
- require 'rake/testtask'
4
+ require 'rake'
5
+
6
6
  begin
7
- require 'rdoc/task'
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "fakeweb"
10
+ gem.rubyforge_project = "fakeweb"
11
+ gem.summary = "A tool for faking responses to HTTP requests"
12
+ gem.description = "FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs."
13
+ gem.email = ["chris@kampers.net", "romeda@gmail.com"]
14
+ gem.authors = ["Chris Kampmeier", "Blaine Cook"]
15
+ gem.homepage = "http://github.com/chrisk/fakeweb"
16
+ gem.add_development_dependency "mocha", ">= 0.9.5"
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ Jeweler::RubyforgeTasks.new do |rubyforge|
20
+ rubyforge.doc_task = "rdoc"
21
+ rubyforge.remote_doc_path = ""
22
+ end
8
23
  rescue LoadError
9
- puts "\nIt looks like you're using an old version of RDoc, but FakeWeb requires a newer one."
10
- puts "You can try upgrading with `sudo gem install rdoc`.\n\n"
11
- raise
24
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
12
25
  end
13
26
 
14
- task :default => :test
15
27
 
16
- desc "Run All Tests"
17
- Rake::TestTask.new :test do |test|
18
- test.test_files = ["test/**/*.rb"]
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb")
19
31
  test.verbose = false
32
+ test.warning = true
20
33
  end
21
34
 
22
- desc "Generate Documentation"
23
- RDoc::Task.new do |rdoc|
24
- rdoc.main = "README.rdoc"
25
- rdoc.rdoc_dir = "doc"
26
- rdoc.rdoc_files.include("README.rdoc", "CHANGELOG", "LICENSE.txt", "lib/*.rb")
27
- rdoc.title = "FakeWeb API Documentation"
28
- rdoc.options << '--line-numbers' << '--charset' << 'utf-8'
29
- end
30
-
31
- desc %{Update ".manifest" with the latest list of project filenames. Respect\
32
- .gitignore by excluding everything that git ignores. Update `files` and\
33
- `test_files` arrays in "*.gemspec" file if it's present.}
34
- task :manifest do
35
- list = Dir['**/*'].sort
36
- spec_file = Dir['*.gemspec'].first
37
- list -= [spec_file] if spec_file
38
-
39
- File.read('.gitignore').each_line do |glob|
40
- glob = glob.chomp.sub(/^\//, '')
41
- list -= Dir[glob]
42
- list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
43
- puts "excluding #{glob}"
44
- end
45
-
46
- if spec_file
47
- spec = File.read spec_file
48
- spec.gsub!(/^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx) do
49
- assignment = $1
50
- bunch = $2 ? list.grep(/^test\//) : list
51
- '%s%%w(%s)' % [assignment, bunch.join(' ')]
52
- end
35
+ task :default => :test
53
36
 
54
- File.open(spec_file, 'w') {|f| f << spec }
55
- end
56
- File.open('.manifest', 'w') {|f| f << list.join("\n") }
57
- end
58
37
 
59
- if RUBY_PLATFORM =~ /java/
60
- puts "rcov support disabled (running under JRuby)."
61
- elsif RUBY_VERSION =~ /^1\.9/
62
- puts "rcov support disabled (running under Ruby 1.9)"
63
- else
38
+ begin
64
39
  require 'rcov/rcovtask'
65
40
  Rcov::RcovTask.new do |t|
66
- t.test_files = FileList['test/**/test*.rb']
41
+ t.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb")
67
42
  t.rcov_opts << "--sort coverage"
68
43
  t.rcov_opts << "--exclude gems"
44
+ t.warning = true
45
+ end
46
+ rescue
47
+ print "rcov support disabled "
48
+ if RUBY_PLATFORM =~ /java/
49
+ puts "(running under JRuby)"
50
+ elsif RUBY_VERSION =~ /^1\.9/
51
+ puts "(running under Ruby 1.9)"
52
+ else
53
+ puts "(install RCov to enable the `rcov` task)"
69
54
  end
70
55
  end
71
56
 
72
- spec = eval(File.read(File.join(File.dirname(__FILE__), "fakeweb.gemspec")))
73
- Rake::GemPackageTask.new(spec) do |pkg|
74
- pkg.need_tar_gz = true
75
- pkg.need_zip = true
57
+
58
+ begin
59
+ require 'rdoc/task'
60
+ Rake::RDocTask.new do |rdoc|
61
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
62
+ rdoc.main = "README.rdoc"
63
+ rdoc.rdoc_files.include("README.rdoc", "CHANGELOG", "LICENSE.txt", "lib/*.rb")
64
+ rdoc.title = "FakeWeb #{version} API Documentation"
65
+ rdoc.options << '--line-numbers' << '--charset' << 'utf-8'
66
+ end
67
+ rescue LoadError
68
+ puts "\nIt looks like you're using an old version of RDoc, but FakeWeb requires a newer one."
69
+ puts "You can try upgrading with `sudo gem install rdoc`.\n\n"
76
70
  end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.2.6
@@ -0,0 +1,88 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{fakeweb}
8
+ s.version = "1.2.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Chris Kampmeier", "Blaine Cook"]
12
+ s.date = %q{2009-08-31}
13
+ s.description = %q{FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs.}
14
+ s.email = ["chris@kampers.net", "romeda@gmail.com"]
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "CHANGELOG",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "fakeweb.gemspec",
27
+ "lib/fake_web.rb",
28
+ "lib/fake_web/ext/net_http.rb",
29
+ "lib/fake_web/registry.rb",
30
+ "lib/fake_web/responder.rb",
31
+ "lib/fake_web/response.rb",
32
+ "lib/fake_web/stub_socket.rb",
33
+ "lib/fake_web/utility.rb",
34
+ "lib/fakeweb.rb",
35
+ "test/fixtures/google_response_from_curl",
36
+ "test/fixtures/google_response_with_transfer_encoding",
37
+ "test/fixtures/google_response_without_transfer_encoding",
38
+ "test/fixtures/test_example.txt",
39
+ "test/fixtures/test_txt_file",
40
+ "test/test_allow_net_connect.rb",
41
+ "test/test_deprecations.rb",
42
+ "test/test_fake_authentication.rb",
43
+ "test/test_fake_web.rb",
44
+ "test/test_fake_web_open_uri.rb",
45
+ "test/test_helper.rb",
46
+ "test/test_missing_open_uri.rb",
47
+ "test/test_precedence.rb",
48
+ "test/test_query_string.rb",
49
+ "test/test_regexes.rb",
50
+ "test/test_response_headers.rb",
51
+ "test/test_trailing_slashes.rb",
52
+ "test/test_utility.rb"
53
+ ]
54
+ s.homepage = %q{http://github.com/chrisk/fakeweb}
55
+ s.rdoc_options = ["--charset=UTF-8"]
56
+ s.require_paths = ["lib"]
57
+ s.rubyforge_project = %q{fakeweb}
58
+ s.rubygems_version = %q{1.3.5}
59
+ s.summary = %q{A tool for faking responses to HTTP requests}
60
+ s.test_files = [
61
+ "test/test_allow_net_connect.rb",
62
+ "test/test_deprecations.rb",
63
+ "test/test_fake_authentication.rb",
64
+ "test/test_fake_web.rb",
65
+ "test/test_fake_web_open_uri.rb",
66
+ "test/test_helper.rb",
67
+ "test/test_missing_open_uri.rb",
68
+ "test/test_precedence.rb",
69
+ "test/test_query_string.rb",
70
+ "test/test_regexes.rb",
71
+ "test/test_response_headers.rb",
72
+ "test/test_trailing_slashes.rb",
73
+ "test/test_utility.rb"
74
+ ]
75
+
76
+ if s.respond_to? :specification_version then
77
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
78
+ s.specification_version = 3
79
+
80
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
81
+ s.add_development_dependency(%q<mocha>, [">= 0.9.5"])
82
+ else
83
+ s.add_dependency(%q<mocha>, [">= 0.9.5"])
84
+ end
85
+ else
86
+ s.add_dependency(%q<mocha>, [">= 0.9.5"])
87
+ end
88
+ end
@@ -9,6 +9,9 @@ require 'fake_web/utility'
9
9
 
10
10
  module FakeWeb
11
11
 
12
+ # Returns the version string for the copy of FakeWeb you have loaded.
13
+ VERSION = File.read(File.join(File.dirname(__FILE__), "..", "VERSION")).strip
14
+
12
15
  # Resets the FakeWeb Registry. This will force all subsequent web requests to
13
16
  # behave as real requests.
14
17
  def self.clean_registry
@@ -47,9 +50,10 @@ module FakeWeb
47
50
  class NetConnectNotAllowedError < StandardError; end;
48
51
 
49
52
  # This exception is raised if a Net::HTTP request matches more than one of
50
- # the regular expression-based stubs you've registered. To fix the problem,
51
- # disambiguate the regular expressions by making them more specific.
52
- class MultipleMatchingRegexpsError < StandardError; end;
53
+ # the stubs you've registered. To fix the problem, remove a duplicate
54
+ # registration or disambiguate any regular expressions by making them more
55
+ # specific.
56
+ class MultipleMatchingURIsError < StandardError; end;
53
57
 
54
58
  # call-seq:
55
59
  # FakeWeb.register_uri(method, uri, options)
@@ -19,7 +19,6 @@ module FakeWeb
19
19
  end
20
20
 
21
21
  def registered_uri?(method, uri)
22
- normalized_uri = normalize_uri(uri)
23
22
  !responses_for(method, uri).empty?
24
23
  end
25
24
 
@@ -45,39 +44,64 @@ module FakeWeb
45
44
  def responses_for(method, uri)
46
45
  uri = normalize_uri(uri)
47
46
 
48
- if uri_map[uri].has_key?(method)
49
- uri_map[uri][method]
50
- elsif uri_map[uri].has_key?(:any)
51
- uri_map[uri][:any]
52
- elsif uri_map_matches?(method, uri)
53
- uri_map_matches(method, uri)
54
- elsif uri_map_matches(:any, uri)
55
- uri_map_matches(:any, uri)
56
- else
57
- []
58
- end
47
+ uri_map_matches(method, uri, URI) ||
48
+ uri_map_matches(:any, uri, URI) ||
49
+ uri_map_matches(method, uri, Regexp) ||
50
+ uri_map_matches(:any, uri, Regexp) ||
51
+ []
59
52
  end
60
53
 
61
- def uri_map_matches?(method, uri)
62
- !uri_map_matches(method, uri).nil?
63
- end
64
-
65
- def uri_map_matches(method, uri)
66
- uri = normalize_uri(uri.to_s).to_s
67
- uri = Utility.strip_default_port_from_uri(uri)
54
+ def uri_map_matches(method, uri, type_to_check = URI)
55
+ uris_to_check = variations_of_uri_as_strings(uri)
68
56
 
69
57
  matches = uri_map.select { |registered_uri, method_hash|
70
- registered_uri.is_a?(Regexp) && uri.match(registered_uri) && method_hash.has_key?(method)
58
+ registered_uri.is_a?(type_to_check) && method_hash.has_key?(method)
59
+ }.select { |registered_uri, method_hash|
60
+ if type_to_check == URI
61
+ uris_to_check.include?(registered_uri.to_s)
62
+ elsif type_to_check == Regexp
63
+ uris_to_check.any? { |u| u.match(registered_uri) }
64
+ end
71
65
  }
72
66
 
73
67
  if matches.size > 1
74
- raise MultipleMatchingRegexpsError,
75
- "More than one regular expression matched this request: #{method.to_s.upcase} #{uri}"
68
+ raise MultipleMatchingURIsError,
69
+ "More than one registered URI matched this request: #{method.to_s.upcase} #{uri}"
76
70
  end
77
71
 
78
72
  matches.map { |_, method_hash| method_hash[method] }.first
79
73
  end
80
74
 
75
+
76
+ def variations_of_uri_as_strings(uri_object)
77
+ uris = []
78
+ normalized_uri = normalize_uri(uri_object)
79
+
80
+ # all orderings of query parameters
81
+ query = normalized_uri.query
82
+ if query.nil? || query.empty?
83
+ uris << normalized_uri
84
+ else
85
+ FakeWeb::Utility.simple_array_permutation(query.split('&')) do |p|
86
+ current_permutation = normalized_uri.dup
87
+ current_permutation.query = p.join('&')
88
+ uris << current_permutation
89
+ end
90
+ end
91
+
92
+ uri_strings = uris.map { |uri| uri.to_s }
93
+
94
+ # including and omitting the default port
95
+ if normalized_uri.default_port == normalized_uri.port
96
+ uri_strings += uris.map { |uri|
97
+ uri.to_s.sub(/#{Regexp.escape(normalized_uri.request_uri)}$/,
98
+ ":#{normalized_uri.port}#{normalized_uri.request_uri}")
99
+ }
100
+ end
101
+
102
+ uri_strings
103
+ end
104
+
81
105
  def normalize_uri(uri)
82
106
  return uri if uri.is_a?(Regexp)
83
107
  normalized_uri =
@@ -18,5 +18,27 @@ module FakeWeb
18
18
  end
19
19
  end
20
20
 
21
+ # Array#permutation wrapper for 1.8.6-compatibility. It only supports the
22
+ # simple case that returns all permutations (so it doesn't take a numeric
23
+ # argument).
24
+ def self.simple_array_permutation(array, &block)
25
+ # use native implementation if it exists
26
+ return array.permutation(&block) if array.respond_to?(:permutation)
27
+
28
+ yield array if array.length <= 1
29
+
30
+ array.length.times do |i|
31
+ rest = array.dup
32
+ picked = rest.delete_at(i)
33
+ next if rest.empty?
34
+
35
+ simple_array_permutation(rest) do |part_of_rest|
36
+ yield [picked] + part_of_rest
37
+ end
38
+ end
39
+
40
+ array
41
+ end
42
+
21
43
  end
22
44
  end
@@ -6,49 +6,49 @@ class TestDeprecations < Test::Unit::TestCase
6
6
  warning = capture_stderr do
7
7
  FakeWeb.register_uri("http://example.com", :body => "test")
8
8
  end
9
- assert_match /deprecation warning: fakeweb/i, warning
9
+ assert_match %r(deprecation warning: fakeweb)i, warning
10
10
  end
11
11
 
12
12
  def test_registered_uri_without_method_argument_prints_deprecation_warning
13
13
  warning = capture_stderr do
14
14
  FakeWeb.registered_uri?("http://example.com")
15
15
  end
16
- assert_match /deprecation warning: fakeweb/i, warning
16
+ assert_match %r(deprecation warning: fakeweb)i, warning
17
17
  end
18
18
 
19
19
  def test_response_for_without_method_argument_prints_deprecation_warning
20
20
  warning = capture_stderr do
21
21
  FakeWeb.response_for("http://example.com")
22
22
  end
23
- assert_match /deprecation warning: fakeweb/i, warning
23
+ assert_match %r(deprecation warning: fakeweb)i, warning
24
24
  end
25
25
 
26
26
  def test_register_uri_without_method_argument_prints_deprecation_warning_with_correct_caller
27
27
  warning = capture_stderr do
28
28
  FakeWeb.register_uri("http://example.com", :body => "test")
29
29
  end
30
- assert_match /Called at.*?test_deprecations\.rb/i, warning
30
+ assert_match %r(Called at.*?test_deprecations\.rb)i, warning
31
31
  end
32
32
 
33
33
  def test_register_uri_with_string_option_prints_deprecation_warning
34
34
  warning = capture_stderr do
35
35
  FakeWeb.register_uri(:get, "http://example.com", :string => "test")
36
36
  end
37
- assert_match /deprecation warning: fakeweb's :string option/i, warning
37
+ assert_match %r(deprecation warning: fakeweb's :string option)i, warning
38
38
  end
39
39
 
40
40
  def test_register_uri_with_file_option_prints_deprecation_warning
41
41
  warning = capture_stderr do
42
42
  FakeWeb.register_uri(:get, "http://example.com", :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
43
43
  end
44
- assert_match /deprecation warning: fakeweb's :file option/i, warning
44
+ assert_match %r(deprecation warning: fakeweb's :file option)i, warning
45
45
  end
46
46
 
47
47
  def test_register_uri_with_string_option_prints_deprecation_warning_with_correct_caller
48
48
  warning = capture_stderr do
49
49
  FakeWeb.register_uri(:get, "http://example.com", :string => "test")
50
50
  end
51
- assert_match /Called at.*?test_deprecations\.rb/i, warning
51
+ assert_match %r(Called at.*?test_deprecations\.rb)i, warning
52
52
  end
53
53
 
54
54
  end
@@ -104,6 +104,23 @@ class TestFakeWeb < Test::Unit::TestCase
104
104
  end
105
105
  end
106
106
 
107
+ def test_clean_registry_affects_registered_uri
108
+ FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
109
+ assert FakeWeb.registered_uri?(:get, "http://example.com")
110
+ FakeWeb.clean_registry
111
+ assert !FakeWeb.registered_uri?(:get, "http://example.com")
112
+ end
113
+
114
+ def test_clean_registry_affects_net_http_requests
115
+ FakeWeb.register_uri(:get, "http://example.com", :body => "registered")
116
+ response = Net::HTTP.start("example.com") { |query| query.get("/") }
117
+ assert_equal "registered", response.body
118
+ FakeWeb.clean_registry
119
+ assert_raise FakeWeb::NetConnectNotAllowedError do
120
+ Net::HTTP.start("example.com") { |query| query.get("/") }
121
+ end
122
+ end
123
+
107
124
  def test_response_for_with_registered_uri
108
125
  FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
109
126
  assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
@@ -515,4 +532,8 @@ class TestFakeWeb < Test::Unit::TestCase
515
532
  assert_equal "1.0", response.http_version
516
533
  end
517
534
 
535
+ def test_version
536
+ assert_equal "1.2.6", FakeWeb::VERSION
537
+ end
538
+
518
539
  end
@@ -9,12 +9,14 @@ require 'mocha'
9
9
 
10
10
  # Give all tests a common setup and teardown that prevents shared state
11
11
  class Test::Unit::TestCase
12
+ alias setup_without_fakeweb setup
12
13
  def setup
13
14
  FakeWeb.clean_registry
14
15
  @original_allow_net_connect = FakeWeb.allow_net_connect?
15
16
  FakeWeb.allow_net_connect = false
16
17
  end
17
18
 
19
+ alias teardown_without_fakeweb teardown
18
20
  def teardown
19
21
  FakeWeb.allow_net_connect = @original_allow_net_connect
20
22
  end
@@ -48,4 +48,32 @@ class TestPrecedence < Test::Unit::TestCase
48
48
  assert_equal "string", response.body
49
49
  end
50
50
 
51
+ def test_identical_registration_replaces_previous_registration
52
+ FakeWeb.register_uri(:get, "http://example.com/test", :body => "first")
53
+ FakeWeb.register_uri(:get, "http://example.com/test", :body => "second")
54
+ response = Net::HTTP.start("example.com") { |query| query.get('/test') }
55
+ assert_equal "second", response.body
56
+ end
57
+
58
+ def test_identical_registration_replaces_previous_registration_accounting_for_normalization
59
+ FakeWeb.register_uri(:get, "http://example.com/test?", :body => "first")
60
+ FakeWeb.register_uri(:get, "http://example.com:80/test", :body => "second")
61
+ response = Net::HTTP.start("example.com") { |query| query.get('/test') }
62
+ assert_equal "second", response.body
63
+ end
64
+
65
+ def test_identical_registration_replaces_previous_registration_accounting_for_query_params
66
+ FakeWeb.register_uri(:get, "http://example.com/test?a=1&b=2", :body => "first")
67
+ FakeWeb.register_uri(:get, "http://example.com/test?b=2&a=1", :body => "second")
68
+ response = Net::HTTP.start("example.com") { |query| query.get('/test?a=1&b=2') }
69
+ assert_equal "second", response.body
70
+ end
71
+
72
+ def test_identical_registration_replaces_previous_registration_with_regexes
73
+ FakeWeb.register_uri(:get, /test/, :body => "first")
74
+ FakeWeb.register_uri(:get, /test/, :body => "second")
75
+ response = Net::HTTP.start("example.com") { |query| query.get('/test') }
76
+ assert_equal "second", response.body
77
+ end
78
+
51
79
  end
@@ -39,17 +39,28 @@ class TestRegexes < Test::Unit::TestCase
39
39
  def test_requesting_a_uri_that_matches_two_registered_regexes_raises_an_error
40
40
  FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "first")
41
41
  FakeWeb.register_uri(:get, %r|http://example\.com/a|, :body => "second")
42
- assert_raise FakeWeb::MultipleMatchingRegexpsError do
42
+ assert_raise FakeWeb::MultipleMatchingURIsError do
43
43
  Net::HTTP.start("example.com") { |query| query.get('/a') }
44
44
  end
45
45
  end
46
46
 
47
+ def test_requesting_a_uri_that_matches_two_registered_regexes_with_differently_ordered_query_params_raises_an_error
48
+ FakeWeb.register_uri(:get, %r[example.com/list\?b=2&a=1], :body => "first")
49
+ FakeWeb.register_uri(:get, %r[example.com/list\?a=1&b=2], :body => "second")
50
+ assert_raise FakeWeb::MultipleMatchingURIsError do
51
+ Net::HTTP.start("example.com") { |query| query.get('/list?a=1&b=2') }
52
+ end
53
+ assert_raise FakeWeb::MultipleMatchingURIsError do
54
+ Net::HTTP.start("example.com") { |query| query.get('/list?b=2&a=1') }
55
+ end
56
+ end
57
+
47
58
  def test_requesting_a_uri_that_matches_two_registered_regexes_raises_an_error_including_request_info
48
59
  FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "first")
49
60
  FakeWeb.register_uri(:get, %r|http://example\.com/a|, :body => "second")
50
61
  begin
51
62
  Net::HTTP.start("example.com") { |query| query.get('/a') }
52
- rescue FakeWeb::MultipleMatchingRegexpsError => exception
63
+ rescue FakeWeb::MultipleMatchingURIsError => exception
53
64
  end
54
65
  assert exception.message.include?("GET http://example.com/a")
55
66
  end
@@ -60,6 +71,36 @@ class TestRegexes < Test::Unit::TestCase
60
71
  assert !FakeWeb.registered_uri?(:get, "http://www.example.com:443")
61
72
  end
62
73
 
74
+ def test_registry_finds_using_non_default_port
75
+ FakeWeb.register_uri(:get, %r|example\.com:8080|, :body => "example")
76
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:8080/path")
77
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:8080/path")
78
+ end
79
+
80
+ def test_registry_finds_using_default_port_and_http_when_registered_with_explicit_port_80
81
+ FakeWeb.register_uri(:get, %r|example\.com:80|, :body => "example")
82
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com/path")
83
+
84
+ # check other permutations, too
85
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:80/path")
86
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:8080/path")
87
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:80/path")
88
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:8080/path")
89
+ assert !FakeWeb.registered_uri?(:get, "https://www.example.com/path")
90
+ end
91
+
92
+ def test_registry_finds_using_default_port_and_https_when_registered_with_explicit_port_443
93
+ FakeWeb.register_uri(:get, %r|example\.com:443|, :body => "example")
94
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com/path")
95
+
96
+ # check other permutations, too
97
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:443/path")
98
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:44321/path")
99
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:443/path")
100
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:44321/path")
101
+ assert !FakeWeb.registered_uri?(:get, "http://www.example.com/path")
102
+ end
103
+
63
104
  def test_registry_only_finds_using_default_port_when_registered_without_if_protocol_matches
64
105
  FakeWeb.register_uri(:get, %r|http://www.example.com/test|, :body => "example")
65
106
  assert FakeWeb.registered_uri?(:get, "http://www.example.com:80/test")
@@ -80,14 +121,14 @@ class TestRegexes < Test::Unit::TestCase
80
121
  assert !FakeWeb.registered_uri?(:get, "https://www.example.com")
81
122
  end
82
123
 
83
- def test_registry_matches_using_any_protocol_and_port_when_registered_without_protocol_or_port
84
- FakeWeb.register_uri(:get, %r|www.example.com|, :body => "example")
85
- assert FakeWeb.registered_uri?(:get, "http://www.example.com")
86
- assert FakeWeb.registered_uri?(:get, "http://www.example.com:80")
87
- assert FakeWeb.registered_uri?(:get, "http://www.example.com:443")
88
- assert FakeWeb.registered_uri?(:get, "https://www.example.com")
89
- assert FakeWeb.registered_uri?(:get, "https://www.example.com:80")
90
- assert FakeWeb.registered_uri?(:get, "https://www.example.com:443")
124
+ def test_registry_matches_using_default_port_for_protocol_when_registered_without_protocol_or_port
125
+ FakeWeb.register_uri(:get, %r|www.example.com/home|, :body => "example")
126
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com/home")
127
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com/home")
128
+ assert FakeWeb.registered_uri?(:get, "http://www.example.com:80/home")
129
+ assert FakeWeb.registered_uri?(:get, "https://www.example.com:443/home")
130
+ assert !FakeWeb.registered_uri?(:get, "https://www.example.com:80/home")
131
+ assert !FakeWeb.registered_uri?(:get, "http://www.example.com:443/home")
91
132
  end
92
133
 
93
134
  def test_registry_matches_with_query_params
@@ -100,4 +141,12 @@ class TestRegexes < Test::Unit::TestCase
100
141
  assert !FakeWeb.registered_uri?(:get, "http://example.com/list?hash=123&important=2&unimportant=1")
101
142
  assert !FakeWeb.registered_uri?(:get, "http://example.com/list?notimportant=1&unimportant=1")
102
143
  end
144
+
145
+ def test_registry_matches_with_unsorted_query_params
146
+ FakeWeb.register_uri(:get, %r[example\.com/list\?b=2&a=1], :body => "example")
147
+ assert FakeWeb.registered_uri?(:get, "http://example.com/list?b=2&a=1")
148
+ assert FakeWeb.registered_uri?(:get, "http://example.com/list?a=1&b=2")
149
+ assert FakeWeb.registered_uri?(:get, "https://example.com:443/list?b=2&a=1")
150
+ end
151
+
103
152
  end
@@ -2,13 +2,19 @@ require File.join(File.dirname(__FILE__), "test_helper")
2
2
 
3
3
  class TestResponseHeaders < Test::Unit::TestCase
4
4
 
5
- def test_content_type_when_registering_with_string_and_content_type_header
5
+ def test_content_type_when_registering_with_string_and_content_type_header_as_symbol_option
6
6
  FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', :content_type => "application/json")
7
7
  response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
8
8
  assert_equal '[{"username": "chrisk"}]', response.body
9
9
  assert_equal "application/json", response['Content-Type']
10
10
  end
11
11
 
12
+ def test_content_type_when_registering_with_string_and_content_type_header_as_string_option
13
+ FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', 'Content-Type' => "application/json")
14
+ response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
15
+ assert_equal "application/json", response['Content-Type']
16
+ end
17
+
12
18
  def test_content_type_when_registering_with_string_only
13
19
  FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]')
14
20
  response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
@@ -67,4 +67,25 @@ class TestUtility < Test::Unit::TestCase
67
67
  assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
68
68
  end
69
69
 
70
+ def test_simple_array_permutation_with_one_element
71
+ array, permutations = [1], [[1]]
72
+ FakeWeb::Utility.simple_array_permutation(array) do |permutation|
73
+ permutations.delete(permutation)
74
+ end
75
+ assert permutations.empty?
76
+ end
77
+
78
+ def test_simple_array_permutation_with_three_elements
79
+ array = [1, 2, 3]
80
+ permutations = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
81
+ FakeWeb::Utility.simple_array_permutation(array) do |permutation|
82
+ permutations.delete(permutation)
83
+ end
84
+ assert permutations.empty?
85
+ end
86
+
87
+ def test_simple_array_permutation_return_value
88
+ array = [1, 2, 3]
89
+ assert array, FakeWeb::Utility.simple_array_permutation(array) { }
90
+ end
70
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakeweb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.5
4
+ version: 1.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Kampmeier
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-07-08 00:00:00 -07:00
13
+ date: 2009-08-31 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -32,14 +32,16 @@ executables: []
32
32
  extensions: []
33
33
 
34
34
  extra_rdoc_files:
35
- - CHANGELOG
36
35
  - LICENSE.txt
37
36
  - README.rdoc
38
37
  files:
38
+ - .gitignore
39
39
  - CHANGELOG
40
40
  - LICENSE.txt
41
41
  - README.rdoc
42
42
  - Rakefile
43
+ - VERSION
44
+ - fakeweb.gemspec
43
45
  - lib/fake_web.rb
44
46
  - lib/fake_web/ext/net_http.rb
45
47
  - lib/fake_web/registry.rb
@@ -72,14 +74,7 @@ licenses: []
72
74
 
73
75
  post_install_message:
74
76
  rdoc_options:
75
- - --main
76
- - README.rdoc
77
- - --title
78
- - FakeWeb API Documentation
79
- - --charset
80
- - utf-8
81
- - --line-numbers
82
- - --inline-source
77
+ - --charset=UTF-8
83
78
  require_paths:
84
79
  - lib
85
80
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -97,16 +92,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
92
  requirements: []
98
93
 
99
94
  rubyforge_project: fakeweb
100
- rubygems_version: 1.3.4
95
+ rubygems_version: 1.3.5
101
96
  signing_key:
102
97
  specification_version: 3
103
98
  summary: A tool for faking responses to HTTP requests
104
99
  test_files:
105
- - test/fixtures/google_response_from_curl
106
- - test/fixtures/google_response_with_transfer_encoding
107
- - test/fixtures/google_response_without_transfer_encoding
108
- - test/fixtures/test_example.txt
109
- - test/fixtures/test_txt_file
110
100
  - test/test_allow_net_connect.rb
111
101
  - test/test_deprecations.rb
112
102
  - test/test_fake_authentication.rb