filter exception messages with control characters
[unicorn.git] / test / unit / test_response.rb
blob1c2707acb140d0d9d2c804c5f0d870abbf7991b9
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'
10 require 'time'
12 include Unicorn
14 class ResponseTest < Test::Unit::TestCase
15   include Unicorn::HttpResponse
17   def test_httpdate
18     before = Time.now.to_i - 1
19     str = httpdate
20     assert_kind_of(String, str)
21     middle = Time.parse(str).to_i
22     after = Time.now.to_i
23     assert before <= middle
24     assert middle <= after
25   end
27   def test_response_headers
28     out = StringIO.new
29     http_response_write(out, 200, {"X-Whatever" => "stuff"}, ["cool"])
30     assert ! out.closed?
32     assert out.length > 0, "output didn't have data"
33   end
35   def test_response_string_status
36     out = StringIO.new
37     http_response_write(out,'200', {}, [])
38     assert ! out.closed?
39     assert out.length > 0, "output didn't have data"
40     assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
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     assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
74   end
76   def test_body_closed
77     expect_body = %w(1 2 3 4).join("\n")
78     body = StringIO.new(expect_body)
79     body.rewind
80     out = StringIO.new
81     http_response_write(out,200, {}, body)
82     assert ! out.closed?
83     assert body.closed?
84     assert_match(expect_body, out.string.split(/\r\n/).last)
85   end
87   def test_unknown_status_pass_through
88     out = StringIO.new
89     http_response_write(out,"666 I AM THE BEAST", {}, [] )
90     assert ! out.closed?
91     headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
92     assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
93     status = headers.grep(/\AStatus:/i).first
94     assert status
95     assert_equal "Status: 666 I AM THE BEAST", status
96   end
98 end