Permalink
Newer
Older
100644 44 lines (36 sloc) 1.69 KB
1
require 'rack/mock'
2
require 'rack/content_length'
3
4
context "Rack::ContentLength" do
5
specify "sets Content-Length on String bodies if none is set" do
6
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
7
response = Rack::ContentLength.new(app).call({})
8
response[1]['Content-Length'].should.equal '13'
9
end
10
11
specify "sets Content-Length on Array bodies if none is set" do
12
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
13
response = Rack::ContentLength.new(app).call({})
14
response[1]['Content-Length'].should.equal '13'
15
end
16
17
specify "does not set Content-Length on variable length bodies" do
18
body = lambda { "Hello World!" }
19
def body.each ; yield call ; end
20
21
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
22
response = Rack::ContentLength.new(app).call({})
23
response[1]['Content-Length'].should.be.nil
24
end
25
26
specify "does not change Content-Length if it is already set" do
27
app = lambda { |env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '1'}, "Hello, World!"] }
28
response = Rack::ContentLength.new(app).call({})
29
response[1]['Content-Length'].should.equal '1'
30
end
31
32
specify "does not set Content-Length on 304 responses" do
33
app = lambda { |env| [304, {'Content-Type' => 'text/plain'}, []] }
34
response = Rack::ContentLength.new(app).call({})
35
response[1]['Content-Length'].should.equal nil
36
end
37
38
specify "does not set Content-Length when Transfer-Encoding is chunked" do
39
app = lambda { |env| [200, {'Transfer-Encoding' => 'chunked'}, []] }
40
response = Rack::ContentLength.new(app).call({})
41
response[1]['Content-Length'].should.equal nil
42
end
43
end