Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
36 lib/rack/mock.rb
@@ -2,6 +2,7 @@
2 2 require 'stringio'
3 3 require 'rack/lint'
4 4 require 'rack/utils'
  5 +require 'rack/response'
5 6
6 7 module Rack
7 8 # Rack::MockRequest helps testing your Rack application without
@@ -129,45 +130,13 @@ def initialize(status, headers, body, errors=StringIO.new(""))
129 130 # Status
130 131 attr_reader :status
131 132
132   - def invalid?; @status < 100 || @status >= 600; end
133   -
134   - def informational?; @status >= 100 && @status < 200; end
135   - def successful?; @status >= 200 && @status < 300; end
136   - def redirection?; @status >= 300 && @status < 400; end
137   - def client_error?; @status >= 400 && @status < 500; end
138   - def server_error?; @status >= 500 && @status < 600; end
139   -
140   - def ok?; @status == 200; end
141   - def forbidden?; @status == 403; end
142   - def not_found?; @status == 404; end
143   -
144   - def redirect?; [301, 302, 303, 307].include? @status; end
145   - def empty?; [201, 204, 304].include? @status; end
146   -
147 133 # Headers
148 134 attr_reader :headers, :original_headers
149 135
150   - def include?(header)
151   - !!headers[header]
152   - end
153   -
154 136 def [](field)
155 137 headers[field]
156 138 end
157 139
158   - def content_type
159   - headers["Content-Type"]
160   - end
161   -
162   - def content_length
163   - cl = headers["Content-Length"]
164   - cl ? cl.to_i : cl
165   - end
166   -
167   - def location
168   - headers["Location"]
169   - end
170   -
171 140
172 141 # Body
173 142 attr_reader :body
@@ -183,5 +152,8 @@ def match(other)
183 152
184 153 # Errors
185 154 attr_accessor :errors
  155 +
  156 +
  157 + include Response::Helpers
186 158 end
187 159 end
41 lib/rack/response.rb
@@ -115,5 +115,46 @@ def write(str)
115 115 def empty?
116 116 @block == nil && @body.empty?
117 117 end
  118 +
  119 + alias headers header
  120 +
  121 + module Helpers
  122 + def invalid?; @status < 100 || @status >= 600; end
  123 +
  124 + def informational?; @status >= 100 && @status < 200; end
  125 + def successful?; @status >= 200 && @status < 300; end
  126 + def redirection?; @status >= 300 && @status < 400; end
  127 + def client_error?; @status >= 400 && @status < 500; end
  128 + def server_error?; @status >= 500 && @status < 600; end
  129 +
  130 + def ok?; @status == 200; end
  131 + def forbidden?; @status == 403; end
  132 + def not_found?; @status == 404; end
  133 +
  134 + def redirect?; [301, 302, 303, 307].include? @status; end
  135 + def empty?; [201, 204, 304].include? @status; end
  136 +
  137 + # Headers
  138 + attr_reader :headers, :original_headers
  139 +
  140 + def include?(header)
  141 + !!headers[header]
  142 + end
  143 +
  144 + def content_type
  145 + headers["Content-Type"]
  146 + end
  147 +
  148 + def content_length
  149 + cl = headers["Content-Length"]
  150 + cl ? cl.to_i : cl
  151 + end
  152 +
  153 + def location
  154 + headers["Location"]
  155 + end
  156 + end
  157 +
  158 + include Helpers
118 159 end
119 160 end
32 test/spec_rack_response.rb
@@ -124,4 +124,36 @@
124 124 r.finish { }
125 125 r.should.not.be.empty
126 126 end
  127 +
  128 + specify "should provide access to the HTTP status" do
  129 + res = Rack::Response.new
  130 + res.status = 200
  131 + res.should.be.successful
  132 + res.should.be.ok
  133 +
  134 + res.status = 404
  135 + res.should.not.be.successful
  136 + res.should.be.client_error
  137 + res.should.be.not_found
  138 +
  139 + res.status = 501
  140 + res.should.not.be.successful
  141 + res.should.be.server_error
  142 +
  143 + res.status = 307
  144 + res.should.be.redirect
  145 + end
  146 +
  147 + specify "should provide access to the HTTP headers" do
  148 + res = Rack::Response.new
  149 + res["Content-Type"] = "text/yaml"
  150 +
  151 + res.should.include "Content-Type"
  152 + res.headers["Content-Type"].should.equal "text/yaml"
  153 + res["Content-Type"].should.equal "text/yaml"
  154 + res.content_type.should.equal "text/yaml"
  155 + res.content_length.should.be.nil
  156 + res.location.should.be.nil
  157 + end
  158 +
127 159 end

0 comments on commit 7f889ec

Please sign in to comment.
Something went wrong with that request. Please try again.