|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2026, by Samuel Williams. |
| 5 | + |
| 6 | +require "falcon/body/request_finished" |
| 7 | +require "protocol/http/body/buffered" |
| 8 | +require "protocol/http/response" |
| 9 | +require "async/utilization" |
| 10 | +require "sus/fixtures/async" |
| 11 | + |
| 12 | +describe Falcon::Body::RequestFinished do |
| 13 | + include Sus::Fixtures::Async::ReactorContext |
| 14 | + |
| 15 | + let(:registry) { Async::Utilization::Registry.new } |
| 16 | + let(:metric) { registry.metric(:requests_active) } |
| 17 | + let(:body) { Protocol::HTTP::Body::Buffered.wrap("Hello World") } |
| 18 | + let(:response) { Protocol::HTTP::Response[200, {"content-type" => "text/plain"}, body] } |
| 19 | + |
| 20 | + with ".wrap" do |
| 21 | + with "non-empty body" do |
| 22 | + it "wraps body and decrements metric when body is closed" do |
| 23 | + metric.increment |
| 24 | + expect(metric.value).to be == 1 |
| 25 | + |
| 26 | + wrapped = subject.wrap(response, metric) |
| 27 | + expect(wrapped).to be == response |
| 28 | + expect(response.body).to be_a(subject) |
| 29 | + expect(metric.value).to be == 1 |
| 30 | + |
| 31 | + response.body.close |
| 32 | + expect(metric.value).to be == 0 |
| 33 | + end |
| 34 | + |
| 35 | + it "decrements only once on multiple close calls" do |
| 36 | + metric.increment |
| 37 | + subject.wrap(response, metric) |
| 38 | + |
| 39 | + response.body.close |
| 40 | + response.body.close |
| 41 | + |
| 42 | + expect(metric.value).to be == 0 |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + with "empty body" do |
| 47 | + let(:body) { Protocol::HTTP::Body::Buffered.new } |
| 48 | + |
| 49 | + it "decrements immediately" do |
| 50 | + metric.increment |
| 51 | + expect(metric.value).to be == 1 |
| 52 | + |
| 53 | + subject.wrap(response, metric) |
| 54 | + expect(metric.value).to be == 0 |
| 55 | + expect(response.body).to be_equal(body) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + with "nil body" do |
| 60 | + let(:response) { Protocol::HTTP::Response[204, {}, nil] } |
| 61 | + |
| 62 | + it "decrements immediately" do |
| 63 | + metric.increment |
| 64 | + expect(metric.value).to be == 1 |
| 65 | + |
| 66 | + subject.wrap(response, metric) |
| 67 | + expect(metric.value).to be == 0 |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + with "nil message" do |
| 72 | + it "decrements immediately" do |
| 73 | + metric.increment |
| 74 | + subject.wrap(nil, metric) |
| 75 | + expect(metric.value).to be == 0 |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + with "#rewindable?" do |
| 81 | + it "returns false" do |
| 82 | + subject.wrap(response, metric) |
| 83 | + expect(response.body).not_to be(:rewindable?) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + with "#rewind" do |
| 88 | + it "returns false" do |
| 89 | + subject.wrap(response, metric) |
| 90 | + expect(response.body.rewind).to be == false |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments