igrigorik-em-http-request 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/em-http-request.gemspec +1 -1
- data/lib/em-http/client.rb +13 -6
- data/test/stallion.rb +3 -0
- data/test/test_request.rb +26 -0
- metadata +1 -1
data/em-http-request.gemspec
CHANGED
data/lib/em-http/client.rb
CHANGED
@@ -171,6 +171,9 @@ module EventMachine
|
|
171
171
|
|
172
172
|
# start HTTP request once we establish connection to host
|
173
173
|
def connection_completed
|
174
|
+
ssl = @options[:tls] || @options[:ssl] || {}
|
175
|
+
start_tls(ssl) if @uri.scheme == "https" or @uri.port == 443
|
176
|
+
|
174
177
|
send_request_header
|
175
178
|
send_request_body
|
176
179
|
end
|
@@ -191,10 +194,18 @@ module EventMachine
|
|
191
194
|
unbind
|
192
195
|
end
|
193
196
|
|
197
|
+
def normalize_body
|
198
|
+
if @options[:body].is_a? Hash
|
199
|
+
@options[:body].to_params
|
200
|
+
else
|
201
|
+
@options[:body]
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
194
205
|
def send_request_header
|
195
206
|
query = @options[:query]
|
196
207
|
head = @options[:head] ? munge_header_keys(@options[:head]) : {}
|
197
|
-
body =
|
208
|
+
body = normalize_body
|
198
209
|
|
199
210
|
# Set the Host header if it hasn't been specified already
|
200
211
|
head['host'] ||= encode_host
|
@@ -220,11 +231,7 @@ module EventMachine
|
|
220
231
|
|
221
232
|
def send_request_body
|
222
233
|
return unless @options[:body]
|
223
|
-
|
224
|
-
body = @options[:body].to_params
|
225
|
-
else
|
226
|
-
body = @options[:body]
|
227
|
-
end
|
234
|
+
body = normalize_body
|
228
235
|
send_data body
|
229
236
|
end
|
230
237
|
|
data/test/stallion.rb
CHANGED
@@ -80,6 +80,9 @@ Stallion.saddle :spec do |stable|
|
|
80
80
|
elsif stable.request.path_info == '/echo_query'
|
81
81
|
stable.response.write stable.request.query_string
|
82
82
|
|
83
|
+
elsif stable.request.path_info == '/echo_content_length'
|
84
|
+
stable.response.write stable.request.content_length
|
85
|
+
|
83
86
|
elsif stable.request.post?
|
84
87
|
stable.response.write stable.request.body.read
|
85
88
|
|
data/test/test_request.rb
CHANGED
@@ -145,6 +145,20 @@ describe EventMachine::HttpRequest do
|
|
145
145
|
}
|
146
146
|
}
|
147
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
|
148
162
|
|
149
163
|
it "should perform successfull GET with custom header" do
|
150
164
|
EventMachine.run {
|
@@ -277,4 +291,16 @@ describe EventMachine::HttpRequest do
|
|
277
291
|
}
|
278
292
|
end
|
279
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
|
+
|
280
306
|
end
|