From 1ffa95c55394c862798727ac8b203ecedda8842a Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Dec 2009 20:58:34 -0600 Subject: Status code lookup utility --- lib/rack/utils.rb | 8 ++++++++ test/spec_rack_utils.rb | 24 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index 333d8bf5..68fd6ace 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -399,6 +399,14 @@ module Rack 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. # diff --git a/test/spec_rack_utils.rb b/test/spec_rack_utils.rb index c61ab363..269a52bd 100644 --- a/test/spec_rack_utils.rb +++ b/test/spec_rack_utils.rb @@ -204,6 +204,18 @@ context "Rack::Utils" do 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 @@ context "Rack::Utils::HeaderHash" do 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 @@ context "Rack::Utils::Multipart" do 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, -- cgit v1.2.3-24-ge0c7