|
| 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