spec/request_spec.rb in em-http-request-0.2.10 vs spec/request_spec.rb in em-http-request-0.2.11

- old
+ new

@@ -27,21 +27,32 @@ http = EventMachine::HttpRequest.new('http://somethinglocal/').get :timeout => 1 http.callback { failed } http.errback { http.response_header.status.should == 0 http.error.should match(/unable to resolve server address/) + http.uri.to_s.should match('http://somethinglocal:80/') EventMachine.stop } } end - it "should fail GET on missing path" do + it "should raise error on invalid URL" do EventMachine.run { lambda { - EventMachine::HttpRequest.new('http://www.google.com').get - }.should raise_error(ArgumentError) + EventMachine::HttpRequest.new('random?text').get + }.should raise_error + EM.stop + } + end + + it "should succeed GET on missing path" do + EventMachine.run { + lambda { + EventMachine::HttpRequest.new('http://127.0.0.1:8080').get + }.should_not raise_error(ArgumentError) + EventMachine.stop } end it "should perform successfull GET" do @@ -55,10 +66,23 @@ EventMachine.stop } } end + it "should accept optional host override" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://google.com:8080/').get :host => '127.0.0.1' + + http.errback { failed } + http.callback { + http.response_header.status.should == 200 + http.response.should match(/Hello/) + EventMachine.stop + } + } + end + it "should perform successfull GET with a URI passed as argument" do EventMachine.run { uri = URI.parse('http://127.0.0.1:8080/') http = EventMachine::HttpRequest.new(uri).get @@ -270,10 +294,24 @@ EventMachine.stop } } end + it "should return ETag and Last-Modified headers" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/echo_query').get + + http.errback { failed } + http.callback { + http.response_header.status.should == 200 + http.response_header.etag.should match('abcdefg') + http.response_header.last_modified.should match('Fri, 13 Aug 2010 17:31:21 GMT') + EventMachine.stop + } + } + end + it "should detect deflate encoding" do EventMachine.run { http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate"} @@ -316,62 +354,99 @@ } 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/' + context "redirect" do + 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 + EM.stop + } } - } - end + 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 + 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 + EM.stop + } } - } - end + 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 + 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 + EM.stop + } } - } - end + end - it "should not invoke redirect logic on failed connections" do - EventMachine.run { - http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get :timeout => 0.1, :redirects => 5 - http.callback { failed } - http.errback { - http.redirects.should == 0 - EM.stop + it "should not invoke redirect logic on failed connections" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get :timeout => 0.1, :redirects => 5 + http.callback { failed } + http.errback { + http.redirects.should == 0 + EM.stop + } } - } + end + + it "should normalize redirect urls" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/redirect/bad').get :redirects => 1 + http.errback { failed } + http.callback { + http.last_effective_url.to_s.should match('http://127.0.0.1:8080/') + http.response.should match('Hello, World!') + EM.stop + } + } + end + + it "should fail gracefully on a missing host in absolute Location header" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/redirect/nohost').get :redirects => 1 + http.callback { failed } + http.errback { + http.error.should == 'Location header format error' + EM.stop + } + } + end + + it "should fail gracefully on an invalid host in Location header" do + pending "validate tld's?" + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/redirect/badhost').get :redirects => 1 + http.callback { failed } + http.errback { + http.error.should == 'Location header format error' + EM.stop + } + } + end end it "should optionally pass the response body progressively" do EventMachine.run { body = '' @@ -591,38 +666,96 @@ } EventMachine::MockHttpRequest.count('http://www.google.ca:80/', :get, {}).should == 1 end - context "connections via proxy" do + context "connections via" do + context "direct proxy" do + it "should default to skip CONNECT" do + EventMachine.run { - it "should work with proxy servers" do - EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/?q=test').get :proxy => { + :host => '127.0.0.1', :port => 8083 + } - http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :proxy => {:host => '127.0.0.1', :port => 8082} + http.errback { p http.inspect; failed } + http.callback { + http.response_header.status.should == 200 + http.response.should match('test') + EventMachine.stop + } + } + end - http.errback { p http.inspect; failed } - http.callback { - http.response_header.status.should == 200 - http.response.should == 'Hello, World!' - EventMachine.stop + it "should send absolute URIs to the proxy server" do + EventMachine.run { + + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/?q=test').get :proxy => { + :host => '127.0.0.1', :port => 8083 + } + + http.errback { p http.inspect; failed } + http.callback { + http.response_header.status.should == 200 + # The test proxy server gives the requested uri back in this header + http.response_header['X_THE_REQUESTED_URI'].should == 'http://127.0.0.1:8080/?q=test' + http.response_header['X_THE_REQUESTED_URI'].should_not == '/?q=test' + http.response.should match('test') + EventMachine.stop + } } - } + end + + it "should include query parameters specified in the options" do + EventMachine.run { + + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get( + :proxy => { :host => '127.0.0.1', :port => 8083 }, + :query => { 'q' => 'test' } + ) + + http.errback { p http.inspect; failed } + http.callback { + http.response_header.status.should == 200 + http.response.should match('test') + EventMachine.stop + } + } + end end - it "should proxy POST data" do - EventMachine.run { + context "CONNECT proxy" do + it "should work with CONNECT proxy servers" do + EventMachine.run { - http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post({ - :body => "data", :proxy => {:host => '127.0.0.1', :port => 8082}}) + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({ + :proxy => {:host => '127.0.0.1', :port => 8082, :use_connect => true} + }) - http.errback { failed } - http.callback { - http.response_header.status.should == 200 - http.response.should match(/data/) - EventMachine.stop + http.errback { p http.inspect; failed } + http.callback { + http.response_header.status.should == 200 + http.response.should == 'Hello, World!' + EventMachine.stop + } } - } + end + + it "should proxy POST data" do + EventMachine.run { + + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').post({ + :body => "data", :proxy => {:host => '127.0.0.1', :port => 8082, :use_connect => true} + }) + + http.errback { failed } + http.callback { + http.response_header.status.should == 200 + http.response.should match(/data/) + EventMachine.stop + } + } + end end end context "websocket connection" do # Spec: http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-55