spec/request_spec.rb in em-http-request-0.2.7 vs spec/request_spec.rb in em-http-request-0.2.9
- old
+ new
@@ -9,10 +9,11 @@
fail
end
it "should fail GET on DNS timeout" do
EventMachine.run {
+ EventMachine.heartbeat_interval = 0.1
http = EventMachine::HttpRequest.new('http://127.1.1.1/').get :timeout => 1
http.callback { failed }
http.errback {
http.response_header.status.should == 0
EventMachine.stop
@@ -20,15 +21,16 @@
}
end
it "should fail GET on invalid host" do
EventMachine.run {
+ EventMachine.heartbeat_interval = 0.1
http = EventMachine::HttpRequest.new('http://somethinglocal/').get :timeout => 1
http.callback { failed }
http.errback {
http.response_header.status.should == 0
- http.errors.should match(/unable to resolve server address/)
+ http.error.should match(/unable to resolve server address/)
EventMachine.stop
}
}
end
@@ -72,11 +74,11 @@
it "should perform successfull HEAD with a URI passed as argument" do
EventMachine.run {
uri = URI.parse('http://127.0.0.1:8080/')
http = EventMachine::HttpRequest.new(uri).head
- http.errback { failed }
+ http.errback { p http; failed }
http.callback {
http.response_header.status.should == 200
http.response.should == ""
EventMachine.stop
}
@@ -300,23 +302,67 @@
EventMachine.stop
}
}
end
- it "should timeout after 10 seconds" do
+ it "should timeout after 1 second" do
EventMachine.run {
t = Time.now.to_i
+ EventMachine.heartbeat_interval = 0.1
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/timeout').get :timeout => 1
http.errback {
- (Time.now.to_i - t).should >= 2
+ (Time.now.to_i - t).should <= 5
EventMachine.stop
}
http.callback { failed }
}
end
+ it "should report last_effective_url" do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get
+ http.errback { failed }
+ http.callback {
+ http.response_header.status.should == 200
+ http.last_effective_url.to_s.should == 'http://127.0.0.1:8080/'
+
+ EM.stop
+ }
+ }
+ end
+
+ it "should follow location redirects" do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/redirect').get :redirects => 1
+ http.errback { failed }
+ http.callback {
+ http.response_header.status.should == 200
+ http.response_header["CONTENT_ENCODING"].should == "gzip"
+ http.response.should == "compressed"
+ http.last_effective_url.to_s.should == 'http://127.0.0.1:8080/gzip'
+ http.redirects.should == 1
+
+ EM.stop
+ }
+ }
+ end
+
+ it "should default to 0 redirects" do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/redirect').get
+ http.errback { failed }
+ http.callback {
+ http.response_header.status.should == 301
+ http.last_effective_url.to_s.should == 'http://127.0.0.1:8080/gzip'
+ http.redirects.should == 0
+
+ EM.stop
+ }
+ }
+ end
+
it "should optionally pass the response body progressively" do
EventMachine.run {
body = ''
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get
@@ -484,12 +530,24 @@
http.callback {
http.response_header['LOCATION'].should == 'http://127.0.0.1:8080/forwarded'
EventMachine.stop
}
}
- end
-
+ end
+
+ it "should stream a file off disk" do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post :file => 'spec/fixtures/google.ca'
+
+ http.errback { failed }
+ http.callback {
+ http.response.should match('google')
+ EventMachine.stop
+ }
+ }
+ end
+
it 'should let you pass a block to be called once the client is created' do
client = nil
EventMachine.run {
request = EventMachine::HttpRequest.new('http://127.0.0.1:8080/')
http = request.post { |c|
@@ -499,18 +557,17 @@
http.errback { failed }
http.callback {
client.should be_kind_of(EventMachine::HttpClient)
http.response_header.status.should == 200
http.response.should match(/callback_run=yes/)
- client.normalize_uri.should == Addressable::URI.parse('http://127.0.0.1:8080/')
EventMachine.stop
}
}
end
it "should retrieve multiple cookies" do
- EventMachine::MockHttpRequest.register_file('http://www.google.ca:80/', :get, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
+ EventMachine::MockHttpRequest.register_file('http://www.google.ca:80/', :get, {}, File.join(File.dirname(__FILE__), 'fixtures', 'google.ca'))
EventMachine.run {
http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get
http.errback { fail }
http.callback {
@@ -520,11 +577,11 @@
EventMachine.stop
}
}
- EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get).should == 1
+ EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1
end
context "connections via proxy" do
it "should work with proxy servers" do
@@ -593,10 +650,10 @@
http.response_header['UPGRADE'].should match(/WebSocket/)
# push should only be invoked after handshake is complete
http.send(MSG)
}
-
+
http.stream { |chunk|
chunk.should == MSG
EventMachine.stop
}
}