spec/reel/connection_spec.rb in reel-0.5.0 vs spec/reel/connection_spec.rb in reel-0.6.0.pre1

- old
+ new

@@ -1,26 +1,26 @@ require 'spec_helper' -describe Reel::Connection do +RSpec.describe Reel::Connection do let(:fixture_path) { File.expand_path("../../fixtures/example.txt", __FILE__) } it "reads requests without bodies" do with_socket_pair do |client, peer| connection = Reel::Connection.new(peer) client << ExampleRequest.new.to_s request = connection.request - request.url.should eq "/" - request.version.should eq "1.1" + expect(request.url).to eq "/" + expect(request.version).to eq "1.1" - request['Host'].should eq "www.example.com" - request['Connection'].should eq "keep-alive" - request['User-Agent'].should eq "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S" - request['Accept'].should eq "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" - request['Accept-Encoding'].should eq "gzip,deflate,sdch" - request['Accept-Language'].should eq "en-US,en;q=0.8" - request['Accept-Charset'].should eq "ISO-8859-1,utf-8;q=0.7,*;q=0.3" + expect(request['Host']).to eq "www.example.com" + expect(request['Connection']).to eq "keep-alive" + expect(request['User-Agent']).to eq "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S" + expect(request['Accept']).to eq "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + expect(request['Accept-Encoding']).to eq "gzip,deflate,sdch" + expect(request['Accept-Language']).to eq "en-US,en;q=0.8" + expect(request['Accept-Charset']).to eq "ISO-8859-1,utf-8;q=0.7,*;q=0.3" end end it "reads requests with bodies" do with_socket_pair do |client, peer| @@ -30,14 +30,14 @@ example_request.body = body client << example_request.to_s request = connection.request - request.url.should eq "/" - request.version.should eq "1.1" - request['Content-Length'].should eq body.length.to_s - request.body.to_s.should eq example_request.body + expect(request.url).to eq "/" + expect(request.version).to eq "1.1" + expect(request['Content-Length']).to eq body.length.to_s + expect(request.body.to_s).to eq example_request.body end end it "reads requests with large bodies" do with_socket_pair do |client, peer| @@ -50,11 +50,11 @@ connection.respond :ok, file connection.close end response = client.read(4096) - response[(response.length - fixture_text.length)..-1].should eq fixture_text + expect(response[(response.length - fixture_text.length)..-1]).to eq fixture_text end end it "enumerates requests with #each_request" do with_socket_pair do |client, peer| @@ -62,16 +62,16 @@ client << ExampleRequest.new.to_s request_count = 0 connection.each_request do |request| request_count += 1 - request.url.should eq "/" + expect(request.url).to eq "/" request.respond :ok client.close end - request_count.should eq 1 + expect(request_count).to eq 1 end end context "streams responses when transfer-encoding is chunked" do def test_chunked_response(request, client) @@ -93,11 +93,11 @@ rescue EOFError end crlf = "\r\n" fixture = "5#{crlf}Hello#{crlf}5#{crlf}World#{crlf}0#{crlf*2}" - response[(response.length - fixture.length)..-1].should eq fixture + expect(response[(response.length - fixture.length)..-1]).to eq fixture end it "with keep-alive" do with_socket_pair do |client, peer| connection = Reel::Connection.new(peer) @@ -146,18 +146,37 @@ with_socket_pair do |client, peer| connection = Reel::Connection.new(peer) example_request = ExampleRequest.new(:get, "/", "1.1", {'Connection' => 'close'}) client << example_request - connection.request.should_not be_nil + expect(connection.request).not_to be_nil connection.respond :ok, "Response sent" - connection.request.should be_nil + expect(connection.request).to be_nil end end + it "resets if client dropped connection" do + with_socket_pair do |client, peer| + connection = Reel::Connection.new(peer) + example_request = ExampleRequest.new + client << example_request + + expect(connection.request).not_to be_nil + + client.close # client drops connection + # now send more than the send buffer can hold, triggering a + # error (ECONNRESET or EPIPE) + connection.respond :ok, ("Some Big Response sent"*100000) + + # connection should be at end + expect(connection.request).to be_nil + end + end + + it "raises an error trying to read two pipelines without responding first" do with_socket_pair do |client, peer| connection = Reel::Connection.new(peer) 2.times do @@ -177,20 +196,20 @@ 3.times { client << ExampleRequest.new.to_s } 3.times do request = connection.request - request.url.should eq "/" - request.version.should eq "1.1" + expect(request.url).to eq "/" + expect(request.version).to eq "1.1" - request['Host'].should eq "www.example.com" - request['Connection'].should eq "keep-alive" - request['User-Agent'].should eq "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S" - request['Accept'].should eq "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" - request['Accept-Encoding'].should eq "gzip,deflate,sdch" - request['Accept-Language'].should eq "en-US,en;q=0.8" - request['Accept-Charset'].should eq "ISO-8859-1,utf-8;q=0.7,*;q=0.3" + expect(request['Host']).to eq "www.example.com" + expect(request['Connection']).to eq "keep-alive" + expect(request['User-Agent']).to eq "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S" + expect(request['Accept']).to eq "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" + expect(request['Accept-Encoding']).to eq "gzip,deflate,sdch" + expect(request['Accept-Language']).to eq "en-US,en;q=0.8" + expect(request['Accept-Charset']).to eq "ISO-8859-1,utf-8;q=0.7,*;q=0.3" connection.respond :ok, {}, "" end end end @@ -208,14 +227,14 @@ 3.times do |i| request = connection.request expected_body = "Hello, world number #{i}!" - request.url.should eq "/" - request.version.should eq "1.1" - request['Content-Length'].should eq expected_body.length.to_s - request.body.to_s.should eq expected_body + expect(request.url).to eq "/" + expect(request.version).to eq "1.1" + expect(request['Content-Length']).to eq expected_body.length.to_s + expect(request.body.to_s).to eq expected_body connection.respond :ok, {}, "" end end end @@ -234,20 +253,20 @@ 3.times do |i| request = connection.request expected_body = "Hello, world number #{i}!" - request.url.should eq "/" - request.version.should eq "1.1" - request['Content-Length'].should eq expected_body.length.to_s - request.should_not be_finished_reading + expect(request.url).to eq "/" + expect(request.version).to eq "1.1" + expect(request['Content-Length']).to eq expected_body.length.to_s + expect(request).not_to be_finished_reading new_content = "" while chunk = request.body.readpartial(1) new_content << chunk end - new_content.should == expected_body - request.should be_finished_reading + expect(new_content).to eq(expected_body) + expect(request).to be_finished_reading connection.respond :ok, {}, "" end end end @@ -262,13 +281,13 @@ content = "Hi guys! Sorry I'm late to the party." example_request['Content-Length'] = content.length client << example_request.to_s request = connection.request - request.should be_a(Reel::Request) + expect(request).to be_a(Reel::Request) client << content - request.body.to_s.should == content + expect(request.body.to_s).to eq(content) end end it "blocks on read until written" do with_socket_pair do |client, peer| @@ -278,23 +297,23 @@ content = "Hi guys! Sorry I'm late to the party." example_request['Content-Length'] = content.length client << example_request.to_s request = connection.request - timers = Timers.new + timers = Timers::Group.new timers.after(0.2){ client << content } read_body = "" timers.after(0.1){ timers.wait # continue timers, the next bit will block waiting for content read_body = request.read(8) } timers.wait - request.should be_a(Reel::Request) - read_body.should == content[0..7] + expect(request).to be_a(Reel::Request) + expect(read_body).to eq(content[0..7]) end end it "streams body properly with #read and buffered body" do with_socket_pair do |client, peer| @@ -304,20 +323,20 @@ content = "I'm data you can stream!" example_request['Content-Length'] = content.length client << example_request.to_s request = connection.request - request.should be_a(Reel::Request) - request.should_not be_finished_reading + expect(request).to be_a(Reel::Request) + expect(request).not_to be_finished_reading client << content rebuilt = [] connection.readpartial(64) # Buffer some body while chunk = request.read(8) rebuilt << chunk end - request.should be_finished_reading - rebuilt.should == ["I'm data", " you can", " stream!"] + expect(request).to be_finished_reading + expect(rebuilt).to eq(["I'm data", " you can", " stream!"]) end end context "#readpartial" do it "streams request bodies" do @@ -328,19 +347,19 @@ content = "I'm data you can stream!" example_request['Content-Length'] = content.length client << example_request.to_s request = connection.request - request.should be_a(Reel::Request) - request.should_not be_finished_reading + expect(request).to be_a(Reel::Request) + expect(request).not_to be_finished_reading client << content rebuilt = [] while chunk = request.body.readpartial(8) rebuilt << chunk end - request.should be_finished_reading - rebuilt.should == ["I'm data", " you can", " stream!"] + expect(request).to be_finished_reading + expect(rebuilt).to eq(["I'm data", " you can", " stream!"]) end end end context "#respond" do @@ -362,18 +381,18 @@ content = "I'm data you can stream!" example_request['Content-Length'] = content.length client << example_request.to_s request = connection.request - request.should be_a(Reel::Request) - request.should_not be_finished_reading + expect(request).to be_a(Reel::Request) + expect(request).not_to be_finished_reading client << content data = "" request.body.each { |chunk| data << chunk } - request.should be_finished_reading - data.should == "I'm data you can stream!" + expect(request).to be_finished_reading + expect(data).to eq("I'm data you can stream!") end end end describe "IO#read duck typing" do @@ -383,11 +402,11 @@ example_request = ExampleRequest.new client << example_request.to_s request = connection.request - lambda { request.read(-1) }.should raise_error(ArgumentError) + expect { request.read(-1) }.to raise_error(ArgumentError) end end it "returns an empty string if the length is zero" do with_socket_pair do |client, peer| @@ -395,25 +414,25 @@ example_request = ExampleRequest.new client << example_request.to_s request = connection.request - request.read(0).should be_empty + expect(request.read(0)).to be_empty end end it "reads to EOF if length is nil, even small buffer" do with_socket_pair do |client, peer| connection = Reel::Connection.new(peer, 4) example_request = ExampleRequest.new example_request.body = "Hello, world!" - connection.buffer_size.should == 4 + expect(connection.buffer_size).to eq(4) client << example_request.to_s request = connection.request - request.read.should eq "Hello, world!" + expect(request.read).to eq "Hello, world!" end end it "reads to EOF if length is nil" do with_socket_pair do |client, peer| @@ -423,11 +442,11 @@ client << example_request.to_s request = connection.request - request.read.should eq "Hello, world!" + expect(request.read).to eq "Hello, world!" end end it "uses the optional buffer to recieve data" do with_socket_pair do |client, peer| @@ -437,12 +456,12 @@ client << example_request.to_s request = connection.request buffer = '' - request.read(nil, buffer).should eq "Hello, world!" - buffer.should eq "Hello, world!" + expect(request.read(nil, buffer)).to eq "Hello, world!" + expect(buffer).to eq "Hello, world!" end end it "returns with the content it could read when the length longer than EOF" do with_socket_pair do |client, peer| @@ -451,11 +470,11 @@ example_request.body = "Hello, world!" client << example_request.to_s request = connection.request - request.read(1024).should eq "Hello, world!" + expect(request.read(1024)).to eq "Hello, world!" end end it "returns nil at EOF if a length is passed" do with_socket_pair do |client, peer| @@ -463,11 +482,11 @@ example_request = ExampleRequest.new client << example_request.to_s request = connection.request - request.read(1024).should be_nil + expect(request.read(1024)).to be_nil end end it "returns an empty string at EOF if length is nil" do with_socket_pair do |client, peer| @@ -475,10 +494,10 @@ example_request = ExampleRequest.new client << example_request.to_s request = connection.request - request.read.should be_empty + expect(request.read).to be_empty end end end end