spec/client_spec.rb in em-http-request-1.0.3 vs spec/client_spec.rb in em-http-request-1.1.0

- old
+ new

@@ -451,10 +451,11 @@ t = Time.now.to_i EventMachine.heartbeat_interval = 0.1 http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/timeout', :inactivity_timeout => 0.1).get http.errback { + http.error.should == Errno::ETIMEDOUT (Time.now.to_i - t).should <= 1 EventMachine.stop } http.callback { failed(http) } } @@ -680,13 +681,10 @@ } } end it "should get the body without Content-Length" do - pending "blocked on new http_parser.rb release" - # https://github.com/igrigorik/em-http-request/issues/168 - EventMachine.run { @s = StubServer.new("HTTP/1.1 200 OK\r\n\r\nFoo") http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get http.errback { failed(http) } @@ -761,26 +759,45 @@ } end it "should reconnect if connection was closed between requests" do EventMachine.run { - conn = EM::HttpRequest.new('http://127.0.0.1:8090/', :inactivity_timeout => 0.5) - req = conn.get :keepalive => true + conn = EM::HttpRequest.new('http://127.0.0.1:8090/') + req = conn.get req.callback do - EM.add_timer(1) do - req = conn.get + conn.close('client closing connection') + EM.next_tick do + req = conn.get :path => "/gzip" req.callback do req.response_header.status.should == 200 + req.response.should match('compressed') EventMachine.stop end end end } end + it "should report error if connection was closed by server on client keepalive requests" do + EventMachine.run { + conn = EM::HttpRequest.new('http://127.0.0.1:8090/') + req = conn.get :keepalive => true + + req.callback do + req = conn.get + + req.callback { failed(http) } + req.errback do + req.error.should match('connection closed by server') + EventMachine.stop + end + end + } + end + it 'should handle malformed Content-Type header repetitions' do EventMachine.run { response =<<-HTTP.gsub(/^ +/, '').strip HTTP/1.0 200 OK Content-Type: text/plain; charset=iso-8859-1 @@ -827,7 +844,45 @@ http.response_header["X_CUSTOM_HEADER"].should == "foo" EventMachine.stop } } + end + + context "User-Agent" do + it 'should default to "EventMachine HttpClient"' do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo-user-agent').get + + http.errback { failed(http) } + http.callback { + http.response.should == '"EventMachine HttpClient"' + EventMachine.stop + } + } + end + + it 'should keep header if given empty string' do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo-user-agent').get(:head => { 'user-agent'=>'' }) + + http.errback { failed(http) } + http.callback { + http.response.should == '""' + EventMachine.stop + } + } + end + + it 'should ommit header if given nil' do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo-user-agent').get(:head => { 'user-agent'=>nil }) + + http.errback { failed(http) } + http.callback { + http.response.should == 'nil' + EventMachine.stop + } + } + end end end