fakeweb 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +22 -0
- data/README.rdoc +20 -9
- data/lib/fake_web.rb +57 -44
- data/lib/fake_web/ext/net_http.rb +3 -1
- data/lib/fake_web/registry.rb +38 -7
- data/lib/fake_web/responder.rb +27 -13
- data/test/test_allow_net_connect.rb +46 -1
- data/test/test_deprecations.rb +58 -0
- data/test/test_fake_authentication.rb +13 -13
- data/test/test_fake_web.rb +98 -73
- data/test/test_fake_web_open_uri.rb +8 -8
- data/test/test_helper.rb +8 -0
- data/test/test_query_string.rb +16 -16
- data/test/test_regexes.rb +96 -0
- data/test/test_response_headers.rb +71 -0
- data/test/test_trailing_slashes.rb +8 -8
- metadata +16 -6
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
|
3
|
+
class TestDeprecations < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
FakeWeb.clean_registry
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_register_uri_without_method_argument_prints_deprecation_warning
|
10
|
+
warning = capture_stderr do
|
11
|
+
FakeWeb.register_uri("http://example.com", :body => "test")
|
12
|
+
end
|
13
|
+
assert_match /deprecation warning: fakeweb/i, warning
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_registered_uri_without_method_argument_prints_deprecation_warning
|
17
|
+
warning = capture_stderr do
|
18
|
+
FakeWeb.registered_uri?("http://example.com")
|
19
|
+
end
|
20
|
+
assert_match /deprecation warning: fakeweb/i, warning
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_response_for_without_method_argument_prints_deprecation_warning
|
24
|
+
warning = capture_stderr do
|
25
|
+
FakeWeb.response_for("http://example.com")
|
26
|
+
end
|
27
|
+
assert_match /deprecation warning: fakeweb/i, warning
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_register_uri_without_method_argument_prints_deprecation_warning_with_correct_caller
|
31
|
+
warning = capture_stderr do
|
32
|
+
FakeWeb.register_uri("http://example.com", :body => "test")
|
33
|
+
end
|
34
|
+
assert_match /Called at.*?test_deprecations\.rb/i, warning
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_register_uri_with_string_option_prints_deprecation_warning
|
38
|
+
warning = capture_stderr do
|
39
|
+
FakeWeb.register_uri(:get, "http://example.com", :string => "test")
|
40
|
+
end
|
41
|
+
assert_match /deprecation warning: fakeweb's :string option/i, warning
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_register_uri_with_file_option_prints_deprecation_warning
|
45
|
+
warning = capture_stderr do
|
46
|
+
FakeWeb.register_uri(:get, "http://example.com", :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
47
|
+
end
|
48
|
+
assert_match /deprecation warning: fakeweb's :file option/i, warning
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_register_uri_with_string_option_prints_deprecation_warning_with_correct_caller
|
52
|
+
warning = capture_stderr do
|
53
|
+
FakeWeb.register_uri(:get, "http://example.com", :string => "test")
|
54
|
+
end
|
55
|
+
assert_match /Called at.*?test_deprecations\.rb/i, warning
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -2,48 +2,48 @@ require File.join(File.dirname(__FILE__), "test_helper")
|
|
2
2
|
|
3
3
|
class TestFakeAuthentication < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
FakeWeb.register_uri('http://user:pass@mock/auth.txt', :
|
6
|
-
FakeWeb.register_uri('http://user2:pass@mock/auth.txt', :
|
7
|
-
FakeWeb.register_uri('http://mock/auth.txt', :
|
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
8
|
end
|
9
9
|
|
10
10
|
def test_register_uri_with_authentication
|
11
|
-
FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :
|
12
|
-
assert FakeWeb.registered_uri?('http://user:pass@mock/test_example.txt')
|
11
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
|
12
|
+
assert FakeWeb.registered_uri?(:get, 'http://user:pass@mock/test_example.txt')
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_register_uri_with_authentication_doesnt_trigger_without
|
16
|
-
FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :
|
17
|
-
assert !FakeWeb.registered_uri?('http://mock/test_example.txt')
|
16
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
|
17
|
+
assert !FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_register_uri_with_authentication_doesnt_trigger_with_incorrect_credentials
|
21
|
-
FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :
|
22
|
-
assert !FakeWeb.registered_uri?('http://user:wrong@mock/test_example.txt')
|
21
|
+
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
|
22
|
+
assert !FakeWeb.registered_uri?(:get, 'http://user:wrong@mock/test_example.txt')
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_unauthenticated_request
|
26
26
|
http = Net::HTTP.new('mock', 80)
|
27
27
|
req = Net::HTTP::Get.new('/auth.txt')
|
28
|
-
assert_equal http.request(req).body
|
28
|
+
assert_equal 'unauthorized', http.request(req).body
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_authenticated_request
|
32
32
|
http = Net::HTTP.new('mock',80)
|
33
33
|
req = Net::HTTP::Get.new('/auth.txt')
|
34
34
|
req.basic_auth 'user', 'pass'
|
35
|
-
assert_equal http.request(req).body
|
35
|
+
assert_equal 'authorized', http.request(req).body
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_incorrectly_authenticated_request
|
39
39
|
http = Net::HTTP.new('mock',80)
|
40
40
|
req = Net::HTTP::Get.new('/auth.txt')
|
41
41
|
req.basic_auth 'user2', 'pass'
|
42
|
-
assert_equal http.request(req).body
|
42
|
+
assert_equal 'wrong user', http.request(req).body
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_basic_auth_support_is_transparent_to_oauth
|
46
|
-
FakeWeb.register_uri(:get, "http://sp.example.com/protected", :
|
46
|
+
FakeWeb.register_uri(:get, "http://sp.example.com/protected", :body => "secret")
|
47
47
|
|
48
48
|
# from http://oauth.net/core/1.0/#auth_header
|
49
49
|
auth_header = <<-HEADER
|
data/test/test_fake_web.rb
CHANGED
@@ -8,8 +8,8 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_register_uri
|
11
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
12
|
-
assert FakeWeb.registered_uri?('http://mock/test_example.txt')
|
11
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "example")
|
12
|
+
assert FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_register_uri_with_wrong_number_of_arguments
|
@@ -17,7 +17,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
17
17
|
FakeWeb.register_uri("http://example.com")
|
18
18
|
end
|
19
19
|
assert_raises ArgumentError do
|
20
|
-
FakeWeb.register_uri(:get, "http://example.com", "/example", :
|
20
|
+
FakeWeb.register_uri(:get, "http://example.com", "/example", :body => "example")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -41,92 +41,96 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
41
41
|
|
42
42
|
def test_register_uri_without_domain_name
|
43
43
|
assert_raises URI::InvalidURIError do
|
44
|
-
FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
44
|
+
FakeWeb.register_uri(:get, 'test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_register_uri_with_port_and_check_with_port
|
49
|
-
FakeWeb.register_uri('http://example.com:3000/', :
|
50
|
-
assert FakeWeb.registered_uri?('http://example.com:3000/')
|
49
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')
|
50
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com:3000/')
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_register_uri_with_port_and_check_without_port
|
54
|
-
FakeWeb.register_uri('http://example.com:3000/', :
|
55
|
-
assert !FakeWeb.registered_uri?('http://example.com/')
|
54
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')
|
55
|
+
assert !FakeWeb.registered_uri?(:get, 'http://example.com/')
|
56
56
|
end
|
57
57
|
|
58
58
|
def test_register_uri_with_default_port_for_http_and_check_without_port
|
59
|
-
FakeWeb.register_uri('http://example.com:80/', :
|
60
|
-
assert FakeWeb.registered_uri?('http://example.com/')
|
59
|
+
FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'foo')
|
60
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com/')
|
61
61
|
end
|
62
62
|
|
63
63
|
def test_register_uri_with_default_port_for_https_and_check_without_port
|
64
|
-
FakeWeb.register_uri('https://example.com:443/', :
|
65
|
-
assert FakeWeb.registered_uri?('https://example.com/')
|
64
|
+
FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'foo')
|
65
|
+
assert FakeWeb.registered_uri?(:get, 'https://example.com/')
|
66
66
|
end
|
67
67
|
|
68
68
|
def test_register_uri_with_no_port_for_http_and_check_with_default_port
|
69
|
-
FakeWeb.register_uri('http://example.com/', :
|
70
|
-
assert FakeWeb.registered_uri?('http://example.com:80/')
|
69
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
|
70
|
+
assert FakeWeb.registered_uri?(:get, 'http://example.com:80/')
|
71
71
|
end
|
72
72
|
|
73
73
|
def test_register_uri_with_no_port_for_https_and_check_with_default_port
|
74
|
-
FakeWeb.register_uri('https://example.com/', :
|
75
|
-
assert FakeWeb.registered_uri?('https://example.com:443/')
|
74
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')
|
75
|
+
assert FakeWeb.registered_uri?(:get, 'https://example.com:443/')
|
76
76
|
end
|
77
77
|
|
78
78
|
def test_register_uri_with_no_port_for_https_and_check_with_443_on_http
|
79
|
-
FakeWeb.register_uri('https://example.com/', :
|
80
|
-
assert !FakeWeb.registered_uri?('http://example.com:443/')
|
79
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')
|
80
|
+
assert !FakeWeb.registered_uri?(:get, 'http://example.com:443/')
|
81
81
|
end
|
82
82
|
|
83
83
|
def test_register_uri_with_no_port_for_http_and_check_with_80_on_https
|
84
|
-
FakeWeb.register_uri('http://example.com/', :
|
85
|
-
assert !FakeWeb.registered_uri?('https://example.com:80/')
|
84
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')
|
85
|
+
assert !FakeWeb.registered_uri?(:get, 'https://example.com:80/')
|
86
86
|
end
|
87
87
|
|
88
88
|
def test_register_uri_for_any_method_explicitly
|
89
|
-
FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :
|
89
|
+
FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :body => "OK")
|
90
90
|
assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint")
|
91
91
|
assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint")
|
92
92
|
assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")
|
93
93
|
assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")
|
94
94
|
assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")
|
95
|
-
|
95
|
+
capture_stderr do # silence deprecation warning
|
96
|
+
assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
|
97
|
+
end
|
96
98
|
end
|
97
99
|
|
98
100
|
def test_register_uri_for_get_method_only
|
99
|
-
FakeWeb.register_uri(:get, "http://example.com/users", :
|
101
|
+
FakeWeb.register_uri(:get, "http://example.com/users", :body => "User list")
|
100
102
|
assert FakeWeb.registered_uri?(:get, "http://example.com/users")
|
101
103
|
assert !FakeWeb.registered_uri?(:post, "http://example.com/users")
|
102
104
|
assert !FakeWeb.registered_uri?(:put, "http://example.com/users")
|
103
105
|
assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")
|
104
106
|
assert !FakeWeb.registered_uri?(:any, "http://example.com/users")
|
105
|
-
|
107
|
+
capture_stderr do # silence deprecation warning
|
108
|
+
assert !FakeWeb.registered_uri?("http://example.com/users")
|
109
|
+
end
|
106
110
|
end
|
107
111
|
|
108
112
|
def test_response_for_with_registered_uri
|
109
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
110
|
-
assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
|
113
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
114
|
+
assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
|
111
115
|
end
|
112
116
|
|
113
117
|
def test_response_for_with_unknown_uri
|
114
|
-
|
118
|
+
assert_nil FakeWeb.response_for(:get, 'http://example.com/')
|
115
119
|
end
|
116
120
|
|
117
121
|
def test_response_for_with_put_method
|
118
|
-
FakeWeb.register_uri(:put, "http://example.com", :
|
122
|
+
FakeWeb.register_uri(:put, "http://example.com", :body => "response")
|
119
123
|
assert_equal 'response', FakeWeb.response_for(:put, "http://example.com").body
|
120
124
|
end
|
121
125
|
|
122
126
|
def test_response_for_with_any_method_explicitly
|
123
|
-
FakeWeb.register_uri(:any, "http://example.com", :
|
127
|
+
FakeWeb.register_uri(:any, "http://example.com", :body => "response")
|
124
128
|
assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body
|
125
129
|
assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body
|
126
130
|
end
|
127
131
|
|
128
132
|
def test_content_for_registered_uri_with_port_and_request_with_port
|
129
|
-
FakeWeb.register_uri('http://example.com:3000/', :
|
133
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'test example content')
|
130
134
|
Net::HTTP.start('example.com', 3000) do |http|
|
131
135
|
response = http.get('/')
|
132
136
|
assert_equal 'test example content', response.body
|
@@ -134,7 +138,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
134
138
|
end
|
135
139
|
|
136
140
|
def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
|
137
|
-
FakeWeb.register_uri('http://example.com:80/', :
|
141
|
+
FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'test example content')
|
138
142
|
Net::HTTP.start('example.com') do |http|
|
139
143
|
response = http.get('/')
|
140
144
|
assert_equal 'test example content', response.body
|
@@ -142,7 +146,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
142
146
|
end
|
143
147
|
|
144
148
|
def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
|
145
|
-
FakeWeb.register_uri('http://example.com/', :
|
149
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'test example content')
|
146
150
|
Net::HTTP.start('example.com', 80) do |http|
|
147
151
|
response = http.get('/')
|
148
152
|
assert_equal 'test example content', response.body
|
@@ -150,7 +154,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
150
154
|
end
|
151
155
|
|
152
156
|
def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
|
153
|
-
FakeWeb.register_uri('https://example.com:443/', :
|
157
|
+
FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'test example content')
|
154
158
|
http = Net::HTTP.new('example.com', 443)
|
155
159
|
http.use_ssl = true
|
156
160
|
response = http.get('/')
|
@@ -158,7 +162,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
158
162
|
end
|
159
163
|
|
160
164
|
def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
|
161
|
-
FakeWeb.register_uri('https://example.com/', :
|
165
|
+
FakeWeb.register_uri(:get, 'https://example.com/', :body => 'test example content')
|
162
166
|
http = Net::HTTP.new('example.com', 443)
|
163
167
|
http.use_ssl = true
|
164
168
|
response = http.get('/')
|
@@ -166,8 +170,8 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
166
170
|
end
|
167
171
|
|
168
172
|
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
|
169
|
-
FakeWeb.register_uri('http://example.com:3000/', :
|
170
|
-
FakeWeb.register_uri('http://example.com/', :
|
173
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
174
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
171
175
|
Net::HTTP.start('example.com') do |http|
|
172
176
|
response = http.get('/')
|
173
177
|
assert_equal 'port 80', response.body
|
@@ -175,8 +179,8 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
175
179
|
end
|
176
180
|
|
177
181
|
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
|
178
|
-
FakeWeb.register_uri('http://example.com:3000/', :
|
179
|
-
FakeWeb.register_uri('http://example.com/', :
|
182
|
+
FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')
|
183
|
+
FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')
|
180
184
|
Net::HTTP.start('example.com', 3000) do |http|
|
181
185
|
response = http.get('/')
|
182
186
|
assert_equal 'port 3000', response.body
|
@@ -185,7 +189,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
185
189
|
|
186
190
|
def test_content_for_registered_uri_with_get_method_only
|
187
191
|
FakeWeb.allow_net_connect = false
|
188
|
-
FakeWeb.register_uri(:get, "http://example.com/", :
|
192
|
+
FakeWeb.register_uri(:get, "http://example.com/", :body => "test example content")
|
189
193
|
Net::HTTP.start('example.com') do |http|
|
190
194
|
assert_equal 'test example content', http.get('/').body
|
191
195
|
assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }
|
@@ -196,7 +200,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
196
200
|
|
197
201
|
def test_content_for_registered_uri_with_any_method_explicitly
|
198
202
|
FakeWeb.allow_net_connect = false
|
199
|
-
FakeWeb.register_uri(:any, "http://example.com/", :
|
203
|
+
FakeWeb.register_uri(:any, "http://example.com/", :body => "test example content")
|
200
204
|
Net::HTTP.start('example.com') do |http|
|
201
205
|
assert_equal 'test example content', http.get('/').body
|
202
206
|
assert_equal 'test example content', http.post('/', nil).body
|
@@ -207,7 +211,10 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
207
211
|
|
208
212
|
def test_content_for_registered_uri_with_any_method_implicitly
|
209
213
|
FakeWeb.allow_net_connect = false
|
210
|
-
|
214
|
+
capture_stderr do # silence deprecation warning
|
215
|
+
FakeWeb.register_uri("http://example.com/", :body => "test example content")
|
216
|
+
end
|
217
|
+
|
211
218
|
Net::HTTP.start('example.com') do |http|
|
212
219
|
assert_equal 'test example content', http.get('/').body
|
213
220
|
assert_equal 'test example content', http.post('/', nil).body
|
@@ -217,7 +224,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
217
224
|
end
|
218
225
|
|
219
226
|
def test_mock_request_with_block
|
220
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
227
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
221
228
|
Net::HTTP.start('mock') do |http|
|
222
229
|
response = http.get('/test_example.txt')
|
223
230
|
assert_equal 'test example content', response.body
|
@@ -225,7 +232,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
225
232
|
end
|
226
233
|
|
227
234
|
def test_mock_request_with_undocumented_full_uri_argument_style
|
228
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
235
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
229
236
|
Net::HTTP.start('mock') do |query|
|
230
237
|
response = query.get('http://mock/test_example.txt')
|
231
238
|
assert_equal 'test example content', response.body
|
@@ -233,7 +240,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
233
240
|
end
|
234
241
|
|
235
242
|
def test_mock_request_with_undocumented_full_uri_argument_style_and_query
|
236
|
-
FakeWeb.register_uri('http://mock/test_example.txt?a=b', :
|
243
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt?a=b', :body => 'test query content')
|
237
244
|
Net::HTTP.start('mock') do |query|
|
238
245
|
response = query.get('http://mock/test_example.txt?a=b')
|
239
246
|
assert_equal 'test query content', response.body
|
@@ -241,7 +248,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
241
248
|
end
|
242
249
|
|
243
250
|
def test_mock_post
|
244
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
251
|
+
FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
245
252
|
response = nil
|
246
253
|
Net::HTTP.start('mock') do |query|
|
247
254
|
response = query.post('/test_example.txt', '')
|
@@ -251,7 +258,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
251
258
|
|
252
259
|
def test_mock_post_with_string_as_registered_uri
|
253
260
|
response = nil
|
254
|
-
FakeWeb.register_uri('http://mock/test_string.txt', :
|
261
|
+
FakeWeb.register_uri(:post, 'http://mock/test_string.txt', :body => 'foo')
|
255
262
|
Net::HTTP.start('mock') do |query|
|
256
263
|
response = query.post('/test_string.txt', '')
|
257
264
|
end
|
@@ -260,7 +267,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
260
267
|
|
261
268
|
def test_mock_get_with_request_as_registered_uri
|
262
269
|
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
263
|
-
FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
|
270
|
+
FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response)
|
264
271
|
response = nil
|
265
272
|
Net::HTTP.start('mock') do |query|
|
266
273
|
response = query.get('/test_response')
|
@@ -270,7 +277,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
270
277
|
end
|
271
278
|
|
272
279
|
def test_mock_get_with_request_from_file_as_registered_uri
|
273
|
-
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
280
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
274
281
|
response = nil
|
275
282
|
Net::HTTP.start('www.google.com') do |query|
|
276
283
|
response = query.get('/')
|
@@ -280,7 +287,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
280
287
|
end
|
281
288
|
|
282
289
|
def test_mock_post_with_request_from_file_as_registered_uri
|
283
|
-
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
290
|
+
FakeWeb.register_uri(:post, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
284
291
|
response = nil
|
285
292
|
Net::HTTP.start('www.google.com') do |query|
|
286
293
|
response = query.post('/', '')
|
@@ -290,8 +297,8 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
290
297
|
end
|
291
298
|
|
292
299
|
def test_proxy_request
|
293
|
-
FakeWeb.register_uri('http://www.example.com/', :
|
294
|
-
FakeWeb.register_uri('http://your.proxy.host/', :
|
300
|
+
FakeWeb.register_uri(:get, 'http://www.example.com/', :body => "hello world")
|
301
|
+
FakeWeb.register_uri(:get, 'http://your.proxy.host/', :body => "lala")
|
295
302
|
proxy_addr = 'your.proxy.host'
|
296
303
|
proxy_port = 8080
|
297
304
|
|
@@ -302,7 +309,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
302
309
|
end
|
303
310
|
|
304
311
|
def test_https_request
|
305
|
-
FakeWeb.register_uri('https://www.example.com/', :
|
312
|
+
FakeWeb.register_uri(:get, 'https://www.example.com/', :body => "Hello World")
|
306
313
|
http = Net::HTTP.new('www.example.com', 443)
|
307
314
|
http.use_ssl = true
|
308
315
|
response = http.get('/')
|
@@ -310,7 +317,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
310
317
|
end
|
311
318
|
|
312
319
|
def test_register_unimplemented_response
|
313
|
-
FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
|
320
|
+
FakeWeb.register_uri(:get, 'http://mock/unimplemented', :response => 1)
|
314
321
|
assert_raises StandardError do
|
315
322
|
Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
|
316
323
|
end
|
@@ -352,7 +359,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
352
359
|
def test_real_request_on_same_domain_as_mock
|
353
360
|
setup_expectations_for_real_apple_hot_news_request
|
354
361
|
|
355
|
-
FakeWeb.register_uri('http://images.apple.com/test_string.txt', :
|
362
|
+
FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')
|
356
363
|
|
357
364
|
resp = nil
|
358
365
|
Net::HTTP.start('images.apple.com') do |query|
|
@@ -363,7 +370,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
363
370
|
end
|
364
371
|
|
365
372
|
def test_mock_request_on_real_domain
|
366
|
-
FakeWeb.register_uri('http://images.apple.com/test_string.txt', :
|
373
|
+
FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')
|
367
374
|
resp = nil
|
368
375
|
Net::HTTP.start('images.apple.com') do |query|
|
369
376
|
resp = query.get('/test_string.txt')
|
@@ -372,7 +379,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
372
379
|
end
|
373
380
|
|
374
381
|
def test_mock_post_that_raises_exception
|
375
|
-
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
|
382
|
+
FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => StandardError)
|
376
383
|
assert_raises(StandardError) do
|
377
384
|
Net::HTTP.start('mock') do |query|
|
378
385
|
query.post('/raising_exception.txt', 'some data')
|
@@ -381,7 +388,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
381
388
|
end
|
382
389
|
|
383
390
|
def test_mock_post_that_raises_an_http_error
|
384
|
-
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
|
391
|
+
FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => Net::HTTPError)
|
385
392
|
assert_raises(Net::HTTPError) do
|
386
393
|
Net::HTTP.start('mock') do |query|
|
387
394
|
query.post('/raising_exception.txt', '')
|
@@ -397,7 +404,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
397
404
|
end
|
398
405
|
|
399
406
|
def test_mock_instance_syntax
|
400
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
407
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
401
408
|
response = nil
|
402
409
|
uri = URI.parse('http://mock/test_example.txt')
|
403
410
|
http = Net::HTTP.new(uri.host, uri.port)
|
@@ -412,7 +419,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
412
419
|
response = nil
|
413
420
|
proxy_address = nil
|
414
421
|
proxy_port = nil
|
415
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
422
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
416
423
|
uri = URI.parse('http://mock/test_example.txt')
|
417
424
|
http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
|
418
425
|
uri.host, (uri.port or 80))
|
@@ -424,7 +431,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
424
431
|
end
|
425
432
|
|
426
433
|
def test_response_type
|
427
|
-
FakeWeb.register_uri('http://mock/test_example.txt', :
|
434
|
+
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "test")
|
428
435
|
Net::HTTP.start('mock') do |http|
|
429
436
|
response = http.get('/test_example.txt')
|
430
437
|
assert_kind_of(Net::HTTPSuccess, response)
|
@@ -432,7 +439,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
432
439
|
end
|
433
440
|
|
434
441
|
def test_mock_request_that_raises_an_http_error_with_a_specific_status
|
435
|
-
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
|
442
|
+
FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
|
436
443
|
exception = assert_raises(Net::HTTPError) do
|
437
444
|
Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
|
438
445
|
end
|
@@ -441,10 +448,10 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
441
448
|
end
|
442
449
|
|
443
450
|
def test_mock_rotate_responses
|
444
|
-
FakeWeb.register_uri('http://mock/multiple_test_example.txt',
|
445
|
-
[ {:
|
446
|
-
{:
|
447
|
-
{:
|
451
|
+
FakeWeb.register_uri(:get, 'http://mock/multiple_test_example.txt',
|
452
|
+
[ {:body => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},
|
453
|
+
{:body => "thrice", :times => 3},
|
454
|
+
{:body => "ever_more"} ])
|
448
455
|
|
449
456
|
uri = URI.parse('http://mock/multiple_test_example.txt')
|
450
457
|
2.times { assert_equal 'test example content', Net::HTTP.get(uri) }
|
@@ -453,7 +460,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
453
460
|
end
|
454
461
|
|
455
462
|
def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
|
456
|
-
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
|
463
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
|
457
464
|
response = nil
|
458
465
|
Net::HTTP.start('www.google.com') do |query|
|
459
466
|
response = query.get('/')
|
@@ -463,7 +470,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
463
470
|
end
|
464
471
|
|
465
472
|
def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
|
466
|
-
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
473
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
|
467
474
|
response = nil
|
468
475
|
Net::HTTP.start('www.google.com') do |query|
|
469
476
|
response = query.get('/')
|
@@ -472,7 +479,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
472
479
|
end
|
473
480
|
|
474
481
|
def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
|
475
|
-
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
|
482
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
|
476
483
|
response = nil
|
477
484
|
Net::HTTP.start('www.google.com') do |query|
|
478
485
|
response = query.get('/')
|
@@ -482,7 +489,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
482
489
|
end
|
483
490
|
|
484
491
|
def test_txt_file_should_have_three_lines
|
485
|
-
FakeWeb.register_uri('http://www.google.com/', :
|
492
|
+
FakeWeb.register_uri(:get, 'http://www.google.com/', :body => File.dirname(__FILE__) + '/fixtures/test_txt_file')
|
486
493
|
response = nil
|
487
494
|
Net::HTTP.start('www.google.com') do |query|
|
488
495
|
response = query.get('/')
|
@@ -494,26 +501,44 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
494
501
|
require "fakeweb"
|
495
502
|
end
|
496
503
|
|
497
|
-
def
|
504
|
+
def test_registering_with_string_containing_null_byte
|
498
505
|
# Regression test for File.exists? raising an ArgumentError ("string
|
499
506
|
# contains null byte") since :response first tries to find by filename.
|
500
507
|
# The string should be treated as a response body, instead, and an
|
501
508
|
# EOFError is raised when the byte is encountered.
|
502
|
-
FakeWeb.register_uri("http://example.com", :response => "test\0test")
|
509
|
+
FakeWeb.register_uri(:get, "http://example.com", :response => "test\0test")
|
503
510
|
assert_raise EOFError do
|
504
511
|
Net::HTTP.get(URI.parse("http://example.com"))
|
505
512
|
end
|
513
|
+
|
514
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "test\0test")
|
515
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
516
|
+
assert_equal "test\0test", body
|
517
|
+
end
|
518
|
+
|
519
|
+
def test_registering_with_string_that_is_a_directory_name
|
520
|
+
# Similar to above, but for Errno::EISDIR being raised since File.exists?
|
521
|
+
# returns true for directories
|
522
|
+
FakeWeb.register_uri(:get, "http://example.com", :response => File.dirname(__FILE__))
|
523
|
+
assert_raise EOFError do
|
524
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
525
|
+
end
|
526
|
+
|
527
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__))
|
528
|
+
body = Net::HTTP.get(URI.parse("http://example.com"))
|
529
|
+
assert_equal File.dirname(__FILE__), body
|
506
530
|
end
|
507
531
|
|
508
532
|
def test_http_version_from_string_response
|
509
|
-
FakeWeb.register_uri(:get, "http://example.com", :
|
533
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
|
510
534
|
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
511
535
|
assert_equal "1.0", response.http_version
|
512
536
|
end
|
513
537
|
|
514
538
|
def test_http_version_from_file_response
|
515
|
-
FakeWeb.register_uri(:get, "http://example.com", :
|
539
|
+
FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')
|
516
540
|
response = Net::HTTP.start("example.com") { |http| http.get("/") }
|
517
541
|
assert_equal "1.0", response.http_version
|
518
542
|
end
|
543
|
+
|
519
544
|
end
|