Skip to content

Commit

Permalink
SPEC: header values must be Strings instead of responding to #each [#27]
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Feb 6, 2009
1 parent 49d78d7 commit 732a4fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
17 changes: 7 additions & 10 deletions lib/rack/lint.rb
Expand Up @@ -338,16 +338,13 @@ def check_headers(header)
## but only contain keys that consist of
## letters, digits, <tt>_</tt> or <tt>-</tt> and start with a letter.
assert("invalid header name: #{key}") { key =~ /\A[a-zA-Z][a-zA-Z0-9_-]*\z/ }
##
## The values of the header must respond to #each.
assert("header values must respond to #each, but the value of " +
"'#{key}' doesn't (is #{value.class})") { value.respond_to? :each }
value.each { |item|
## The values passed on #each must be Strings
assert("header values must consist of Strings, but '#{key}' also contains a #{item.class}") {
item.instance_of?(String)
}
## and not contain characters below 037.

## The values of the header must be Strings,
assert("a header value must be a String, but the value of " +
"'#{key}' is a #{value.class}") { value.kind_of? String }
## consisting of lines (for multiple header values) seperated by "\n".
value.split("\n").each { |item|
## The lines must not contain characters below 037.
assert("invalid header value #{key}: #{item.inspect}") {
item !~ /[\000-\037]/
}
Expand Down
13 changes: 10 additions & 3 deletions test/spec_rack_lint.rb
Expand Up @@ -178,14 +178,14 @@ def env(*args)
[200, {"Foo" => Object.new}, ""]
}).call(env({}))
}.should.raise(Rack::Lint::LintError).
message.should.equal("header values must respond to #each, but the value of 'Foo' doesn't (is Object)")
message.should.equal("a header value must be a String, but the value of 'Foo' is a Object")

lambda {
Rack::Lint.new(lambda { |env|
[200, {"Foo" => [1,2,3]}, ""]
[200, {"Foo" => [1, 2, 3]}, ""]
}).call(env({}))
}.should.raise(Rack::Lint::LintError).
message.should.equal("header values must consist of Strings, but 'Foo' also contains a Fixnum")
message.should.equal("a header value must be a String, but the value of 'Foo' is a Array")


lambda {
Expand All @@ -194,6 +194,13 @@ def env(*args)
}).call(env({}))
}.should.raise(Rack::Lint::LintError).
message.should.match(/invalid header/)

# line ends (010) should be allowed in header values.
lambda {
Rack::Lint.new(lambda { |env|
[200, {"Foo-Bar" => "one\ntwo\nthree", "Content-Length" => "0", "Content-Type" => "text/plain" }, []]
}).call(env({}))
}.should.not.raise(Rack::Lint::LintError)
end

specify "notices content-type errors" do
Expand Down

0 comments on commit 732a4fa

Please sign in to comment.