2006-06-30 16:42:12 -04:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
#
|
|
|
|
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
|
|
|
# for more information.
|
2006-05-21 10:46:42 -04:00
|
|
|
|
2006-01-28 14:34:12 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'mongrel'
|
2006-03-19 18:31:30 -05:00
|
|
|
require 'benchmark'
|
2006-01-28 14:34:12 -05:00
|
|
|
|
|
|
|
include Mongrel
|
|
|
|
|
|
|
|
class ResponseTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_response_headers
|
|
|
|
out = StringIO.new
|
|
|
|
resp = HttpResponse.new(out)
|
|
|
|
resp.status = 200
|
|
|
|
resp.header["Accept"] = "text/plain"
|
|
|
|
resp.header["X-Whatever"] = "stuff"
|
2006-01-28 15:27:34 -05:00
|
|
|
resp.body.write("test")
|
2006-01-28 14:34:12 -05:00
|
|
|
resp.finished
|
|
|
|
|
2006-01-28 15:27:34 -05:00
|
|
|
assert out.length > 0, "output didn't have data"
|
2006-01-28 14:34:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_response_200
|
|
|
|
io = StringIO.new
|
|
|
|
resp = HttpResponse.new(io)
|
|
|
|
resp.start do |head,out|
|
|
|
|
head["Accept"] = "text/plain"
|
|
|
|
out.write("tested")
|
|
|
|
out.write("hello!")
|
|
|
|
end
|
2006-03-26 18:57:11 -05:00
|
|
|
|
|
|
|
resp.finished
|
2006-01-28 15:27:34 -05:00
|
|
|
assert io.length > 0, "output didn't have data"
|
2006-01-28 14:34:12 -05:00
|
|
|
end
|
|
|
|
|
2006-11-24 22:53:09 -05:00
|
|
|
def test_response_duplicate_header_squash
|
|
|
|
io = StringIO.new
|
|
|
|
resp = HttpResponse.new(io)
|
|
|
|
resp.start do |head,out|
|
|
|
|
head["Content-Length"] = 30
|
|
|
|
head["Content-Length"] = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
resp.finished
|
|
|
|
|
|
|
|
assert_equal io.length, 95, "too much output"
|
|
|
|
end
|
|
|
|
|
2006-12-15 23:26:54 -05:00
|
|
|
|
|
|
|
def test_response_some_duplicates_allowed
|
|
|
|
allowed_duplicates = ["Set-Cookie", "Set-Cookie2", "Warning", "WWW-Authenticate"]
|
|
|
|
io = StringIO.new
|
|
|
|
resp = HttpResponse.new(io)
|
|
|
|
resp.start do |head,out|
|
|
|
|
allowed_duplicates.each do |dup|
|
|
|
|
10.times do |i|
|
|
|
|
head[dup] = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
resp.finished
|
|
|
|
|
|
|
|
assert_equal io.length, 734, "wrong amount of output"
|
|
|
|
end
|
|
|
|
|
2006-01-28 14:34:12 -05:00
|
|
|
def test_response_404
|
|
|
|
io = StringIO.new
|
|
|
|
|
|
|
|
resp = HttpResponse.new(io)
|
|
|
|
resp.start(404) do |head,out|
|
|
|
|
head['Accept'] = "text/plain"
|
|
|
|
out.write("NOT FOUND")
|
|
|
|
end
|
|
|
|
|
2006-03-26 18:57:11 -05:00
|
|
|
resp.finished
|
2006-01-28 15:27:34 -05:00
|
|
|
assert io.length > 0, "output didn't have data"
|
2006-01-28 14:34:12 -05:00
|
|
|
end
|
|
|
|
|
2006-04-10 20:00:52 -04:00
|
|
|
def test_response_file
|
|
|
|
contents = "PLAIN TEXT\r\nCONTENTS\r\n"
|
|
|
|
require 'tempfile'
|
2006-05-16 01:06:22 -04:00
|
|
|
tmpf = Tempfile.new("test_response_file")
|
|
|
|
tmpf.binmode
|
2006-04-10 20:00:52 -04:00
|
|
|
tmpf.write(contents)
|
|
|
|
tmpf.rewind
|
2006-03-19 18:31:30 -05:00
|
|
|
|
2006-04-10 20:00:52 -04:00
|
|
|
io = StringIO.new
|
|
|
|
resp = HttpResponse.new(io)
|
|
|
|
resp.start(200) do |head,out|
|
|
|
|
head['Content-Type'] = 'text/plain'
|
|
|
|
resp.send_header
|
|
|
|
resp.send_file(tmpf.path)
|
|
|
|
end
|
|
|
|
io.rewind
|
|
|
|
tmpf.close
|
2006-05-16 00:51:00 -04:00
|
|
|
|
2006-04-10 20:00:52 -04:00
|
|
|
assert io.length > 0, "output didn't have data"
|
2006-05-03 01:06:30 -04:00
|
|
|
assert io.read[-contents.length..-1] == contents, "output doesn't end with file payload"
|
2006-04-10 20:00:52 -04:00
|
|
|
end
|
2007-10-11 03:22:37 -04:00
|
|
|
|
|
|
|
def test_response_with_custom_reason
|
|
|
|
reason = "You made a bad request"
|
|
|
|
io = StringIO.new
|
|
|
|
resp = HttpResponse.new(io)
|
|
|
|
resp.start(400, false, reason) { |head,out| }
|
|
|
|
resp.finished
|
|
|
|
|
|
|
|
io.rewind
|
|
|
|
assert_match(/.* #{reason}$/, io.readline.chomp, "wrong custom reason phrase")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_response_with_default_reason
|
|
|
|
code = 400
|
|
|
|
io = StringIO.new
|
|
|
|
resp = HttpResponse.new(io)
|
|
|
|
resp.start(code) { |head,out| }
|
|
|
|
resp.finished
|
|
|
|
|
|
|
|
io.rewind
|
|
|
|
assert_match(/.* #{HTTP_STATUS_CODES[code]}$/, io.readline.chomp, "wrong default reason phrase")
|
|
|
|
end
|
|
|
|
|
2006-01-28 14:34:12 -05:00
|
|
|
end
|
|
|
|
|