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"]])
20 assert out.length > 0, "output didn't have data"
23 def test_response_string_status
25 HttpResponse.write(out,['200', {}, []])
27 assert out.length > 0, "output didn't have data"
28 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
31 def test_response_OFS_set
35 HttpResponse.write(out,[200, {"X-k" => "cd","X-y" => "z"}, ["cool"]])
38 assert ! resp.include?("\f\v"), "output didn't use $, ($OFS)"
45 HttpResponse.write(io, [200, {}, []])
47 assert io.length > 0, "output didn't have data"
50 def test_response_with_default_reason
53 HttpResponse.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 HttpResponse.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 HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, []])
73 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
76 # we always favor the code returned by the application, since "Status"
77 # in the header hash is not allowed by Rack (but not every app is
78 # fully Rack-compliant).
79 def test_status_header_ignores_app_hash
81 header_hash = {"X-Whatever" => "stuff", 'StaTus' => "666" }
82 HttpResponse.write(out,[200, header_hash, []])
84 assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
85 assert_equal 1, out.string.split(/\r\n/).grep(/^Status:/i).size
89 expect_body = %w(1 2 3 4).join("\n")
90 body = StringIO.new(expect_body)
93 HttpResponse.write(out,[200, {}, body])
96 assert_match(expect_body, out.string.split(/\r\n/).last)
99 def test_unknown_status_pass_through
101 HttpResponse.write(out,["666 I AM THE BEAST", {}, [] ])
103 headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
104 assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
105 status = headers.grep(/\AStatus:/i).first
107 assert_equal "Status: 666 I AM THE BEAST", status