Skip to content

Commit

Permalink
Add better tests for HTTP_UPGRADE and rack.protocol. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Dec 9, 2023
1 parent b47dae8 commit 5cbff20
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions test/protocol/rack/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@
let(:app) {proc{|env| [200, {}, []]}}
let(:adapter) {Protocol::Rack::Adapter.new(app)}

let(:headers) {Protocol::HTTP::Headers[{'accept' => 'text/html'}]}
let(:body) {Protocol::HTTP::Body::Buffered.new}

let(:request) {Protocol::HTTP::Request.new(
'https', 'example.com', 'GET', '/', 'http/1.1', headers, body
)}

let(:env) {adapter.make_environment(request)}

let(:wrapped_request) {subject.new(env)}

with 'incoming rack env' do
let(:body) {Protocol::HTTP::Body::Buffered.new}

let(:request) {Protocol::HTTP::Request.new(
'https', 'example.com', 'GET', '/', 'http/1.1', Protocol::HTTP::Headers[{'accept' => 'text/html'}], body
)}

let(:env) {adapter.make_environment(request)}

it "can restore request from original request" do
expect(subject[env]).to be == request
end

it "can regenerate request from generic env" do
env.delete(Protocol::Rack::PROTOCOL_HTTP_REQUEST)

wrapped_request = subject[env]

expect(wrapped_request.scheme).to be == request.scheme
expect(wrapped_request.authority).to be == request.authority
expect(wrapped_request.method).to be == request.method
Expand All @@ -38,4 +37,26 @@
expect(wrapped_request.protocol).to be == request.protocol
end
end

with 'incoming rack env which includes HTTP upgrade' do
let(:headers) {Protocol::HTTP::Headers[{'upgrade' => 'websocket'}]}

it "can extract upgrade request" do
expect(wrapped_request).to have_attributes(
protocol: be == ["websocket"]
)
end
end

with 'incoming rack env which includes rack.protocol' do
let(:request) {Protocol::HTTP::Request.new(
'https', 'example.com', 'GET', '/', 'http/1.1', headers, body, ["websocket"]
)}

it "can extract upgrade request" do
expect(wrapped_request).to have_attributes(
protocol: be == ["websocket"]
)
end
end
end

0 comments on commit 5cbff20

Please sign in to comment.