forked from rack/rack
-
Notifications
You must be signed in to change notification settings - Fork 4
/
spec_rack_response.rb
110 lines (92 loc) · 3.01 KB
/
spec_rack_response.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'test/spec'
require 'rack/response'
context "Rack::Response" do
specify "has sensible default values" do
response = Rack::Response.new
status, header, body = response.finish
status.should.equal 200
header.should.equal "Content-Type" => "text/html"
body.each { |part|
part.should.equal ""
}
response = Rack::Response.new
status, header, body = *response
status.should.equal 200
header.should.equal "Content-Type" => "text/html"
body.each { |part|
part.should.equal ""
}
end
specify "can be written to" do
response = Rack::Response.new
status, header, body = response.finish do
response.write "foo"
response.write "bar"
response.write "baz"
end
parts = []
body.each { |part| parts << part }
parts.should.equal ["foo", "bar", "baz"]
end
specify "can set and read headers" do
response = Rack::Response.new
response["Content-Type"].should.equal "text/html"
response["Content-Type"] = "text/plain"
response["Content-Type"].should.equal "text/plain"
end
specify "can set cookies" do
response = Rack::Response.new
response.set_cookie "foo", "bar"
response["Set-Cookie"].should.equal "foo=bar"
response.set_cookie "foo2", "bar2"
response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2"]
response.set_cookie "foo3", "bar3"
response["Set-Cookie"].should.equal ["foo=bar", "foo2=bar2", "foo3=bar3"]
end
specify "can delete cookies" do
response = Rack::Response.new
response.set_cookie "foo", "bar"
response.set_cookie "foo2", "bar2"
response.delete_cookie "foo"
response["Set-Cookie"].should.equal ["foo2=bar2",
"foo=; expires=Thu, 01 Jan 1970 00:00:00 GMT"]
end
specify "has a useful constructor" do
r = Rack::Response.new("foo")
status, header, body = r.finish
str = ""; body.each { |part| str << part }
str.should.equal "foo"
r = Rack::Response.new(["foo", "bar"])
status, header, body = r.finish
str = ""; body.each { |part| str << part }
str.should.equal "foobar"
r = Rack::Response.new({"foo", "bar"})
r.write "foo"
status, header, body = r.finish
str = ""; body.each { |part| str << part }
str.should.equal "foobarfoo"
r = Rack::Response.new([], 500)
r.status.should.equal 500
end
specify "has a constructor that can take a block" do
r = Rack::Response.new { |res|
res.status = 404
res.write "foo"
}
status, header, body = r.finish
str = ""; body.each { |part| str << part }
str.should.equal "foo"
status.should.equal 404
end
specify "doesn't return invalid responses" do
r = Rack::Response.new(["foo", "bar"], 201)
status, header, body = r.finish
str = ""; body.each { |part| str << part }
str.should.be.empty
header["Content-Type"].should.equal nil
lambda {
Rack::Response.new(Object.new)
}.should.raise(TypeError).
message.should =~ /String or iterable required/
end
end