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'
15 class ResponseTest < Test::Unit::TestCase
16 include Unicorn::HttpResponse
19 before = Time.now.to_i - 1
21 assert_kind_of(String, str)
22 middle = Time.parse(str).to_i
24 assert before <= middle
25 assert middle <= after
28 def test_response_headers
30 http_response_write(out, 200, {"X-Whatever" => "stuff"}, ["cool"])
33 assert out.length > 0, "output didn't have data"
36 def test_response_string_status
38 http_response_write(out,'200', {}, [])
40 assert out.length > 0, "output didn't have data"
41 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
46 http_response_write(io, 200, {}, [])
48 assert io.length > 0, "output didn't have data"
51 def test_response_with_default_reason
54 http_response_write(io, code, {}, [])
56 lines = io.string.split(/\r\n/)
57 assert_match(/.* Bad Request$/, lines.first,
58 "wrong default reason phrase")
61 def test_rack_multivalue_headers
63 http_response_write(out,200, {"X-Whatever" => "stuff\nbleh"}, [])
65 assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string)
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
72 http_response_write(out,200, {"X-Whatever" => "stuff"}, [])
74 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
78 expect_body = %w(1 2 3 4).join("\n")
79 body = StringIO.new(expect_body)
82 http_response_write(out,200, {}, body)
85 assert_match(expect_body, out.string.split(/\r\n/).last)
88 def test_unknown_status_pass_through
90 http_response_write(out,"666 I AM THE BEAST", {}, [] )
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
96 assert_equal "Status: 666 I AM THE BEAST", status