igrigorik-em-http-request 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -2
- data/lib/em-http/client.rb +0 -2
- data/lib/em-http/request.rb +13 -7
- data/test/stallion.rb +4 -0
- data/test/test_request.rb +34 -9
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -3,12 +3,13 @@
|
|
3
3
|
EventMachine based HTTP Request interface. Supports streaming response processing, uses Ragel HTTP parser.
|
4
4
|
- Simple interface for single & parallel requests via deferred callbacks
|
5
5
|
- Automatic gzip & deflate decoding
|
6
|
-
- Basic-Auth support
|
6
|
+
- Basic-Auth support
|
7
|
+
- Custom timeouts
|
7
8
|
|
8
9
|
== Simple client example
|
9
10
|
|
10
11
|
EventMachine.run {
|
11
|
-
http = EventMachine::HttpRequest.new('http://127.0.0.1/').get :query => {'keyname' => 'value'}
|
12
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1/').get :query => {'keyname' => 'value'}, :timeout => 10
|
12
13
|
|
13
14
|
http.callback {
|
14
15
|
p http.response_header.status
|
data/lib/em-http/client.rb
CHANGED
@@ -155,8 +155,6 @@ module EventMachine
|
|
155
155
|
attr_reader :response, :response_header, :errors
|
156
156
|
|
157
157
|
def post_init
|
158
|
-
self.comm_inactivity_timeout = 5
|
159
|
-
|
160
158
|
@parser = HttpClientParser.new
|
161
159
|
@data = EventMachine::Buffer.new
|
162
160
|
@response_header = HttpResponseHeader.new
|
data/lib/em-http/request.rb
CHANGED
@@ -24,7 +24,6 @@ module EventMachine
|
|
24
24
|
#
|
25
25
|
|
26
26
|
class HttpRequest
|
27
|
-
attr_reader :response, :headers
|
28
27
|
|
29
28
|
def initialize(host, headers = {})
|
30
29
|
@headers = headers
|
@@ -52,12 +51,19 @@ module EventMachine
|
|
52
51
|
raise ArgumentError, "invalid request path" unless /^\// === @uri.path
|
53
52
|
|
54
53
|
method = method.to_s.upcase
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
54
|
+
begin
|
55
|
+
EventMachine.connect(@uri.host, @uri.port, EventMachine::HttpClient) { |c|
|
56
|
+
c.uri = @uri
|
57
|
+
c.method = method
|
58
|
+
c.options = options
|
59
|
+
c.comm_inactivity_timeout = options[:timeout] || 5
|
60
|
+
}
|
61
|
+
rescue RuntimeError => e
|
62
|
+
raise e unless e.message == "no connection"
|
63
|
+
conn = EventMachine::HttpClient.new("")
|
64
|
+
conn.on_error("no connection")
|
65
|
+
conn
|
66
|
+
end
|
61
67
|
end
|
62
68
|
end
|
63
69
|
end
|
data/test/stallion.rb
CHANGED
@@ -83,6 +83,10 @@ Stallion.saddle :spec do |stable|
|
|
83
83
|
elsif stable.request.post?
|
84
84
|
stable.response.write 'test'
|
85
85
|
|
86
|
+
elsif stable.request.path_info == '/timeout'
|
87
|
+
sleep(10)
|
88
|
+
stable.response.write 'timeout'
|
89
|
+
|
86
90
|
elsif stable.request.path_info == '/gzip'
|
87
91
|
io = StringIO.new
|
88
92
|
gzip = Zlib::GzipWriter.new(io)
|
data/test/test_request.rb
CHANGED
@@ -8,9 +8,9 @@ describe EventMachine::HttpRequest do
|
|
8
8
|
fail
|
9
9
|
end
|
10
10
|
|
11
|
-
it "should fail GET on
|
11
|
+
it "should fail GET on DNS timeout" do
|
12
12
|
EventMachine.run {
|
13
|
-
http = EventMachine::HttpRequest.new('http://
|
13
|
+
http = EventMachine::HttpRequest.new('http://127.1.1.1/').get
|
14
14
|
http.callback { failed }
|
15
15
|
http.errback {
|
16
16
|
http.response_header.status.should == 0
|
@@ -19,6 +19,18 @@ describe EventMachine::HttpRequest do
|
|
19
19
|
}
|
20
20
|
end
|
21
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
|
+
|
22
34
|
it "should fail GET on missing path" do
|
23
35
|
EventMachine.run {
|
24
36
|
lambda {
|
@@ -122,11 +134,11 @@ describe EventMachine::HttpRequest do
|
|
122
134
|
EventMachine.run {
|
123
135
|
|
124
136
|
# digg.com uses chunked encoding
|
125
|
-
http = EventMachine::HttpRequest.new('http://
|
137
|
+
http = EventMachine::HttpRequest.new('http://digg.com/').get
|
126
138
|
|
127
139
|
http.errback { failed }
|
128
140
|
http.callback {
|
129
|
-
http.response_header.status == 200
|
141
|
+
http.response_header.status.should == 200
|
130
142
|
EventMachine.stop
|
131
143
|
}
|
132
144
|
}
|
@@ -139,7 +151,7 @@ describe EventMachine::HttpRequest do
|
|
139
151
|
|
140
152
|
http.errback { failed }
|
141
153
|
http.callback {
|
142
|
-
http.response_header.status == 200
|
154
|
+
http.response_header.status.should == 200
|
143
155
|
EventMachine.stop
|
144
156
|
}
|
145
157
|
}
|
@@ -152,7 +164,7 @@ describe EventMachine::HttpRequest do
|
|
152
164
|
|
153
165
|
http.errback { failed }
|
154
166
|
http.callback {
|
155
|
-
http.response_header.status == 200
|
167
|
+
http.response_header.status.should == 200
|
156
168
|
EventMachine.stop
|
157
169
|
}
|
158
170
|
}
|
@@ -165,7 +177,7 @@ describe EventMachine::HttpRequest do
|
|
165
177
|
|
166
178
|
http.errback { failed }
|
167
179
|
http.callback {
|
168
|
-
http.response_header.status == 200
|
180
|
+
http.response_header.status.should == 200
|
169
181
|
http.response_header["CONTENT_ENCODING"].should == "deflate"
|
170
182
|
http.response.should == "compressed"
|
171
183
|
|
@@ -174,14 +186,14 @@ describe EventMachine::HttpRequest do
|
|
174
186
|
}
|
175
187
|
end
|
176
188
|
|
177
|
-
|
189
|
+
it "should detect gzip encoding" do
|
178
190
|
EventMachine.run {
|
179
191
|
|
180
192
|
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/gzip').get :head => {"accept-encoding" => "gzip, compressed"}
|
181
193
|
|
182
194
|
http.errback { failed }
|
183
195
|
http.callback {
|
184
|
-
http.response_header.status == 200
|
196
|
+
http.response_header.status.should == 200
|
185
197
|
http.response_header["CONTENT_ENCODING"].should == "gzip"
|
186
198
|
http.response.should == "compressed"
|
187
199
|
|
@@ -189,4 +201,17 @@ describe EventMachine::HttpRequest do
|
|
189
201
|
}
|
190
202
|
}
|
191
203
|
end
|
204
|
+
|
205
|
+
it "should timeout after 10 seconds" do
|
206
|
+
EventMachine.run {
|
207
|
+
t = Time.now.to_i
|
208
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/timeout').get :timeout => 2
|
209
|
+
|
210
|
+
http.errback {
|
211
|
+
(Time.now.to_i - t).should == 2
|
212
|
+
EventMachine.stop
|
213
|
+
}
|
214
|
+
http.callback { failed }
|
215
|
+
}
|
216
|
+
end
|
192
217
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: igrigorik-em-http-request
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Grigorik
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-20 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|