From bcdcea2e06a171e77a88770875ef6b91d7ab3ca3 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 9 May 2015 08:51:52 +0000 Subject: support for Rack::TempfileReaper middleware Rack::TempfileReaper was added in rack 1.6 to cleanup temporary files. Make Yahns::TmpIO ducktype-compatible and put it into env['rack.tempfiles'] array so Rack::TempfileReaper may be used to free up space used by temporary buffer files. ref: commit 3bdf5481e49d76b4502c51e5bdd93f68bfd1f0b4 in unicorn --- test/test_input.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_input.rb b/test/test_input.rb index fe09a9a..63cf6ce 100644 --- a/test/test_input.rb +++ b/test/test_input.rb @@ -11,13 +11,28 @@ class TestInput < Testcase MD5 = lambda do |e| input = e["rack.input"] + tmp = e["rack.tempfiles"] + case input + when StringIO, Yahns::StreamInput + abort "unexpected tempfiles" if tmp && tmp.include?(input) + when Yahns::TmpIO + abort "rack.tempfiles missing" unless tmp + abort "rack.tempfiles missing rack.input" unless tmp.include?(input) + else + abort "unrecognized input type: #{input.class}" + end + buf = "" md5 = Digest::MD5.new while input.read(16384, buf) md5 << buf end body = md5.hexdigest - h = { "Content-Length" => body.size.to_s, "Content-Type" => 'text/plain' } + h = { + "Content-Length" => body.size.to_s, + "Content-Type" => 'text/plain', + "X-Input-Class" => input.class.to_s, + } [ 200, h, [body] ] end @@ -63,6 +78,40 @@ class TestInput < Testcase [ host, port, pid ] end + def test_big_buffer_true + host, port, pid = input_server(MD5, true) + + c = get_tcp_client(host, port) + buf = 'hello' + c.write "PUT / HTTP/1.0\r\nContent-Length: 5\r\n\r\n#{buf}" + head, body = c.read.split(/\r\n\r\n/) + assert_match %r{^X-Input-Class: StringIO\r\n}, head + assert_equal Digest::MD5.hexdigest(buf), body + c.close + + c = get_tcp_client(host, port) + buf = 'hello' * 10000 + c.write "PUT / HTTP/1.0\r\nContent-Length: 50000\r\n\r\n#{buf}" + head, body = c.read.split(/\r\n\r\n/) + + # TODO: shouldn't need CapInput with known Content-Length... + assert_match %r{^X-Input-Class: Yahns::(CapInput|TmpIO)\r\n}, head + assert_equal Digest::MD5.hexdigest(buf), body + c.close + + c = get_tcp_client(host, port) + c.write "PUT / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n" + c.write "Transfer-Encoding: chunked\r\n\r\n" + c.write "#{50000.to_s(16)}\r\n#{buf}\r\n0\r\n\r\n" + head, body = c.read.split(/\r\n\r\n/) + assert_match %r{^X-Input-Class: Yahns::CapInput\r\n}, head + assert_equal Digest::MD5.hexdigest(buf), body + c.close + + ensure + quit_wait(pid) + end + def test_read_negative_lazy; _read_neg(:lazy); end def test_read_negative_nobuffer; _read_neg(false); end -- cgit v1.2.3-24-ge0c7