em-http-request 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of em-http-request might be problematic. Click here for more details.
- data/examples/fibered-http.rb +51 -51
- data/lib/em-http/client.rb +309 -307
- data/lib/em-http/http_client_options.rb +3 -9
- data/lib/em-http/http_connection.rb +205 -199
- data/lib/em-http/version.rb +1 -1
- data/spec/client_fiber_spec.rb +10 -8
- data/spec/client_spec.rb +763 -734
- data/spec/http_proxy_spec.rb +22 -1
- data/spec/redirect_spec.rb +4 -4
- data/spec/stallion.rb +270 -273
- metadata +22 -22
data/spec/client_spec.rb
CHANGED
@@ -1,734 +1,763 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
describe EventMachine::HttpRequest do
|
4
|
-
|
5
|
-
def failed(http=nil)
|
6
|
-
EventMachine.stop
|
7
|
-
http ? fail(http.error) : fail
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should perform successful GET" do
|
11
|
-
EventMachine.run {
|
12
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
|
13
|
-
|
14
|
-
http.errback { failed(http) }
|
15
|
-
http.callback {
|
16
|
-
http.response_header.status.should == 200
|
17
|
-
http.response.should match(/Hello/)
|
18
|
-
EventMachine.stop
|
19
|
-
}
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should perform successful GET with a URI passed as argument" do
|
24
|
-
EventMachine.run {
|
25
|
-
uri = URI.parse('http://127.0.0.1:8090/')
|
26
|
-
http = EventMachine::HttpRequest.new(uri).get
|
27
|
-
|
28
|
-
http.errback { failed(http) }
|
29
|
-
http.callback {
|
30
|
-
http.response_header.status.should == 200
|
31
|
-
http.response.should match(/Hello/)
|
32
|
-
EventMachine.stop
|
33
|
-
}
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should succeed GET on missing path" do
|
38
|
-
EventMachine.run {
|
39
|
-
lambda {
|
40
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090').get
|
41
|
-
http.callback {
|
42
|
-
http.response.should match(/Hello/)
|
43
|
-
EventMachine.stop
|
44
|
-
}
|
45
|
-
}.should_not raise_error(ArgumentError)
|
46
|
-
|
47
|
-
}
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should raise error on invalid URL" do
|
51
|
-
EventMachine.run {
|
52
|
-
lambda {
|
53
|
-
EventMachine::HttpRequest.new('random?text').get
|
54
|
-
}.should raise_error
|
55
|
-
|
56
|
-
EM.stop
|
57
|
-
}
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should perform successful HEAD with a URI passed as argument" do
|
61
|
-
EventMachine.run {
|
62
|
-
uri = URI.parse('http://127.0.0.1:8090/')
|
63
|
-
http = EventMachine::HttpRequest.new(uri).head
|
64
|
-
|
65
|
-
http.errback { failed(http) }
|
66
|
-
http.callback {
|
67
|
-
http.response_header.status.should == 200
|
68
|
-
http.response.should == ""
|
69
|
-
EventMachine.stop
|
70
|
-
}
|
71
|
-
}
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should perform successful DELETE with a URI passed as argument" do
|
75
|
-
EventMachine.run {
|
76
|
-
uri = URI.parse('http://127.0.0.1:8090/')
|
77
|
-
http = EventMachine::HttpRequest.new(uri).delete
|
78
|
-
|
79
|
-
http.errback { failed(http) }
|
80
|
-
http.callback {
|
81
|
-
http.response_header.status.should == 200
|
82
|
-
http.response.should == ""
|
83
|
-
EventMachine.stop
|
84
|
-
}
|
85
|
-
}
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should return 404 on invalid path" do
|
89
|
-
EventMachine.run {
|
90
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/fail').get
|
91
|
-
|
92
|
-
http.errback { failed(http) }
|
93
|
-
http.callback {
|
94
|
-
http.response_header.status.should == 404
|
95
|
-
EventMachine.stop
|
96
|
-
}
|
97
|
-
}
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should return HTTP reason" do
|
101
|
-
EventMachine.run {
|
102
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/fail').get
|
103
|
-
|
104
|
-
http.errback { failed(http) }
|
105
|
-
http.callback {
|
106
|
-
http.response_header.status.should == 404
|
107
|
-
http.response_header.http_reason.should == 'Not Found'
|
108
|
-
EventMachine.stop
|
109
|
-
}
|
110
|
-
}
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should return HTTP reason 'unknown' on a non-standard status code" do
|
114
|
-
EventMachine.run {
|
115
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/fail_with_nonstandard_response').get
|
116
|
-
|
117
|
-
http.errback { failed(http) }
|
118
|
-
http.callback {
|
119
|
-
http.response_header.status.should == 420
|
120
|
-
http.response_header.http_reason.should == 'unknown'
|
121
|
-
EventMachine.stop
|
122
|
-
}
|
123
|
-
}
|
124
|
-
end
|
125
|
-
|
126
|
-
it "should build query parameters from Hash" do
|
127
|
-
EventMachine.run {
|
128
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :query => {:q => 'test'}
|
129
|
-
|
130
|
-
http.errback { failed(http) }
|
131
|
-
http.callback {
|
132
|
-
http.response_header.status.should == 200
|
133
|
-
http.response.should match(/test/)
|
134
|
-
EventMachine.stop
|
135
|
-
}
|
136
|
-
}
|
137
|
-
end
|
138
|
-
|
139
|
-
it "should pass query parameters string" do
|
140
|
-
EventMachine.run {
|
141
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :query => "q=test"
|
142
|
-
|
143
|
-
http.errback { failed(http) }
|
144
|
-
http.callback {
|
145
|
-
http.response_header.status.should == 200
|
146
|
-
http.response.should match(/test/)
|
147
|
-
EventMachine.stop
|
148
|
-
}
|
149
|
-
}
|
150
|
-
end
|
151
|
-
|
152
|
-
it "should encode an array of query parameters" do
|
153
|
-
EventMachine.run {
|
154
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_query').get :query => {:hash =>['value1','value2']}
|
155
|
-
|
156
|
-
http.errback { failed(http) }
|
157
|
-
http.callback {
|
158
|
-
http.response_header.status.should == 200
|
159
|
-
http.response.should match(/hash\[\]=value1&hash\[\]=value2/)
|
160
|
-
EventMachine.stop
|
161
|
-
}
|
162
|
-
}
|
163
|
-
end
|
164
|
-
|
165
|
-
it "should perform successful PUT" do
|
166
|
-
EventMachine.run {
|
167
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').put :body => "data"
|
168
|
-
|
169
|
-
http.errback { failed(http) }
|
170
|
-
http.callback {
|
171
|
-
http.response_header.status.should == 200
|
172
|
-
http.response.should match(/data/)
|
173
|
-
EventMachine.stop
|
174
|
-
}
|
175
|
-
}
|
176
|
-
end
|
177
|
-
|
178
|
-
it "should perform successful POST" do
|
179
|
-
EventMachine.run {
|
180
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').post :body => "data"
|
181
|
-
|
182
|
-
http.errback { failed(http) }
|
183
|
-
http.callback {
|
184
|
-
http.response_header.status.should == 200
|
185
|
-
http.response.should match(/data/)
|
186
|
-
EventMachine.stop
|
187
|
-
}
|
188
|
-
}
|
189
|
-
end
|
190
|
-
|
191
|
-
it "should escape body on POST" do
|
192
|
-
EventMachine.run {
|
193
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').post :body => {:stuff => 'string&string'}
|
194
|
-
|
195
|
-
http.errback { failed(http) }
|
196
|
-
http.callback {
|
197
|
-
http.response_header.status.should == 200
|
198
|
-
http.response.should == "stuff=string%26string"
|
199
|
-
EventMachine.stop
|
200
|
-
}
|
201
|
-
}
|
202
|
-
end
|
203
|
-
|
204
|
-
it "should perform successful POST with Ruby Hash/Array as params" do
|
205
|
-
EventMachine.run {
|
206
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').post :body => {"key1" => 1, "key2" => [2,3]}
|
207
|
-
|
208
|
-
http.errback { failed(http) }
|
209
|
-
http.callback {
|
210
|
-
http.response_header.status.should == 200
|
211
|
-
|
212
|
-
http.response.should match(/key1=1&key2\[0\]=2&key2\[1\]=3/)
|
213
|
-
EventMachine.stop
|
214
|
-
}
|
215
|
-
}
|
216
|
-
end
|
217
|
-
|
218
|
-
it "should set content-length to 0 on posts with empty bodies" do
|
219
|
-
EventMachine.run {
|
220
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_length_from_header').post
|
221
|
-
|
222
|
-
http.errback { failed(http) }
|
223
|
-
http.callback {
|
224
|
-
http.response_header.status.should == 200
|
225
|
-
|
226
|
-
http.response.strip.split(':')[1].should == '0'
|
227
|
-
EventMachine.stop
|
228
|
-
}
|
229
|
-
}
|
230
|
-
end
|
231
|
-
|
232
|
-
it "should perform successful POST with Ruby Hash/Array as params and with the correct content length" do
|
233
|
-
EventMachine.run {
|
234
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_length').post :body => {"key1" => "data1"}
|
235
|
-
|
236
|
-
http.errback { failed(http) }
|
237
|
-
http.callback {
|
238
|
-
http.response_header.status.should == 200
|
239
|
-
|
240
|
-
http.response.to_i.should == 10
|
241
|
-
EventMachine.stop
|
242
|
-
}
|
243
|
-
}
|
244
|
-
end
|
245
|
-
|
246
|
-
it "should perform successful GET with custom header" do
|
247
|
-
EventMachine.run {
|
248
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :head => {'if-none-match' => 'evar!'}
|
249
|
-
|
250
|
-
http.errback { p http; failed(http) }
|
251
|
-
http.callback {
|
252
|
-
http.response_header.status.should == 304
|
253
|
-
EventMachine.stop
|
254
|
-
}
|
255
|
-
}
|
256
|
-
end
|
257
|
-
|
258
|
-
it "should perform basic auth" do
|
259
|
-
EventMachine.run {
|
260
|
-
|
261
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :head => {'authorization' => ['user', 'pass']}
|
262
|
-
|
263
|
-
http.errback { failed(http) }
|
264
|
-
http.callback {
|
265
|
-
http.response_header.status.should == 200
|
266
|
-
EventMachine.stop
|
267
|
-
}
|
268
|
-
}
|
269
|
-
end
|
270
|
-
|
271
|
-
it "should
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
it "should
|
304
|
-
EventMachine.run {
|
305
|
-
|
306
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/auth').get :head =>
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
http.errback { failed(http) }
|
324
|
-
http.callback {
|
325
|
-
http.response_header.status.should == 200
|
326
|
-
http.
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
http
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
http.response_header.
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
http
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
EventMachine.stop
|
396
|
-
}
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
http.
|
407
|
-
|
408
|
-
EventMachine.stop
|
409
|
-
}
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
it "should set content
|
428
|
-
EventMachine.run {
|
429
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_type').post :body =>
|
430
|
-
|
431
|
-
http.errback { failed(http) }
|
432
|
-
http.callback {
|
433
|
-
http.response_header.status.should == 200
|
434
|
-
http.response.should
|
435
|
-
EventMachine.stop
|
436
|
-
}
|
437
|
-
}
|
438
|
-
end
|
439
|
-
|
440
|
-
it "should
|
441
|
-
EventMachine.run {
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
http.
|
447
|
-
http.
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
http.
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
body
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
http.
|
576
|
-
|
577
|
-
EventMachine.stop
|
578
|
-
}
|
579
|
-
}
|
580
|
-
end
|
581
|
-
|
582
|
-
it "should
|
583
|
-
EventMachine.run {
|
584
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/
|
585
|
-
|
586
|
-
http.errback { failed(http) }
|
587
|
-
http.callback {
|
588
|
-
http.response_header.status.should == 200
|
589
|
-
http.response_header.cookie.
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
http
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
http.
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
http.
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
EventMachine.
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
http.
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
http.
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe EventMachine::HttpRequest do
|
4
|
+
|
5
|
+
def failed(http=nil)
|
6
|
+
EventMachine.stop
|
7
|
+
http ? fail(http.error) : fail
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should perform successful GET" do
|
11
|
+
EventMachine.run {
|
12
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
|
13
|
+
|
14
|
+
http.errback { failed(http) }
|
15
|
+
http.callback {
|
16
|
+
http.response_header.status.should == 200
|
17
|
+
http.response.should match(/Hello/)
|
18
|
+
EventMachine.stop
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should perform successful GET with a URI passed as argument" do
|
24
|
+
EventMachine.run {
|
25
|
+
uri = URI.parse('http://127.0.0.1:8090/')
|
26
|
+
http = EventMachine::HttpRequest.new(uri).get
|
27
|
+
|
28
|
+
http.errback { failed(http) }
|
29
|
+
http.callback {
|
30
|
+
http.response_header.status.should == 200
|
31
|
+
http.response.should match(/Hello/)
|
32
|
+
EventMachine.stop
|
33
|
+
}
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should succeed GET on missing path" do
|
38
|
+
EventMachine.run {
|
39
|
+
lambda {
|
40
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090').get
|
41
|
+
http.callback {
|
42
|
+
http.response.should match(/Hello/)
|
43
|
+
EventMachine.stop
|
44
|
+
}
|
45
|
+
}.should_not raise_error(ArgumentError)
|
46
|
+
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should raise error on invalid URL" do
|
51
|
+
EventMachine.run {
|
52
|
+
lambda {
|
53
|
+
EventMachine::HttpRequest.new('random?text').get
|
54
|
+
}.should raise_error
|
55
|
+
|
56
|
+
EM.stop
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should perform successful HEAD with a URI passed as argument" do
|
61
|
+
EventMachine.run {
|
62
|
+
uri = URI.parse('http://127.0.0.1:8090/')
|
63
|
+
http = EventMachine::HttpRequest.new(uri).head
|
64
|
+
|
65
|
+
http.errback { failed(http) }
|
66
|
+
http.callback {
|
67
|
+
http.response_header.status.should == 200
|
68
|
+
http.response.should == ""
|
69
|
+
EventMachine.stop
|
70
|
+
}
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should perform successful DELETE with a URI passed as argument" do
|
75
|
+
EventMachine.run {
|
76
|
+
uri = URI.parse('http://127.0.0.1:8090/')
|
77
|
+
http = EventMachine::HttpRequest.new(uri).delete
|
78
|
+
|
79
|
+
http.errback { failed(http) }
|
80
|
+
http.callback {
|
81
|
+
http.response_header.status.should == 200
|
82
|
+
http.response.should == ""
|
83
|
+
EventMachine.stop
|
84
|
+
}
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return 404 on invalid path" do
|
89
|
+
EventMachine.run {
|
90
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/fail').get
|
91
|
+
|
92
|
+
http.errback { failed(http) }
|
93
|
+
http.callback {
|
94
|
+
http.response_header.status.should == 404
|
95
|
+
EventMachine.stop
|
96
|
+
}
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should return HTTP reason" do
|
101
|
+
EventMachine.run {
|
102
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/fail').get
|
103
|
+
|
104
|
+
http.errback { failed(http) }
|
105
|
+
http.callback {
|
106
|
+
http.response_header.status.should == 404
|
107
|
+
http.response_header.http_reason.should == 'Not Found'
|
108
|
+
EventMachine.stop
|
109
|
+
}
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should return HTTP reason 'unknown' on a non-standard status code" do
|
114
|
+
EventMachine.run {
|
115
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/fail_with_nonstandard_response').get
|
116
|
+
|
117
|
+
http.errback { failed(http) }
|
118
|
+
http.callback {
|
119
|
+
http.response_header.status.should == 420
|
120
|
+
http.response_header.http_reason.should == 'unknown'
|
121
|
+
EventMachine.stop
|
122
|
+
}
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should build query parameters from Hash" do
|
127
|
+
EventMachine.run {
|
128
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :query => {:q => 'test'}
|
129
|
+
|
130
|
+
http.errback { failed(http) }
|
131
|
+
http.callback {
|
132
|
+
http.response_header.status.should == 200
|
133
|
+
http.response.should match(/test/)
|
134
|
+
EventMachine.stop
|
135
|
+
}
|
136
|
+
}
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should pass query parameters string" do
|
140
|
+
EventMachine.run {
|
141
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :query => "q=test"
|
142
|
+
|
143
|
+
http.errback { failed(http) }
|
144
|
+
http.callback {
|
145
|
+
http.response_header.status.should == 200
|
146
|
+
http.response.should match(/test/)
|
147
|
+
EventMachine.stop
|
148
|
+
}
|
149
|
+
}
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should encode an array of query parameters" do
|
153
|
+
EventMachine.run {
|
154
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_query').get :query => {:hash =>['value1','value2']}
|
155
|
+
|
156
|
+
http.errback { failed(http) }
|
157
|
+
http.callback {
|
158
|
+
http.response_header.status.should == 200
|
159
|
+
http.response.should match(/hash\[\]=value1&hash\[\]=value2/)
|
160
|
+
EventMachine.stop
|
161
|
+
}
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should perform successful PUT" do
|
166
|
+
EventMachine.run {
|
167
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').put :body => "data"
|
168
|
+
|
169
|
+
http.errback { failed(http) }
|
170
|
+
http.callback {
|
171
|
+
http.response_header.status.should == 200
|
172
|
+
http.response.should match(/data/)
|
173
|
+
EventMachine.stop
|
174
|
+
}
|
175
|
+
}
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should perform successful POST" do
|
179
|
+
EventMachine.run {
|
180
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').post :body => "data"
|
181
|
+
|
182
|
+
http.errback { failed(http) }
|
183
|
+
http.callback {
|
184
|
+
http.response_header.status.should == 200
|
185
|
+
http.response.should match(/data/)
|
186
|
+
EventMachine.stop
|
187
|
+
}
|
188
|
+
}
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should escape body on POST" do
|
192
|
+
EventMachine.run {
|
193
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').post :body => {:stuff => 'string&string'}
|
194
|
+
|
195
|
+
http.errback { failed(http) }
|
196
|
+
http.callback {
|
197
|
+
http.response_header.status.should == 200
|
198
|
+
http.response.should == "stuff=string%26string"
|
199
|
+
EventMachine.stop
|
200
|
+
}
|
201
|
+
}
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should perform successful POST with Ruby Hash/Array as params" do
|
205
|
+
EventMachine.run {
|
206
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').post :body => {"key1" => 1, "key2" => [2,3]}
|
207
|
+
|
208
|
+
http.errback { failed(http) }
|
209
|
+
http.callback {
|
210
|
+
http.response_header.status.should == 200
|
211
|
+
|
212
|
+
http.response.should match(/key1=1&key2\[0\]=2&key2\[1\]=3/)
|
213
|
+
EventMachine.stop
|
214
|
+
}
|
215
|
+
}
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should set content-length to 0 on posts with empty bodies" do
|
219
|
+
EventMachine.run {
|
220
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_length_from_header').post
|
221
|
+
|
222
|
+
http.errback { failed(http) }
|
223
|
+
http.callback {
|
224
|
+
http.response_header.status.should == 200
|
225
|
+
|
226
|
+
http.response.strip.split(':')[1].should == '0'
|
227
|
+
EventMachine.stop
|
228
|
+
}
|
229
|
+
}
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should perform successful POST with Ruby Hash/Array as params and with the correct content length" do
|
233
|
+
EventMachine.run {
|
234
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_length').post :body => {"key1" => "data1"}
|
235
|
+
|
236
|
+
http.errback { failed(http) }
|
237
|
+
http.callback {
|
238
|
+
http.response_header.status.should == 200
|
239
|
+
|
240
|
+
http.response.to_i.should == 10
|
241
|
+
EventMachine.stop
|
242
|
+
}
|
243
|
+
}
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should perform successful GET with custom header" do
|
247
|
+
EventMachine.run {
|
248
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :head => {'if-none-match' => 'evar!'}
|
249
|
+
|
250
|
+
http.errback { p http; failed(http) }
|
251
|
+
http.callback {
|
252
|
+
http.response_header.status.should == 304
|
253
|
+
EventMachine.stop
|
254
|
+
}
|
255
|
+
}
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should perform basic auth" do
|
259
|
+
EventMachine.run {
|
260
|
+
|
261
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/authtest').get :head => {'authorization' => ['user', 'pass']}
|
262
|
+
|
263
|
+
http.errback { failed(http) }
|
264
|
+
http.callback {
|
265
|
+
http.response_header.status.should == 200
|
266
|
+
EventMachine.stop
|
267
|
+
}
|
268
|
+
}
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should perform basic auth via the URL" do
|
272
|
+
EventMachine.run {
|
273
|
+
|
274
|
+
http = EventMachine::HttpRequest.new('http://user:pass@127.0.0.1:8090/authtest').get
|
275
|
+
|
276
|
+
http.errback { failed(http) }
|
277
|
+
http.callback {
|
278
|
+
http.response_header.status.should == 200
|
279
|
+
EventMachine.stop
|
280
|
+
}
|
281
|
+
}
|
282
|
+
end
|
283
|
+
|
284
|
+
it "should return peer's IP address" do
|
285
|
+
EventMachine.run {
|
286
|
+
|
287
|
+
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/')
|
288
|
+
conn.peer.should be_nil
|
289
|
+
|
290
|
+
http = conn.get
|
291
|
+
http.peer.should be_nil
|
292
|
+
|
293
|
+
http.errback { failed(http) }
|
294
|
+
http.callback {
|
295
|
+
conn.peer.should == '127.0.0.1'
|
296
|
+
http.peer.should == '127.0.0.1'
|
297
|
+
|
298
|
+
EventMachine.stop
|
299
|
+
}
|
300
|
+
}
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should remove all newlines from long basic auth header" do
|
304
|
+
EventMachine.run {
|
305
|
+
auth = {'authorization' => ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz']}
|
306
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/auth').get :head => auth
|
307
|
+
http.errback { failed(http) }
|
308
|
+
http.callback {
|
309
|
+
http.response_header.status.should == 200
|
310
|
+
http.response.should == "Basic YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhOnp6enp6enp6enp6enp6enp6enp6enp6enp6enp6eg=="
|
311
|
+
EventMachine.stop
|
312
|
+
}
|
313
|
+
}
|
314
|
+
end
|
315
|
+
|
316
|
+
it "should send proper OAuth auth header" do
|
317
|
+
EventMachine.run {
|
318
|
+
oauth_header = 'OAuth oauth_nonce="oqwgSYFUD87MHmJJDv7bQqOF2EPnVus7Wkqj5duNByU", b=c, d=e'
|
319
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/auth').get :head => {
|
320
|
+
'authorization' => oauth_header
|
321
|
+
}
|
322
|
+
|
323
|
+
http.errback { failed(http) }
|
324
|
+
http.callback {
|
325
|
+
http.response_header.status.should == 200
|
326
|
+
http.response.should == oauth_header
|
327
|
+
EventMachine.stop
|
328
|
+
}
|
329
|
+
}
|
330
|
+
end
|
331
|
+
|
332
|
+
it "should return ETag and Last-Modified headers" do
|
333
|
+
EventMachine.run {
|
334
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_query').get
|
335
|
+
|
336
|
+
http.errback { failed(http) }
|
337
|
+
http.callback {
|
338
|
+
http.response_header.status.should == 200
|
339
|
+
http.response_header.etag.should match('abcdefg')
|
340
|
+
http.response_header.last_modified.should match('Fri, 13 Aug 2010 17:31:21 GMT')
|
341
|
+
EventMachine.stop
|
342
|
+
}
|
343
|
+
}
|
344
|
+
end
|
345
|
+
|
346
|
+
it "should detect deflate encoding" do
|
347
|
+
EventMachine.run {
|
348
|
+
|
349
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/deflate').get :head => {"accept-encoding" => "deflate"}
|
350
|
+
|
351
|
+
http.errback { failed(http) }
|
352
|
+
http.callback {
|
353
|
+
http.response_header.status.should == 200
|
354
|
+
http.response_header["CONTENT_ENCODING"].should == "deflate"
|
355
|
+
http.response.should == "compressed"
|
356
|
+
|
357
|
+
EventMachine.stop
|
358
|
+
}
|
359
|
+
}
|
360
|
+
end
|
361
|
+
|
362
|
+
it "should detect gzip encoding" do
|
363
|
+
EventMachine.run {
|
364
|
+
|
365
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip').get :head => {
|
366
|
+
"accept-encoding" => "gzip, compressed"
|
367
|
+
}
|
368
|
+
|
369
|
+
http.errback { failed(http) }
|
370
|
+
http.callback {
|
371
|
+
http.response_header.status.should == 200
|
372
|
+
http.response_header["CONTENT_ENCODING"].should == "gzip"
|
373
|
+
http.response.should == "compressed"
|
374
|
+
|
375
|
+
EventMachine.stop
|
376
|
+
}
|
377
|
+
}
|
378
|
+
end
|
379
|
+
|
380
|
+
it "should not decode the response when configured so" do
|
381
|
+
EventMachine.run {
|
382
|
+
|
383
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip').get :head => {
|
384
|
+
"accept-encoding" => "gzip, compressed"
|
385
|
+
}, :decoding => false
|
386
|
+
|
387
|
+
http.errback { failed(http) }
|
388
|
+
http.callback {
|
389
|
+
http.response_header.status.should == 200
|
390
|
+
http.response_header["CONTENT_ENCODING"].should == "gzip"
|
391
|
+
|
392
|
+
raw = http.response
|
393
|
+
Zlib::GzipReader.new(StringIO.new(raw)).read.should == "compressed"
|
394
|
+
|
395
|
+
EventMachine.stop
|
396
|
+
}
|
397
|
+
}
|
398
|
+
end
|
399
|
+
|
400
|
+
it "should timeout after 0.1 seconds of inactivity" do
|
401
|
+
EventMachine.run {
|
402
|
+
t = Time.now.to_i
|
403
|
+
EventMachine.heartbeat_interval = 0.1
|
404
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/timeout', :inactivity_timeout => 0.1).get
|
405
|
+
|
406
|
+
http.errback {
|
407
|
+
(Time.now.to_i - t).should <= 1
|
408
|
+
EventMachine.stop
|
409
|
+
}
|
410
|
+
http.callback { failed(http) }
|
411
|
+
}
|
412
|
+
end
|
413
|
+
|
414
|
+
it "should complete a Location: with a relative path" do
|
415
|
+
EventMachine.run {
|
416
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/relative-location').get
|
417
|
+
|
418
|
+
http.errback { failed(http) }
|
419
|
+
http.callback {
|
420
|
+
http.response_header['LOCATION'].should == 'http://127.0.0.1:8090/forwarded'
|
421
|
+
EventMachine.stop
|
422
|
+
}
|
423
|
+
}
|
424
|
+
end
|
425
|
+
|
426
|
+
context "body content-type encoding" do
|
427
|
+
it "should not set content type on string in body" do
|
428
|
+
EventMachine.run {
|
429
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_type').post :body => "data"
|
430
|
+
|
431
|
+
http.errback { failed(http) }
|
432
|
+
http.callback {
|
433
|
+
http.response_header.status.should == 200
|
434
|
+
http.response.should be_empty
|
435
|
+
EventMachine.stop
|
436
|
+
}
|
437
|
+
}
|
438
|
+
end
|
439
|
+
|
440
|
+
it "should set content-type automatically when passed a ruby hash/array for body" do
|
441
|
+
EventMachine.run {
|
442
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_type').post :body => {:a => :b}
|
443
|
+
|
444
|
+
http.errback { failed(http) }
|
445
|
+
http.callback {
|
446
|
+
http.response_header.status.should == 200
|
447
|
+
http.response.should match("application/x-www-form-urlencoded")
|
448
|
+
EventMachine.stop
|
449
|
+
}
|
450
|
+
}
|
451
|
+
end
|
452
|
+
|
453
|
+
it "should not override content-type when passing in ruby hash/array for body" do
|
454
|
+
EventMachine.run {
|
455
|
+
ct = 'text; charset=utf-8'
|
456
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_type').post({
|
457
|
+
:body => {:a => :b}, :head => {'content-type' => ct}})
|
458
|
+
|
459
|
+
http.errback { failed(http) }
|
460
|
+
http.callback {
|
461
|
+
http.response_header.status.should == 200
|
462
|
+
http.content_charset.should == Encoding.find('utf-8') if defined? Encoding
|
463
|
+
http.response_header["CONTENT_TYPE"].should == ct
|
464
|
+
EventMachine.stop
|
465
|
+
}
|
466
|
+
}
|
467
|
+
end
|
468
|
+
|
469
|
+
it "should default to external encoding on invalid encoding" do
|
470
|
+
EventMachine.run {
|
471
|
+
ct = 'text/html; charset=utf-8lias'
|
472
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_type').post({
|
473
|
+
:body => {:a => :b}, :head => {'content-type' => ct}})
|
474
|
+
|
475
|
+
http.errback { failed(http) }
|
476
|
+
http.callback {
|
477
|
+
http.response_header.status.should == 200
|
478
|
+
http.content_charset.should == Encoding.find('utf-8') if defined? Encoding
|
479
|
+
http.response_header["CONTENT_TYPE"].should == ct
|
480
|
+
EventMachine.stop
|
481
|
+
}
|
482
|
+
}
|
483
|
+
end
|
484
|
+
|
485
|
+
it "should processed escaped content-type" do
|
486
|
+
EventMachine.run {
|
487
|
+
ct = "text/html; charset=\"ISO-8859-4\""
|
488
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_type').post({
|
489
|
+
:body => {:a => :b}, :head => {'content-type' => ct}})
|
490
|
+
|
491
|
+
http.errback { failed(http) }
|
492
|
+
http.callback {
|
493
|
+
http.response_header.status.should == 200
|
494
|
+
http.content_charset.should == Encoding.find('ISO-8859-4') if defined? Encoding
|
495
|
+
http.response_header["CONTENT_TYPE"].should == ct
|
496
|
+
EventMachine.stop
|
497
|
+
}
|
498
|
+
}
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
context "optional header callback" do
|
503
|
+
it "should optionally pass the response headers" do
|
504
|
+
EventMachine.run {
|
505
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
|
506
|
+
|
507
|
+
http.errback { failed(http) }
|
508
|
+
http.headers { |hash|
|
509
|
+
hash.should be_an_kind_of Hash
|
510
|
+
hash.should include 'CONNECTION'
|
511
|
+
hash.should include 'CONTENT_LENGTH'
|
512
|
+
}
|
513
|
+
|
514
|
+
http.callback {
|
515
|
+
http.response_header.status.should == 200
|
516
|
+
http.response.should match(/Hello/)
|
517
|
+
EventMachine.stop
|
518
|
+
}
|
519
|
+
}
|
520
|
+
end
|
521
|
+
|
522
|
+
it "should allow to terminate current connection from header callback" do
|
523
|
+
EventMachine.run {
|
524
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
|
525
|
+
|
526
|
+
http.callback { failed(http) }
|
527
|
+
http.headers { |hash|
|
528
|
+
hash.should be_an_kind_of Hash
|
529
|
+
hash.should include 'CONNECTION'
|
530
|
+
hash.should include 'CONTENT_LENGTH'
|
531
|
+
|
532
|
+
http.close('header callback terminated connection')
|
533
|
+
}
|
534
|
+
|
535
|
+
http.errback { |e|
|
536
|
+
http.response_header.status.should == 200
|
537
|
+
http.error.should == 'header callback terminated connection'
|
538
|
+
http.response.should == ''
|
539
|
+
EventMachine.stop
|
540
|
+
}
|
541
|
+
}
|
542
|
+
end
|
543
|
+
end
|
544
|
+
|
545
|
+
it "should optionally pass the response body progressively" do
|
546
|
+
EventMachine.run {
|
547
|
+
body = ''
|
548
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
|
549
|
+
|
550
|
+
http.errback { failed(http) }
|
551
|
+
http.stream { |chunk| body += chunk }
|
552
|
+
|
553
|
+
http.callback {
|
554
|
+
http.response_header.status.should == 200
|
555
|
+
http.response.should == ''
|
556
|
+
body.should match(/Hello/)
|
557
|
+
EventMachine.stop
|
558
|
+
}
|
559
|
+
}
|
560
|
+
end
|
561
|
+
|
562
|
+
it "should optionally pass the deflate-encoded response body progressively" do
|
563
|
+
EventMachine.run {
|
564
|
+
body = ''
|
565
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/deflate').get :head => {
|
566
|
+
"accept-encoding" => "deflate, compressed"
|
567
|
+
}
|
568
|
+
|
569
|
+
http.errback { failed(http) }
|
570
|
+
http.stream { |chunk| body += chunk }
|
571
|
+
|
572
|
+
http.callback {
|
573
|
+
http.response_header.status.should == 200
|
574
|
+
http.response_header["CONTENT_ENCODING"].should == "deflate"
|
575
|
+
http.response.should == ''
|
576
|
+
body.should == "compressed"
|
577
|
+
EventMachine.stop
|
578
|
+
}
|
579
|
+
}
|
580
|
+
end
|
581
|
+
|
582
|
+
it "should accept & return cookie header to user" do
|
583
|
+
EventMachine.run {
|
584
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/set_cookie').get
|
585
|
+
|
586
|
+
http.errback { failed(http) }
|
587
|
+
http.callback {
|
588
|
+
http.response_header.status.should == 200
|
589
|
+
http.response_header.cookie.should == "id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
590
|
+
EventMachine.stop
|
591
|
+
}
|
592
|
+
}
|
593
|
+
end
|
594
|
+
|
595
|
+
it "should return array of cookies on multiple Set-Cookie headers" do
|
596
|
+
EventMachine.run {
|
597
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/set_multiple_cookies').get
|
598
|
+
|
599
|
+
http.errback { failed(http) }
|
600
|
+
http.callback {
|
601
|
+
http.response_header.status.should == 200
|
602
|
+
http.response_header.cookie.size.should == 2
|
603
|
+
http.response_header.cookie.first.should == "id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
604
|
+
http.response_header.cookie.last.should == "id=2;"
|
605
|
+
|
606
|
+
EventMachine.stop
|
607
|
+
}
|
608
|
+
}
|
609
|
+
end
|
610
|
+
|
611
|
+
it "should pass cookie header to server from string" do
|
612
|
+
EventMachine.run {
|
613
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_cookie').get :head => {'cookie' => 'id=2;'}
|
614
|
+
|
615
|
+
http.errback { failed(http) }
|
616
|
+
http.callback {
|
617
|
+
http.response.should == "id=2;"
|
618
|
+
EventMachine.stop
|
619
|
+
}
|
620
|
+
}
|
621
|
+
end
|
622
|
+
|
623
|
+
it "should pass cookie header to server from Hash" do
|
624
|
+
EventMachine.run {
|
625
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_cookie').get :head => {'cookie' => {'id' => 2}}
|
626
|
+
|
627
|
+
http.errback { failed(http) }
|
628
|
+
http.callback {
|
629
|
+
http.response.should == "id=2;"
|
630
|
+
EventMachine.stop
|
631
|
+
}
|
632
|
+
}
|
633
|
+
end
|
634
|
+
|
635
|
+
it "should get the body without Content-Length" do
|
636
|
+
EventMachine.run {
|
637
|
+
@s = StubServer.new("HTTP/1.1 200 OK\r\n\r\nFoo")
|
638
|
+
|
639
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get
|
640
|
+
http.errback { failed(http) }
|
641
|
+
http.callback {
|
642
|
+
http.response.should match(/Foo/)
|
643
|
+
http.response_header['CONTENT_LENGTH'].should be_nil
|
644
|
+
|
645
|
+
@s.stop
|
646
|
+
EventMachine.stop
|
647
|
+
}
|
648
|
+
}
|
649
|
+
end
|
650
|
+
|
651
|
+
context "when talking to a stub HTTP/1.0 server" do
|
652
|
+
it "should get the body without Content-Length" do
|
653
|
+
|
654
|
+
EventMachine.run {
|
655
|
+
@s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo")
|
656
|
+
|
657
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get
|
658
|
+
http.errback { failed(http) }
|
659
|
+
http.callback {
|
660
|
+
http.response.should match(/Foo/)
|
661
|
+
http.response_header['CONTENT_LENGTH'].should be_nil
|
662
|
+
|
663
|
+
@s.stop
|
664
|
+
EventMachine.stop
|
665
|
+
}
|
666
|
+
}
|
667
|
+
end
|
668
|
+
|
669
|
+
it "should work with \\n instead of \\r\\n" do
|
670
|
+
EventMachine.run {
|
671
|
+
@s = StubServer.new("HTTP/1.0 200 OK\nContent-Type: text/plain\nContent-Length: 3\nConnection: close\n\nFoo")
|
672
|
+
|
673
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get
|
674
|
+
http.errback { failed(http) }
|
675
|
+
http.callback {
|
676
|
+
http.response_header.status.should == 200
|
677
|
+
http.response_header['CONTENT_TYPE'].should == 'text/plain'
|
678
|
+
http.response.should match(/Foo/)
|
679
|
+
|
680
|
+
@s.stop
|
681
|
+
EventMachine.stop
|
682
|
+
}
|
683
|
+
}
|
684
|
+
end
|
685
|
+
|
686
|
+
it "should handle invalid HTTP response" do
|
687
|
+
EventMachine.run {
|
688
|
+
@s = StubServer.new("<html></html>")
|
689
|
+
|
690
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get
|
691
|
+
http.callback { failed(http) }
|
692
|
+
http.errback {
|
693
|
+
http.error.should_not be_nil
|
694
|
+
EM.stop
|
695
|
+
}
|
696
|
+
}
|
697
|
+
end
|
698
|
+
end
|
699
|
+
|
700
|
+
it "should stream a file off disk" do
|
701
|
+
EventMachine.run {
|
702
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').post :file => 'spec/fixtures/google.ca'
|
703
|
+
|
704
|
+
http.errback { failed(http) }
|
705
|
+
http.callback {
|
706
|
+
http.response.should match('google')
|
707
|
+
EventMachine.stop
|
708
|
+
}
|
709
|
+
}
|
710
|
+
end
|
711
|
+
|
712
|
+
it 'should handle malformed Content-Type header repetitions' do
|
713
|
+
EventMachine.run {
|
714
|
+
response =<<-HTTP.gsub(/^ +/, '').strip
|
715
|
+
HTTP/1.0 200 OK
|
716
|
+
Content-Type: text/plain; charset=iso-8859-1
|
717
|
+
Content-Type: text/plain; charset=utf-8
|
718
|
+
Content-Length: 5
|
719
|
+
Connection: close
|
720
|
+
|
721
|
+
Hello
|
722
|
+
HTTP
|
723
|
+
|
724
|
+
@s = StubServer.new(response)
|
725
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get
|
726
|
+
http.errback { failed(http) }
|
727
|
+
http.callback {
|
728
|
+
http.content_charset.should == Encoding::ISO_8859_1 if defined? Encoding
|
729
|
+
EventMachine.stop
|
730
|
+
}
|
731
|
+
}
|
732
|
+
end
|
733
|
+
|
734
|
+
it "should allow indifferent access to headers" do
|
735
|
+
EventMachine.run {
|
736
|
+
response =<<-HTTP.gsub(/^ +/, '').strip
|
737
|
+
HTTP/1.0 200 OK
|
738
|
+
Content-Type: text/plain; charset=utf-8
|
739
|
+
X-Custom-Header: foo
|
740
|
+
Content-Length: 5
|
741
|
+
Connection: close
|
742
|
+
|
743
|
+
Hello
|
744
|
+
HTTP
|
745
|
+
|
746
|
+
@s = StubServer.new(response)
|
747
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get
|
748
|
+
http.errback { failed(http) }
|
749
|
+
http.callback {
|
750
|
+
http.response_header["Content-Type"].should == "text/plain; charset=utf-8"
|
751
|
+
http.response_header["CONTENT_TYPE"].should == "text/plain; charset=utf-8"
|
752
|
+
|
753
|
+
http.response_header["Content-Length"].should == "5"
|
754
|
+
http.response_header["CONTENT_LENGTH"].should == "5"
|
755
|
+
|
756
|
+
http.response_header["X-Custom-Header"].should == "foo"
|
757
|
+
http.response_header["X_CUSTOM_HEADER"].should == "foo"
|
758
|
+
|
759
|
+
EventMachine.stop
|
760
|
+
}
|
761
|
+
}
|
762
|
+
end
|
763
|
+
end
|