test_request: tests esoteric/rare REQUEST_URIs
[unicorn.git] / test / unit / test_response.rb
blob203ae4d1e76fcb20dc724f8b9f9d3e80be392d23
1 # Copyright (c) 2005 Zed A. Shaw 
2 # You can redistribute it and/or modify it under the same terms as Ruby.
4 # Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html 
5 # for more information.
7 require 'test/test_helper'
9 include Unicorn
11 class ResponseTest < Test::Unit::TestCase
12   
13   def test_response_headers
14     out = StringIO.new
15     HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, ["cool"]])
16     assert out.closed?
18     assert out.length > 0, "output didn't have data"
19   end
21   def test_response_OFS_set
22     old_ofs = $,
23     $, = "\f\v"
24     out = StringIO.new
25     HttpResponse.write(out,[200, {"X-Whatever" => "stuff"}, ["cool"]])
26     assert out.closed?
27     resp = out.string
28     assert ! resp.include?("\f\v"), "output didn't use $, ($OFS)"
29     ensure
30       $, = old_ofs
31   end
33   def test_response_200
34     io = StringIO.new
35     HttpResponse.write(io, [200, {}, []])
36     assert io.closed?
37     assert io.length > 0, "output didn't have data"
38   end
40   def test_response_with_default_reason
41     code = 400
42     io = StringIO.new
43     HttpResponse.write(io, [code, {}, []])
44     assert io.closed?
45     lines = io.string.split(/\r\n/)
46     assert_match(/.* #{HTTP_STATUS_CODES[code]}$/, lines.first,
47                  "wrong default reason phrase")
48   end
50   def test_rack_multivalue_headers
51     out = StringIO.new
52     HttpResponse.write(out,[200, {"X-Whatever" => "stuff\nbleh"}, []])
53     assert out.closed?
54     assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string)
55   end
57   def test_body_closed
58     expect_body = %w(1 2 3 4).join("\n")
59     body = StringIO.new(expect_body)
60     body.rewind
61     out = StringIO.new
62     HttpResponse.write(out,[200, {}, body])
63     assert out.closed?
64     assert body.closed?
65     assert_match(expect_body, out.string.split(/\r\n/).last)
66   end
68 end