Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Introduce Rack::Response::Helpers and make MockResponse use them, too.

darcs-hash:20070511151228-4fc50-8ef3cb76162c2a0d20f34d2542e1f4e51acacf29.gz
  • Loading branch information...
commit 7f889ec35dfb7dd90828e12af5e1b2d7859f9384 1 parent 30a3550
Christian Neukirchen authored
Showing with 77 additions and 32 deletions.
  1. +4 −32 lib/rack/mock.rb
  2. +41 −0 lib/rack/response.rb
  3. +32 −0 test/spec_rack_response.rb
36 lib/rack/mock.rb
View
@@ -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
41 lib/rack/response.rb
View
@@ -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 test/spec_rack_response.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.