Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 357 lines (296 sloc) 10.607 kb
1fb12f0 @manveru Fix spec requires and the missing ones
manveru authored
1 require 'rack/response'
90ca4df @rkh test for Rack::Response#close
rkh authored
2 require 'stringio'
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
3
c0b511e @manveru Ported specs to bacon
manveru authored
4 describe Rack::Response do
5 should "have sensible default values" do
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
6 response = Rack::Response.new
7 status, header, body = response.finish
8 status.should.equal 200
3623d04 @raggi Correct some of the mime type issues. References #316 and #366.
raggi authored
9 header.should.equal({})
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
10 body.each { |part|
11 part.should.equal ""
12 }
13
14 response = Rack::Response.new
15 status, header, body = *response
16 status.should.equal 200
3623d04 @raggi Correct some of the mime type issues. References #316 and #366.
raggi authored
17 header.should.equal({})
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
18 body.each { |part|
19 part.should.equal ""
20 }
21 end
22
c0b511e @manveru Ported specs to bacon
manveru authored
23 it "can be written to" do
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
24 response = Rack::Response.new
25
cbcae3a @tenderlove fixing unused variable warnings in 1.9.3
tenderlove authored
26 _, _, body = response.finish do
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
27 response.write "foo"
28 response.write "bar"
29 response.write "baz"
30 end
c0b511e @manveru Ported specs to bacon
manveru authored
31
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
32 parts = []
33 body.each { |part| parts << part }
c0b511e @manveru Ported specs to bacon
manveru authored
34
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
35 parts.should.equal ["foo", "bar", "baz"]
36 end
37
c0b511e @manveru Ported specs to bacon
manveru authored
38 it "can set and read headers" do
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
39 response = Rack::Response.new
3623d04 @raggi Correct some of the mime type issues. References #316 and #366.
raggi authored
40 response["Content-Type"].should.equal nil
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
41 response["Content-Type"] = "text/plain"
42 response["Content-Type"].should.equal "text/plain"
43 end
3e4833b @sstephenson Fix initializing Response objects with differently-cased Content-Type…
sstephenson authored
44
45 it "can override the initial Content-Type with a different case" do
46 response = Rack::Response.new("", 200, "content-type" => "text/plain")
47 response["Content-Type"].should.equal "text/plain"
48 end
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
49
c0b511e @manveru Ported specs to bacon
manveru authored
50 it "can set cookies" do
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
51 response = Rack::Response.new
c0b511e @manveru Ported specs to bacon
manveru authored
52
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
53 response.set_cookie "foo", "bar"
54 response["Set-Cookie"].should.equal "foo=bar"
55 response.set_cookie "foo2", "bar2"
c028a23 @rtomayko cookie utility methods use multiline strings instead of arrays
rtomayko authored
56 response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2"].join("\n")
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
57 response.set_cookie "foo3", "bar3"
c028a23 @rtomayko cookie utility methods use multiline strings instead of arrays
rtomayko authored
58 response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2", "foo3=bar3"].join("\n")
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
59 end
60
c0b511e @manveru Ported specs to bacon
manveru authored
61 it "can set cookies with the same name for multiple domains" do
55cbbc9 @zbrock allow delete of cookies with same name but different domain
zbrock authored
62 response = Rack::Response.new
63 response.set_cookie "foo", {:value => "bar", :domain => "sample.example.com"}
64 response.set_cookie "foo", {:value => "bar", :domain => ".example.com"}
65 response["Set-Cookie"].should.equal ["foo=bar; domain=sample.example.com", "foo=bar; domain=.example.com"].join("\n")
66 end
67
5e0a941 @raggi Switch to RFC 2822 expires
raggi authored
68 it "formats the Cookie expiration date accordingly to RFC 6265" do
d0ed44f @chneukirchen Fix Cookie dates accordingly to RFC 2109
chneukirchen authored
69 response = Rack::Response.new
c0b511e @manveru Ported specs to bacon
manveru authored
70
d0ed44f @chneukirchen Fix Cookie dates accordingly to RFC 2109
chneukirchen authored
71 response.set_cookie "foo", {:value => "bar", :expires => Time.now+10}
bff71e5 @chneukirchen Fix warning
chneukirchen authored
72 response["Set-Cookie"].should.match(
5e0a941 @raggi Switch to RFC 2822 expires
raggi authored
73 /expires=..., \d\d ... \d\d\d\d \d\d:\d\d:\d\d .../)
d0ed44f @chneukirchen Fix Cookie dates accordingly to RFC 2109
chneukirchen authored
74 end
75
c0b511e @manveru Ported specs to bacon
manveru authored
76 it "can set secure cookies" do
236bdcc @chneukirchen Add :secure option for set_cookie
chneukirchen authored
77 response = Rack::Response.new
78 response.set_cookie "foo", {:value => "bar", :secure => true}
79 response["Set-Cookie"].should.equal "foo=bar; secure"
80 end
81
c0b511e @manveru Ported specs to bacon
manveru authored
82 it "can set http only cookies" do
c7263d1 @scytrin Addition of HttpOnly cookie support to Rack::Response
scytrin authored
83 response = Rack::Response.new
84 response.set_cookie "foo", {:value => "bar", :httponly => true}
85 response["Set-Cookie"].should.equal "foo=bar; HttpOnly"
86 end
87
65d3894 @gshutler Set HttpOnly for cookies using :http_only
gshutler authored
88 it "can set http only cookies with :http_only" do
89 response = Rack::Response.new
90 response.set_cookie "foo", {:value => "bar", :http_only => true}
91 response["Set-Cookie"].should.equal "foo=bar; HttpOnly"
92 end
93
94 it "can set prefers :httponly for http only cookie setting when :httponly and :http_only provided" do
95 response = Rack::Response.new
96 response.set_cookie "foo", {:value => "bar", :httponly => false, :http_only => true}
97 response["Set-Cookie"].should.equal "foo=bar"
98 end
99
c0b511e @manveru Ported specs to bacon
manveru authored
100 it "can delete cookies" do
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
101 response = Rack::Response.new
102 response.set_cookie "foo", "bar"
103 response.set_cookie "foo2", "bar2"
104 response.delete_cookie "foo"
c028a23 @rtomayko cookie utility methods use multiline strings instead of arrays
rtomayko authored
105 response["Set-Cookie"].should.equal [
106 "foo2=bar2",
5e0a941 @raggi Switch to RFC 2822 expires
raggi authored
107 "foo=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000"
c028a23 @rtomayko cookie utility methods use multiline strings instead of arrays
rtomayko authored
108 ].join("\n")
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
109 end
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
110
c0b511e @manveru Ported specs to bacon
manveru authored
111 it "can delete cookies with the same name from multiple domains" do
55cbbc9 @zbrock allow delete of cookies with same name but different domain
zbrock authored
112 response = Rack::Response.new
113 response.set_cookie "foo", {:value => "bar", :domain => "sample.example.com"}
114 response.set_cookie "foo", {:value => "bar", :domain => ".example.com"}
115 response["Set-Cookie"].should.equal ["foo=bar; domain=sample.example.com", "foo=bar; domain=.example.com"].join("\n")
116 response.delete_cookie "foo", :domain => ".example.com"
5e0a941 @raggi Switch to RFC 2822 expires
raggi authored
117 response["Set-Cookie"].should.equal ["foo=bar; domain=sample.example.com", "foo=; domain=.example.com; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000"].join("\n")
55cbbc9 @zbrock allow delete of cookies with same name but different domain
zbrock authored
118 response.delete_cookie "foo", :domain => "sample.example.com"
5e0a941 @raggi Switch to RFC 2822 expires
raggi authored
119 response["Set-Cookie"].should.equal ["foo=; domain=.example.com; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000",
120 "foo=; domain=sample.example.com; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000"].join("\n")
55cbbc9 @zbrock allow delete of cookies with same name but different domain
zbrock authored
121 end
122
eead3b2 @joevandyk Fix cookie deletion with path specified
joevandyk authored
123 it "can delete cookies with the same name with different paths" do
124 response = Rack::Response.new
125 response.set_cookie "foo", {:value => "bar", :path => "/"}
126 response.set_cookie "foo", {:value => "bar", :path => "/path"}
127 response["Set-Cookie"].should.equal ["foo=bar; path=/",
128 "foo=bar; path=/path"].join("\n")
129
130 response.delete_cookie "foo", :path => "/path"
131 response["Set-Cookie"].should.equal ["foo=bar; path=/",
5e0a941 @raggi Switch to RFC 2822 expires
raggi authored
132 "foo=; path=/path; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000"].join("\n")
eead3b2 @joevandyk Fix cookie deletion with path specified
joevandyk authored
133 end
134
c0b511e @manveru Ported specs to bacon
manveru authored
135 it "can do redirects" do
71b02aa @chneukirchen Add Rack::Response#redirect
chneukirchen authored
136 response = Rack::Response.new
137 response.redirect "/foo"
138 status, header, body = response.finish
139 status.should.equal 302
140 header["Location"].should.equal "/foo"
141
142 response = Rack::Response.new
143 response.redirect "/foo", 307
144 status, header, body = response.finish
145
146 status.should.equal 307
147 end
148
c0b511e @manveru Ported specs to bacon
manveru authored
149 it "has a useful constructor" do
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
150 r = Rack::Response.new("foo")
151 status, header, body = r.finish
152 str = ""; body.each { |part| str << part }
153 str.should.equal "foo"
154
155 r = Rack::Response.new(["foo", "bar"])
156 status, header, body = r.finish
157 str = ""; body.each { |part| str << part }
158 str.should.equal "foobar"
159
17a3e1e @hannesg Test an object which repsonds to each instead of a set.
hannesg authored
160 object_with_each = Object.new
161 def object_with_each.each
162 yield "foo"
163 yield "bar"
164 end
165 r = Rack::Response.new(object_with_each)
f39e08c @chneukirchen Ensure the Response body is writable
chneukirchen authored
166 r.write "foo"
167 status, header, body = r.finish
168 str = ""; body.each { |part| str << part }
169 str.should.equal "foobarfoo"
170
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
171 r = Rack::Response.new([], 500)
172 r.status.should.equal 500
8b71767 @defunkt Response should call #to_i on the status, as per the spec.
defunkt authored
173
174 r = Rack::Response.new([], "200 OK")
175 r.status.should.equal 200
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
176 end
177
c0b511e @manveru Ported specs to bacon
manveru authored
178 it "has a constructor that can take a block" do
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
179 r = Rack::Response.new { |res|
180 res.status = 404
181 res.write "foo"
182 }
cbcae3a @tenderlove fixing unused variable warnings in 1.9.3
tenderlove authored
183 status, _, body = r.finish
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
184 str = ""; body.each { |part| str << part }
185 str.should.equal "foo"
186 status.should.equal 404
187 end
c0b511e @manveru Ported specs to bacon
manveru authored
188
189 it "doesn't return invalid responses" do
5a9768d @chneukirchen Fix test cases that used 201 as a status where Content-Type is not al…
chneukirchen authored
190 r = Rack::Response.new(["foo", "bar"], 204)
cbcae3a @tenderlove fixing unused variable warnings in 1.9.3
tenderlove authored
191 _, header, body = r.finish
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
192 str = ""; body.each { |part| str << part }
193 str.should.be.empty
194 header["Content-Type"].should.equal nil
7b32c33 @raggi Closes #21 Rack::Response#finish should delete Content-Length if stat…
raggi authored
195 header['Content-Length'].should.equal nil
7ade69b @chneukirchen Add some missing tests
chneukirchen authored
196
197 lambda {
198 Rack::Response.new(Object.new)
199 }.should.raise(TypeError).
bfae003 @chneukirchen Ducktype on #to_str for Rack::Response.new
chneukirchen authored
200 message.should =~ /stringable or iterable required/
c525c28 @chneukirchen Improve the Rack::Response constructor
chneukirchen authored
201 end
04f2bdf @chneukirchen Add Rack::Response#empty?
chneukirchen authored
202
c0b511e @manveru Ported specs to bacon
manveru authored
203 it "knows if it's empty" do
04f2bdf @chneukirchen Add Rack::Response#empty?
chneukirchen authored
204 r = Rack::Response.new
205 r.should.be.empty
206 r.write "foo"
207 r.should.not.be.empty
208
209 r = Rack::Response.new
210 r.should.be.empty
211 r.finish
212 r.should.be.empty
213
214 r = Rack::Response.new
215 r.should.be.empty
216 r.finish { }
217 r.should.not.be.empty
218 end
7f889ec @chneukirchen Introduce Rack::Response::Helpers and make MockResponse use them, too.
chneukirchen authored
219
c0b511e @manveru Ported specs to bacon
manveru authored
220 should "provide access to the HTTP status" do
7f889ec @chneukirchen Introduce Rack::Response::Helpers and make MockResponse use them, too.
chneukirchen authored
221 res = Rack::Response.new
222 res.status = 200
223 res.should.be.successful
224 res.should.be.ok
225
3db3f80 @tjmw Add created? and accepted? response helper methods
tjmw authored
226 res.status = 201
227 res.should.be.successful
228 res.should.be.created
229
230 res.status = 202
231 res.should.be.successful
232 res.should.be.accepted
233
b79e153 @BrentWheeldon Add HTTP response helpers for 204, 301, and 412.
BrentWheeldon authored
234 res.status = 204
235 res.should.be.successful
236 res.should.be.no_content
237
238 res.status = 301
239 res.should.be.redirect
240 res.should.be.moved_permanently
241
533a1a9 @alindeman Adds #bad_request? helper
alindeman authored
242 res.status = 400
243 res.should.not.be.successful
244 res.should.be.client_error
245 res.should.be.bad_request
246
cf13f31 @sikachu Add Response#unauthorized? helper
sikachu authored
247 res.status = 401
248 res.should.not.be.successful
249 res.should.be.client_error
250 res.should.be.unauthorized
251
7f889ec @chneukirchen Introduce Rack::Response::Helpers and make MockResponse use them, too.
chneukirchen authored
252 res.status = 404
253 res.should.not.be.successful
254 res.should.be.client_error
255 res.should.be.not_found
256
a9b7b05 @raggi added Rack::Response::Helpers#method_not_allowed?
raggi authored
257 res.status = 405
258 res.should.not.be.successful
259 res.should.be.client_error
260 res.should.be.method_not_allowed
261
b79e153 @BrentWheeldon Add HTTP response helpers for 204, 301, and 412.
BrentWheeldon authored
262 res.status = 412
263 res.should.not.be.successful
264 res.should.be.client_error
265 res.should.be.precondition_failed
266
7fe910e @mattkasa Add support for RFC2324 status code
mattkasa authored
267 res.status = 418
268 res.should.not.be.successful
269 res.should.be.client_error
270 res.should.be.i_m_a_teapot
271
3a7e5cd @alindeman Adds #unprocessable? helper
alindeman authored
272 res.status = 422
273 res.should.not.be.successful
274 res.should.be.client_error
275 res.should.be.unprocessable
276
7f889ec @chneukirchen Introduce Rack::Response::Helpers and make MockResponse use them, too.
chneukirchen authored
277 res.status = 501
278 res.should.not.be.successful
279 res.should.be.server_error
280
281 res.status = 307
282 res.should.be.redirect
283 end
284
c0b511e @manveru Ported specs to bacon
manveru authored
285 should "provide access to the HTTP headers" do
7f889ec @chneukirchen Introduce Rack::Response::Helpers and make MockResponse use them, too.
chneukirchen authored
286 res = Rack::Response.new
287 res["Content-Type"] = "text/yaml"
288
289 res.should.include "Content-Type"
290 res.headers["Content-Type"].should.equal "text/yaml"
291 res["Content-Type"].should.equal "text/yaml"
292 res.content_type.should.equal "text/yaml"
293 res.content_length.should.be.nil
294 res.location.should.be.nil
295 end
296
c0b511e @manveru Ported specs to bacon
manveru authored
297 it "does not add or change Content-Length when #finish()ing" do
ea8fdc3 @rue Update Content-Length in Response#write, not in #finish. Spec.
rue authored
298 res = Rack::Response.new
299 res.status = 200
300 res.finish
301 res.headers["Content-Length"].should.be.nil
302
303 res = Rack::Response.new
304 res.status = 200
305 res.headers["Content-Length"] = "10"
306 res.finish
307 res.headers["Content-Length"].should.equal "10"
308 end
309
c0b511e @manveru Ported specs to bacon
manveru authored
310 it "updates Content-Length when body appended to using #write" do
ea8fdc3 @rue Update Content-Length in Response#write, not in #finish. Spec.
rue authored
311 res = Rack::Response.new
312 res.status = 200
313 res.headers["Content-Length"].should.be.nil
314 res.write "Hi"
315 res.headers["Content-Length"].should.equal "2"
316 res.write " there"
317 res.headers["Content-Length"].should.equal "8"
318 end
319
90ca4df @rkh test for Rack::Response#close
rkh authored
320 it "calls close on #body" do
321 res = Rack::Response.new
322 res.body = StringIO.new
323 res.close
324 res.body.should.be.closed
325 end
5b251a9 @raggi Prevent infinite recursions from Response#to_ary
raggi authored
326
f630f78 @raggi Rack::Response now conforms to body.close SPEC
raggi authored
327 it "calls close on #body when 204, 205, or 304" do
328 res = Rack::Response.new
329 res.body = StringIO.new
330 res.finish
331 res.body.should.not.be.closed
332
333 res.status = 204
334 _, _, b = res.finish
335 res.body.should.be.closed
a13426b @raggi Squash test warnings
raggi authored
336 b.should.not.equal res.body
f630f78 @raggi Rack::Response now conforms to body.close SPEC
raggi authored
337
338 res.body = StringIO.new
339 res.status = 205
340 _, _, b = res.finish
341 res.body.should.be.closed
a13426b @raggi Squash test warnings
raggi authored
342 b.should.not.equal res.body
f630f78 @raggi Rack::Response now conforms to body.close SPEC
raggi authored
343
344 res.body = StringIO.new
345 res.status = 304
346 _, _, b = res.finish
347 res.body.should.be.closed
a13426b @raggi Squash test warnings
raggi authored
348 b.should.not.equal res.body
f630f78 @raggi Rack::Response now conforms to body.close SPEC
raggi authored
349 end
350
5b251a9 @raggi Prevent infinite recursions from Response#to_ary
raggi authored
351 it "wraps the body from #to_ary to prevent infinite loops" do
352 res = Rack::Response.new
353 res.finish.last.should.not.respond_to?(:to_ary)
354 lambda { res.finish.last.to_ary }.should.raise(NoMethodError)
355 end
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
356 end
Something went wrong with that request. Please try again.