Permalink
Browse files

Add unit test for chunked encoding response functionality

Change-Id: I3b4142e670fe1e73ddc845dbfc77e4ac93a901c3
  • Loading branch information...
1 parent 2d28632 commit b24e1a44329c225454db5a9991d89f9b04c02a75 @anfernee anfernee committed Mar 31, 2012
Showing with 81 additions and 0 deletions.
  1. +16 −0 router/spec/integration/router_spec.rb
  2. +65 −0 router/spec/integration/spec_helper.rb
@@ -270,5 +270,21 @@
app.stop
end
+ it 'should support chunked encoding response' do
+ app = TestApp.new('chunk.vcap.me')
+ dea = DummyDea.new(@nats_server.uri, '1234')
+ dea.register_app(app, {"component" => "trace", "runtime" => "ruby"})
+
+ # send chunked-encoded message
+ messages = ["hello", ", world", "!"]
+ rmsg, rbody = app.get_chunked_response("127.0.0.1", RouterServer.port, messages)
+
+ rmsg.status_code.should == 200
+ rbody.should == messages.join
+
+ dea.unregister_app(app)
+
+ app.stop
+ end
end
@@ -16,6 +16,11 @@ def simple_http_request(host, path, http_version='1.1')
"GET #{path} HTTP/#{http_version}\r\nUser-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15\r\nHost: #{host}\r\nAccept: */*\r\nContent-Length: 11\r\n\r\nhello world"
end
+def simple_http_response(body, http_version='1.1')
+ "HTTP/#{http_version} 200 OK\r\nContent-Type: text/html;charset=utf-8\r\n" +
+ "Content-Length: #{body.length}\r\nConnection: keep-alive\r\n\r\n#{body}"
+end
+
def parse_http_msg_from_socket(socket)
parser = Http::Parser.new
complete = false
@@ -143,6 +148,26 @@ def simple_sticky_request(host, path, cookie, http_version='1.1')
"GET #{path} HTTP/#{http_version}\r\nHost: #{host}\r\nConnection: keep-alive\r\nAccept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10\r\nAccept-Encoding: gzip,deflate,sdch\r\nAccept-Language: en-US,en;q=0.8\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\nCookie: #{cookie}\r\n\r\n"
end
+def chunked_encoded_request(host, path, messages, http_version='1.1')
+ chunks = []
+ chunks << "POST #{path} HTTP/#{http_version}\r\nHost: #{host}\r\nTransfer-Encoding: chunked\r\n\r\n"
+ messages.each do |m|
+ chunks << "#{m.size.to_s(16)}\r\n#{m}\r\n"
+ end
+ chunks << "0\r\n\r\n"
+ chunks
+end
+
+def chunked_encoded_response(messages, http_version='1.1')
+ chunks = []
+ chunks << "HTTP/#{http_version} 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
+ messages.each do |m|
+ chunks << "#{m.size.to_s(16)}\r\n#{m}\r\n"
+ end
+ chunks << "0\r\n\r\n"
+ chunks
+end
+
def healthz_request
"GET / HTTP/1.0\r\nUser-Agent: HTTP-Monitor/1.1\r\n\r\n"
end
@@ -277,6 +302,46 @@ def get_trace_header(router_host, router_port, trace_key)
end
end
+ def send_chunked_request(router_host, router_port, messages)
+ chunk_header, chunks = chunked_encoded_request(@uris[0], '/', messages)
+ TCPSocket.open(router_host, router_port) do |rs|
+ rs.send(chunk_header, 0)
+ socket = IO.select([rs], nil, nil, 1)
+ if socket
+ rmsg, rbody = parse_http_msg_from_socket(rs)
+ return [ rmsg, rbody ]
+ end
+
+ chunks.each { |chunk| rs.send(chunk, 0) }
+ IO.select([@socket], nil, nil, 2) # 2 secs timeout
+ ss = @socket.accept_nonblock
+ msg, body = parse_http_msg_from_socket(ss)
+
+ # Send a response back..
+ ss.send(simple_http_response(body), 0)
+ rmsg, rbody = parse_http_msg_from_socket(rs)
+ ss.close
+ return [ rmsg, rbody ]
+ end
+ end
+
+ def get_chunked_response(router_host, router_port, messages)
+ req = simple_http_request(@uris[0], '/')
+ TCPSocket.open(router_host, router_port) do |rs|
+ rs.send(req, 0)
+ IO.select([@socket], nil, nil, 2) # 2 secs timeout
+ ss = @socket.accept_nonblock
+ msg, body = parse_http_msg_from_socket(ss)
+
+ # Send a response back..
+ chunks = chunked_encoded_response(messages)
+ chunks.each { |chunk| ss.send(chunk, 0) }
+ rmsg, rbody = parse_http_msg_from_socket(rs)
+ ss.close
+ return [ rmsg, rbody ]
+ end
+ end
+
private
def verify_path_registered(host, path, router_host, router_port)

0 comments on commit b24e1a4

Please sign in to comment.