tee_input: update documentation for Rack 1.2
[unicorn.git] / test / unit / test_response.rb
blobf9eda8e35ec3068d34f28c5db3171ed0ae6955bd
1 # -*- encoding: binary -*-
3 # Copyright (c) 2005 Zed A. Shaw 
4 # You can redistribute it and/or modify it under the same terms as Ruby.
6 # Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html 
7 # for more information.
9 require 'test/test_helper'
11 include Unicorn
13 class ResponseTest < Test::Unit::TestCase
14   
15   def test_response_headers
16     out = StringIO.new
17     HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, ["cool"]])
18     assert out.closed?
20     assert out.length > 0, "output didn't have data"
21   end
23   def test_response_string_status
24     out = StringIO.new
25     HttpResponse.write(out,['200', {}, []])
26     assert out.closed?
27     assert out.length > 0, "output didn't have data"
28     assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
29   end
31   def test_response_OFS_set
32     old_ofs = $,
33     $, = "\f\v"
34     out = StringIO.new
35     HttpResponse.write(out,[200, {"X-k" => "cd","X-y" => "z"}, ["cool"]])
36     assert out.closed?
37     resp = out.string
38     assert ! resp.include?("\f\v"), "output didn't use $, ($OFS)"
39     ensure
40       $, = old_ofs
41   end
43   def test_response_200
44     io = StringIO.new
45     HttpResponse.write(io, [200, {}, []])
46     assert io.closed?
47     assert io.length > 0, "output didn't have data"
48   end
50   def test_response_with_default_reason
51     code = 400
52     io = StringIO.new
53     HttpResponse.write(io, [code, {}, []])
54     assert io.closed?
55     lines = io.string.split(/\r\n/)
56     assert_match(/.* Bad Request$/, lines.first,
57                  "wrong default reason phrase")
58   end
60   def test_rack_multivalue_headers
61     out = StringIO.new
62     HttpResponse.write(out,[200, {"X-Whatever" => "stuff\nbleh"}, []])
63     assert out.closed?
64     assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string)
65   end
67   # Even though Rack explicitly forbids "Status" in the header hash,
68   # some broken clients still rely on it
69   def test_status_header_added
70     out = StringIO.new
71     HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, []])
72     assert out.closed?
73     assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
74   end
76   # we always favor the code returned by the application, since "Status"
77   # in the header hash is not allowed by Rack (but not every app is
78   # fully Rack-compliant).
79   def test_status_header_ignores_app_hash
80     out = StringIO.new
81     header_hash = {"X-Whatever" => "stuff", 'StaTus' => "666" }
82     HttpResponse.write(out,[200, header_hash, []])
83     assert out.closed?
84     assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
85     assert_equal 1, out.string.split(/\r\n/).grep(/^Status:/i).size
86   end
88   def test_body_closed
89     expect_body = %w(1 2 3 4).join("\n")
90     body = StringIO.new(expect_body)
91     body.rewind
92     out = StringIO.new
93     HttpResponse.write(out,[200, {}, body])
94     assert out.closed?
95     assert body.closed?
96     assert_match(expect_body, out.string.split(/\r\n/).last)
97   end
99   def test_unknown_status_pass_through
100     out = StringIO.new
101     HttpResponse.write(out,["666 I AM THE BEAST", {}, [] ])
102     assert out.closed?
103     headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
104     assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
105     status = headers.grep(/\AStatus:/i).first
106     assert status
107     assert_equal "Status: 666 I AM THE BEAST", status
108   end