diff options
| author | Bryce Kwon <bryce@brycekwon.com> | |
|---|---|---|
| committer | Bryce Kwon <bryce@brycekwon.com> | |
| commit | ||
| parent | ||
| tree | ||
| download | ||
Split preview server output at the first blank line
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/serve-split-check.py | 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -rwxr-xr-x | tests/t0300-serve.sh | 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 files changed, 96 insertions, 0 deletions
diff --git a/tests/serve-split-check.py b/tests/serve-split-check.py new file mode 100644 index 0000000..63c8f97 --- /dev/null +++ b/tests/serve-split-check.py @@ -0,0 +1,76 @@ +"""Check that tools/serve.py splits a CGI response at the right place. + +A CGI response ends its header block at the first blank line. cgit writes its +own headers with bare LF endings, so a body carrying a CRLF blank line, which +any commit message written on DOS does, must not be mistaken for that end. +Getting it wrong takes the document head into the header block, and the page +that reaches the browser then has no stylesheet and no chrome. Run with the +repository root as the only argument. +""" + +import importlib.util +import sys + + +def load_serve(root): + path = root + "/tools/serve.py" + spec = importlib.util.spec_from_file_location("serve", path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def main(): + split = load_serve(sys.argv[1]).split_cgi_output + cases = ( + ( + "bare LF headers, CRLF blank line inside the body", + b"Content-Type: text/html\n\n<!DOCTYPE html>\nmsg\r\n\r\ntail", + 200, + [("Content-Type", "text/html")], + b"<!DOCTYPE html>\nmsg\r\n\r\ntail", + ), + ( + "CRLF headers, LF blank line inside the body", + b"Content-Type: text/plain\r\nX: y\r\n\r\nline\n\nline2", + 200, + [("Content-Type", "text/plain"), ("X", "y")], + b"line\n\nline2", + ), + ( + "a status line is taken off the headers", + b"Status: 404 Not Found\nContent-Type: text/html\n\nnope", + 404, + [("Content-Type", "text/html")], + b"nope", + ), + ( + "an empty body survives, as a redirect leaves one", + b"Status: 302 Found\nLocation: /x\n\n", + 302, + [("Location", "/x")], + b"", + ), + ( + "output with no blank line at all is all headers", + b"Content-Type: text/html\n", + 200, + [("Content-Type", "text/html")], + b"", + ), + ) + + failed = 0 + for name, raw, status, headers, body in cases: + got = split(raw) + if (got.status, got.headers, got.body) == (status, headers, body): + continue + failed += 1 + sys.stderr.write( + "%s\n expected %r\n got %r\n" + % (name, (status, headers, body), (got.status, got.headers, got.body)) + ) + return 1 if failed else 0 + + +sys.exit(main()) diff --git a/tests/t0300-serve.sh b/tests/t0300-serve.sh new file mode 100755 index 0000000..513c33a --- /dev/null +++ b/tests/t0300-serve.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +# Checks how tools/serve.py, the preview server that runs the built binary and +# hands its output back over HTTP, tells the CGI headers from the body. That +# split once keyed on the wrong blank line and swallowed the document head, +# which left a page that still validated but arrived with no stylesheet and no +# chrome. The cases live in serve-split-check.py beside this file. + +test_description='Check the preview server splits CGI output correctly' +. ./setup.sh + +if ! command -v python3 >/dev/null 2>&1; then + test_done +fi + +test_expect_success 'CGI headers and body are split at the first blank line' ' + python3 "$PWD/../serve-split-check.py" "$PWD/../.." +' + +test_done |
