Skip to content

Commit 5a500bc

Browse files
committed
Add support for rack.response_finished to Rack 2.
1 parent f2a95f3 commit 5a500bc

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

lib/protocol/rack/adapter/rack2.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def call(request)
116116

117117
body&.close if body.respond_to?(:close)
118118

119+
# Rack 2 does not include `rack.response_finished` in the specification. However, if the application has set it, we will call the callbacks here as it would be extremely surprising to not do so.
120+
env&.[](RACK_RESPONSE_FINISHED)&.each do |callback|
121+
callback.call(env, status, headers, exception)
122+
end
123+
119124
return failure_response(exception)
120125
end
121126

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Support `rack.response_finished` in Rack 2 if it's present in the environment.
6+
37
## v0.16.0
48

59
- Hijacked IO is no longer duped, as it's not retained by the original connection, and `SSLSocket` does not support duping.

test/protocol/rack/adapter/rack2.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
expect(body).to be(:empty?)
132132
end
133133

134-
135134
it "can wrap streaming response" do
136135
streaming_body = Protocol::Rack::Body::Streaming.new(->(stream){})
137136
response = Protocol::HTTP::Response[200, headers: {}, body: streaming_body]
@@ -145,4 +144,29 @@
145144
expect(body).to be == []
146145
end
147146
end
147+
148+
with "response callbacks" do
149+
let(:adapter) {subject.new(app)}
150+
151+
let(:callback_called) {false}
152+
let(:callback) {->(env, status, headers, exception) {@callback_called = true}}
153+
let(:app) do
154+
proc do |env|
155+
env[Protocol::Rack::RACK_RESPONSE_FINISHED] = [callback]
156+
raise StandardError.new("Test error")
157+
end
158+
end
159+
let(:request) {Protocol::HTTP::Request["GET", "/", {}, nil]}
160+
161+
it "calls response callbacks on failure" do
162+
@callback_called = false
163+
164+
response = adapter.call(request)
165+
166+
expect(@callback_called).to be == true
167+
168+
expect(response.status).to be == 500
169+
expect(response.read).to be == "StandardError: Test error"
170+
end
171+
end
148172
end

0 commit comments

Comments
 (0)