spec/reel/connection_spec.rb in reel-0.4.0.pre7 vs spec/reel/connection_spec.rb in reel-0.4.0
- old
+ new
@@ -2,11 +2,12 @@
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, connection|
+ 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"
@@ -20,11 +21,12 @@
request['Accept-Charset'].should 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, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
body = "Hello, world!"
example_request = ExampleRequest.new
example_request.body = body
client << example_request.to_s
@@ -36,11 +38,12 @@
request.body.to_s.should eq example_request.body
end
end
it "reads requests with large bodies" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
client << ExampleRequest.new.to_s
request = connection.request
fixture_text = File.read(fixture_path)
File.open(fixture_path) do |file|
@@ -52,11 +55,12 @@
response[(response.length - fixture_text.length)..-1].should eq fixture_text
end
end
it "enumerates requests with #each_request" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
client << ExampleRequest.new.to_s
request_count = 0
connection.each_request do |request|
request_count += 1
@@ -68,11 +72,12 @@
request_count.should eq 1
end
end
it "streams responses when transfer-encoding is chunked" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
client << ExampleRequest.new.to_s
request = connection.request
# Sending transfer_encoding chunked without a body enables streaming mode
request.respond :ok, :transfer_encoding => :chunked
@@ -97,11 +102,12 @@
response[(response.length - fixture.length)..-1].should eq fixture
end
end
it "reset the request after a response is sent" do
- with_socket_pair do |client, connection|
+ 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_false
@@ -110,29 +116,29 @@
connection.request.should be_false
end
end
it "raises an error trying to read two pipelines without responding first" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
+
2.times do
client << ExampleRequest.new.to_s
end
- lambda{
- 2.times do
- request = connection.request
- end
- }.should raise_error(Reel::Connection::StateError)
+ expect do
+ 2.times { request = connection.request }
+ end.to raise_error(Reel::Connection::StateError)
end
end
it "reads pipelined requests without bodies" do
- with_socket_pair do |client, connection|
- 3.times do
- client << ExampleRequest.new.to_s
- end
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
+ 3.times { client << ExampleRequest.new.to_s }
+
3.times do
request = connection.request
request.url.should eq "/"
request.version.should eq "1.1"
@@ -148,11 +154,13 @@
end
end
end
it "reads pipelined requests with bodies" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
+
3.times do |i|
body = "Hello, world number #{i}!"
example_request = ExampleRequest.new
example_request.body = body
@@ -172,11 +180,13 @@
end
end
end
it "reads pipelined requests with streamed bodies" do
- with_socket_pair(4) do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer, 4)
+
3.times do |i|
body = "Hello, world number #{i}!"
example_request = ExampleRequest.new
example_request.body = body
@@ -204,12 +214,14 @@
end
# This test will deadlock rspec waiting unless
# connection.request works properly
it "does not block waiting for body to read before handling request" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
+
content = "Hi guys! Sorry I'm late to the party."
example_request['Content-Length'] = content.length
client << example_request.to_s
request = connection.request
@@ -218,12 +230,14 @@
request.body.to_s.should == content
end
end
it "blocks on read until written" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
+
content = "Hi guys! Sorry I'm late to the party."
example_request['Content-Length'] = content.length
client << example_request.to_s
request = connection.request
@@ -242,12 +256,14 @@
read_body.should == content[0..7]
end
end
it "streams body properly with #read and buffered body" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
+
content = "I'm data you can stream!"
example_request['Content-Length'] = content.length
client << example_request.to_s
request = connection.request
@@ -264,12 +280,14 @@
end
end
context "#readpartial" do
it "streams request bodies" do
- with_socket_pair(8) do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer, 8)
example_request = ExampleRequest.new
+
content = "I'm data you can stream!"
example_request['Content-Length'] = content.length
client << example_request.to_s
request = connection.request
@@ -286,12 +304,14 @@
end
end
context "#each" do
it "streams request bodies" do
- with_socket_pair(8) do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
+
content = "I'm data you can stream!"
example_request['Content-Length'] = content.length
client << example_request.to_s
request = connection.request
@@ -307,63 +327,66 @@
end
end
describe "IO#read duck typing" do
it "raises an exception if length is a negative value" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
client << example_request.to_s
request = connection.request
lambda { request.read(-1) }.should raise_error(ArgumentError)
end
end
it "returns an empty string if the length is zero" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
client << example_request.to_s
request = connection.request
request.read(0).should be_empty
end
end
it "reads to EOF if length is nil, even small buffer" do
- with_socket_pair(4) do |client, connection|
- body = "Hello, world!"
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer, 4)
example_request = ExampleRequest.new
- example_request.body = body
+ example_request.body = "Hello, world!"
connection.buffer_size.should == 4
client << example_request.to_s
request = connection.request
request.read.should eq "Hello, world!"
end
end
it "reads to EOF if length is nil" do
- with_socket_pair do |client, connection|
- body = "Hello, world!"
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
- example_request.body = body
+ example_request.body = "Hello, world!"
+
client << example_request.to_s
request = connection.request
request.read.should eq "Hello, world!"
end
end
it "uses the optional buffer to recieve data" do
- with_socket_pair do |client, connection|
- body = "Hello, world!"
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
- example_request.body = body
+ example_request.body = "Hello, world!"
client << example_request.to_s
request = connection.request
buffer = ''
@@ -371,42 +394,42 @@
buffer.should 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, connection|
- body = "Hello, world!"
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
- example_request.body = body
+ example_request.body = "Hello, world!"
client << example_request.to_s
request = connection.request
request.read(1024).should eq "Hello, world!"
end
end
it "returns nil at EOF if a length is passed" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
client << example_request.to_s
request = connection.request
request.read(1024).should be_nil
end
end
it "returns an empty string at EOF if length is nil" do
- with_socket_pair do |client, connection|
+ with_socket_pair do |client, peer|
+ connection = Reel::Connection.new(peer)
example_request = ExampleRequest.new
client << example_request.to_s
request = connection.request
request.read.should be_empty
end
end
-
end
-
end