kronk 1.7.8 → 1.8.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/test/test_request.rb CHANGED
@@ -94,7 +94,7 @@ class TestRequest < Test::Unit::TestCase
94
94
 
95
95
  def test_build_uri_string
96
96
  uri = Kronk::Request.build_uri "example.com"
97
- assert_equal "http://example.com", uri.to_s
97
+ assert_equal "http://example.com/", uri.to_s
98
98
  end
99
99
 
100
100
 
@@ -150,11 +150,7 @@ class TestRequest < Test::Unit::TestCase
150
150
 
151
151
 
152
152
  def test_retrieve_cookies
153
- Kronk.cookie_jar.expects(:get_cookie_header).
154
- with("http://example.com/request/path?foo=bar").returns "mock_cookie"
155
-
156
- Kronk.cookie_jar.expects(:set_cookies_from_headers).
157
- with("http://example.com/request/path?foo=bar", {})
153
+ Kronk.cookie_jar.expects(:get_cookie_header).returns "mock_cookie"
158
154
 
159
155
  expect_request "GET", "http://example.com/request/path?foo=bar",
160
156
  :headers => {'Cookie' => "mock_cookie", 'User-Agent' => "kronk"}
@@ -180,7 +176,7 @@ class TestRequest < Test::Unit::TestCase
180
176
  Kronk.cookie_jar.expects(:get_cookie_header).
181
177
  with("http://example.com/request/path?foo=bar").never
182
178
 
183
- Kronk.cookie_jar.expects(:set_cookies_from_headers).never
179
+ Kronk.cookie_jar.expects(:add_cookie).never
184
180
 
185
181
  expect_request "GET", "http://example.com/request/path?foo=bar",
186
182
  :headers => {'User-Agent' => "kronk"}
@@ -217,8 +213,6 @@ class TestRequest < Test::Unit::TestCase
217
213
  Kronk.cookie_jar.expects(:get_cookie_header).
218
214
  with("http://example.com/request/path?foo=bar").returns ""
219
215
 
220
- Kronk.cookie_jar.expects(:set_cookies_from_headers)
221
-
222
216
  expect_request "GET", "http://example.com/request/path?foo=bar",
223
217
  :headers => {'User-Agent' => "kronk"}
224
218
 
@@ -383,8 +377,8 @@ class TestRequest < Test::Unit::TestCase
383
377
 
384
378
  expect_request "GET", "http://example.com"
385
379
 
386
- Net::HTTP.expects(:Proxy).with("proxy.com", 8080, "john", "smith").
387
- returns Net::HTTP
380
+ Kronk::HTTP.expects(:Proxy).with("proxy.com", 8080, "john", "smith").
381
+ returns Kronk::HTTP
388
382
 
389
383
  Kronk::Request.new("http://example.com", :proxy => proxy).retrieve
390
384
  end
@@ -395,15 +389,15 @@ class TestRequest < Test::Unit::TestCase
395
389
 
396
390
  expect_request "GET", "http://example.com"
397
391
 
398
- Net::HTTP.expects(:Proxy).with("proxy.com", "8888", nil, nil).
399
- returns Net::HTTP
392
+ Kronk::HTTP.expects(:Proxy).with("proxy.com", "8888", nil, nil).
393
+ returns Kronk::HTTP
400
394
 
401
395
  Kronk::Request.new("http://example.com", :proxy => proxy).retrieve
402
396
  end
403
397
 
404
398
 
405
399
  def test_proxy_nil
406
- assert_equal Net::HTTP, Kronk::Request.new("host.com").http_proxy(nil)
400
+ assert_equal Kronk::HTTP, Kronk::Request.new("host.com").http_proxy(nil)
407
401
  end
408
402
 
409
403
 
@@ -21,6 +21,199 @@ class TestResponse < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
 
24
+ def test_init_no_cookies_from_file
25
+ assert @html_resp.cookies.empty?
26
+ end
27
+
28
+
29
+ def test_init_cookies
30
+ Kronk.cookie_jar.expects(:add_cookie).twice
31
+
32
+ html_resp = Kronk::Response.new File.read("test/mocks/200_response.txt"),
33
+ :request => Kronk::Request.new("http://google.com"),
34
+ :no_cookies => false
35
+
36
+ expected_cookies =
37
+ [{"name"=>"PREF",
38
+ "value"=>
39
+ "ID=99d644506f26d85e:FF=0:TM=1290788168:LM=1290788168:S=VSMemgJxlmlToFA3",
40
+ "domain"=>".google.com",
41
+ "path"=>"/",
42
+ "expiry"=>Time.parse("2012-11-25 08:16:08 -0800")},
43
+ {"name"=>"NID",
44
+ "value"=>
45
+ "41=CcmNDE4SfDu5cdTOYVkrCVjlrGO-oVbdo1awh_p8auk2gI4uaX1vNznO0QN8nZH4Mh9WprRy3yI2yd_Fr1WaXVru6Xq3adlSLGUTIRW8SzX58An2nH3D2PhAY5JfcJrl",
46
+ "domain"=>".google.com",
47
+ "path"=>"/",
48
+ "expiry"=>Time.parse("2011-05-28 09:16:08 -0700"),
49
+ "http_only"=>true
50
+ }]
51
+
52
+ assert_equal expected_cookies, html_resp.cookies
53
+ end
54
+
55
+
56
+ def test_init_no_cookies_opt
57
+ Kronk.cookie_jar.expects(:add_cookie).never
58
+
59
+ req = Kronk::Request.new("http://google.com")
60
+
61
+ html_resp = Kronk::Response.new File.read("test/mocks/200_response.txt"),
62
+ :request => req,
63
+ :no_cookies => true
64
+
65
+ expected_cookies =
66
+ [{"name"=>"PREF",
67
+ "value"=>
68
+ "ID=99d644506f26d85e:FF=0:TM=1290788168:LM=1290788168:S=VSMemgJxlmlToFA3",
69
+ "domain"=>".google.com",
70
+ "path"=>"/",
71
+ "expiry"=>Time.parse("2012-11-25 08:16:08 -0800")},
72
+ {"name"=>"NID",
73
+ "value"=>
74
+ "41=CcmNDE4SfDu5cdTOYVkrCVjlrGO-oVbdo1awh_p8auk2gI4uaX1vNznO0QN8nZH4Mh9WprRy3yI2yd_Fr1WaXVru6Xq3adlSLGUTIRW8SzX58An2nH3D2PhAY5JfcJrl",
75
+ "domain"=>".google.com",
76
+ "path"=>"/",
77
+ "expiry"=>Time.parse("2011-05-28 09:16:08 -0700"),
78
+ "http_only"=>true
79
+ }]
80
+
81
+ assert_equal expected_cookies, html_resp.cookies
82
+ end
83
+
84
+
85
+ def test_body
86
+ expected = File.read("test/mocks/200_response.json").split("\r\n\r\n")[1]
87
+ assert_equal expected,
88
+ @json_resp.body
89
+ end
90
+
91
+
92
+ def test_body_yield
93
+ count = 0
94
+ expected = File.read("test/mocks/200_response.json").split("\r\n\r\n")[1]
95
+ body = ""
96
+
97
+ json_file = File.open "test/mocks/200_response.json", "r"
98
+
99
+ with_buffer_size 64 do
100
+ json_resp = Kronk::Response.new json_file
101
+ json_resp.content_length = nil
102
+ json_resp.body do |chunk|
103
+ count += 1
104
+ body << chunk
105
+ end
106
+ end
107
+
108
+ json_file.close
109
+
110
+ assert_equal 15, count
111
+ assert_equal expected, body
112
+ end
113
+
114
+
115
+ def test_body_yield_exception
116
+ count = 0
117
+ expected = File.read("test/mocks/200_response.json").split("\r\n\r\n")[1]
118
+ body = ""
119
+
120
+ json_file = File.open "test/mocks/200_response.json", "r"
121
+
122
+ with_buffer_size 64 do
123
+ json_resp = Kronk::Response.new json_file
124
+ json_resp.content_length = nil
125
+ json_resp.body do |chunk|
126
+ count += 1
127
+ body << chunk
128
+ raise IOError if count == 2
129
+ end
130
+ end
131
+
132
+ json_file.close
133
+
134
+ assert_equal 3, count
135
+ assert_equal expected, body
136
+ end
137
+
138
+
139
+ def test_body_yield_inflate
140
+ count = 0
141
+ expected = File.read("test/mocks/200_inflate.txt")
142
+ expected.force_encoding('binary') if expected.respond_to?(:force_encoding)
143
+ expected = expected.split("\r\n\r\n")[1]
144
+ expected = Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(expected)
145
+ body = ""
146
+
147
+ json_file = File.open "test/mocks/200_inflate.txt", "r"
148
+
149
+ with_buffer_size 64 do
150
+ json_resp = Kronk::Response.new json_file
151
+ json_resp.content_length = nil
152
+ json_resp.body do |chunk|
153
+ count += 1
154
+ body << chunk
155
+ end
156
+ end
157
+
158
+ json_file.close
159
+
160
+ assert_equal 1, count
161
+ assert_equal expected, body
162
+ end
163
+
164
+
165
+ def test_body_yield_gzip
166
+ count = 0
167
+ expected = File.read("test/mocks/200_gzip.txt")
168
+ expected.force_encoding('binary') if expected.respond_to?(:force_encoding)
169
+ expected = expected.split("\r\n\r\n")[1]
170
+ expected = Zlib::GzipReader.new(StringIO.new(expected)).read
171
+ body = ""
172
+
173
+ json_file = File.open "test/mocks/200_gzip.txt", "r"
174
+
175
+ with_buffer_size 64 do
176
+ json_resp = Kronk::Response.new json_file
177
+ json_resp.content_length = nil
178
+ json_resp.body do |chunk|
179
+ count += 1
180
+ body << chunk
181
+ end
182
+ end
183
+
184
+ json_file.close
185
+
186
+ assert_equal 19, count
187
+ assert_equal expected, body
188
+ end
189
+
190
+
191
+ def test_body_yield_gzip_exception
192
+ count = 0
193
+ expected = File.read("test/mocks/200_gzip.txt")
194
+ expected.force_encoding('binary') if expected.respond_to?(:force_encoding)
195
+ expected = expected.split("\r\n\r\n")[1]
196
+ expected = Zlib::GzipReader.new(StringIO.new(expected)).read
197
+ body = ""
198
+
199
+ gzip_file = File.open "test/mocks/200_gzip.txt", "r"
200
+
201
+ with_buffer_size 64 do
202
+ gzip_resp = Kronk::Response.new gzip_file
203
+ gzip_resp.body do |chunk|
204
+ count += 1
205
+ body << chunk
206
+ raise IOError if count == 3
207
+ end
208
+ end
209
+
210
+ gzip_file.close
211
+
212
+ assert_equal 4, count
213
+ assert_equal expected, body
214
+ end
215
+
216
+
24
217
  def test_bytes
25
218
  png = Kronk::Response.read_file "test/mocks/200_response.png"
26
219
  assert_equal 8469, png.bytes
@@ -75,30 +268,11 @@ class TestResponse < Test::Unit::TestCase
75
268
 
76
269
  expected_header = "#{mock_200_response.split("\r\n\r\n", 2)[0]}\r\n"
77
270
 
78
- assert Net::HTTPResponse === resp.instance_variable_get("@_res")
79
271
  assert_equal mock_200_response, resp.raw
80
272
  assert_equal expected_header, resp.raw_header
81
273
  end
82
274
 
83
275
 
84
- def test_read_raw_from
85
- resp = mock_200_response
86
- chunks = [resp[0..123], resp[124..200], resp[201..-1]]
87
- chunks = chunks.map{|c| "-> #{c.inspect}"}
88
- str = [chunks[0], "<- reading 123 bytes", chunks[1], chunks[2]].join "\n"
89
- str = "<- \"mock debug request\"\n#{str}\nread 123 bytes"
90
-
91
- io = StringIO.new str
92
-
93
- resp = Kronk::Response.new mock_200_response
94
- req, resp, bytes = resp.send :read_raw_from, io
95
-
96
- assert_equal "mock debug request", req
97
- assert_equal mock_200_response, resp
98
- assert_equal 123, bytes
99
- end
100
-
101
-
102
276
  def test_parsed_body_json
103
277
  raw = File.read "test/mocks/200_response.json"
104
278
  expected = JSON.parse raw.split("\r\n\r\n")[1]
@@ -197,81 +371,75 @@ class TestResponse < Test::Unit::TestCase
197
371
  end
198
372
 
199
373
 
200
- def test_selective_string
201
- body = @json_resp.raw.split("\r\n\r\n")[1]
202
- assert_equal body, @json_resp.selective_string
374
+ def test_to_s
375
+ body = @json_resp.raw
376
+ assert_equal body, @json_resp.to_s
203
377
  end
204
378
 
205
379
 
206
- def test_selective_string_no_body
380
+ def test_to_s_no_body
207
381
  @json_resp.raw.split("\r\n\r\n")[1]
208
382
 
209
- assert_nil @json_resp.selective_string(:no_body => true)
383
+ assert_equal "", @json_resp.to_s(:body => false, :headers => false)
210
384
 
211
385
  assert_equal "#{@json_resp.raw.split("\r\n\r\n")[0]}\r\n",
212
- @json_resp.selective_string(:no_body => true,
213
- :show_headers => true)
386
+ @json_resp.to_s(:body => false)
214
387
  end
215
388
 
216
389
 
217
- def test_selective_string_single_header
390
+ def test_to_s_single_header
218
391
  body = @json_resp.raw.split("\r\n\r\n")[1]
219
392
 
220
393
  expected = "Content-Type: application/json; charset=utf-8\r\n\r\n#{body}"
221
- assert_equal expected,
222
- @json_resp.selective_string(:show_headers => "Content-Type")
394
+ assert_equal expected, @json_resp.to_s(:headers => "Content-Type")
223
395
  end
224
396
 
225
397
 
226
- def test_selective_multiple_headers
398
+ def test_to_s_multiple_headers
227
399
  body = @json_resp.raw.split("\r\n\r\n")[1]
228
400
 
229
401
  expected = "Date: Fri, 03 Dec 2010 21:49:00 GMT\r\nContent-Type: application/json; charset=utf-8\r\n\r\n#{body}"
230
- assert_equal expected,
231
- @json_resp.selective_string(
232
- :show_headers => ["Content-Type", "Date"])
402
+ assert_equal expected, @json_resp.to_s(:headers => ["Content-Type", "Date"])
233
403
 
234
404
  expected = "Date: Fri, 03 Dec 2010 21:49:00 GMT\r\nContent-Type: application/json; charset=utf-8\r\n"
235
405
  assert_equal expected,
236
- @json_resp.selective_string(:no_body => true,
237
- :show_headers => ["Content-Type", "Date"])
406
+ @json_resp.to_s(:body => false, :headers => ["Content-Type", "Date"])
238
407
  end
239
408
 
240
409
 
241
- def test_selective_data
410
+ def test_data
242
411
  body = JSON.parse @json_resp.body
243
412
  @json_resp.to_hash
244
413
 
245
- assert_equal body, @json_resp.selective_data
414
+ assert_equal body, @json_resp.data
246
415
 
247
- assert_nil @json_resp.selective_data(:no_body => true)
416
+ assert_nil @json_resp.data(:no_body => true, :show_headers => false)
248
417
 
249
418
  assert_equal "#{@json_resp.raw.split("\r\n\r\n")[0]}\r\n",
250
- @json_resp.selective_string(:no_body => true,
251
- :show_headers => true)
419
+ @json_resp.to_s(:body => false)
252
420
  end
253
421
 
254
422
 
255
- def test_selective_data_parser
423
+ def test_data_parser
256
424
  assert_raises Kronk::ParserError do
257
- @json_resp.selective_data :parser => Kronk::PlistParser
425
+ @json_resp.data :parser => Kronk::PlistParser
258
426
  end
259
427
 
260
- assert @json_resp.selective_data(:parser => JSON)
428
+ assert @json_resp.data(:parser => JSON)
261
429
  end
262
430
 
263
431
 
264
- def test_selective_data_single_header
432
+ def test_data_single_header
265
433
  body = JSON.parse @json_resp.body
266
434
  expected =
267
435
  [{'content-type' => 'application/json; charset=utf-8'}, body]
268
436
 
269
437
  assert_equal expected,
270
- @json_resp.selective_data(:show_headers => "Content-Type")
438
+ @json_resp.data(:show_headers => "Content-Type")
271
439
  end
272
440
 
273
441
 
274
- def test_selective_data_multiple_headers
442
+ def test_data_multiple_headers
275
443
  body = JSON.parse @json_resp.body
276
444
  expected =
277
445
  [{'content-type' => 'application/json; charset=utf-8',
@@ -279,66 +447,66 @@ class TestResponse < Test::Unit::TestCase
279
447
  }, body]
280
448
 
281
449
  assert_equal expected,
282
- @json_resp.selective_data(
450
+ @json_resp.data(
283
451
  :show_headers => ["Content-Type", "Date"])
284
452
  end
285
453
 
286
454
 
287
- def test_selective_data_no_body
455
+ def test_data_no_body
288
456
  expected = {
289
457
  'content-type' => 'application/json; charset=utf-8',
290
458
  'date' => "Fri, 03 Dec 2010 21:49:00 GMT"
291
459
  }
292
460
 
293
461
  assert_equal expected,
294
- @json_resp.selective_data(:no_body => true,
462
+ @json_resp.data(:no_body => true,
295
463
  :show_headers => ["Content-Type", "Date"])
296
464
  end
297
465
 
298
466
 
299
- def test_selective_data_only_data
467
+ def test_data_only_data
300
468
  expected = {"business" => {"id" => "1234"},
301
469
  "original_request"=> {"id"=>"1234"}}
302
470
 
303
471
  assert_equal expected,
304
- @json_resp.selective_data(:only_data => "**/id")
472
+ @json_resp.data(:only_data => "**/id")
305
473
  end
306
474
 
307
475
 
308
- def test_selective_data_multiple_only_data
476
+ def test_data_multiple_only_data
309
477
  expected = {"business" => {"id" => "1234"},
310
478
  "request_id" => "mock_rid"}
311
479
 
312
480
  assert_equal expected,
313
- @json_resp.selective_data(:only_data => ["business/id", "request_id"])
481
+ @json_resp.data(:only_data => ["business/id", "request_id"])
314
482
  end
315
483
 
316
484
 
317
- def test_selective_data_ignore_data
485
+ def test_data_ignore_data
318
486
  expected = JSON.parse @json_resp.body
319
487
  expected['business'].delete 'id'
320
488
  expected['original_request'].delete 'id'
321
489
 
322
490
  assert_equal expected,
323
- @json_resp.selective_data(:ignore_data => "**/id")
491
+ @json_resp.data(:ignore_data => "**/id")
324
492
  end
325
493
 
326
494
 
327
- def test_selective_data_multiple_ignore_data
495
+ def test_data_multiple_ignore_data
328
496
  expected = JSON.parse @json_resp.body
329
497
  expected['business'].delete 'id'
330
498
  expected.delete 'request_id'
331
499
 
332
500
  assert_equal expected,
333
- @json_resp.selective_data(:ignore_data => ["business/id", "request_id"])
501
+ @json_resp.data(:ignore_data => ["business/id", "request_id"])
334
502
  end
335
503
 
336
504
 
337
- def test_selective_data_collected_and_ignored
505
+ def test_data_collected_and_ignored
338
506
  expected = {"business" => {"id" => "1234"}}
339
507
 
340
508
  assert_equal expected,
341
- @json_resp.selective_data(:only_data => "**/id",
509
+ @json_resp.data(:only_data => "**/id",
342
510
  :ignore_data => "original_request")
343
511
  end
344
512
 
@@ -462,7 +630,7 @@ STR
462
630
 
463
631
 
464
632
  def test_stringify_missing_parser
465
- str = Kronk::Response.read_file("test/mocks/200_response.txt").stringify
633
+ str = @html_resp.stringify
466
634
  expected = File.read("test/mocks/200_response.txt").split("\r\n\r\n")[1]
467
635
 
468
636
  assert_equal expected, str