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'
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"
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"}, [])
75 def test_unknown_status_pass_through
77 http_response_write(out,"666 I AM THE BEAST", {}, [] )
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])
83 def test_modified_rack_http_status_codes_late
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, {}, [])
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?
100 w.close unless w.closed?