From 7f889ec35dfb7dd90828e12af5e1b2d7859f9384 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Fri, 11 May 2007 15:12:00 +0000 Subject: [PATCH] Introduce Rack::Response::Helpers and make MockResponse use them, too. darcs-hash:20070511151228-4fc50-8ef3cb76162c2a0d20f34d2542e1f4e51acacf29.gz --- lib/rack/mock.rb | 36 ++++----------------------------- lib/rack/response.rb | 41 ++++++++++++++++++++++++++++++++++++++ test/spec_rack_response.rb | 32 +++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 32 deletions(-) diff --git a/lib/rack/mock.rb b/lib/rack/mock.rb index 5f0e531c0..7cad9ab8e 100644 --- a/lib/rack/mock.rb +++ b/lib/rack/mock.rb @@ -2,6 +2,7 @@ require 'stringio' require 'rack/lint' require 'rack/utils' +require 'rack/response' module Rack # Rack::MockRequest helps testing your Rack application without @@ -129,45 +130,13 @@ def initialize(status, headers, body, errors=StringIO.new("")) # Status attr_reader :status - def invalid?; @status < 100 || @status >= 600; end - - def informational?; @status >= 100 && @status < 200; end - def successful?; @status >= 200 && @status < 300; end - def redirection?; @status >= 300 && @status < 400; end - def client_error?; @status >= 400 && @status < 500; end - def server_error?; @status >= 500 && @status < 600; end - - def ok?; @status == 200; end - def forbidden?; @status == 403; end - def not_found?; @status == 404; end - - def redirect?; [301, 302, 303, 307].include? @status; end - def empty?; [201, 204, 304].include? @status; end - # Headers attr_reader :headers, :original_headers - def include?(header) - !!headers[header] - end - def [](field) headers[field] end - def content_type - headers["Content-Type"] - end - - def content_length - cl = headers["Content-Length"] - cl ? cl.to_i : cl - end - - def location - headers["Location"] - end - # Body attr_reader :body @@ -183,5 +152,8 @@ def match(other) # Errors attr_accessor :errors + + + include Response::Helpers end end diff --git a/lib/rack/response.rb b/lib/rack/response.rb index 1a57518fb..647bd0056 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -115,5 +115,46 @@ def write(str) def empty? @block == nil && @body.empty? end + + alias headers header + + module Helpers + def invalid?; @status < 100 || @status >= 600; end + + def informational?; @status >= 100 && @status < 200; end + def successful?; @status >= 200 && @status < 300; end + def redirection?; @status >= 300 && @status < 400; end + def client_error?; @status >= 400 && @status < 500; end + def server_error?; @status >= 500 && @status < 600; end + + def ok?; @status == 200; end + def forbidden?; @status == 403; end + def not_found?; @status == 404; end + + def redirect?; [301, 302, 303, 307].include? @status; end + def empty?; [201, 204, 304].include? @status; end + + # Headers + attr_reader :headers, :original_headers + + def include?(header) + !!headers[header] + end + + def content_type + headers["Content-Type"] + end + + def content_length + cl = headers["Content-Length"] + cl ? cl.to_i : cl + end + + def location + headers["Location"] + end + end + + include Helpers end end diff --git a/test/spec_rack_response.rb b/test/spec_rack_response.rb index bf9bacd32..2ceb0148b 100644 --- a/test/spec_rack_response.rb +++ b/test/spec_rack_response.rb @@ -124,4 +124,36 @@ r.finish { } r.should.not.be.empty end + + specify "should provide access to the HTTP status" do + res = Rack::Response.new + res.status = 200 + res.should.be.successful + res.should.be.ok + + res.status = 404 + res.should.not.be.successful + res.should.be.client_error + res.should.be.not_found + + res.status = 501 + res.should.not.be.successful + res.should.be.server_error + + res.status = 307 + res.should.be.redirect + end + + specify "should provide access to the HTTP headers" do + res = Rack::Response.new + res["Content-Type"] = "text/yaml" + + res.should.include "Content-Type" + res.headers["Content-Type"].should.equal "text/yaml" + res["Content-Type"].should.equal "text/yaml" + res.content_type.should.equal "text/yaml" + res.content_length.should.be.nil + res.location.should.be.nil + end + end