Permalink
Browse files

Status code lookup utility

  • Loading branch information...
1 parent d8ccbf3 commit 1ffa95c55394c862798727ac8b203ecedda8842a @josh josh committed Dec 21, 2009
Showing with 26 additions and 6 deletions.
  1. +8 −0 lib/rack/utils.rb
  2. +18 −6 test/spec_rack_utils.rb
View
@@ -399,6 +399,14 @@ def replace(other)
hash
}
+ def status_code(status)
+ if status.is_a?(Symbol)
+ SYMBOL_TO_STATUS_CODE[status] || 500
+ else
+ status.to_i
+ end
+ end
+ module_function :status_code
# A multipart form data parser, adapted from IOWA.
#
View
@@ -204,6 +204,18 @@
specify "should return the bytesize of String" do
Rack::Utils.bytesize("FOO\xE2\x82\xAC").should.equal 6
end
+
+ specify "should return status code for integer" do
+ Rack::Utils.status_code(200).should.equal 200
+ end
+
+ specify "should return status code for string" do
+ Rack::Utils.status_code("200").should.equal 200
+ end
+
+ specify "should return status code for symbol" do
+ Rack::Utils.status_code(:ok).should.equal 200
+ end
end
context "Rack::Utils::HeaderHash" do
@@ -249,26 +261,26 @@
h.replace(j)
h["foo"].should.equal "bar"
end
-
+
specify "should be able to delete the given key case-sensitively" do
h = Rack::Utils::HeaderHash.new("foo" => "bar")
h.delete("foo")
h["foo"].should.be.nil
h["FOO"].should.be.nil
end
-
+
specify "should be able to delete the given key case-insensitively" do
h = Rack::Utils::HeaderHash.new("foo" => "bar")
h.delete("FOO")
h["foo"].should.be.nil
h["FOO"].should.be.nil
end
-
+
specify "should return the deleted value when #delete is called on an existing key" do
h = Rack::Utils::HeaderHash.new("foo" => "bar")
h.delete("Foo").should.equal("bar")
end
-
+
specify "should return nil when #delete is called on a non-existant key" do
h = Rack::Utils::HeaderHash.new("foo" => "bar")
h.delete("Hello").should.be.nil
@@ -478,10 +490,10 @@ def context env, app=@app; app.call(env); end
params["people"][0]["files"][:filename].should.equal "file1.txt"
params["people"][0]["files"][:tempfile].read.should.equal "contents"
end
-
+
specify "can parse fields that end at the end of the buffer" do
input = File.read(multipart_file("bad_robots"))
-
+
req = Rack::Request.new Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => "multipart/form-data, boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon",
"CONTENT_LENGTH" => input.size,

0 comments on commit 1ffa95c

Please sign in to comment.