reflect changes in Rack::Utils::HTTP_STATUS_CODES
[unicorn.git] / test / unit / test_response.rb
blob1e9d74a86f9d7642ee1c8722b75617d4d5a31d27
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 git history
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   end
43   def test_response_200
44     io = StringIO.new
45     http_response_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     http_response_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     http_response_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     http_response_write(out,200, {"X-Whatever" => "stuff"}, [])
72     assert ! out.closed?
73   end
75   def test_unknown_status_pass_through
76     out = StringIO.new
77     http_response_write(out,"666 I AM THE BEAST", {}, [] )
78     assert ! out.closed?
79     headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
80     assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
81   end
83   def test_modified_rack_http_status_codes_late
84     r, w = IO.pipe
85     pid = fork do
86       r.close
87       # Users may want to globally override the status text associated
88       # with an HTTP status code in their app.
89       Rack::Utils::HTTP_STATUS_CODES[200] = "HI"
90       http_response_write(w, 200, {}, [])
91       w.close
92     end
93     w.close
94     assert_equal "HTTP/1.1 200 HI\r\n", r.gets
95     r.read # just drain the pipe
96     pid, status = Process.waitpid2(pid)
97     assert_predicate status, :success?
98   ensure
99     r.close
100     w.close unless w.closed?
101   end