configurator: validate :reuseport for boolean-ess
[unicorn.git] / test / unit / test_response.rb
blob85ac085c1e06e93e134842bb09a6315d8acc3d5d
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 1.8 or
5 # the GPLv2+ (GPLv3+ preferred)
7 # Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html 
8 # for more information.
10 require 'test/test_helper'
11 require 'time'
13 include Unicorn
15 class ResponseTest < Test::Unit::TestCase
16   include Unicorn::HttpResponse
18   def test_httpdate
19     before = Time.now.to_i - 1
20     str = httpdate
21     assert_kind_of(String, str)
22     middle = Time.parse(str).to_i
23     after = Time.now.to_i
24     assert before <= middle
25     assert middle <= after
26   end
28   def test_response_headers
29     out = StringIO.new
30     http_response_write(out, 200, {"X-Whatever" => "stuff"}, ["cool"])
31     assert ! out.closed?
33     assert out.length > 0, "output didn't have data"
34   end
36   def test_response_string_status
37     out = StringIO.new
38     http_response_write(out,'200', {}, [])
39     assert ! out.closed?
40     assert out.length > 0, "output didn't have data"
41     assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
42   end
44   def test_response_200
45     io = StringIO.new
46     http_response_write(io, 200, {}, [])
47     assert ! io.closed?
48     assert io.length > 0, "output didn't have data"
49   end
51   def test_response_with_default_reason
52     code = 400
53     io = StringIO.new
54     http_response_write(io, code, {}, [])
55     assert ! io.closed?
56     lines = io.string.split(/\r\n/)
57     assert_match(/.* Bad Request$/, lines.first,
58                  "wrong default reason phrase")
59   end
61   def test_rack_multivalue_headers
62     out = StringIO.new
63     http_response_write(out,200, {"X-Whatever" => "stuff\nbleh"}, [])
64     assert ! out.closed?
65     assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string)
66   end
68   # Even though Rack explicitly forbids "Status" in the header hash,
69   # some broken clients still rely on it
70   def test_status_header_added
71     out = StringIO.new
72     http_response_write(out,200, {"X-Whatever" => "stuff"}, [])
73     assert ! out.closed?
74     assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
75   end
77   def test_body_closed
78     expect_body = %w(1 2 3 4).join("\n")
79     body = StringIO.new(expect_body)
80     body.rewind
81     out = StringIO.new
82     http_response_write(out,200, {}, body)
83     assert ! out.closed?
84     assert body.closed?
85     assert_match(expect_body, out.string.split(/\r\n/).last)
86   end
88   def test_unknown_status_pass_through
89     out = StringIO.new
90     http_response_write(out,"666 I AM THE BEAST", {}, [] )
91     assert ! out.closed?
92     headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
93     assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
94     status = headers.grep(/\AStatus:/i).first
95     assert status
96     assert_equal "Status: 666 I AM THE BEAST", status
97   end
99 end