Skip to content

Commit

Permalink
Document more clearly that rack.input must be opened in binary mode, …
Browse files Browse the repository at this point in the history
…and enforce it in Rack::Lint.

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
FooBarWidget authored and josh committed Jun 17, 2009
1 parent dfab30d commit 8517826
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rack/lint.rb
Expand Up @@ -233,8 +233,17 @@ def check_env(env)
## === The Input Stream
##
## The input stream is an IO-like object which contains the raw HTTP
## POST data. If it is a file then it must be opened in binary mode.
## POST data.
def check_input(input)
## When applicable, its external encoding must be "ASCII-8BIT" and it
## must be opened in binary mode, for Ruby 1.9 compatibility.
assert("rack.input #{input} does not have ASCII-8BIT as its external encoding") {
input.external_encoding.name == "ASCII-8BIT"
} if input.respond_to?(:external_encoding)
assert("rack.input #{input} is not opened in binary mode") {
input.binmode?
} if input.respond_to?(:binmode?)

## The input stream must respond to +gets+, +each+, +read+ and +rewind+.
[:gets, :each, :read, :rewind].each { |method|
assert("rack.input #{input} does not respond to ##{method}") {
Expand Down
22 changes: 22 additions & 0 deletions test/spec_rack_lint.rb
Expand Up @@ -110,6 +110,28 @@ def env(*args)
Rack::Lint.new(nil).call(env("rack.input" => ""))
}.should.raise(Rack::Lint::LintError).
message.should.match(/does not respond to #gets/)

lambda {
input = Object.new
def input.binmode?
false
end
Rack::Lint.new(nil).call(env("rack.input" => input))
}.should.raise(Rack::Lint::LintError).
message.should.match(/is not opened in binary mode/)

lambda {
input = Object.new
def input.external_encoding
result = Object.new
def result.name
"US-ASCII"
end
result
end
Rack::Lint.new(nil).call(env("rack.input" => input))
}.should.raise(Rack::Lint::LintError).
message.should.match(/does not have ASCII-8BIT as its external encoding/);puts RUBY_VERSION
end

specify "notices error errors" do
Expand Down

0 comments on commit 8517826

Please sign in to comment.