kronk 1.0.3 → 1.1.0
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/{History.txt → History.rdoc} +22 -0
- data/Manifest.txt +14 -2
- data/README.rdoc +258 -0
- data/Rakefile +4 -0
- data/lib/kronk.rb +239 -102
- data/lib/kronk/diff.rb +2 -2
- data/lib/kronk/request.rb +146 -20
- data/lib/kronk/xml_parser.rb +1 -1
- data/test/mocks/200_response.json +28 -0
- data/test/mocks/200_response.plist +97 -0
- data/test/mocks/200_response.txt +23 -0
- data/test/mocks/200_response.xml +58 -0
- data/test/mocks/301_response.txt +16 -0
- data/test/mocks/302_response.txt +17 -0
- data/test/test_kronk.rb +142 -32
- data/test/test_request.rb +319 -12
- metadata +36 -20
- data/README.txt +0 -122
@@ -0,0 +1,16 @@
|
|
1
|
+
HTTP/1.1 301 Moved Permanently
|
2
|
+
Location: http://www.google.com/
|
3
|
+
Content-Type: text/html; charset=UTF-8
|
4
|
+
Date: Fri, 26 Nov 2010 16:14:45 GMT
|
5
|
+
Expires: Sun, 26 Dec 2010 16:14:45 GMT
|
6
|
+
Cache-Control: public, max-age=2592000
|
7
|
+
Server: gws
|
8
|
+
Content-Length: 219
|
9
|
+
X-XSS-Protection: 1; mode=block
|
10
|
+
|
11
|
+
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
|
12
|
+
<TITLE>301 Moved</TITLE></HEAD><BODY>
|
13
|
+
<H1>301 Moved</H1>
|
14
|
+
The document has moved
|
15
|
+
<A HREF="http://www.google.com/">here</A>.
|
16
|
+
</BODY></HTML>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
HTTP/1.1 302 Found
|
2
|
+
Location: http://igoogle.com/
|
3
|
+
Content-Type: text/html; charset=UTF-8
|
4
|
+
Date: Fri, 26 Nov 2010 16:14:45 GMT
|
5
|
+
Expires: Sun, 26 Dec 2010 16:14:45 GMT
|
6
|
+
Cache-Control: public, max-age=2592000
|
7
|
+
Server: gws
|
8
|
+
Content-Length: 260
|
9
|
+
X-XSS-Protection: 1; mode=block
|
10
|
+
|
11
|
+
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
|
12
|
+
<TITLE>302 Found</TITLE></HEAD><BODY>
|
13
|
+
<H1>302 Found</H1>
|
14
|
+
The document has moved
|
15
|
+
<A HREF="http://www.google.com/">here</A>.
|
16
|
+
<A HREF="http://igoogle.com/">here</A>.
|
17
|
+
</BODY></HTML>
|
data/test/test_kronk.rb
CHANGED
@@ -4,17 +4,20 @@ class TestKronk < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
def test_default_config
|
6
6
|
expected = {
|
7
|
-
:content_types
|
7
|
+
:content_types => {
|
8
8
|
'js' => 'JSON',
|
9
9
|
'json' => 'JSON',
|
10
10
|
'plist' => 'PlistParser',
|
11
11
|
'xml' => 'XMLParser'
|
12
12
|
},
|
13
|
-
:cache_file
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
17
|
-
:
|
13
|
+
:cache_file => Kronk::DEFAULT_CACHE_FILE,
|
14
|
+
:cookies_file => Kronk::DEFAULT_COOKIES_FILE,
|
15
|
+
:diff_format => :ascii_diff,
|
16
|
+
:show_lines => false,
|
17
|
+
:use_cookies => true,
|
18
|
+
:requires => [],
|
19
|
+
:uri_options => {},
|
20
|
+
:user_agents => Kronk::USER_AGENTS
|
18
21
|
}
|
19
22
|
|
20
23
|
assert_equal expected, Kronk::DEFAULT_CONFIG
|
@@ -29,9 +32,12 @@ class TestKronk < Test::Unit::TestCase
|
|
29
32
|
},
|
30
33
|
:ignore_headers => ["Content-Type"],
|
31
34
|
:cache_file => Kronk::DEFAULT_CACHE_FILE,
|
35
|
+
:cookies_file => Kronk::DEFAULT_COOKIES_FILE,
|
32
36
|
:show_lines => false,
|
37
|
+
:use_cookies => true,
|
33
38
|
:requires => [],
|
34
39
|
:uri_options => {'example.com' => {:parser => 'JSON'}},
|
40
|
+
:user_agents => {:win_ie6 => 'piece of crap browser'},
|
35
41
|
:foo => :bar
|
36
42
|
}
|
37
43
|
|
@@ -50,10 +56,14 @@ class TestKronk < Test::Unit::TestCase
|
|
50
56
|
},
|
51
57
|
:diff_format => :ascii_diff,
|
52
58
|
:cache_file => Kronk::DEFAULT_CACHE_FILE,
|
59
|
+
:cookies_file => Kronk::DEFAULT_COOKIES_FILE,
|
53
60
|
:requires => [],
|
54
61
|
:show_lines => false,
|
62
|
+
:use_cookies => true,
|
55
63
|
:ignore_headers => ["Content-Type"],
|
56
64
|
:uri_options => {'example.com' => {:parser => 'JSON'}},
|
65
|
+
:user_agents =>
|
66
|
+
Kronk::USER_AGENTS.merge(:win_ie6 => 'piece of crap browser'),
|
57
67
|
:foo => :bar
|
58
68
|
}
|
59
69
|
|
@@ -144,6 +154,62 @@ class TestKronk < Test::Unit::TestCase
|
|
144
154
|
end
|
145
155
|
|
146
156
|
|
157
|
+
def test_load_cookie_jar
|
158
|
+
Kronk.clear_cookies!
|
159
|
+
mock_cookie_jar = YAML.load_file("test/mocks/cookies.yml")
|
160
|
+
|
161
|
+
File.expects(:file?).with(Kronk::DEFAULT_COOKIES_FILE).returns true
|
162
|
+
YAML.expects(:load_file).with(Kronk::DEFAULT_COOKIES_FILE).
|
163
|
+
returns mock_cookie_jar
|
164
|
+
|
165
|
+
cookie_jar = Kronk.load_cookie_jar
|
166
|
+
|
167
|
+
assert CookieJar::Jar === cookie_jar
|
168
|
+
assert !cookie_jar.get_cookies("http://rubygems.org/").empty?
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
def test_load_cookie_jar_no_file
|
173
|
+
Kronk.clear_cookies!
|
174
|
+
mock_cookie_jar = YAML.load_file("test/mocks/cookies.yml")
|
175
|
+
|
176
|
+
File.expects(:file?).with(Kronk::DEFAULT_COOKIES_FILE).returns false
|
177
|
+
YAML.expects(:load_file).with(Kronk::DEFAULT_COOKIES_FILE).never
|
178
|
+
|
179
|
+
cookie_jar = Kronk.load_cookie_jar
|
180
|
+
assert cookie_jar.get_cookies("http://rubygems.org/").empty?
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
def test_save_cookie_jar
|
185
|
+
mock_file = mock "mockfile"
|
186
|
+
mock_file.expects(:write).with Kronk.cookie_jar.to_yaml
|
187
|
+
File.expects(:open).with(Kronk::DEFAULT_COOKIES_FILE, "w").yields mock_file
|
188
|
+
|
189
|
+
Kronk.save_cookie_jar
|
190
|
+
end
|
191
|
+
|
192
|
+
|
193
|
+
def test_clear_cookies
|
194
|
+
mock_cookie_jar = YAML.load_file("test/mocks/cookies.yml")
|
195
|
+
|
196
|
+
File.expects(:file?).with(Kronk::DEFAULT_COOKIES_FILE).returns true
|
197
|
+
YAML.expects(:load_file).with(Kronk::DEFAULT_COOKIES_FILE).
|
198
|
+
returns mock_cookie_jar
|
199
|
+
|
200
|
+
assert !Kronk.cookie_jar.get_cookies("http://rubygems.org/").empty?
|
201
|
+
|
202
|
+
Kronk.clear_cookies!
|
203
|
+
|
204
|
+
assert Kronk.cookie_jar.get_cookies("http://rubygems.org/").empty?
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
def test_cookie_jar
|
209
|
+
assert_equal Kronk.instance_variable_get("@cookie_jar"), Kronk.cookie_jar
|
210
|
+
end
|
211
|
+
|
212
|
+
|
147
213
|
def test_compare_data
|
148
214
|
diff = Kronk.compare "test/mocks/200_response.json",
|
149
215
|
"test/mocks/200_response.xml",
|
@@ -163,42 +229,86 @@ class TestKronk < Test::Unit::TestCase
|
|
163
229
|
end
|
164
230
|
|
165
231
|
|
166
|
-
def
|
167
|
-
|
168
|
-
|
169
|
-
|
232
|
+
def test_retrieve_data_string
|
233
|
+
str = Kronk.retrieve_data_string "test/mocks/200_response.json"
|
234
|
+
expected = <<-STR
|
235
|
+
{
|
236
|
+
"business" => {
|
237
|
+
"address" => "3845 Rivertown Pkwy SW Ste 500",
|
238
|
+
"city" => "Grandville",
|
239
|
+
"description" => {
|
240
|
+
"additional_urls" => [
|
241
|
+
{
|
242
|
+
"destination" => "http://example.com",
|
243
|
+
"url_click" => "http://example.com"
|
244
|
+
}
|
245
|
+
],
|
246
|
+
"general_info" => "<p>A Paint Your Own Pottery Studios..</p>",
|
247
|
+
"op_hours" => "Fri 1pm-7pm, Sat 10am-6pm, Sun 1pm-4pm, Appointments Available",
|
248
|
+
"payment_text" => "DISCOVER, AMEX, VISA, MASTERCARD",
|
249
|
+
"slogan" => "<p>Pottery YOU dress up</p>"
|
250
|
+
},
|
251
|
+
"distance" => 0.0,
|
252
|
+
"has_detail_page" => true,
|
253
|
+
"headings" => [
|
254
|
+
"Pottery"
|
255
|
+
],
|
256
|
+
"id" => "1234",
|
257
|
+
"impression_id" => "mock_iid",
|
258
|
+
"improvable" => true,
|
259
|
+
"latitude" => 42.882561,
|
260
|
+
"listing_id" => "1234",
|
261
|
+
"listing_type" => "free",
|
262
|
+
"longitude" => -85.759586,
|
263
|
+
"mappable" => true,
|
264
|
+
"name" => "Naked Plates",
|
265
|
+
"omit_address" => false,
|
266
|
+
"omit_phone" => false,
|
267
|
+
"phone" => "6168055326",
|
268
|
+
"rateable" => true,
|
269
|
+
"rating_count" => 0,
|
270
|
+
"red_listing" => false,
|
271
|
+
"state" => "MI",
|
272
|
+
"website" => "http://example.com",
|
273
|
+
"year_established" => "1996",
|
274
|
+
"zip" => "49418"
|
275
|
+
},
|
276
|
+
"original_request" => {
|
277
|
+
"id" => "1234"
|
278
|
+
},
|
279
|
+
"request_id" => "mock_rid"
|
280
|
+
}
|
281
|
+
STR
|
282
|
+
assert_equal expected.strip, str
|
283
|
+
end
|
170
284
|
|
171
|
-
resp1 = Kronk::Request.retrieve "test/mocks/200_response.json",
|
172
|
-
:with_headers => true,
|
173
|
-
:raw => true
|
174
285
|
|
175
|
-
|
176
|
-
|
177
|
-
|
286
|
+
def test_retrieve_data_string_raw
|
287
|
+
str = Kronk.retrieve_data_string "test/mocks/200_response.json", :raw => 1
|
288
|
+
expected = File.read("test/mocks/200_response.json").split("\r\n\r\n")[1]
|
289
|
+
assert_equal expected, str
|
290
|
+
end
|
178
291
|
|
179
|
-
exp_diff = Kronk::Diff.new resp1.selective_string(:with_headers => true),
|
180
|
-
resp2.selective_string(:with_headers => true)
|
181
292
|
|
182
|
-
|
183
|
-
|
293
|
+
def test_retrieve_data_string_struct
|
294
|
+
str = Kronk.retrieve_data_string "test/mocks/200_response.json",
|
295
|
+
:struct => true
|
184
296
|
|
297
|
+
expected = JSON.parse \
|
298
|
+
File.read("test/mocks/200_response.json").split("\r\n\r\n")[1]
|
185
299
|
|
186
|
-
|
187
|
-
diff = Kronk.data_diff "test/mocks/200_response.json",
|
188
|
-
"test/mocks/200_response.xml",
|
189
|
-
:with_headers => true
|
300
|
+
expected = Kronk::Diff.ordered_data_string expected, true
|
190
301
|
|
191
|
-
|
192
|
-
|
302
|
+
assert_equal expected, str
|
303
|
+
end
|
193
304
|
|
194
|
-
resp2 = Kronk::Request.retrieve "test/mocks/200_response.xml",
|
195
|
-
:with_headers => true
|
196
305
|
|
197
|
-
|
198
|
-
|
199
|
-
resp2.selective_data(:with_headers => true)
|
306
|
+
def test_retrieve_data_string_missing_parser
|
307
|
+
str = Kronk.retrieve_data_string "test/mocks/200_response.txt"
|
200
308
|
|
201
|
-
|
309
|
+
expected = File.read("test/mocks/200_response.txt").split("\r\n\r\n")[1]
|
310
|
+
|
311
|
+
assert_equal expected, str
|
202
312
|
end
|
203
313
|
|
204
314
|
|
data/test/test_request.rb
CHANGED
@@ -6,7 +6,7 @@ class TestRequest < Test::Unit::TestCase
|
|
6
6
|
resp = mock "resp"
|
7
7
|
resp.expects(:[]).with("Location").returns "http://example.com"
|
8
8
|
|
9
|
-
options = {:follow_redirects => true}
|
9
|
+
options = {:follow_redirects => true, :http_method => :get}
|
10
10
|
Kronk::Request.expects(:retrieve_uri).with("http://example.com", options)
|
11
11
|
|
12
12
|
Kronk::Request.follow_redirect resp, options
|
@@ -19,7 +19,7 @@ class TestRequest < Test::Unit::TestCase
|
|
19
19
|
|
20
20
|
options = {:follow_redirects => 1}
|
21
21
|
Kronk::Request.expects(:retrieve_uri).
|
22
|
-
with "http://example.com", :follow_redirects => 0
|
22
|
+
with "http://example.com", :follow_redirects => 0, :http_method => :get
|
23
23
|
|
24
24
|
Kronk::Request.follow_redirect resp, options
|
25
25
|
end
|
@@ -114,7 +114,8 @@ class TestRequest < Test::Unit::TestCase
|
|
114
114
|
:status => '301'
|
115
115
|
|
116
116
|
Kronk::Request.expects(:follow_redirect).
|
117
|
-
with resp, :follow_redirects => true
|
117
|
+
with resp, :follow_redirects => true,
|
118
|
+
:headers => {'User-Agent' => Kronk::USER_AGENTS['kronk']}
|
118
119
|
|
119
120
|
Kronk::Request.retrieve_uri "http://example.com/request/path?foo=bar",
|
120
121
|
:follow_redirects => true
|
@@ -126,7 +127,8 @@ class TestRequest < Test::Unit::TestCase
|
|
126
127
|
:status => '301', :data => "foo=bar"
|
127
128
|
|
128
129
|
Kronk::Request.expects(:follow_redirect).
|
129
|
-
with resp, :follow_redirects => 3, :data => {:foo => "bar"}
|
130
|
+
with resp, :follow_redirects => 3, :data => {:foo => "bar"},
|
131
|
+
:headers => {'User-Agent' => Kronk::USER_AGENTS['kronk']}
|
130
132
|
|
131
133
|
Kronk::Request.retrieve_uri "http://example.com/request/path?foo=bar",
|
132
134
|
:follow_redirects => 3, :data => {:foo => "bar"}
|
@@ -142,6 +144,7 @@ class TestRequest < Test::Unit::TestCase
|
|
142
144
|
Kronk::Request.retrieve_uri "http://example.com/request/path?foo=bar"
|
143
145
|
end
|
144
146
|
|
147
|
+
|
145
148
|
def test_retrieve_uri_post
|
146
149
|
expect_request "POST", "http://example.com/request/path?foo=bar",
|
147
150
|
:data => 'test=thing', :headers => {'X-THING' => 'thing'}
|
@@ -172,6 +175,290 @@ class TestRequest < Test::Unit::TestCase
|
|
172
175
|
end
|
173
176
|
|
174
177
|
|
178
|
+
def test_call_cookies
|
179
|
+
Kronk.cookie_jar.expects(:get_cookie_header).
|
180
|
+
with("http://example.com/request/path?foo=bar").returns "mock_cookie"
|
181
|
+
|
182
|
+
Kronk.cookie_jar.expects(:set_cookies_from_headers).
|
183
|
+
with("http://example.com/request/path?foo=bar", {})
|
184
|
+
|
185
|
+
expect_request "GET", "http://example.com/request/path?foo=bar",
|
186
|
+
:headers => {'Cookie' => "mock_cookie", 'User-Agent' => "kronk"}
|
187
|
+
|
188
|
+
resp = Kronk::Request.call :get, "http://example.com/request/path",
|
189
|
+
:query => "foo=bar", :headers => {'User-Agent' => "kronk"}
|
190
|
+
end
|
191
|
+
|
192
|
+
|
193
|
+
def test_call_no_cookies_found
|
194
|
+
Kronk.cookie_jar.expects(:get_cookie_header).
|
195
|
+
with("http://example.com/request/path?foo=bar").returns ""
|
196
|
+
|
197
|
+
expect_request "GET", "http://example.com/request/path?foo=bar",
|
198
|
+
:headers => {'User-Agent' => "kronk"}
|
199
|
+
|
200
|
+
resp = Kronk::Request.call :get, "http://example.com/request/path",
|
201
|
+
:query => "foo=bar", :headers => {'User-Agent' => "kronk"}
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
|
+
def test_call_no_cookies
|
206
|
+
Kronk.cookie_jar.expects(:get_cookie_header).
|
207
|
+
with("http://example.com/request/path?foo=bar").never
|
208
|
+
|
209
|
+
Kronk.cookie_jar.expects(:set_cookies_from_headers).never
|
210
|
+
|
211
|
+
expect_request "GET", "http://example.com/request/path?foo=bar",
|
212
|
+
:headers => {'User-Agent' => "kronk"}
|
213
|
+
|
214
|
+
resp = Kronk::Request.call :get, "http://example.com/request/path",
|
215
|
+
:query => "foo=bar", :headers => {'User-Agent' => "kronk"},
|
216
|
+
:no_cookies => true
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
def test_call_no_cookies_config
|
221
|
+
old_config = Kronk.config[:use_cookies]
|
222
|
+
Kronk.config[:use_cookies] = false
|
223
|
+
|
224
|
+
Kronk.cookie_jar.expects(:get_cookie_header).
|
225
|
+
with("http://example.com/request/path?foo=bar").never
|
226
|
+
|
227
|
+
Kronk.cookie_jar.expects(:set_cookies_from_headers).never
|
228
|
+
|
229
|
+
expect_request "GET", "http://example.com/request/path?foo=bar",
|
230
|
+
:headers => {'User-Agent' => "kronk"}
|
231
|
+
|
232
|
+
resp = Kronk::Request.call :get, "http://example.com/request/path",
|
233
|
+
:query => "foo=bar", :headers => {'User-Agent' => "kronk"}
|
234
|
+
|
235
|
+
Kronk.config[:use_cookies] = old_config
|
236
|
+
end
|
237
|
+
|
238
|
+
|
239
|
+
def test_call_no_cookies_config_override
|
240
|
+
old_config = Kronk.config[:use_cookies]
|
241
|
+
Kronk.config[:use_cookies] = false
|
242
|
+
|
243
|
+
Kronk.cookie_jar.expects(:get_cookie_header).
|
244
|
+
with("http://example.com/request/path?foo=bar").returns ""
|
245
|
+
|
246
|
+
Kronk.cookie_jar.expects(:set_cookies_from_headers)
|
247
|
+
|
248
|
+
expect_request "GET", "http://example.com/request/path?foo=bar",
|
249
|
+
:headers => {'User-Agent' => "kronk"}
|
250
|
+
|
251
|
+
resp = Kronk::Request.call :get, "http://example.com/request/path",
|
252
|
+
:query => "foo=bar", :headers => {'User-Agent' => "kronk"},
|
253
|
+
:no_cookies => false
|
254
|
+
|
255
|
+
Kronk.config[:use_cookies] = old_config
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
def test_call_cookies_already_set
|
260
|
+
Kronk.cookie_jar.expects(:get_cookie_header).
|
261
|
+
with("http://example.com/request/path?foo=bar").never
|
262
|
+
|
263
|
+
expect_request "GET", "http://example.com/request/path?foo=bar",
|
264
|
+
:headers => {'User-Agent' => "kronk", 'Cookie' => "mock_cookie"}
|
265
|
+
|
266
|
+
resp = Kronk::Request.call :get, "http://example.com/request/path",
|
267
|
+
:query => "foo=bar",
|
268
|
+
:headers => {'User-Agent' => "kronk", 'Cookie' => "mock_cookie"},
|
269
|
+
:no_cookies => true
|
270
|
+
end
|
271
|
+
|
272
|
+
|
273
|
+
def test_call_query
|
274
|
+
expect_request "GET", "http://example.com/path?foo=bar"
|
275
|
+
Kronk::Request.call :get, "http://example.com/path",
|
276
|
+
:query => {:foo => :bar}
|
277
|
+
end
|
278
|
+
|
279
|
+
|
280
|
+
def test_call_query_appended
|
281
|
+
expect_request "GET", "http://example.com/path?foo=bar&test=thing"
|
282
|
+
Kronk::Request.call :get, "http://example.com/path?foo=bar",
|
283
|
+
:query => {:test => :thing}
|
284
|
+
end
|
285
|
+
|
286
|
+
|
287
|
+
def test_call_query_appended_string
|
288
|
+
expect_request "GET", "http://example.com/path?foo=bar&test=thing"
|
289
|
+
Kronk::Request.call :get, "http://example.com/path?foo=bar",
|
290
|
+
:query => "test=thing"
|
291
|
+
end
|
292
|
+
|
293
|
+
|
294
|
+
def test_call_basic_auth
|
295
|
+
auth_opts = {:username => "user", :password => "pass"}
|
296
|
+
|
297
|
+
expect_request "GET", "http://example.com" do |http, req, resp|
|
298
|
+
req.expects(:basic_auth).with auth_opts[:username], auth_opts[:password]
|
299
|
+
end
|
300
|
+
|
301
|
+
resp = Kronk::Request.call :get, "http://example.com", :auth => auth_opts
|
302
|
+
end
|
303
|
+
|
304
|
+
|
305
|
+
def test_call_bad_basic_auth
|
306
|
+
auth_opts = {:password => "pass"}
|
307
|
+
|
308
|
+
expect_request "GET", "http://example.com" do |http, req, resp|
|
309
|
+
req.expects(:basic_auth).with(auth_opts[:username], auth_opts[:password]).
|
310
|
+
never
|
311
|
+
end
|
312
|
+
|
313
|
+
resp = Kronk::Request.call :get, "http://example.com", :auth => auth_opts
|
314
|
+
end
|
315
|
+
|
316
|
+
|
317
|
+
def test_call_no_basic_auth
|
318
|
+
expect_request "GET", "http://example.com" do |http, req, resp|
|
319
|
+
req.expects(:basic_auth).never
|
320
|
+
end
|
321
|
+
|
322
|
+
resp = Kronk::Request.call :get, "http://example.com"
|
323
|
+
end
|
324
|
+
|
325
|
+
|
326
|
+
def test_call_ssl
|
327
|
+
expect_request "GET", "https://example.com" do |http, req, resp|
|
328
|
+
req.expects(:use_ssl=).with true
|
329
|
+
end
|
330
|
+
|
331
|
+
resp = Kronk::Request.call :get, "https://example.com"
|
332
|
+
|
333
|
+
assert_equal mock_200_response, resp.raw
|
334
|
+
end
|
335
|
+
|
336
|
+
|
337
|
+
def test_call_no_ssl
|
338
|
+
expect_request "GET", "http://example.com" do |http, req, resp|
|
339
|
+
req.expects(:use_ssl=).with(true).never
|
340
|
+
end
|
341
|
+
|
342
|
+
resp = Kronk::Request.call :get, "http://example.com"
|
343
|
+
|
344
|
+
assert_equal mock_200_response, resp.raw
|
345
|
+
end
|
346
|
+
|
347
|
+
|
348
|
+
def test_call_user_agent_default
|
349
|
+
expect_request "GET", "http://example.com",
|
350
|
+
:headers => {
|
351
|
+
'User-Agent' =>
|
352
|
+
"Kronk/#{Kronk::VERSION} (http://github.com/yaksnrainbows/kronk)"
|
353
|
+
}
|
354
|
+
|
355
|
+
resp = Kronk::Request.call :get, "http://example.com"
|
356
|
+
end
|
357
|
+
|
358
|
+
|
359
|
+
def test_call_user_agent_alias
|
360
|
+
expect_request "GET", "http://example.com",
|
361
|
+
:headers => {'User-Agent' => "Mozilla/5.0 (compatible; Konqueror/3; Linux)"}
|
362
|
+
|
363
|
+
resp = Kronk::Request.call :get, "http://example.com",
|
364
|
+
:user_agent => 'linux_konqueror'
|
365
|
+
end
|
366
|
+
|
367
|
+
|
368
|
+
def test_call_user_agent_custom
|
369
|
+
expect_request "GET", "http://example.com",
|
370
|
+
:headers => {'User-Agent' => "custom user agent"}
|
371
|
+
|
372
|
+
resp = Kronk::Request.call :get, "http://example.com",
|
373
|
+
:user_agent => 'custom user agent'
|
374
|
+
end
|
375
|
+
|
376
|
+
|
377
|
+
def test_call_user_agent_header_already_set
|
378
|
+
expect_request "GET", "http://example.com",
|
379
|
+
:headers => {'User-Agent' => "custom user agent"}
|
380
|
+
|
381
|
+
resp = Kronk::Request.call :get, "http://example.com",
|
382
|
+
:user_agent => 'mac_safari',
|
383
|
+
:headers => {'User-Agent' => "custom user agent"}
|
384
|
+
end
|
385
|
+
|
386
|
+
|
387
|
+
def test_call_proxy
|
388
|
+
proxy = {
|
389
|
+
:address => "proxy.com",
|
390
|
+
:username => "john",
|
391
|
+
:password => "smith"
|
392
|
+
}
|
393
|
+
|
394
|
+
expect_request "GET", "http://example.com"
|
395
|
+
|
396
|
+
Net::HTTP.expects(:Proxy).with("proxy.com", 8080, "john", "smith").
|
397
|
+
returns Net::HTTP
|
398
|
+
|
399
|
+
Kronk::Request.call :get, "http://example.com", :proxy => proxy
|
400
|
+
end
|
401
|
+
|
402
|
+
|
403
|
+
def test_call_proxy_string
|
404
|
+
proxy = "proxy.com:8888"
|
405
|
+
|
406
|
+
expect_request "GET", "http://example.com"
|
407
|
+
|
408
|
+
Net::HTTP.expects(:Proxy).with("proxy.com", "8888", nil, nil).
|
409
|
+
returns Net::HTTP
|
410
|
+
|
411
|
+
Kronk::Request.call :get, "http://example.com", :proxy => proxy
|
412
|
+
end
|
413
|
+
|
414
|
+
|
415
|
+
def test_proxy_nil
|
416
|
+
assert_equal Net::HTTP, Kronk::Request.proxy(nil)
|
417
|
+
end
|
418
|
+
|
419
|
+
|
420
|
+
def test_proxy_string
|
421
|
+
proxy_class = Kronk::Request.proxy("myproxy.com:80")
|
422
|
+
|
423
|
+
assert_equal "myproxy.com",
|
424
|
+
proxy_class.instance_variable_get("@proxy_address")
|
425
|
+
|
426
|
+
assert_equal '80', proxy_class.instance_variable_get("@proxy_port")
|
427
|
+
|
428
|
+
assert_nil proxy_class.instance_variable_get("@proxy_user")
|
429
|
+
assert_nil proxy_class.instance_variable_get("@proxy_pass")
|
430
|
+
end
|
431
|
+
|
432
|
+
|
433
|
+
def test_proxy_no_port
|
434
|
+
proxy_class = Kronk::Request.proxy("myproxy.com")
|
435
|
+
|
436
|
+
assert_equal "myproxy.com",
|
437
|
+
proxy_class.instance_variable_get("@proxy_address")
|
438
|
+
|
439
|
+
assert_equal 8080, proxy_class.instance_variable_get("@proxy_port")
|
440
|
+
|
441
|
+
assert_nil proxy_class.instance_variable_get("@proxy_user")
|
442
|
+
assert_nil proxy_class.instance_variable_get("@proxy_pass")
|
443
|
+
end
|
444
|
+
|
445
|
+
|
446
|
+
def test_proxy_hash
|
447
|
+
proxy_class = Kronk::Request.proxy "myproxy.com",
|
448
|
+
:port => 8080,
|
449
|
+
:username => "john",
|
450
|
+
:password => "smith"
|
451
|
+
|
452
|
+
assert_equal "myproxy.com",
|
453
|
+
proxy_class.instance_variable_get("@proxy_address")
|
454
|
+
|
455
|
+
assert_equal 8080, proxy_class.instance_variable_get("@proxy_port")
|
456
|
+
|
457
|
+
assert_equal "john", proxy_class.instance_variable_get("@proxy_user")
|
458
|
+
assert_equal "smith", proxy_class.instance_variable_get("@proxy_pass")
|
459
|
+
end
|
460
|
+
|
461
|
+
|
175
462
|
def test_build_query_hash
|
176
463
|
hash = {
|
177
464
|
:foo => :bar,
|
@@ -185,15 +472,27 @@ class TestRequest < Test::Unit::TestCase
|
|
185
472
|
|
186
473
|
|
187
474
|
def test_build_query_non_hash
|
188
|
-
|
189
|
-
Kronk::Request.build_query [1,2,3]
|
190
|
-
end
|
475
|
+
assert_equal [1,2,3].to_s, Kronk::Request.build_query([1,2,3])
|
191
476
|
|
192
477
|
assert_equal "q[]=1&q[]=2&q[]=3", Kronk::Request.build_query([1,2,3], "q")
|
193
478
|
assert_equal "key=val", Kronk::Request.build_query("val", "key")
|
194
479
|
end
|
195
480
|
|
196
481
|
|
482
|
+
def test_vanilla_request
|
483
|
+
req = Kronk::Request::VanillaRequest.new :my_http_method,
|
484
|
+
"some/path", 'User-Agent' => 'vanilla kronk'
|
485
|
+
|
486
|
+
assert Net::HTTPRequest === req
|
487
|
+
assert_equal "MY_HTTP_METHOD", req.class::METHOD
|
488
|
+
assert req.class::REQUEST_HAS_BODY
|
489
|
+
assert req.class::RESPONSE_HAS_BODY
|
490
|
+
|
491
|
+
assert_equal "some/path", req.path
|
492
|
+
assert_equal "vanilla kronk", req['User-Agent']
|
493
|
+
end
|
494
|
+
|
495
|
+
|
197
496
|
private
|
198
497
|
|
199
498
|
def expect_request req_method, url, options={}
|
@@ -201,25 +500,33 @@ class TestRequest < Test::Unit::TestCase
|
|
201
500
|
|
202
501
|
resp = mock 'resp'
|
203
502
|
resp.stubs(:code).returns(options[:status] || '200')
|
503
|
+
resp.stubs(:to_hash).returns Hash.new
|
204
504
|
|
205
|
-
http
|
505
|
+
http = mock 'http'
|
206
506
|
socket = mock 'socket'
|
507
|
+
req = mock 'req'
|
207
508
|
|
208
509
|
data = options[:data]
|
209
510
|
data &&= Hash === data ? Kronk::Request.build_query(data) : data.to_s
|
210
511
|
|
512
|
+
headers = options[:headers] || Hash.new
|
513
|
+
headers['User-Agent'] ||= Kronk.config[:user_agents]['kronk']
|
514
|
+
|
211
515
|
socket.expects(:debug_output=)
|
212
516
|
|
213
|
-
|
214
|
-
with(req_method, uri.request_uri,
|
215
|
-
|
517
|
+
Kronk::Request::VanillaRequest.expects(:new).
|
518
|
+
with(req_method, uri.request_uri, headers).returns req
|
519
|
+
|
520
|
+
http.expects(:request).with(req, data).returns resp
|
216
521
|
|
217
522
|
http.expects(:instance_variable_get).with("@socket").returns socket
|
218
523
|
|
219
|
-
Net::HTTP.expects(:
|
524
|
+
Net::HTTP.expects(:new).with(uri.host, uri.port).returns req
|
525
|
+
req.expects(:start).yields(http).returns resp
|
220
526
|
|
221
527
|
Kronk::Response.expects(:read_raw_from).returns ["", mock_200_response, 0]
|
222
528
|
|
529
|
+
yield http, req, resp if block_given?
|
223
530
|
resp
|
224
531
|
end
|
225
532
|
end
|