diff options
author | Eric Wong <e@80x24.org> | 2013-11-02 10:54:17 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-11-02 22:03:54 +0000 |
commit | 3192ec1b4054bcc228dfb88e57d5e1c828682a7b (patch) | |
tree | e7f7ac214f4a6e4ccdc1f1c109f49b555921a881 /test | |
parent | f78020396ac822c31f7f0b1a593bd3f58362a27a (diff) | |
download | yahns-3192ec1b4054bcc228dfb88e57d5e1c828682a7b.tar.gz |
These applications are what I'll be using to run on yahns on my personal server. Including them here will be helpful for me to find bugs. I've already found some, the following commits were directly the result of playing with these extras: * stream_file: only close FDs we opened ourselves * worker-less server should not waitpid indiscriminately * http: do not drop Content-Range from response headers
Diffstat (limited to 'test')
-rw-r--r-- | test/helper.rb | 2 | ||||
-rw-r--r-- | test/test_extras_exec_cgi.rb | 81 | ||||
-rwxr-xr-x | test/test_extras_exec_cgi.sh | 35 | ||||
-rw-r--r-- | test/test_extras_try_gzip_static.rb | 177 |
4 files changed, 294 insertions, 1 deletions
diff --git a/test/helper.rb b/test/helper.rb index 5e417ae..ae22e7f 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -16,7 +16,7 @@ GTL = Mutex.new # fork-aware coverage data gatherer, see also test/covshow.rb if ENV["COVERAGE"] require "coverage" - COVMATCH = %r{/lib/yahns\b.*rb\z} + COVMATCH = %r{(/lib/yahns\b|extras/).*rb\z} COVDUMPFILE = File.expand_path("coverage.dump") def __covmerge diff --git a/test/test_extras_exec_cgi.rb b/test/test_extras_exec_cgi.rb new file mode 100644 index 0000000..403925b --- /dev/null +++ b/test/test_extras_exec_cgi.rb @@ -0,0 +1,81 @@ +# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +require_relative 'server_helper' + +class TestExtrasExecCGI < Testcase + ENV["N"].to_i > 1 and parallelize_me! + include ServerHelper + alias setup server_helper_setup + alias teardown server_helper_teardown + + def test_exec_cgi + err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1] + runme = "#{Dir.pwd}/test/test_extras_exec_cgi.sh" + assert File.executable?(runme), "run test in project root" + pid = mkserver(cfg) do + require './extras/exec_cgi' + cfg.instance_eval do + app(:rack, ExecCgi.new(runme)) do + listen "#{host}:#{port}" + end + stderr_path err.path + end + end + + Timeout.timeout(30) do # we can chunk + c = get_tcp_client(host, port) + c.write "GET / HTTP/1.1\r\nConnection: close\r\n" \ + "Host: example.com\r\n\r\n" + head, body = c.read.split(/\r\n\r\n/, 2) + assert_match %r{^Transfer-Encoding: chunked\b}, head + assert_equal "5\r\nHIHI\n\r\n0\r\n\r\n", body + c.close + cerr = tmpfile(%w(curl .err)) + assert_equal "HIHI\n", `curl -sSfv 2>#{cerr.path} http://#{host}:#{port}/` + assert_match %r{\bTransfer-Encoding: chunked\b}, cerr.read + cerr.close! + end + + Timeout.timeout(30) do # do not chunk on clients who can't handle chunking + c = get_tcp_client(host, port) + c.write "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n" + head, body = c.read.split(/\r\n\r\n/) + assert_equal "HIHI\n", body + refute_match %r{^Transfer-Encoding: chunked\b}, head + c.close + end + + Timeout.timeout(30) do # sure env is sane + c = get_tcp_client(host, port) + c.write "GET /env\r\n\r\n" + head, body = c.read.split(/\r\n\r\n/) + assert_nil body + assert_match %r{^REQUEST_METHOD=GET$}, head + assert_match %r{^PATH_INFO=/env$}, head + assert_match %r{^QUERY_STRING=$}, head + c.close + end + + Timeout.timeout(30) do # known length should not chunk + c = get_tcp_client(host, port) + c.write "GET /known-length HTTP/1.1\r\nConnection: close\r\n" \ + "Host: example.com\r\n\r\n" + head, body = c.read.split(/\r\n\r\n/, 2) + refute_match %r{^Transfer-Encoding: chunked\b}, head + assert_match %r{^Content-Length: 5\b}, head + assert_equal "HIHI\n", body + c.close + end + + Timeout.timeout(30) do # 404 + c = get_tcp_client(host, port) + c.write "GET /not-found HTTP/1.0\r\n\r\n" + head, body = c.read.split(/\r\n\r\n/) + assert_match %r{\AHTTP/1\.1 404 Not Found}, head + assert_nil body + c.close + end + ensure + quit_wait(pid) + end +end diff --git a/test/test_extras_exec_cgi.sh b/test/test_extras_exec_cgi.sh new file mode 100755 index 0000000..e580773 --- /dev/null +++ b/test/test_extras_exec_cgi.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) + +# test CGI program, this remains portable POSIX shell (not bash) +set -e + +stdhead () { + echo Content-Type: text/plain + echo Status: 200 OK + echo +} + +case $PATH_INFO in +/) + stdhead + echo HIHI + ;; +/env) + stdhead + env + ;; +/known-length) + echo Content-Type: text/plain + echo Status: 200 OK + echo Content-Length: 5 + echo + echo HIHI + ;; +*) + echo Content-Type: text/plain + echo Status: 404 Not Found + echo + ;; +esac diff --git a/test/test_extras_try_gzip_static.rb b/test/test_extras_try_gzip_static.rb new file mode 100644 index 0000000..416af71 --- /dev/null +++ b/test/test_extras_try_gzip_static.rb @@ -0,0 +1,177 @@ +# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +require_relative 'server_helper' +require 'zlib' +require 'time' + +class TestExtrasTryGzipStatic < Testcase + ENV["N"].to_i > 1 and parallelize_me! + include ServerHelper + GPL_TEXT = IO.binread("COPYING").freeze + + def setup + @tmpdir = Dir.mktmpdir + server_helper_setup + end + + def teardown + server_helper_teardown + FileUtils.rm_rf @tmpdir + end + + def test_gzip_static + err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1] + tmpdir = @tmpdir + pid = mkserver(cfg) do + require './extras/try_gzip_static' + cfg.instance_eval do + app(:rack, TryGzipStatic.new(tmpdir)) do + listen "#{host}:#{port}" + end + stderr_path err.path + end + end + + begin # setup + gpl = "#{tmpdir}/COPYING" + gplgz = "#{tmpdir}/COPYING.gz" + FileUtils.cp("COPYING", gpl) + _, status = Process.waitpid2(fork do + File.open(gplgz, "w") do |fp| + Zlib::GzipWriter.wrap(fp.dup) { |io| io.write(GPL_TEXT) } + end + exit!(0) + end) + assert status.success?, status.inspect + st = File.stat(gpl) + gz_st = File.stat(gplgz) + assert_equal GPL_TEXT, `zcat #{gplgz}`, "Eric screwed up using zlib" + File.utime(st.atime, st.mtime, gplgz) + end + + check = lambda do |req, &blk| + c = get_tcp_client(host, port) + begin + c.write "#{req}\r\n\r\n" + head, body = c.read.split(/\r\n\r\n/) + blk.call(head) + body + ensure + c.close + end + end + + Timeout.timeout(30) do # basic tests + %w(GET HEAD).each do |m| + body = check.call("#{m} /COPYING HTTP/1.0") do |head| + refute_match %r{^Content-Encoding: gzip\b}, head + assert_match %r{^Content-Type: text/plain\b}, head + assert_match %r{^Content-Length: #{st.size}\b}, head + end + case m + when "GET" then assert_equal GPL_TEXT, body + when "HEAD" then assert_nil body + end + + req = "#{m} /COPYING HTTP/1.0\r\nAccept-Encoding: gzip" + body = check.call(req) do |head| + assert_match %r{^Content-Encoding: gzip\b}, head + assert_match %r{^Content-Type: text/plain\b}, head + assert_match %r{^Content-Length: #{gz_st.size}\b}, head + end + case m + when "GET" + assert_equal GPL_TEXT, Zlib::GzipReader.new(StringIO.new(body)).read + when "HEAD" then assert_nil body + end + end + end + + Timeout.timeout(30) do # range tests + %w(HEAD GET).each do |m| + req = "#{m} /COPYING HTTP/1.0\r\n" \ + "Range: bytes=5-46\r\nAccept-Encoding: gzip" + body = check.call(req) do |head| + assert_match %r{\AHTTP/1\.1 206 Partial Content\r\n}, head + refute_match %r{^Content-Encoding: gzip\b}, head + assert_match %r{^Content-Type: text/plain\b}, head + assert_match %r{^Content-Length: 42\b}, head + assert_match %r{^Content-Range: bytes 5-46/#{st.size}\r\n}, head + end + case m + when "GET" then assert_equal GPL_TEXT[5..46], body + when "HEAD" then assert_nil body + end + + req = "#{m} /COPYING HTTP/1.0\r\n" \ + "Range: bytes=66666666-\r\nAccept-Encoding: gzip" + body = check.call(req) do |head| + assert_match %r{^Content-Range: bytes \*/#{st.size}\r\n}, head + assert_match %r{\AHTTP/1\.1 416 }, head + end + assert_nil body + end + end + + Timeout.timeout(30) do # gzip counterpart is nonexistent + File.link(gpl, "#{gpl}.hardlink") + %w(GET HEAD).each do |m| + req = "#{m} /COPYING.hardlink HTTP/1.0\r\nAccept-Encoding: gzip" + body = check.call(req) do |head| + refute_match %r{^Content-Encoding: gzip\b}, head + assert_match %r{^Content-Type: text/plain\b}, head + assert_match %r{^Content-Length: #{st.size}\b}, head + end + case m + when "GET" then assert_equal GPL_TEXT, body + when "HEAD" then assert_nil body + end + end + end + + Timeout.timeout(30) do # If-Modified-Since + %w(GET HEAD).each do |m| + req = "#{m} /COPYING HTTP/1.0\r\n" \ + "If-Modified-Since: #{st.mtime.httpdate}" + body = check.call(req) do |head| + assert_match %r{\AHTTP/1\.1 304 Not Modified}, head + end + assert_nil body + end + end + + # skew the times of the gzip file, should now fail to use gzipped + Timeout.timeout(30) do + File.utime(Time.at(0), Time.at(0), gplgz) + + %w(GET HEAD).each do |m| + req = "#{m} /COPYING HTTP/1.0\r\nAccept-Encoding: gzip" + body = check.call(req) do |head| + refute_match %r{^Content-Encoding: gzip\b}, head + assert_match %r{^Content-Type: text/plain\b}, head + assert_match %r{^Content-Length: #{st.size}\b}, head + end + case m + when "GET" then assert_equal GPL_TEXT, body + when "HEAD" then assert_nil body + end + end + end + + Timeout.timeout(30) do # 404 + %w(GET HEAD).each do |m| + req = "#{m} /cp-ing HTTP/1.0\r\nAccept-Encoding: gzip" + body = check.call(req) do |head| + assert_match %r{HTTP/1\.1 404 }, head + end + assert_nil body + end + body = check.call("FOO /COPYING HTTP/1.0") do |head| + assert_match %r{HTTP/1\.1 405 }, head + end + assert_nil body + end + ensure + quit_wait(pid) + end +end |