test/test_request.rb in astro-em-http-request-0.1.6 vs test/test_request.rb in astro-em-http-request-0.1.12

- old
+ new

@@ -1,29 +1,30 @@ require 'test/helper' require 'test/stallion' - +require 'test/stub_server' + describe EventMachine::HttpRequest do def failed EventMachine.stop fail end - + it "should fail GET on DNS timeout" do EventMachine.run { - http = EventMachine::HttpRequest.new('http://127.1.1.1/').get + 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 } } end it "should fail GET on invalid host" do EventMachine.run { - http = EventMachine::HttpRequest.new('http://google1.com/').get + http = EventMachine::HttpRequest.new('http://google1.com/').get :timeout => 1 http.callback { failed } http.errback { http.response_header.status.should == 0 http.errors.should match(/no connection/) EventMachine.stop @@ -66,16 +67,30 @@ EventMachine.stop } } end + 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.callback { + http.response_header.status.should == 200 + http.response.should == "" + EventMachine.stop + } + } + end + it "should return 404 on invalid path" do EventMachine.run { http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/fail').get http.errback { failed } - http.callback { + http.callback { http.response_header.status.should == 404 EventMachine.stop } } end @@ -143,11 +158,25 @@ http.response.should match(/key1=1&key2\[0\]=2&key2\[1\]=3/) EventMachine.stop } } end + + it "should perform successfull POST with Ruby Hash/Array as params and with the correct content length" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_content_length').post :body => {"key1" => "data1"} + http.errback { failed } + http.callback { + http.response_header.status.should == 200 + + http.response.to_i.should == 10 + EventMachine.stop + } + } + end + it "should perform successfull GET with custom header" do EventMachine.run { http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :head => {'if-none-match' => 'evar!'} http.errback { failed } @@ -231,27 +260,28 @@ end it "should timeout after 10 seconds" do EventMachine.run { t = Time.now.to_i - http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/timeout').get :timeout => 2 + 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 >= 2 EventMachine.stop } http.callback { failed } } end it "should optionally pass the response body progressively" do EventMachine.run { body = '' - on_body = lambda { |chunk| body += chunk } - http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :on_response => on_body + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get http.errback { failed } + http.stream { |chunk| body += chunk } + http.callback { http.response_header.status.should == 200 http.response.should == '' body.should match(/Hello/) EventMachine.stop @@ -260,33 +290,103 @@ end it "should optionally pass the deflate-encoded response body progressively" do EventMachine.run { body = '' - on_body = lambda { |chunk| body += chunk } - http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate, compressed"}, - :on_response => on_body + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate, compressed"} http.errback { failed } + http.stream { |chunk| body += chunk } + http.callback { http.response_header.status.should == 200 http.response_header["CONTENT_ENCODING"].should == "deflate" http.response.should == '' body.should == "compressed" EventMachine.stop } } end - it "should respect manually-passed host address" do + it "should initiate SSL/TLS on HTTPS connections" do EventMachine.run { - http = EventMachine::HttpRequest.new('http://127.1.1.1:8080/').get :host => '127.0.0.1' + http = EventMachine::HttpRequest.new('https://mail.google.com:443/mail/').get http.errback { failed } http.callback { + http.response_header.status.should == 302 + EventMachine.stop + } + } + end + + it "should accept & return cookie header to user" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/set_cookie').get + + http.errback { failed } + http.callback { http.response_header.status.should == 200 - http.response.should match(/Hello/) + http.response_header.cookie.should == "id=1; expires=Tue, 09-Aug-2011 17:53:39 GMT; path=/;" EventMachine.stop } } + end + + it "should pass cookie header to server from string" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_cookie').get :head => {'cookie' => 'id=2;'} + + http.errback { failed } + http.callback { + http.response.should == "id=2;" + EventMachine.stop + } + } + end + + it "should pass cookie header to server from Hash" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_cookie').get :head => {'cookie' => {'id' => 2}} + + http.errback { failed } + http.callback { + http.response.should == "id=2;" + EventMachine.stop + } + } + end + + context "when talking to a stub HTTP/1.0 server" do + it "should get the body without Content-Length" do + EventMachine.run { + @s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo") + + http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get + http.errback { failed } + http.callback { + http.response.should match(/Foo/) + + @s.stop + EventMachine.stop + } + } + end + + it "should work with \\n instead of \\r\\n" do + EventMachine.run { + @s = StubServer.new("HTTP/1.0 200 OK\nContent-Type: text/plain\nContent-Length: 3\nConnection: close\n\nFoo") + + http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get + http.errback { failed } + http.callback { + http.response_header.status.should == 200 + http.response_header['CONTENT_TYPE'].should == 'text/plain' + http.response.should match(/Foo/) + + @s.stop + EventMachine.stop + } + } + end end end