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
|
|
|
|
|
|
|
|
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'
|
|
|
|
tmpf = Tempfile.new("test_response_file")
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2006-01-28 14:34:12 -05:00
|
|
|
end
|
|
|
|
|