camerontaylor-em-http-request 0.1.7

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.
@@ -0,0 +1,34 @@
1
+ require 'test/helper'
2
+ require 'test/stallion'
3
+
4
+ describe EventMachine::MultiRequest do
5
+
6
+ def failed
7
+ EventMachine.stop
8
+ fail
9
+ end
10
+
11
+ it "should submit multiple requests in parallel and return once all of them are complete" do
12
+ EventMachine.run {
13
+
14
+ # create an instance of multi-request handler, and the requests themselves
15
+ multi = EventMachine::MultiRequest.new
16
+
17
+ # add multiple requests to the multi-handler
18
+ multi.add(EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get(:query => {:q => 'test'}))
19
+ multi.add(EventMachine::HttpRequest.new('http://169.169.169.169/').get)
20
+
21
+ multi.callback {
22
+ # verify successfull request
23
+ multi.responses[:succeeded].size.should == 1
24
+ multi.responses[:succeeded].first.response.should match(/test/)
25
+
26
+ # verify invalid requests
27
+ multi.responses[:failed].size.should == 1
28
+ multi.responses[:failed].first.response_header.status.should == 0
29
+
30
+ EventMachine.stop
31
+ }
32
+ }
33
+ end
34
+ end
@@ -0,0 +1,306 @@
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 return 404 on invalid path" do
72
+ EventMachine.run {
73
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/fail').get
74
+
75
+ http.errback { failed }
76
+ http.callback {
77
+ http.response_header.status.should == 404
78
+ EventMachine.stop
79
+ }
80
+ }
81
+ end
82
+
83
+ it "should build query parameters from Hash" do
84
+ EventMachine.run {
85
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :query => {:q => 'test'}
86
+
87
+ http.errback { failed }
88
+ http.callback {
89
+ http.response_header.status.should == 200
90
+ http.response.should match(/test/)
91
+ EventMachine.stop
92
+ }
93
+ }
94
+ end
95
+
96
+ it "should pass query parameters string" do
97
+ EventMachine.run {
98
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :query => "q=test"
99
+
100
+ http.errback { failed }
101
+ http.callback {
102
+ http.response_header.status.should == 200
103
+ http.response.should match(/test/)
104
+ EventMachine.stop
105
+ }
106
+ }
107
+ end
108
+
109
+ it "should encode an array of query parameters" do
110
+ EventMachine.run {
111
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_query').get :query => {:hash => ['value1', 'value2']}
112
+
113
+ http.errback { failed }
114
+ http.callback {
115
+ http.response_header.status.should == 200
116
+ http.response.should match(/hash\[\]=value1&hash\[\]=value2/)
117
+ EventMachine.stop
118
+ }
119
+ }
120
+ end
121
+
122
+ it "should perform successfull POST" do
123
+ EventMachine.run {
124
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post :body => "data"
125
+
126
+ http.errback { failed }
127
+ http.callback {
128
+ http.response_header.status.should == 200
129
+ http.response.should match(/data/)
130
+ EventMachine.stop
131
+ }
132
+ }
133
+ end
134
+
135
+ it "should perform successfull POST with Ruby Hash/Array as params" do
136
+ EventMachine.run {
137
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post :body => {"key1" => 1, "key2" => [2,3]}
138
+
139
+ http.errback { failed }
140
+ http.callback {
141
+ http.response_header.status.should == 200
142
+
143
+ http.response.should match(/key1=1&key2\[0\]=2&key2\[1\]=3/)
144
+ EventMachine.stop
145
+ }
146
+ }
147
+ end
148
+
149
+ it "should perform successfull POST with Ruby Hash/Array as params and with the correct content length" do
150
+ EventMachine.run {
151
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_content_length').post :body => {"key1" => "data1"}
152
+
153
+ http.errback { failed }
154
+ http.callback {
155
+ http.response_header.status.should == 200
156
+
157
+ http.response.to_i.should == 10
158
+ EventMachine.stop
159
+ }
160
+ }
161
+ end
162
+
163
+ it "should perform successfull GET with custom header" do
164
+ EventMachine.run {
165
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :head => {'if-none-match' => 'evar!'}
166
+
167
+ http.errback { failed }
168
+ http.callback {
169
+ http.response_header.status.should == 304
170
+ EventMachine.stop
171
+ }
172
+ }
173
+ end
174
+
175
+ it "should perform a streaming GET" do
176
+ EventMachine.run {
177
+
178
+ # digg.com uses chunked encoding
179
+ http = EventMachine::HttpRequest.new('http://digg.com/').get
180
+
181
+ http.errback { failed }
182
+ http.callback {
183
+ http.response_header.status.should == 200
184
+ EventMachine.stop
185
+ }
186
+ }
187
+ end
188
+
189
+ it "should perform basic auth" do
190
+ EventMachine.run {
191
+
192
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :head => {'authorization' => ['user', 'pass']}
193
+
194
+ http.errback { failed }
195
+ http.callback {
196
+ http.response_header.status.should == 200
197
+ EventMachine.stop
198
+ }
199
+ }
200
+ end
201
+
202
+ it "should work with keep-alive servers" do
203
+ EventMachine.run {
204
+
205
+ http = EventMachine::HttpRequest.new('http://mexicodiario.com/touch.public.json.php').get
206
+
207
+ http.errback { failed }
208
+ http.callback {
209
+ http.response_header.status.should == 200
210
+ EventMachine.stop
211
+ }
212
+ }
213
+ end
214
+
215
+ it "should detect deflate encoding" do
216
+ EventMachine.run {
217
+
218
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate"}
219
+
220
+ http.errback { failed }
221
+ http.callback {
222
+ http.response_header.status.should == 200
223
+ http.response_header["CONTENT_ENCODING"].should == "deflate"
224
+ http.response.should == "compressed"
225
+
226
+ EventMachine.stop
227
+ }
228
+ }
229
+ end
230
+
231
+ it "should detect gzip encoding" do
232
+ EventMachine.run {
233
+
234
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/gzip').get :head => {"accept-encoding" => "gzip, compressed"}
235
+
236
+ http.errback { failed }
237
+ http.callback {
238
+ http.response_header.status.should == 200
239
+ http.response_header["CONTENT_ENCODING"].should == "gzip"
240
+ http.response.should == "compressed"
241
+
242
+ EventMachine.stop
243
+ }
244
+ }
245
+ end
246
+
247
+ it "should timeout after 10 seconds" do
248
+ EventMachine.run {
249
+ t = Time.now.to_i
250
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/timeout').get :timeout => 2
251
+
252
+ http.errback {
253
+ (Time.now.to_i - t).should == 2
254
+ EventMachine.stop
255
+ }
256
+ http.callback { failed }
257
+ }
258
+ end
259
+
260
+ it "should optionally pass the response body progressively" do
261
+ EventMachine.run {
262
+ body = ''
263
+ on_body = lambda { |chunk| body += chunk }
264
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :on_response => on_body
265
+
266
+ http.errback { failed }
267
+ http.callback {
268
+ http.response_header.status.should == 200
269
+ http.response.should == ''
270
+ body.should match(/Hello/)
271
+ EventMachine.stop
272
+ }
273
+ }
274
+ end
275
+
276
+ it "should optionally pass the deflate-encoded response body progressively" do
277
+ EventMachine.run {
278
+ body = ''
279
+ on_body = lambda { |chunk| body += chunk }
280
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate, compressed"},
281
+ :on_response => on_body
282
+
283
+ http.errback { failed }
284
+ http.callback {
285
+ http.response_header.status.should == 200
286
+ http.response_header["CONTENT_ENCODING"].should == "deflate"
287
+ http.response.should == ''
288
+ body.should == "compressed"
289
+ EventMachine.stop
290
+ }
291
+ }
292
+ end
293
+
294
+ it "should initiate SSL/TLS on HTTPS connections" do
295
+ EventMachine.run {
296
+ http = EventMachine::HttpRequest.new('https://mail.google.com:443/mail/').get
297
+
298
+ http.errback { failed }
299
+ http.callback {
300
+ http.response_header.status.should == 302
301
+ EventMachine.stop
302
+ }
303
+ }
304
+ end
305
+
306
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camerontaylor-em-http-request
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
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/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
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: em-http-request
82
+ rubygems_version: 1.2.0
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: EventMachine based HTTP Request interface
86
+ test_files: []
87
+