Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Document more clearly that rack.input must be opened in binary mode, …

…and enforce it in Rack::Lint

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
commit fd1c1d2e494d9a7d15e0458602ef121f244addc3 1 parent d8d812e
@FooBarWidget FooBarWidget authored josh committed
Showing with 32 additions and 1 deletion.
  1. +10 −1 lib/rack/lint.rb
  2. +22 −0 test/spec_rack_lint.rb
View
11 lib/rack/lint.rb
@@ -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}") {
View
22 test/spec_rack_lint.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.