Skip to content

Commit

Permalink
Introduce Rack::Response::Helpers and make MockResponse use them, too.
Browse files Browse the repository at this point in the history
darcs-hash:20070511151228-4fc50-8ef3cb76162c2a0d20f34d2542e1f4e51acacf29.gz
  • Loading branch information
leahneukirchen committed May 11, 2007
1 parent 30a3550 commit 7f889ec
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 32 deletions.
36 changes: 4 additions & 32 deletions lib/rack/mock.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -183,5 +152,8 @@ def match(other)

# Errors
attr_accessor :errors


include Response::Helpers
end
end
41 changes: 41 additions & 0 deletions lib/rack/response.rb
Expand Up @@ -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
32 changes: 32 additions & 0 deletions test/spec_rack_response.rb
Expand Up @@ -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

0 comments on commit 7f889ec

Please sign in to comment.