From f36f4cf2e58d3314addbbc7a552a24f8c7cc1043 Mon Sep 17 00:00:00 2001 From: Aayush Ranaut Date: Sat, 18 Jun 2016 20:12:27 +0530 Subject: [PATCH 1/6] Added tests for Reel::Request --- spec/reel/connection_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/reel/connection_spec.rb b/spec/reel/connection_spec.rb index da24627..61920a1 100644 --- a/spec/reel/connection_spec.rb +++ b/spec/reel/connection_spec.rb @@ -338,6 +338,29 @@ def test_chunked_response(request, client) end end + it "returns friendlier inspect output" do + with_socket_pair do |client, peer| + connection = Reel::Connection.new(peer) + client << ExampleRequest.new.to_s + request = connection.request + + expect(request.inspect).to eq '#"www.example.com", "Connection"=>"keep-alive", "User-Agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S", "Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding"=>"gzip,deflate,sdch", "Accept-Language"=>"en-US,en;q=0.8", "Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.3"}>' + end + end + + it "raises an exception if not in chunked body mode" do + with_socket_pair do |client, peer| + connection = Reel::Connection.new(peer) + client << ExampleRequest.new.tap{ |r| + r['Connection'] = 'keep-alive' + }.to_s + request = connection.request + request.respond :ok, :transfer_encoding => '' + + expect {request << "Hello"}.to raise_error(Reel::StateError) + end + end + context "#readpartial" do it "streams request bodies" do with_socket_pair do |client, peer| From 09afb91c1242bb1b7d74615bbb721c00242bfce0 Mon Sep 17 00:00:00 2001 From: Aayush Ranaut Date: Sat, 18 Jun 2016 20:54:42 +0530 Subject: [PATCH 2/6] Added a test to make sure that the connection detaches successfully --- spec/reel/connection_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/reel/connection_spec.rb b/spec/reel/connection_spec.rb index 61920a1..b70fbc5 100644 --- a/spec/reel/connection_spec.rb +++ b/spec/reel/connection_spec.rb @@ -338,6 +338,17 @@ def test_chunked_response(request, client) end end + it "detaches a connection properly" do + with_socket_pair do |client, peer| + connection = Reel::Connection.new(peer) + client << ExampleRequest.new.to_s + c = connection.detach + + expect(c).to be_a Reel::Connection + expect(connection.attached?).to eq(false) + end + end + it "returns friendlier inspect output" do with_socket_pair do |client, peer| connection = Reel::Connection.new(peer) From 1dfca6bcdd7ad9d986bc665b1648bb2eb87a1873 Mon Sep 17 00:00:00 2001 From: Aayush Ranaut Date: Sun, 19 Jun 2016 20:25:49 +0530 Subject: [PATCH 3/6] Refactored a test --- spec/reel/connection_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/reel/connection_spec.rb b/spec/reel/connection_spec.rb index b70fbc5..ff063a6 100644 --- a/spec/reel/connection_spec.rb +++ b/spec/reel/connection_spec.rb @@ -342,9 +342,8 @@ def test_chunked_response(request, client) with_socket_pair do |client, peer| connection = Reel::Connection.new(peer) client << ExampleRequest.new.to_s - c = connection.detach - expect(c).to be_a Reel::Connection + expect(connection.detach).to be_a Reel::Connection expect(connection.attached?).to eq(false) end end From ff9047a52c817fae4b2f1628d43530ea9d5e7606 Mon Sep 17 00:00:00 2001 From: Aayush Ranaut Date: Wed, 3 Aug 2016 20:58:35 +0530 Subject: [PATCH 4/6] Spec to write array messages --- spec/reel/websocket_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/reel/websocket_spec.rb b/spec/reel/websocket_spec.rb index e62c6f3..c5ec541 100644 --- a/spec/reel/websocket_spec.rb +++ b/spec/reel/websocket_spec.rb @@ -6,6 +6,7 @@ let(:example_message) { "Hello, World!" } let(:another_message) { "What's going on?" } + let(:example_array_message) { Array.new(2) { |e| e = e * 2} } it "performs websocket handshakes" do with_socket_pair do |client, peer| @@ -122,6 +123,18 @@ def initialize(websocket) end end + it "writes array messages" do + with_websocket_pair do |client, websocket| + websocket.write example_array_message + websocket.write another_message + + parser = WebSocket::Parser.new + + parser.append client.readpartial(4096) until first_message = parser.next_message + expect(first_message).to eq(example_array_message.to) + end + end + it "closes" do with_websocket_pair do |_, websocket| expect(websocket).not_to be_closed From bd7f592c4f54a8269b96aaaa031ffe737af2c511 Mon Sep 17 00:00:00 2001 From: Aayush Ranaut Date: Thu, 4 Aug 2016 12:36:37 +0530 Subject: [PATCH 5/6] Added test for array and non-string websocket messages --- spec/reel/websocket_spec.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/spec/reel/websocket_spec.rb b/spec/reel/websocket_spec.rb index c5ec541..64cc504 100644 --- a/spec/reel/websocket_spec.rb +++ b/spec/reel/websocket_spec.rb @@ -6,7 +6,6 @@ let(:example_message) { "Hello, World!" } let(:another_message) { "What's going on?" } - let(:example_array_message) { Array.new(2) { |e| e = e * 2} } it "performs websocket handshakes" do with_socket_pair do |client, peer| @@ -125,16 +124,21 @@ def initialize(websocket) it "writes array messages" do with_websocket_pair do |client, websocket| - websocket.write example_array_message - websocket.write another_message + websocket.write Array.new(2) { |e| e = e * 2} parser = WebSocket::Parser.new parser.append client.readpartial(4096) until first_message = parser.next_message - expect(first_message).to eq(example_array_message.to) + expect(first_message).to eq("\x00\x02") end end + it "it raises exception when trying to write besides string or array messages" do + with_websocket_pair do |client, websocket| + expect { websocket.write 1 }.to raise_exception('Can only send byte array or string over driver.') + end + end + it "closes" do with_websocket_pair do |_, websocket| expect(websocket).not_to be_closed From 9a8ed5fb91935ab8c90c2f1f47b9194d30057ac9 Mon Sep 17 00:00:00 2001 From: Aayush Ranaut Date: Tue, 16 Aug 2016 14:47:53 +0530 Subject: [PATCH 6/6] Extracted the example request string --- spec/reel/connection_spec.rb | 5 +++-- spec/support/example_request.rb | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/spec/reel/connection_spec.rb b/spec/reel/connection_spec.rb index ff063a6..3a54559 100644 --- a/spec/reel/connection_spec.rb +++ b/spec/reel/connection_spec.rb @@ -351,10 +351,11 @@ def test_chunked_response(request, client) it "returns friendlier inspect output" do with_socket_pair do |client, peer| connection = Reel::Connection.new(peer) - client << ExampleRequest.new.to_s + example_request = ExampleRequest.new + client << example_request.to_s request = connection.request - expect(request.inspect).to eq '#"www.example.com", "Connection"=>"keep-alive", "User-Agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S", "Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding"=>"gzip,deflate,sdch", "Accept-Language"=>"en-US,en;q=0.8", "Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.3"}>' + expect(request.inspect).to eq example_request.inspect_method end end diff --git a/spec/support/example_request.rb b/spec/support/example_request.rb index 4a8d50d..09582ed 100644 --- a/spec/support/example_request.rb +++ b/spec/support/example_request.rb @@ -31,4 +31,8 @@ def to_s @headers.map { |k, v| "#{k}: #{v}" }.join("\r\n") << "\r\n\r\n" << (@body ? @body : '') end + + def inspect_method + "#" + end end