fakeweb 1.2.4 → 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/Rakefile +11 -5
- data/lib/fake_web.rb +6 -0
- data/lib/fake_web/ext/net_http.rb +3 -3
- data/lib/fake_web/registry.rb +41 -48
- data/lib/fake_web/utility.rb +22 -0
- data/test/test_allow_net_connect.rb +9 -10
- data/test/test_deprecations.rb +0 -4
- data/test/test_fake_authentication.rb +32 -8
- data/test/test_fake_web.rb +73 -99
- data/test/test_fake_web_open_uri.rb +3 -7
- data/test/test_helper.rb +15 -1
- data/test/test_missing_open_uri.rb +2 -1
- data/test/test_precedence.rb +51 -0
- data/test/test_query_string.rb +0 -4
- data/test/test_regexes.rb +19 -12
- data/test/test_response_headers.rb +9 -13
- data/test/test_trailing_slashes.rb +0 -9
- data/test/test_utility.rb +70 -0
- metadata +11 -10
data/CHANGELOG
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
fakeweb (1.2.5)
|
2
|
+
|
3
|
+
* fix handling of userinfo strings that contain percent-encoded unsafe
|
4
|
+
characters [Chris Kampmeier, Ken Mayer]
|
5
|
+
|
6
|
+
* fix that exact matches against strings/URIs with the :any method had a lower
|
7
|
+
precedence than regex matches using a real HTTP method (exact matches now
|
8
|
+
always take precedence) [Chris Kampmeier]
|
9
|
+
|
10
|
+
* change request handling to raise an exception when more than one registered
|
11
|
+
regex matches a request URI [Chris Kampmeier]
|
12
|
+
|
13
|
+
|
1
14
|
fakeweb (1.2.4)
|
2
15
|
|
3
16
|
* add experimental support for matching URIs via regular expressions
|
data/Rakefile
CHANGED
@@ -1,7 +1,15 @@
|
|
1
|
+
puts "Using ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
|
2
|
+
|
1
3
|
require 'rubygems'
|
2
4
|
require 'rake/gempackagetask'
|
3
5
|
require 'rake/testtask'
|
4
|
-
|
6
|
+
begin
|
7
|
+
require 'rdoc/task'
|
8
|
+
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
|
12
|
+
end
|
5
13
|
|
6
14
|
task :default => :test
|
7
15
|
|
@@ -12,13 +20,12 @@ Rake::TestTask.new :test do |test|
|
|
12
20
|
end
|
13
21
|
|
14
22
|
desc "Generate Documentation"
|
15
|
-
|
23
|
+
RDoc::Task.new do |rdoc|
|
16
24
|
rdoc.main = "README.rdoc"
|
17
25
|
rdoc.rdoc_dir = "doc"
|
18
26
|
rdoc.rdoc_files.include("README.rdoc", "CHANGELOG", "LICENSE.txt", "lib/*.rb")
|
19
27
|
rdoc.title = "FakeWeb API Documentation"
|
20
|
-
rdoc.options << '--line-numbers' << '--
|
21
|
-
rdoc.options << '--charset' << 'utf-8'
|
28
|
+
rdoc.options << '--line-numbers' << '--charset' << 'utf-8'
|
22
29
|
end
|
23
30
|
|
24
31
|
desc %{Update ".manifest" with the latest list of project filenames. Respect\
|
@@ -59,7 +66,6 @@ else
|
|
59
66
|
t.test_files = FileList['test/**/test*.rb']
|
60
67
|
t.rcov_opts << "--sort coverage"
|
61
68
|
t.rcov_opts << "--exclude gems"
|
62
|
-
t.rcov_opts << "--no-validator-links"
|
63
69
|
end
|
64
70
|
end
|
65
71
|
|
data/lib/fake_web.rb
CHANGED
@@ -5,6 +5,7 @@ require 'fake_web/registry'
|
|
5
5
|
require 'fake_web/response'
|
6
6
|
require 'fake_web/responder'
|
7
7
|
require 'fake_web/stub_socket'
|
8
|
+
require 'fake_web/utility'
|
8
9
|
|
9
10
|
module FakeWeb
|
10
11
|
|
@@ -45,6 +46,11 @@ module FakeWeb
|
|
45
46
|
# stubbed.
|
46
47
|
class NetConnectNotAllowedError < StandardError; end;
|
47
48
|
|
49
|
+
# 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
|
+
|
48
54
|
# call-seq:
|
49
55
|
# FakeWeb.register_uri(method, uri, options)
|
50
56
|
#
|
@@ -41,7 +41,8 @@ module Net #:nodoc: all
|
|
41
41
|
path = URI.parse(request.path).request_uri if request.path =~ /^http/
|
42
42
|
|
43
43
|
if request["authorization"] =~ /^Basic /
|
44
|
-
userinfo = request["authorization"]
|
44
|
+
userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
|
45
|
+
userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
|
45
46
|
else
|
46
47
|
userinfo = ""
|
47
48
|
end
|
@@ -56,8 +57,7 @@ module Net #:nodoc: all
|
|
56
57
|
connect_without_fakeweb
|
57
58
|
request_without_fakeweb(request, body, &block)
|
58
59
|
else
|
59
|
-
uri.
|
60
|
-
uri.sub!(":443/", "/") if uri =~ %r|^https://|
|
60
|
+
uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
61
61
|
raise FakeWeb::NetConnectNotAllowedError,
|
62
62
|
"Real HTTP connections are disabled. Unregistered request: #{request.method} #{uri}"
|
63
63
|
end
|
data/lib/fake_web/registry.rb
CHANGED
@@ -2,60 +2,30 @@ module FakeWeb
|
|
2
2
|
class Registry #:nodoc:
|
3
3
|
include Singleton
|
4
4
|
|
5
|
-
attr_accessor :uri_map
|
5
|
+
attr_accessor :uri_map
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
clean_registry
|
9
9
|
end
|
10
10
|
|
11
11
|
def clean_registry
|
12
|
-
self.uri_map = Hash.new
|
13
|
-
hash[key] = Hash.new(&hash.default_proc)
|
14
|
-
end
|
15
|
-
self.pattern_map = []
|
12
|
+
self.uri_map = Hash.new { |hash, key| hash[key] = {} }
|
16
13
|
end
|
17
14
|
|
18
15
|
def register_uri(method, uri, options)
|
19
|
-
|
20
|
-
|
21
|
-
uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
|
22
|
-
FakeWeb::Responder.new(method, uri, option, option[:times])
|
23
|
-
end
|
24
|
-
when Regexp
|
25
|
-
responders = [*[options]].flatten.collect do |option|
|
26
|
-
FakeWeb::Responder.new(method, uri, option, option[:times])
|
27
|
-
end
|
28
|
-
pattern_map << {:pattern => uri,
|
29
|
-
:responders => responders,
|
30
|
-
:method => method }
|
31
|
-
|
16
|
+
uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
|
17
|
+
FakeWeb::Responder.new(method, uri, option, option[:times])
|
32
18
|
end
|
33
19
|
end
|
34
20
|
|
35
21
|
def registered_uri?(method, uri)
|
36
22
|
normalized_uri = normalize_uri(uri)
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def registered_uri(method, uri)
|
41
|
-
uri = normalize_uri(uri)
|
42
|
-
registered = registered_uri?(method, uri)
|
43
|
-
if registered && uri_map[uri].has_key?(method)
|
44
|
-
uri_map[uri][method]
|
45
|
-
elsif registered && pattern_map_matches?(method, uri)
|
46
|
-
pattern_map_matches(method, uri).map{|m| m[:responders]}.flatten
|
47
|
-
elsif registered && uri_map[uri].has_key?(:any)
|
48
|
-
uri_map[uri][:any]
|
49
|
-
elsif registered && pattern_map_matches(:any, uri)
|
50
|
-
pattern_map_matches(:any, uri).map{|m| m[:responders]}.flatten
|
51
|
-
else
|
52
|
-
nil
|
53
|
-
end
|
23
|
+
!responses_for(method, uri).empty?
|
54
24
|
end
|
55
25
|
|
56
26
|
def response_for(method, uri, &block)
|
57
|
-
responses =
|
58
|
-
return nil if responses.
|
27
|
+
responses = responses_for(method, uri)
|
28
|
+
return nil if responses.empty?
|
59
29
|
|
60
30
|
next_response = responses.last
|
61
31
|
responses.each do |response|
|
@@ -69,24 +39,47 @@ module FakeWeb
|
|
69
39
|
next_response.response(&block)
|
70
40
|
end
|
71
41
|
|
72
|
-
def pattern_map_matches?(method, uri)
|
73
|
-
!pattern_map_matches(method, uri).empty?
|
74
|
-
end
|
75
42
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
uri
|
80
|
-
|
43
|
+
private
|
44
|
+
|
45
|
+
def responses_for(method, uri)
|
46
|
+
uri = normalize_uri(uri)
|
47
|
+
|
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
|
81
59
|
end
|
82
60
|
|
83
|
-
def
|
84
|
-
|
61
|
+
def uri_map_matches?(method, uri)
|
62
|
+
!uri_map_matches(method, uri).nil?
|
85
63
|
end
|
86
64
|
|
87
|
-
|
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)
|
68
|
+
|
69
|
+
matches = uri_map.select { |registered_uri, method_hash|
|
70
|
+
registered_uri.is_a?(Regexp) && uri.match(registered_uri) && method_hash.has_key?(method)
|
71
|
+
}
|
72
|
+
|
73
|
+
if matches.size > 1
|
74
|
+
raise MultipleMatchingRegexpsError,
|
75
|
+
"More than one regular expression matched this request: #{method.to_s.upcase} #{uri}"
|
76
|
+
end
|
77
|
+
|
78
|
+
matches.map { |_, method_hash| method_hash[method] }.first
|
79
|
+
end
|
88
80
|
|
89
81
|
def normalize_uri(uri)
|
82
|
+
return uri if uri.is_a?(Regexp)
|
90
83
|
normalized_uri =
|
91
84
|
case uri
|
92
85
|
when URI then uri
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module FakeWeb
|
2
|
+
module Utility #:nodoc:
|
3
|
+
|
4
|
+
def self.decode_userinfo_from_header(header)
|
5
|
+
header.sub(/^Basic /, "").unpack("m").first
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.encode_unsafe_chars_in_userinfo(userinfo)
|
9
|
+
unsafe_in_userinfo = /[^#{URI::REGEXP::PATTERN::UNRESERVED};&=+$,]|^(#{URI::REGEXP::PATTERN::ESCAPED})/
|
10
|
+
userinfo.split(":").map { |part| URI.escape(part, unsafe_in_userinfo) }.join(":")
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.strip_default_port_from_uri(uri)
|
14
|
+
case uri
|
15
|
+
when %r{^http://} then uri.sub(%r{:80(/|$)}, '\1')
|
16
|
+
when %r{^https://} then uri.sub(%r{:443(/|$)}, '\1')
|
17
|
+
else uri
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -2,15 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestFakeWebAllowNetConnect < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
@original_allow_net_connect = FakeWeb.allow_net_connect?
|
7
|
-
end
|
8
|
-
|
9
|
-
def teardown
|
10
|
-
FakeWeb.allow_net_connect = @original_allow_net_connect
|
11
|
-
end
|
12
|
-
|
13
|
-
|
14
5
|
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
|
15
6
|
FakeWeb.allow_net_connect = true
|
16
7
|
setup_expectations_for_real_apple_hot_news_request
|
@@ -79,8 +70,16 @@ class TestFakeWebAllowNetConnect < Test::Unit::TestCase
|
|
79
70
|
assert !FakeWeb.allow_net_connect?
|
80
71
|
end
|
81
72
|
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
class TestFakeWebAllowNetConnectWithCleanState < Test::Unit::TestCase
|
77
|
+
# Our test_helper.rb sets allow_net_connect = false in an inherited #setup
|
78
|
+
# method. Disable that here to test the default setting.
|
79
|
+
def setup; end
|
80
|
+
def teardown; end
|
81
|
+
|
82
82
|
def test_allow_net_connect_is_true_by_default
|
83
83
|
assert FakeWeb.allow_net_connect?
|
84
84
|
end
|
85
|
-
|
86
85
|
end
|
data/test/test_deprecations.rb
CHANGED
@@ -2,10 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestDeprecations < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
FakeWeb.clean_registry
|
7
|
-
end
|
8
|
-
|
9
5
|
def test_register_uri_without_method_argument_prints_deprecation_warning
|
10
6
|
warning = capture_stderr do
|
11
7
|
FakeWeb.register_uri("http://example.com", :body => "test")
|
@@ -1,11 +1,6 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "test_helper")
|
2
2
|
|
3
3
|
class TestFakeAuthentication < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
FakeWeb.register_uri(:get, 'http://user:pass@mock/auth.txt', :body => 'authorized')
|
6
|
-
FakeWeb.register_uri(:get, 'http://user2:pass@mock/auth.txt', :body => 'wrong user')
|
7
|
-
FakeWeb.register_uri(:get, 'http://mock/auth.txt', :body => 'unauthorized')
|
8
|
-
end
|
9
4
|
|
10
5
|
def test_register_uri_with_authentication
|
11
6
|
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
|
@@ -23,23 +18,27 @@ class TestFakeAuthentication < Test::Unit::TestCase
|
|
23
18
|
end
|
24
19
|
|
25
20
|
def test_unauthenticated_request
|
21
|
+
FakeWeb.register_uri(:get, 'http://mock/auth.txt', :body => 'unauthorized')
|
26
22
|
http = Net::HTTP.new('mock', 80)
|
27
23
|
req = Net::HTTP::Get.new('/auth.txt')
|
28
24
|
assert_equal 'unauthorized', http.request(req).body
|
29
25
|
end
|
30
26
|
|
31
27
|
def test_authenticated_request
|
28
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/auth.txt', :body => 'authorized')
|
32
29
|
http = Net::HTTP.new('mock',80)
|
33
30
|
req = Net::HTTP::Get.new('/auth.txt')
|
34
31
|
req.basic_auth 'user', 'pass'
|
35
32
|
assert_equal 'authorized', http.request(req).body
|
36
33
|
end
|
37
34
|
|
38
|
-
def
|
39
|
-
|
35
|
+
def test_authenticated_request_where_only_userinfo_differs
|
36
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/auth.txt', :body => 'first user')
|
37
|
+
FakeWeb.register_uri(:get, 'http://user2:pass@mock/auth.txt', :body => 'second user')
|
38
|
+
http = Net::HTTP.new('mock')
|
40
39
|
req = Net::HTTP::Get.new('/auth.txt')
|
41
40
|
req.basic_auth 'user2', 'pass'
|
42
|
-
assert_equal '
|
41
|
+
assert_equal 'second user', http.request(req).body
|
43
42
|
end
|
44
43
|
|
45
44
|
def test_basic_auth_support_is_transparent_to_oauth
|
@@ -65,4 +64,29 @@ class TestFakeAuthentication < Test::Unit::TestCase
|
|
65
64
|
end
|
66
65
|
assert_equal "secret", response.body
|
67
66
|
end
|
67
|
+
|
68
|
+
def test_basic_auth_when_userinfo_contains_allowed_unencoded_characters
|
69
|
+
FakeWeb.register_uri(:get, "http://roses&hel1o,(+$):so;longs=@example.com", :body => "authorized")
|
70
|
+
http = Net::HTTP.new("example.com")
|
71
|
+
request = Net::HTTP::Get.new("/")
|
72
|
+
request.basic_auth("roses&hel1o,(+$)", "so;longs=")
|
73
|
+
assert_equal "authorized", http.request(request).body
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_basic_auth_when_userinfo_contains_encoded_at_sign
|
77
|
+
FakeWeb.register_uri(:get, "http://user%40example.com:secret@example.com", :body => "authorized")
|
78
|
+
http = Net::HTTP.new("example.com")
|
79
|
+
request = Net::HTTP::Get.new("/")
|
80
|
+
request.basic_auth("user@example.com", "secret")
|
81
|
+
assert_equal "authorized", http.request(request).body
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_basic_auth_when_userinfo_contains_allowed_encoded_characters
|
85
|
+
FakeWeb.register_uri(:get, "http://us%20er:sec%20%2F%2Fret%3F@example.com", :body => "authorized")
|
86
|
+
http = Net::HTTP.new("example.com")
|
87
|
+
request = Net::HTTP::Get.new("/")
|
88
|
+
request.basic_auth("us er", "sec //ret?")
|
89
|
+
assert_equal "authorized", http.request(request).body
|
90
|
+
end
|
91
|
+
|
68
92
|
end
|
data/test/test_fake_web.rb
CHANGED
@@ -2,11 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestFakeWeb < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
FakeWeb.allow_net_connect = true
|
7
|
-
FakeWeb.clean_registry
|
8
|
-
end
|
9
|
-
|
10
5
|
def test_register_uri
|
11
6
|
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "example")
|
12
7
|
assert FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
|
@@ -131,26 +126,20 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
131
126
|
|
132
127
|
def test_content_for_registered_uri_with_port_and_request_with_port
|
133
128
|
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'test example content')
|
134
|
-
Net::HTTP.start('example.com', 3000)
|
135
|
-
|
136
|
-
assert_equal 'test example content', response.body
|
137
|
-
end
|
129
|
+
response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }
|
130
|
+
assert_equal 'test example content', response.body
|
138
131
|
end
|
139
132
|
|
140
133
|
def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
|
141
134
|
FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'test example content')
|
142
|
-
Net::HTTP.start('example.com')
|
143
|
-
|
144
|
-
assert_equal 'test example content', response.body
|
145
|
-
end
|
135
|
+
response = Net::HTTP.start('example.com') { |http| http.get('/') }
|
136
|
+
assert_equal 'test example content', response.body
|
146
137
|
end
|
147
138
|
|
148
139
|
def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
|
149
140
|
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'test example content')
|
150
|
-
Net::HTTP.start('example.com', 80)
|
151
|
-
|
152
|
-
assert_equal 'test example content', response.body
|
153
|
-
end
|
141
|
+
response = Net::HTTP.start('example.com', 80) { |http| http.get('/') }
|
142
|
+
assert_equal 'test example content', response.body
|
154
143
|
end
|
155
144
|
|
156
145
|
def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
|
@@ -172,41 +161,35 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
172
161
|
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
|
173
162
|
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
174
163
|
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
175
|
-
Net::HTTP.start('example.com')
|
176
|
-
|
177
|
-
assert_equal 'port 80', response.body
|
178
|
-
end
|
164
|
+
response = Net::HTTP.start('example.com') { |http| http.get('/') }
|
165
|
+
assert_equal 'port 80', response.body
|
179
166
|
end
|
180
167
|
|
181
168
|
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
|
182
169
|
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
183
170
|
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
184
|
-
Net::HTTP.start('example.com', 3000)
|
185
|
-
|
186
|
-
assert_equal 'port 3000', response.body
|
187
|
-
end
|
171
|
+
response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }
|
172
|
+
assert_equal 'port 3000', response.body
|
188
173
|
end
|
189
174
|
|
190
175
|
def test_content_for_registered_uri_with_get_method_only
|
191
176
|
FakeWeb.allow_net_connect = false
|
192
177
|
FakeWeb.register_uri(:get, "http://example.com/", :body => "test example content")
|
193
|
-
Net::HTTP.
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
end
|
178
|
+
http = Net::HTTP.new('example.com')
|
179
|
+
assert_equal 'test example content', http.get('/').body
|
180
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }
|
181
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) }
|
182
|
+
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/') }
|
199
183
|
end
|
200
184
|
|
201
185
|
def test_content_for_registered_uri_with_any_method_explicitly
|
202
186
|
FakeWeb.allow_net_connect = false
|
203
187
|
FakeWeb.register_uri(:any, "http://example.com/", :body => "test example content")
|
204
|
-
Net::HTTP.
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
end
|
188
|
+
http = Net::HTTP.new('example.com')
|
189
|
+
assert_equal 'test example content', http.get('/').body
|
190
|
+
assert_equal 'test example content', http.post('/', nil).body
|
191
|
+
assert_equal 'test example content', http.put('/', nil).body
|
192
|
+
assert_equal 'test example content', http.delete('/').body
|
210
193
|
end
|
211
194
|
|
212
195
|
def test_content_for_registered_uri_with_any_method_implicitly
|
@@ -215,83 +198,84 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
215
198
|
FakeWeb.register_uri("http://example.com/", :body => "test example content")
|
216
199
|
end
|
217
200
|
|
218
|
-
Net::HTTP.
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
end
|
201
|
+
http = Net::HTTP.new('example.com')
|
202
|
+
assert_equal 'test example content', http.get('/').body
|
203
|
+
assert_equal 'test example content', http.post('/', nil).body
|
204
|
+
assert_equal 'test example content', http.put('/', nil).body
|
205
|
+
assert_equal 'test example content', http.delete('/').body
|
224
206
|
end
|
225
207
|
|
226
208
|
def test_mock_request_with_block
|
227
209
|
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
228
|
-
Net::HTTP.start('mock')
|
229
|
-
|
230
|
-
|
210
|
+
response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
|
211
|
+
assert_equal 'test example content', response.body
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_request_with_registered_body_yields_the_response_body_to_a_request_block
|
215
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "content")
|
216
|
+
body = nil
|
217
|
+
Net::HTTP.start("example.com") do |http|
|
218
|
+
http.get("/") do |response_body|
|
219
|
+
body = response_body
|
220
|
+
end
|
221
|
+
end
|
222
|
+
assert_equal "content", body
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_request_with_registered_response_yields_the_response_body_to_a_request_block
|
226
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
227
|
+
fake_response.instance_variable_set(:@body, "content")
|
228
|
+
FakeWeb.register_uri(:get, 'http://example.com', :response => fake_response)
|
229
|
+
body = nil
|
230
|
+
Net::HTTP.start("example.com") do |http|
|
231
|
+
http.get("/") do |response_body|
|
232
|
+
body = response_body
|
233
|
+
end
|
231
234
|
end
|
235
|
+
assert_equal "content", body
|
232
236
|
end
|
233
237
|
|
234
238
|
def test_mock_request_with_undocumented_full_uri_argument_style
|
235
239
|
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
236
|
-
Net::HTTP.start('mock')
|
237
|
-
|
238
|
-
assert_equal 'test example content', response.body
|
239
|
-
end
|
240
|
+
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt') }
|
241
|
+
assert_equal 'test example content', response.body
|
240
242
|
end
|
241
243
|
|
242
244
|
def test_mock_request_with_undocumented_full_uri_argument_style_and_query
|
243
245
|
FakeWeb.register_uri(:get, 'http://mock/test_example.txt?a=b', :body => 'test query content')
|
244
|
-
Net::HTTP.start('mock')
|
245
|
-
|
246
|
-
assert_equal 'test query content', response.body
|
247
|
-
end
|
246
|
+
response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt?a=b') }
|
247
|
+
assert_equal 'test query content', response.body
|
248
248
|
end
|
249
249
|
|
250
250
|
def test_mock_post
|
251
251
|
FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
252
|
-
response =
|
253
|
-
Net::HTTP.start('mock') do |query|
|
254
|
-
response = query.post('/test_example.txt', '')
|
255
|
-
end
|
252
|
+
response = Net::HTTP.start('mock') { |query| query.post('/test_example.txt', '') }
|
256
253
|
assert_equal 'test example content', response.body
|
257
254
|
end
|
258
255
|
|
259
256
|
def test_mock_post_with_string_as_registered_uri
|
260
|
-
response = nil
|
261
257
|
FakeWeb.register_uri(:post, 'http://mock/test_string.txt', :body => 'foo')
|
262
|
-
Net::HTTP.start('mock')
|
263
|
-
response = query.post('/test_string.txt', '')
|
264
|
-
end
|
258
|
+
response = Net::HTTP.start('mock') { |query| query.post('/test_string.txt', '') }
|
265
259
|
assert_equal 'foo', response.body
|
266
260
|
end
|
267
261
|
|
268
262
|
def test_mock_get_with_request_as_registered_uri
|
269
263
|
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
270
264
|
FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response)
|
271
|
-
response =
|
272
|
-
Net::HTTP.start('mock') do |query|
|
273
|
-
response = query.get('/test_response')
|
274
|
-
end
|
275
|
-
|
265
|
+
response = Net::HTTP.start('mock') { |query| query.get('/test_response') }
|
276
266
|
assert_equal fake_response, response
|
277
267
|
end
|
278
268
|
|
279
269
|
def test_mock_get_with_request_from_file_as_registered_uri
|
280
270
|
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
281
|
-
response =
|
282
|
-
Net::HTTP.start('www.google.com') do |query|
|
283
|
-
response = query.get('/')
|
284
|
-
end
|
271
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
285
272
|
assert_equal '200', response.code
|
286
273
|
assert response.body.include?('<title>Google</title>')
|
287
274
|
end
|
288
275
|
|
289
276
|
def test_mock_post_with_request_from_file_as_registered_uri
|
290
277
|
FakeWeb.register_uri(:post, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
291
|
-
response =
|
292
|
-
Net::HTTP.start('www.google.com') do |query|
|
293
|
-
response = query.post('/', '')
|
294
|
-
end
|
278
|
+
response = Net::HTTP.start('www.google.com') { |query| query.post('/', '') }
|
295
279
|
assert_equal "200", response.code
|
296
280
|
assert response.body.include?('<title>Google</title>')
|
297
281
|
end
|
@@ -299,13 +283,12 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
299
283
|
def test_proxy_request
|
300
284
|
FakeWeb.register_uri(:get, 'http://www.example.com/', :body => "hello world")
|
301
285
|
FakeWeb.register_uri(:get, 'http://your.proxy.host/', :body => "lala")
|
302
|
-
proxy_addr = 'your.proxy.host'
|
303
|
-
proxy_port = 8080
|
304
286
|
|
305
|
-
|
287
|
+
response = nil
|
288
|
+
Net::HTTP::Proxy('your.proxy.host', 8080).start('www.example.com') do |http|
|
306
289
|
response = http.get('/')
|
307
|
-
assert_equal "hello world", response.body
|
308
290
|
end
|
291
|
+
assert_equal "hello world", response.body
|
309
292
|
end
|
310
293
|
|
311
294
|
def test_https_request
|
@@ -324,6 +307,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
324
307
|
end
|
325
308
|
|
326
309
|
def test_real_http_request
|
310
|
+
FakeWeb.allow_net_connect = true
|
327
311
|
setup_expectations_for_real_apple_hot_news_request
|
328
312
|
|
329
313
|
resp = nil
|
@@ -335,6 +319,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
335
319
|
end
|
336
320
|
|
337
321
|
def test_real_http_request_with_undocumented_full_uri_argument_style
|
322
|
+
FakeWeb.allow_net_connect = true
|
338
323
|
setup_expectations_for_real_apple_hot_news_request(:path => 'http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
339
324
|
|
340
325
|
resp = nil
|
@@ -346,6 +331,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
346
331
|
end
|
347
332
|
|
348
333
|
def test_real_https_request
|
334
|
+
FakeWeb.allow_net_connect = true
|
349
335
|
setup_expectations_for_real_apple_hot_news_request(:port => 443)
|
350
336
|
|
351
337
|
http = Net::HTTP.new('images.apple.com', 443)
|
@@ -357,6 +343,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
357
343
|
end
|
358
344
|
|
359
345
|
def test_real_request_on_same_domain_as_mock
|
346
|
+
FakeWeb.allow_net_connect = true
|
360
347
|
setup_expectations_for_real_apple_hot_news_request
|
361
348
|
|
362
349
|
FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')
|
@@ -432,16 +419,14 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
432
419
|
|
433
420
|
def test_response_type
|
434
421
|
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "test")
|
435
|
-
Net::HTTP.start('mock')
|
436
|
-
|
437
|
-
assert_kind_of(Net::HTTPSuccess, response)
|
438
|
-
end
|
422
|
+
response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }
|
423
|
+
assert_kind_of Net::HTTPSuccess, response
|
439
424
|
end
|
440
425
|
|
441
426
|
def test_mock_request_that_raises_an_http_error_with_a_specific_status
|
442
427
|
FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
|
443
428
|
exception = assert_raises(Net::HTTPError) do
|
444
|
-
Net::HTTP.start('mock') { |http|
|
429
|
+
Net::HTTP.start('mock') { |http| http.get('/raising_exception.txt') }
|
445
430
|
end
|
446
431
|
assert_equal '404', exception.response.code
|
447
432
|
assert_equal 'Not Found', exception.response.msg
|
@@ -461,10 +446,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
461
446
|
|
462
447
|
def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
|
463
448
|
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
|
464
|
-
response =
|
465
|
-
Net::HTTP.start('www.google.com') do |query|
|
466
|
-
response = query.get('/')
|
467
|
-
end
|
449
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
468
450
|
assert_not_nil response['transfer-encoding']
|
469
451
|
assert response['transfer-encoding'] == 'chunked'
|
470
452
|
end
|
@@ -472,28 +454,20 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
472
454
|
def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
|
473
455
|
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
474
456
|
response = nil
|
475
|
-
Net::HTTP.start('www.google.com')
|
476
|
-
response = query.get('/')
|
477
|
-
end
|
457
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
478
458
|
assert !response.key?('transfer-encoding')
|
479
459
|
end
|
480
460
|
|
481
461
|
def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
|
482
462
|
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
|
483
|
-
response =
|
484
|
-
Net::HTTP.start('www.google.com') do |query|
|
485
|
-
response = query.get('/')
|
486
|
-
end
|
463
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
487
464
|
assert_not_nil response['transfer-encoding']
|
488
465
|
assert response['transfer-encoding'] == 'chunked'
|
489
466
|
end
|
490
467
|
|
491
468
|
def test_txt_file_should_have_three_lines
|
492
469
|
FakeWeb.register_uri(:get, 'http://www.google.com/', :body => File.dirname(__FILE__) + '/fixtures/test_txt_file')
|
493
|
-
response =
|
494
|
-
Net::HTTP.start('www.google.com') do |query|
|
495
|
-
response = query.get('/')
|
496
|
-
end
|
470
|
+
response = Net::HTTP.start('www.google.com') { |query| query.get('/') }
|
497
471
|
assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"
|
498
472
|
end
|
499
473
|
|
@@ -2,10 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestFakeWebOpenURI < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
FakeWeb.clean_registry
|
7
|
-
end
|
8
|
-
|
9
5
|
def test_content_for_registered_uri
|
10
6
|
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
11
7
|
assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
|
@@ -22,6 +18,7 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
|
|
22
18
|
end
|
23
19
|
|
24
20
|
def test_real_open
|
21
|
+
FakeWeb.allow_net_connect = true
|
25
22
|
setup_expectations_for_real_apple_hot_news_request
|
26
23
|
resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
27
24
|
assert_equal "200", resp.status.first
|
@@ -55,8 +52,7 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
|
|
55
52
|
|
56
53
|
def test_mock_open_with_block
|
57
54
|
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
58
|
-
open('http://mock/test_example.txt')
|
59
|
-
|
60
|
-
end
|
55
|
+
body = open('http://mock/test_example.txt') { |f| f.readlines }
|
56
|
+
assert_equal 'test example content', body.first
|
61
57
|
end
|
62
58
|
end
|
data/test/test_helper.rb
CHANGED
@@ -6,6 +6,21 @@ require 'fake_web'
|
|
6
6
|
require 'rubygems'
|
7
7
|
require 'mocha'
|
8
8
|
|
9
|
+
|
10
|
+
# Give all tests a common setup and teardown that prevents shared state
|
11
|
+
class Test::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
FakeWeb.clean_registry
|
14
|
+
@original_allow_net_connect = FakeWeb.allow_net_connect?
|
15
|
+
FakeWeb.allow_net_connect = false
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
FakeWeb.allow_net_connect = @original_allow_net_connect
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
9
24
|
module FakeWebTestHelper
|
10
25
|
|
11
26
|
def capture_stderr
|
@@ -40,7 +55,6 @@ module FakeWebTestHelper
|
|
40
55
|
request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
|
41
56
|
socket.expects(:write).with(all_of(includes(request_parts[0]), includes(request_parts[1]))).returns(100)
|
42
57
|
|
43
|
-
# TODO: handle long response bodies that use more than one #sysread call
|
44
58
|
socket.expects(:sysread).at_least_once.returns("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}").then.raises(EOFError)
|
45
59
|
end
|
46
60
|
|
@@ -3,12 +3,13 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
3
3
|
class TestMissingOpenURI < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def setup
|
6
|
-
|
6
|
+
super
|
7
7
|
@saved_open_uri = OpenURI
|
8
8
|
Object.send(:remove_const, :OpenURI)
|
9
9
|
end
|
10
10
|
|
11
11
|
def teardown
|
12
|
+
super
|
12
13
|
Object.const_set(:OpenURI, @saved_open_uri)
|
13
14
|
end
|
14
15
|
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestPrecedence < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_matching_get_strings_have_precedence_over_matching_get_regexes
|
6
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "string")
|
7
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/test|, :body => "regex")
|
8
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
9
|
+
assert_equal "string", response.body
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_matching_any_strings_have_precedence_over_matching_any_regexes
|
13
|
+
FakeWeb.register_uri(:any, "http://example.com/test", :body => "string")
|
14
|
+
FakeWeb.register_uri(:any, %r|http://example\.com/test|, :body => "regex")
|
15
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
16
|
+
assert_equal "string", response.body
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_matching_get_strings_have_precedence_over_matching_any_strings
|
20
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "get method")
|
21
|
+
FakeWeb.register_uri(:any, "http://example.com/test", :body => "any method")
|
22
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
23
|
+
assert_equal "get method", response.body
|
24
|
+
|
25
|
+
# registration order should not matter
|
26
|
+
FakeWeb.register_uri(:any, "http://example.com/test2", :body => "any method")
|
27
|
+
FakeWeb.register_uri(:get, "http://example.com/test2", :body => "get method")
|
28
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test2') }
|
29
|
+
assert_equal "get method", response.body
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_matching_any_strings_have_precedence_over_matching_get_regexes
|
33
|
+
FakeWeb.register_uri(:any, "http://example.com/test", :body => "any string")
|
34
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/test|, :body => "get regex")
|
35
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
36
|
+
assert_equal "any string", response.body
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_registered_strings_and_uris_are_equivalent_so_second_takes_precedence
|
40
|
+
FakeWeb.register_uri(:get, "http://example.com/test", :body => "string")
|
41
|
+
FakeWeb.register_uri(:get, URI.parse("http://example.com/test"), :body => "uri")
|
42
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test') }
|
43
|
+
assert_equal "uri", response.body
|
44
|
+
|
45
|
+
FakeWeb.register_uri(:get, URI.parse("http://example.com/test2"), :body => "uri")
|
46
|
+
FakeWeb.register_uri(:get, "http://example.com/test2", :body => "string")
|
47
|
+
response = Net::HTTP.start("example.com") { |query| query.get('/test2') }
|
48
|
+
assert_equal "string", response.body
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/test/test_query_string.rb
CHANGED
@@ -2,10 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestFakeWebQueryString < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
FakeWeb.clean_registry
|
7
|
-
end
|
8
|
-
|
9
5
|
def test_register_uri_string_with_query_params
|
10
6
|
FakeWeb.register_uri(:get, 'http://example.com/?a=1&b=1', :body => 'foo')
|
11
7
|
assert FakeWeb.registered_uri?(:get, 'http://example.com/?a=1&b=1')
|
data/test/test_regexes.rb
CHANGED
@@ -2,15 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestRegexes < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
FakeWeb.clean_registry
|
7
|
-
@original_allow_net_connect = FakeWeb.allow_net_connect?
|
8
|
-
end
|
9
|
-
|
10
|
-
def teardown
|
11
|
-
FakeWeb.allow_net_connect = @old_allow_net_conncet
|
12
|
-
end
|
13
|
-
|
14
5
|
def test_registered_uri_with_pattern
|
15
6
|
FakeWeb.register_uri(:get, %r|http://example.com/test_example/\d+|, :body => "example")
|
16
7
|
assert FakeWeb.registered_uri?(:get, "http://example.com/test_example/25")
|
@@ -22,6 +13,11 @@ class TestRegexes < Test::Unit::TestCase
|
|
22
13
|
assert_equal "Welcome to Google!", FakeWeb.response_for(:get, "http://www.google.com").body
|
23
14
|
end
|
24
15
|
|
16
|
+
def test_response_for_with_matching_registered_uri_and_get_method_matching_to_any_method
|
17
|
+
FakeWeb.register_uri(:any, %r|http://www.example.com|, :body => "example")
|
18
|
+
assert_equal "example", FakeWeb.response_for(:get, "http://www.example.com").body
|
19
|
+
end
|
20
|
+
|
25
21
|
def test_registered_uri_with_authentication_and_pattern
|
26
22
|
FakeWeb.register_uri(:get, %r|http://user:pass@mock/example\.\w+|i, :body => "example")
|
27
23
|
assert FakeWeb.registered_uri?(:get, 'http://user:pass@mock/example.txt')
|
@@ -40,11 +36,22 @@ class TestRegexes < Test::Unit::TestCase
|
|
40
36
|
assert_equal "example", http.request(req).body
|
41
37
|
end
|
42
38
|
|
43
|
-
def
|
39
|
+
def test_requesting_a_uri_that_matches_two_registered_regexes_raises_an_error
|
40
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "first")
|
41
|
+
FakeWeb.register_uri(:get, %r|http://example\.com/a|, :body => "second")
|
42
|
+
assert_raise FakeWeb::MultipleMatchingRegexpsError do
|
43
|
+
Net::HTTP.start("example.com") { |query| query.get('/a') }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_requesting_a_uri_that_matches_two_registered_regexes_raises_an_error_including_request_info
|
44
48
|
FakeWeb.register_uri(:get, %r|http://example\.com/|, :body => "first")
|
45
49
|
FakeWeb.register_uri(:get, %r|http://example\.com/a|, :body => "second")
|
46
|
-
|
47
|
-
|
50
|
+
begin
|
51
|
+
Net::HTTP.start("example.com") { |query| query.get('/a') }
|
52
|
+
rescue FakeWeb::MultipleMatchingRegexpsError => exception
|
53
|
+
end
|
54
|
+
assert exception.message.include?("GET http://example.com/a")
|
48
55
|
end
|
49
56
|
|
50
57
|
def test_registry_does_not_find_using_mismatched_protocols_or_ports_when_registered_with_both
|
@@ -2,10 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestResponseHeaders < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
FakeWeb.clean_registry
|
7
|
-
end
|
8
|
-
|
9
5
|
def test_content_type_when_registering_with_string_and_content_type_header
|
10
6
|
FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', :content_type => "application/json")
|
11
7
|
response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
|
@@ -45,17 +41,17 @@ class TestResponseHeaders < Test::Unit::TestCase
|
|
45
41
|
:content_type => "text/plain"},
|
46
42
|
{:body => 'test2', :expires => "Thu, 14 Jun 2009 16:00:01 GMT"}])
|
47
43
|
|
44
|
+
first_response = second_response = nil
|
48
45
|
Net::HTTP.start("example.com") do |query|
|
49
|
-
|
50
|
-
|
51
|
-
assert_equal "Thu, 14 Jun 2009 16:00:00 GMT", response['Expires']
|
52
|
-
assert_equal "text/plain", response['Content-Type']
|
53
|
-
|
54
|
-
response = query.get("/")
|
55
|
-
assert_equal 'test2', response.body
|
56
|
-
assert_equal "Thu, 14 Jun 2009 16:00:01 GMT", response['Expires']
|
57
|
-
assert_nil response['Content-Type']
|
46
|
+
first_response = query.get("/")
|
47
|
+
second_response = query.get("/")
|
58
48
|
end
|
49
|
+
assert_equal 'test1', first_response.body
|
50
|
+
assert_equal "Thu, 14 Jun 2009 16:00:00 GMT", first_response['Expires']
|
51
|
+
assert_equal "text/plain", first_response['Content-Type']
|
52
|
+
assert_equal 'test2', second_response.body
|
53
|
+
assert_equal "Thu, 14 Jun 2009 16:00:01 GMT", second_response['Expires']
|
54
|
+
assert_nil second_response['Content-Type']
|
59
55
|
end
|
60
56
|
|
61
57
|
def test_registering_with_status_option_and_response_headers
|
@@ -2,15 +2,6 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestFakeWebTrailingSlashes < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def setup
|
6
|
-
FakeWeb.clean_registry
|
7
|
-
@original_allow_net_connect = FakeWeb.allow_net_connect?
|
8
|
-
end
|
9
|
-
|
10
|
-
def teardown
|
11
|
-
FakeWeb.allow_net_connect = @old_allow_net_conncet
|
12
|
-
end
|
13
|
-
|
14
5
|
def test_registering_root_without_slash_and_ask_predicate_method_with_slash
|
15
6
|
FakeWeb.register_uri(:get, "http://www.example.com", :body => "root")
|
16
7
|
assert FakeWeb.registered_uri?(:get, "http://www.example.com/")
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestUtility < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_decode_userinfo_from_header_handles_basic_auth
|
6
|
+
authorization_header = "Basic dXNlcm5hbWU6c2VjcmV0"
|
7
|
+
userinfo = FakeWeb::Utility.decode_userinfo_from_header(authorization_header)
|
8
|
+
assert_equal "username:secret", userinfo
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_encode_unsafe_chars_in_userinfo_does_not_encode_userinfo_safe_punctuation
|
12
|
+
userinfo = "user;&=+$,:secret"
|
13
|
+
assert_equal userinfo, FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_encode_unsafe_chars_in_userinfo_does_not_encode_rfc_3986_unreserved_characters
|
17
|
+
userinfo = "-_.!~*'()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:secret"
|
18
|
+
assert_equal userinfo, FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_encode_unsafe_chars_in_userinfo_does_encode_other_characters
|
22
|
+
userinfo, safe_userinfo = 'us#rn@me:sec//ret?"', 'us%23rn%40me:sec%2F%2Fret%3F%22'
|
23
|
+
assert_equal safe_userinfo, FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_strip_default_port_from_uri_strips_80_from_http_with_path
|
27
|
+
uri = "http://example.com:80/foo/bar"
|
28
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
29
|
+
assert_equal "http://example.com/foo/bar", stripped_uri
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_strip_default_port_from_uri_strips_80_from_http_without_path
|
33
|
+
uri = "http://example.com:80"
|
34
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
35
|
+
assert_equal "http://example.com", stripped_uri
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_strip_default_port_from_uri_strips_443_from_https_without_path
|
39
|
+
uri = "https://example.com:443"
|
40
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
41
|
+
assert_equal "https://example.com", stripped_uri
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_strip_default_port_from_uri_strips_443_from_https
|
45
|
+
uri = "https://example.com:443/foo/bar"
|
46
|
+
stripped_uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
|
47
|
+
assert_equal "https://example.com/foo/bar", stripped_uri
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_strip_default_port_from_uri_does_not_strip_8080_from_http
|
51
|
+
uri = "http://example.com:8080/foo/bar"
|
52
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_strip_default_port_from_uri_does_not_strip_443_from_http
|
56
|
+
uri = "http://example.com:443/foo/bar"
|
57
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_strip_default_port_from_uri_does_not_strip_80_from_query_string
|
61
|
+
uri = "http://example.com/?a=:80&b=c"
|
62
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_strip_default_port_from_uri_does_not_modify_strings_that_do_not_start_with_http_or_https
|
66
|
+
uri = "httpz://example.com:80/"
|
67
|
+
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
68
|
+
end
|
69
|
+
|
70
|
+
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.
|
4
|
+
version: 1.2.5
|
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-
|
13
|
+
date: 2009-07-08 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -40,18 +40,14 @@ files:
|
|
40
40
|
- LICENSE.txt
|
41
41
|
- README.rdoc
|
42
42
|
- Rakefile
|
43
|
-
- lib
|
44
|
-
- lib/fake_web
|
45
43
|
- lib/fake_web.rb
|
46
|
-
- lib/fake_web/ext
|
47
44
|
- lib/fake_web/ext/net_http.rb
|
48
45
|
- lib/fake_web/registry.rb
|
49
46
|
- lib/fake_web/responder.rb
|
50
47
|
- lib/fake_web/response.rb
|
51
48
|
- lib/fake_web/stub_socket.rb
|
49
|
+
- lib/fake_web/utility.rb
|
52
50
|
- lib/fakeweb.rb
|
53
|
-
- test
|
54
|
-
- test/fixtures
|
55
51
|
- test/fixtures/google_response_from_curl
|
56
52
|
- test/fixtures/google_response_with_transfer_encoding
|
57
53
|
- test/fixtures/google_response_without_transfer_encoding
|
@@ -64,12 +60,16 @@ files:
|
|
64
60
|
- test/test_fake_web_open_uri.rb
|
65
61
|
- test/test_helper.rb
|
66
62
|
- test/test_missing_open_uri.rb
|
63
|
+
- test/test_precedence.rb
|
67
64
|
- test/test_query_string.rb
|
68
65
|
- test/test_regexes.rb
|
69
66
|
- test/test_response_headers.rb
|
70
67
|
- test/test_trailing_slashes.rb
|
68
|
+
- test/test_utility.rb
|
71
69
|
has_rdoc: true
|
72
70
|
homepage: http://github.com/chrisk/fakeweb
|
71
|
+
licenses: []
|
72
|
+
|
73
73
|
post_install_message:
|
74
74
|
rdoc_options:
|
75
75
|
- --main
|
@@ -97,12 +97,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
requirements: []
|
98
98
|
|
99
99
|
rubyforge_project: fakeweb
|
100
|
-
rubygems_version: 1.3.
|
100
|
+
rubygems_version: 1.3.4
|
101
101
|
signing_key:
|
102
|
-
specification_version:
|
102
|
+
specification_version: 3
|
103
103
|
summary: A tool for faking responses to HTTP requests
|
104
104
|
test_files:
|
105
|
-
- test/fixtures
|
106
105
|
- test/fixtures/google_response_from_curl
|
107
106
|
- test/fixtures/google_response_with_transfer_encoding
|
108
107
|
- test/fixtures/google_response_without_transfer_encoding
|
@@ -115,7 +114,9 @@ test_files:
|
|
115
114
|
- test/test_fake_web_open_uri.rb
|
116
115
|
- test/test_helper.rb
|
117
116
|
- test/test_missing_open_uri.rb
|
117
|
+
- test/test_precedence.rb
|
118
118
|
- test/test_query_string.rb
|
119
119
|
- test/test_regexes.rb
|
120
120
|
- test/test_response_headers.rb
|
121
121
|
- test/test_trailing_slashes.rb
|
122
|
+
- test/test_utility.rb
|