spec/reel/connection_spec.rb in reel-0.0.2 vs spec/reel/connection_spec.rb in reel-0.1.0

- old
+ new

@@ -50,20 +50,51 @@ response = client.readpartial(4096) response[(response.length - fixture_text.length)..-1].should eq fixture_text end end + it "streams responses when transfer-encoding is chunked" do + with_socket_pair do |client, connection| + client << ExampleRequest.new.to_s + request = connection.read_request + + # Sending transfer_encoding chunked without a body enables streaming mode + connection.respond :ok, :transfer_encoding => :chunked + + # This will send individual chunks + connection << "Hello" + connection << "World" + connection.finish_response # Write trailer and reset connection to header mode + connection.close + + response = "" + + begin + while chunk = client.readpartial(4096) + response << chunk + end + rescue EOFError + end + + crlf = "\r\n" + fixture = "5#{crlf}Hello5#{crlf}World0#{crlf*2}" + response[(response.length - fixture.length)..-1].should eq fixture + end + end + def with_socket_pair host = '127.0.0.1' port = 10103 server = TCPServer.new(host, port) client = TCPSocket.new(host, port) peer = server.accept - yield client, Reel::Connection.new(peer) - - server.close - client.close - peer.close + begin + yield client, Reel::Connection.new(peer) + ensure + server.close rescue nil + client.close rescue nil + peer.close rescue nil + end end end