dwaite-em-http-request 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_hash.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'test/helper'
2
+
3
+ describe Hash do
4
+
5
+ describe ".to_params" do
6
+ it "should transform a basic hash into HTTP POST Params" do
7
+ {:a => "alpha", :b => "beta"}.to_params.should == "a=alpha&b=beta"
8
+ end
9
+
10
+ it "should transform a more complex hash into HTTP POST Params" do
11
+ {:a => "a", :b => ["c", "d", "e"]}.to_params.should == "a=a&b[0]=c&b[1]=d&b[2]=e"
12
+ end
13
+
14
+ # Ruby 1.8 Hash is not sorted, so this test breaks randomly. Maybe once we're all on 1.9. ;-)
15
+ # it "should transform a very complex hash into HTTP POST Params" do
16
+ # {:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]}.to_params.should == "a=a&b[0][d]=d&b[0][c]=c&b[1][f]=f&b[1][e]=e"
17
+ # end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ require 'test/helper'
2
+ require 'test/stallion'
3
+
4
+ describe EventMachine::MultiRequest do
5
+
6
+ it "should submit multiple requests in parallel and return once all of them are complete" do
7
+ EventMachine.run {
8
+
9
+ # create an instance of multi-request handler, and the requests themselves
10
+ multi = EventMachine::MultiRequest.new
11
+
12
+ # add multiple requests to the multi-handler
13
+ multi.add(EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get(:query => {:q => 'test'}))
14
+ multi.add(EventMachine::HttpRequest.new('http://0.0.0.0/').get(:timeout => 1))
15
+
16
+ multi.callback {
17
+ # verify successfull request
18
+ multi.responses[:succeeded].size.should == 1
19
+ multi.responses[:succeeded].first.response.should match(/test/)
20
+
21
+ # verify invalid requests
22
+ multi.responses[:failed].size.should == 1
23
+ multi.responses[:failed].first.response_header.status.should == 0
24
+
25
+ EventMachine.stop
26
+ }
27
+ }
28
+ end
29
+ end
@@ -0,0 +1,358 @@
1
+ require 'test/helper'
2
+ require 'test/stallion'
3
+
4
+ describe EventMachine::HttpRequest do
5
+
6
+ def failed
7
+ EventMachine.stop
8
+ fail
9
+ end
10
+
11
+ it "should fail GET on DNS timeout" do
12
+ EventMachine.run {
13
+ http = EventMachine::HttpRequest.new('http://127.1.1.1/').get
14
+ http.callback { failed }
15
+ http.errback {
16
+ http.response_header.status.should == 0
17
+ EventMachine.stop
18
+ }
19
+ }
20
+ end
21
+
22
+ it "should fail GET on invalid host" do
23
+ EventMachine.run {
24
+ http = EventMachine::HttpRequest.new('http://google1.com/').get
25
+ http.callback { failed }
26
+ http.errback {
27
+ http.response_header.status.should == 0
28
+ http.errors.should match(/no connection/)
29
+ EventMachine.stop
30
+ }
31
+ }
32
+ end
33
+
34
+ it "should fail GET on missing path" do
35
+ EventMachine.run {
36
+ lambda {
37
+ EventMachine::HttpRequest.new('http://www.google.com').get
38
+ }.should raise_error(ArgumentError)
39
+
40
+ EventMachine.stop
41
+ }
42
+ end
43
+
44
+ it "should perform successfull GET" do
45
+ EventMachine.run {
46
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get
47
+
48
+ http.errback { failed }
49
+ http.callback {
50
+ http.response_header.status.should == 200
51
+ http.response.should match(/Hello/)
52
+ EventMachine.stop
53
+ }
54
+ }
55
+ end
56
+
57
+ it "should perform successfull GET with a URI passed as argument" do
58
+ EventMachine.run {
59
+ uri = URI.parse('http://127.0.0.1:8080/')
60
+ http = EventMachine::HttpRequest.new(uri).get
61
+
62
+ http.errback { failed }
63
+ http.callback {
64
+ http.response_header.status.should == 200
65
+ http.response.should match(/Hello/)
66
+ EventMachine.stop
67
+ }
68
+ }
69
+ end
70
+
71
+ it "should perform successfull HEAD with a URI passed as argument" do
72
+ EventMachine.run {
73
+ uri = URI.parse('http://127.0.0.1:8080/')
74
+ http = EventMachine::HttpRequest.new(uri).head
75
+
76
+ http.errback { failed }
77
+ http.callback {
78
+ http.response_header.status.should == 200
79
+ http.response.should == ""
80
+ EventMachine.stop
81
+ }
82
+ }
83
+ end
84
+
85
+ it "should return 404 on invalid path" do
86
+ EventMachine.run {
87
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/fail').get
88
+
89
+ http.errback { failed }
90
+ http.callback {
91
+ http.response_header.status.should == 404
92
+ EventMachine.stop
93
+ }
94
+ }
95
+ end
96
+
97
+ it "should build query parameters from Hash" do
98
+ EventMachine.run {
99
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :query => {:q => 'test'}
100
+
101
+ http.errback { failed }
102
+ http.callback {
103
+ http.response_header.status.should == 200
104
+ http.response.should match(/test/)
105
+ EventMachine.stop
106
+ }
107
+ }
108
+ end
109
+
110
+ it "should pass query parameters string" do
111
+ EventMachine.run {
112
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :query => "q=test"
113
+
114
+ http.errback { failed }
115
+ http.callback {
116
+ http.response_header.status.should == 200
117
+ http.response.should match(/test/)
118
+ EventMachine.stop
119
+ }
120
+ }
121
+ end
122
+
123
+ it "should encode an array of query parameters" do
124
+ EventMachine.run {
125
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_query').get :query => {:hash => ['value1', 'value2']}
126
+
127
+ http.errback { failed }
128
+ http.callback {
129
+ http.response_header.status.should == 200
130
+ http.response.should match(/hash\[\]=value1&hash\[\]=value2/)
131
+ EventMachine.stop
132
+ }
133
+ }
134
+ end
135
+
136
+ it "should perform successfull POST" do
137
+ EventMachine.run {
138
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post :body => "data"
139
+
140
+ http.errback { failed }
141
+ http.callback {
142
+ http.response_header.status.should == 200
143
+ http.response.should match(/data/)
144
+ EventMachine.stop
145
+ }
146
+ }
147
+ end
148
+
149
+ it "should perform successfull POST with Ruby Hash/Array as params" do
150
+ EventMachine.run {
151
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post :body => {"key1" => 1, "key2" => [2,3]}
152
+
153
+ http.errback { failed }
154
+ http.callback {
155
+ http.response_header.status.should == 200
156
+
157
+ http.response.should match(/key1=1&key2\[0\]=2&key2\[1\]=3/)
158
+ EventMachine.stop
159
+ }
160
+ }
161
+ end
162
+
163
+ it "should perform successfull POST with Ruby Hash/Array as params and with the correct content length" do
164
+ EventMachine.run {
165
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_content_length').post :body => {"key1" => "data1"}
166
+
167
+ http.errback { failed }
168
+ http.callback {
169
+ http.response_header.status.should == 200
170
+
171
+ http.response.to_i.should == 10
172
+ EventMachine.stop
173
+ }
174
+ }
175
+ end
176
+
177
+ it "should perform successfull GET with custom header" do
178
+ EventMachine.run {
179
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :head => {'if-none-match' => 'evar!'}
180
+
181
+ http.errback { failed }
182
+ http.callback {
183
+ http.response_header.status.should == 304
184
+ EventMachine.stop
185
+ }
186
+ }
187
+ end
188
+
189
+ it "should perform a streaming GET" do
190
+ EventMachine.run {
191
+
192
+ # digg.com uses chunked encoding
193
+ http = EventMachine::HttpRequest.new('http://digg.com/').get
194
+
195
+ http.errback { failed }
196
+ http.callback {
197
+ http.response_header.status.should == 200
198
+ EventMachine.stop
199
+ }
200
+ }
201
+ end
202
+
203
+ it "should perform basic auth" do
204
+ EventMachine.run {
205
+
206
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :head => {'authorization' => ['user', 'pass']}
207
+
208
+ http.errback { failed }
209
+ http.callback {
210
+ http.response_header.status.should == 200
211
+ EventMachine.stop
212
+ }
213
+ }
214
+ end
215
+
216
+ it "should work with keep-alive servers" do
217
+ EventMachine.run {
218
+
219
+ http = EventMachine::HttpRequest.new('http://mexicodiario.com/touch.public.json.php').get
220
+
221
+ http.errback { failed }
222
+ http.callback {
223
+ http.response_header.status.should == 200
224
+ EventMachine.stop
225
+ }
226
+ }
227
+ end
228
+
229
+ it "should detect deflate encoding" do
230
+ EventMachine.run {
231
+
232
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate"}
233
+
234
+ http.errback { failed }
235
+ http.callback {
236
+ http.response_header.status.should == 200
237
+ http.response_header["CONTENT_ENCODING"].should == "deflate"
238
+ http.response.should == "compressed"
239
+
240
+ EventMachine.stop
241
+ }
242
+ }
243
+ end
244
+
245
+ it "should detect gzip encoding" do
246
+ EventMachine.run {
247
+
248
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/gzip').get :head => {"accept-encoding" => "gzip, compressed"}
249
+
250
+ http.errback { failed }
251
+ http.callback {
252
+ http.response_header.status.should == 200
253
+ http.response_header["CONTENT_ENCODING"].should == "gzip"
254
+ http.response.should == "compressed"
255
+
256
+ EventMachine.stop
257
+ }
258
+ }
259
+ end
260
+
261
+ it "should timeout after 10 seconds" do
262
+ EventMachine.run {
263
+ t = Time.now.to_i
264
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/timeout').get :timeout => 1
265
+
266
+ http.errback {
267
+ (Time.now.to_i - t).should >= 2
268
+ EventMachine.stop
269
+ }
270
+ http.callback { failed }
271
+ }
272
+ end
273
+
274
+ it "should optionally pass the response body progressively" do
275
+ EventMachine.run {
276
+ body = ''
277
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get
278
+
279
+ http.errback { failed }
280
+ http.stream { |chunk| body += chunk }
281
+
282
+ http.callback {
283
+ http.response_header.status.should == 200
284
+ http.response.should == ''
285
+ body.should match(/Hello/)
286
+ EventMachine.stop
287
+ }
288
+ }
289
+ end
290
+
291
+ it "should optionally pass the deflate-encoded response body progressively" do
292
+ EventMachine.run {
293
+ body = ''
294
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate, compressed"}
295
+
296
+ http.errback { failed }
297
+ http.stream { |chunk| body += chunk }
298
+
299
+ http.callback {
300
+ http.response_header.status.should == 200
301
+ http.response_header["CONTENT_ENCODING"].should == "deflate"
302
+ http.response.should == ''
303
+ body.should == "compressed"
304
+ EventMachine.stop
305
+ }
306
+ }
307
+ end
308
+
309
+ it "should initiate SSL/TLS on HTTPS connections" do
310
+ EventMachine.run {
311
+ http = EventMachine::HttpRequest.new('https://mail.google.com:443/mail/').get
312
+
313
+ http.errback { failed }
314
+ http.callback {
315
+ http.response_header.status.should == 302
316
+ EventMachine.stop
317
+ }
318
+ }
319
+ end
320
+
321
+ it "should accept & return cookie header to user" do
322
+ EventMachine.run {
323
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/set_cookie').get
324
+
325
+ http.errback { failed }
326
+ http.callback {
327
+ http.response_header.status.should == 200
328
+ http.response_header.cookie.should == "id=1; expires=Tue, 09-Aug-2011 17:53:39 GMT; path=/;"
329
+ EventMachine.stop
330
+ }
331
+ }
332
+ end
333
+
334
+ it "should pass cookie header to server from string" do
335
+ EventMachine.run {
336
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_cookie').get :head => {'cookie' => 'id=2;'}
337
+
338
+ http.errback { failed }
339
+ http.callback {
340
+ http.response.should == "id=2;"
341
+ EventMachine.stop
342
+ }
343
+ }
344
+ end
345
+
346
+ it "should pass cookie header to server from Hash" do
347
+ EventMachine.run {
348
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_cookie').get :head => {'cookie' => {'id' => 2}}
349
+
350
+ http.errback { failed }
351
+ http.callback {
352
+ http.response.should == "id=2;"
353
+ EventMachine.stop
354
+ }
355
+ }
356
+ end
357
+
358
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dwaite-em-http-request
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.9
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Grigorik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: eventmachine
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.12.2
24
+ version:
25
+ description: EventMachine based HTTP Request interface
26
+ email: ilya@igvita.com
27
+ executables: []
28
+
29
+ extensions:
30
+ - ext/buffer/extconf.rb
31
+ - ext/http11_client/extconf.rb
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - .autotest
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - em-http-request.gemspec
41
+ - ext/buffer/em_buffer.c
42
+ - ext/buffer/extconf.rb
43
+ - ext/http11_client/ext_help.h
44
+ - ext/http11_client/extconf.rb
45
+ - ext/http11_client/http11_client.c
46
+ - ext/http11_client/http11_parser.c
47
+ - ext/http11_client/http11_parser.h
48
+ - ext/http11_client/http11_parser.rl
49
+ - lib/em-http.rb
50
+ - lib/em-http/client.rb
51
+ - lib/em-http/core_ext/hash.rb
52
+ - lib/em-http/decoders.rb
53
+ - lib/em-http/multi.rb
54
+ - lib/em-http/request.rb
55
+ - test/test_hash.rb
56
+ - test/helper.rb
57
+ - test/stallion.rb
58
+ - test/test_multi.rb
59
+ - test/test_request.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/igrigorik/em-http-request
62
+ licenses:
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project: em-http-request
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: EventMachine based HTTP Request interface
87
+ test_files: []
88
+