Skip to content

Commit

Permalink
Add rack.response_finished to Rack::Lint
Browse files Browse the repository at this point in the history
This updates Rack::Lint to validate that `rack.response_finished` is an
array of callables when present in the `env`. e.g. procs, lambdas, or
objects that respond to `call`.

This validates that:

* `rack.response_finished` is an array
* The contents of the array all respond to `call`
  • Loading branch information
BlakeWilliams authored and ioquatix committed Aug 26, 2022
1 parent 293b8e7 commit d491cae
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SPEC.rdoc
Expand Up @@ -132,6 +132,8 @@ There are the following restrictions:
set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
<tt>SCRIPT_NAME</tt> is empty.
<tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
<tt>rack.response_finished</tt>:: An array of callables run after the HTTP response has been sent.
Invoked with <tt>(env, status, headers, error)</tt> arguments.

=== The Input Stream

Expand Down
1 change: 1 addition & 0 deletions lib/rack/constants.rb
Expand Up @@ -51,6 +51,7 @@ module Rack
RACK_RECURSIVE_INCLUDE = 'rack.recursive.include'
RACK_MULTIPART_BUFFER_SIZE = 'rack.multipart.buffer_size'
RACK_MULTIPART_TEMPFILE_FACTORY = 'rack.multipart.tempfile_factory'
RACK_RESPONSE_FINISHED = 'rack.response_finished'
RACK_REQUEST_FORM_INPUT = 'rack.request.form_input'
RACK_REQUEST_FORM_HASH = 'rack.request.form_hash'
RACK_REQUEST_FORM_VARS = 'rack.request.form_vars'
Expand Down
10 changes: 10 additions & 0 deletions lib/rack/lint.rb
Expand Up @@ -363,6 +363,16 @@ def check_environment(env)
unless env[SCRIPT_NAME] != "/"
raise LintError, "SCRIPT_NAME cannot be '/', make it '' and PATH_INFO '/'"
end

## <tt>rack.response_finished</tt>:: An array of callables run after the HTTP response has been sent.
## Invoked with <tt>(env, status, headers, error)</tt> arguments.
if callables = env[RACK_RESPONSE_FINISHED]
raise LintError, "rack.response_finished must be an array of callable objects" unless callables.is_a?(Array)

callables.each do |callable|
raise LintError, "rack.response_finished values must respond to call(env, status, headers, error)" unless callable.respond_to?(:call)
end
end
end

##
Expand Down
36 changes: 36 additions & 0 deletions test/spec_lint.rb
Expand Up @@ -253,6 +253,31 @@ def obj.fatal(*) end
Rack::Lint.new(nil).call(env("SCRIPT_NAME" => "/"))
}.must_raise(Rack::Lint::LintError).
message.must_match(/cannot be .* make it ''/)

lambda {
Rack::Lint.new(nil).call(env("rack.response_finished" => "not a callable"))
}.must_raise(Rack::Lint::LintError).
message.must_match(/rack.response_finished must be an array of callable objects/)

lambda {
Rack::Lint.new(nil).call(env("rack.response_finished" => [-> (env) {}, "not a callable"]))
}.must_raise(Rack::Lint::LintError).
message.must_match(/rack.response_finished values must respond to call/)

lambda {
Rack::Lint.new(nil).call(env("rack.response_finished" => [-> () {}]))
}.must_raise(Rack::Lint::LintError).
message.must_match(/rack.response_finished values must accept an env argument/)

callable_object = Class.new do
def call
end
end.new

lambda {
Rack::Lint.new(nil).call(env("rack.response_finished" => [callable_object]))
}.must_raise(Rack::Lint::LintError).
message.must_match(/rack.response_finished values must accept an env argument/)
end

it "notice input errors" do
Expand Down Expand Up @@ -811,4 +836,15 @@ def assert_lint(*args)
}.must_raise(Rack::Lint::LintError).
message.must_equal 'rack.hijack header must respond to #call'
end

it "pass valid rack.response_finished" do
callable_object = Class.new do
def call(env)
end
end.new

Rack::Lint.new(lambda { |env|
[200, {}, ["foo"]]
}).call(env({ "rack.response_finished" => [-> (env) {}, lambda { |env| }, callable_object], "content-length" => "3" })).first.must_equal 200
end
end

0 comments on commit d491cae

Please sign in to comment.