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'
14 class ResponseTest < Test::Unit::TestCase
15 include Unicorn::HttpResponse
18 before = Time.now.to_i - 1
20 assert_kind_of(String, str)
21 middle = Time.parse(str).to_i
23 assert before <= middle
24 assert middle <= after
27 def test_response_headers
29 http_response_write(out, 200, {"X-Whatever" => "stuff"}, ["cool"])
32 assert out.length > 0, "output didn't have data"
35 def test_response_string_status
37 http_response_write(out,'200', {}, [])
39 assert out.length > 0, "output didn't have data"
40 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
45 http_response_write(io, 200, {}, [])
47 assert io.length > 0, "output didn't have data"
50 def test_response_with_default_reason
53 http_response_write(io, code, {}, [])
55 lines = io.string.split(/\r\n/)
56 assert_match(/.* Bad Request$/, lines.first,
57 "wrong default reason phrase")
60 def test_rack_multivalue_headers
62 http_response_write(out,200, {"X-Whatever" => "stuff\nbleh"}, [])
64 assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string)
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
71 http_response_write(out,200, {"X-Whatever" => "stuff"}, [])
73 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
77 expect_body = %w(1 2 3 4).join("\n")
78 body = StringIO.new(expect_body)
81 http_response_write(out,200, {}, body)
84 assert_match(expect_body, out.string.split(/\r\n/).last)
87 def test_unknown_status_pass_through
89 http_response_write(out,"666 I AM THE BEAST", {}, [] )
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
95 assert_equal "Status: 666 I AM THE BEAST", status