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'
13 class ResponseTest < Test::Unit::TestCase
15 def test_response_headers
17 HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, ["cool"]])
19 assert out.length > 0, "output didn't have data"
22 def test_response_string_status
24 HttpResponse.write(out,['200', {}, []])
26 assert out.length > 0, "output didn't have data"
27 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
30 def test_response_OFS_set
34 HttpResponse.write(out,[200, {"X-k" => "cd","X-y" => "z"}, ["cool"]])
37 assert ! resp.include?("\f\v"), "output didn't use $, ($OFS)"
44 HttpResponse.write(io, [200, {}, []])
46 assert io.length > 0, "output didn't have data"
49 def test_response_with_default_reason
52 HttpResponse.write(io, [code, {}, []])
54 lines = io.string.split(/\r\n/)
55 assert_match(/.* Bad Request$/, lines.first,
56 "wrong default reason phrase")
59 def test_rack_multivalue_headers
61 HttpResponse.write(out,[200, {"X-Whatever" => "stuff\nbleh"}, []])
63 assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string)
66 # Even though Rack explicitly forbids "Status" in the header hash,
67 # some broken clients still rely on it
68 def test_status_header_added
70 HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, []])
72 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
75 # we always favor the code returned by the application, since "Status"
76 # in the header hash is not allowed by Rack (but not every app is
77 # fully Rack-compliant).
78 def test_status_header_ignores_app_hash
80 header_hash = {"X-Whatever" => "stuff", 'StaTus' => "666" }
81 HttpResponse.write(out,[200, header_hash, []])
83 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
84 assert_equal 1, out.string.split(/\r\n/).grep(/^Status:/i).size
88 expect_body = %w(1 2 3 4).join("\n")
89 body = StringIO.new(expect_body)
92 HttpResponse.write(out,[200, {}, body])
95 assert_match(expect_body, out.string.split(/\r\n/).last)
98 def test_unknown_status_pass_through
100 HttpResponse.write(out,["666 I AM THE BEAST", {}, [] ])
102 headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
103 assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
104 status = headers.grep(/\AStatus:/i).first
106 assert_equal "Status: 666 I AM THE BEAST", status