Skip to content

Commit db77ec3

Browse files
committed
Add tests for Rack 3.1 and fix existing tests.
1 parent 5822c33 commit db77ec3

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

fixtures/protocol/rack/an_adapter.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# frozen_string_literal: true
33

44
require "sus"
5+
56
require "rack/lint"
7+
68
require "sus/fixtures/console"
9+
require "protocol/rack/adapter"
710
require "protocol/rack/server_context"
811

912
module Protocol

gems/gems.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
gem "bake-test-external"
1111
gem "sus"
1212
gem "sus-fixtures-async-http"
13+
gem "sus-fixtures-console"
1314
gem "covered"

lib/protocol/rack/adapter/rack3.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def make_environment(request)
5757
# I'm not sure what sane defaults should be here:
5858
CGI::SERVER_NAME => server_name,
5959
}
60-
60+
6161
# SERVER_PORT is optional but must not be set if it is not present.
6262
if server_port
6363
env[CGI::SERVER_PORT] = server_port

lib/protocol/rack/adapter/rack31.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def make_environment(request)
4848
# I'm not sure what sane defaults should be here:
4949
CGI::SERVER_NAME => server_name,
5050
}
51-
51+
5252
# SERVER_PORT is optional but must not be set if it is not present.
5353
if server_port
5454
env[CGI::SERVER_PORT] = server_port

test/protocol/rack/adapter/rack3.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "sus/fixtures/console"
77

88
require "protocol/http/request"
9+
require "protocol/rack/an_adapter"
910
require "protocol/rack/adapter/rack3"
1011

1112
describe Protocol::Rack::Adapter::Rack3 do
@@ -45,17 +46,17 @@
4546
end
4647

4748
with "body that responds to #to_path" do
48-
let(:body) {Array.new}
49-
let(:app) {->(env) {[200, {}, body]}}
49+
let(:fake_file) {Array.new}
50+
let(:app) {->(env) {[200, {}, fake_file]}}
5051

5152
it "should generate file body" do
52-
expect(body).to receive(:to_path).and_return("/dev/null")
53+
expect(fake_file).to receive(:to_path).and_return("/dev/null")
5354

5455
expect(response.body).to be(:kind_of?, Protocol::HTTP::Body::File)
5556
end
5657

5758
with "206 partial response status" do
58-
let(:app) {->(env) {[200, {}, body]}}
59+
let(:app) {->(env) {[200, {}, fake_file]}}
5960

6061
it "should not modify partial responses" do
6162
expect(response.body).to be(:kind_of?, Protocol::Rack::Body::Enumerable)

test/protocol/rack/adapter/rack31.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2022-2024, by Samuel Williams.
5+
6+
require "sus/fixtures/console"
7+
8+
require "protocol/http/request"
9+
require "protocol/rack/an_adapter"
10+
require "protocol/rack/adapter/rack31"
11+
12+
describe Protocol::Rack::Adapter::Rack31 do
13+
include Sus::Fixtures::Console::CapturedLogger
14+
15+
let(:app) {->(env) {[200, {}, []]}}
16+
let(:adapter) {subject.new(app)}
17+
18+
let(:body) {Protocol::HTTP::Body::Buffered.new}
19+
let(:request) {Protocol::HTTP::Request.new("https", "example.com", "GET", "/", "http/1.1", Protocol::HTTP::Headers[{"accept" => "text/html"}], body)}
20+
let(:response) {adapter.call(request)}
21+
22+
it_behaves_like Protocol::Rack::AnAdapter
23+
24+
with "set-cookie headers that has multiple values" do
25+
let(:app) {->(env) {[200, {"set-cookie" => ["a=b", "x=y"]}, []]}}
26+
27+
it "can make a response newline separated headers" do
28+
expect(response.headers["set-cookie"]).to be == ["a=b", "x=y"]
29+
end
30+
end
31+
32+
with "content-length header" do
33+
let(:app) {->(env) {[200, {"content-length" => "10"}, ["1234567890"]]}}
34+
35+
it "removes content-length header" do
36+
expect(response.headers).not.to be(:include?, "content-length")
37+
end
38+
end
39+
40+
with "connection: close header" do
41+
let(:app) {->(env) {[200, {"connection" => "close"}, []]}}
42+
43+
it "removes content-length header" do
44+
expect(response.headers).not.to be(:include?, "connection")
45+
end
46+
end
47+
48+
with "body that responds to #to_path" do
49+
let(:fake_file) {Array.new}
50+
let(:app) {->(env) {[200, {}, fake_file]}}
51+
52+
it "should generate file body" do
53+
expect(fake_file).to receive(:to_path).and_return("/dev/null")
54+
55+
expect(response.body).to be(:kind_of?, Protocol::HTTP::Body::File)
56+
end
57+
58+
with "206 partial response status" do
59+
let(:app) {->(env) {[200, {}, fake_file]}}
60+
61+
it "should not modify partial responses" do
62+
expect(response.body).to be(:kind_of?, Protocol::Rack::Body::Enumerable)
63+
end
64+
end
65+
end
66+
67+
with "a request that has response finished callbacks" do
68+
let(:callback) {->(env, status, headers, error){}}
69+
let(:app) {->(env) {env["rack.response_finished"] << callback; [200, {}, ["Hello World!"]]}}
70+
71+
it "should call the callbacks" do
72+
expect(callback).to receive(:call)
73+
74+
expect(response).to be(:success?)
75+
expect(response.read).to be == "Hello World!"
76+
end
77+
end
78+
end

0 commit comments

Comments
 (0)