Diff options
| author | Bryce Kwon <bryce@brycekwon.com> | |
|---|---|---|
| committer | Bryce Kwon <bryce@brycekwon.com> | |
| commit | ||
| parent | ||
| tree | ||
| download | ||
Report each preview server split case on its own
The five cases ran as one test, and their names reached the log only
with -v. python3 also ran under the trash directory's HOME, where a
version manager's shim may find no interpreter, and without -I, so
PYTHONPATH or the user's site packages could shadow a module.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/serve-split-check.py | 96 | |
| -rwxr-xr-x | tests/t0401-serve.sh | 34 |
2 files changed, 77 insertions, 53 deletions
diff --git a/tests/serve-split-check.py b/tests/serve-split-check.py index 3a74803..8a41b5b 100644 --- a/tests/serve-split-check.py +++ b/tests/serve-split-check.py @@ -5,7 +5,7 @@ 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. +repository root and the name of one case. """ import importlib.util @@ -24,57 +24,53 @@ def load_serve(root): return module +# Each case names the raw output, then the status, headers and body it should +# split into. +CASES = { + "crlf-in-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", + ), + "lf-in-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", + ), + "status": ( + b"Status: 404 Not Found\nContent-Type: text/html\n\nnope", + 404, + [("Content-Type", "text/html")], + b"nope", + ), + "empty-body": ( + b"Status: 302 Found\nLocation: /x\n\n", + 302, + [("Location", "/x")], + b"", + ), + "no-blank-line": ( + b"Content-Type: text/html\n", + 200, + [("Content-Type", "text/html")], + b"", + ), +} + + 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"", - ), + root, name = sys.argv[1:] + raw, status, headers, body = CASES[name] + got = load_serve(root).split_cgi_output(raw) + if (got.status, got.headers, got.body) == (status, headers, body): + return 0 + sys.stderr.write( + "expected %r\ngot %r\n" + % ((status, headers, body), (got.status, got.headers, got.body)) ) - - 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 + return 1 sys.exit(main()) diff --git a/tests/t0401-serve.sh b/tests/t0401-serve.sh index 4a2d08b..ca15e1c 100755 --- a/tests/t0401-serve.sh +++ b/tests/t0401-serve.sh @@ -9,9 +9,16 @@ test_description='Check the preview server splits CGI output correctly' CGIT_TEST_NO_CREATE_REPOS=YesPlease . ./setup.sh +# A version manager's shim may find its interpreters through HOME, which the +# library has moved into the trash directory, so the interpreter is looked up +# once under the account's own. -I keeps the user's site packages and PYTHON +# variables out of the checks. +PYTHON=$(HOME=$USER_HOME python3 -I -c 'import sys; print(sys.executable)' 2>/dev/null) + # serve.py uses syntax that came with Python 3.8. test_lazy_prereq PYTHON ' - python3 -c "import sys; sys.exit(sys.version_info < (3, 8))" + test -n "$PYTHON" && + "$PYTHON" -I -c "import sys; sys.exit(sys.version_info < (3, 8))" ' test_have_prereq PYTHON || { @@ -19,8 +26,29 @@ test_have_prereq PYTHON || { test_done } -test_expect_success 'CGI headers and body are split at the first blank line' ' - python3 "$TEST_OUTPUT_DIRECTORY/serve-split-check.py" "$TEST_OUTPUT_DIRECTORY/.." +split_check() { + "$PYTHON" -I "$TEST_OUTPUT_DIRECTORY/serve-split-check.py" \ + "$TEST_OUTPUT_DIRECTORY/.." "$1" +} + +test_expect_success 'a CRLF blank line in the body stays in the body' ' + split_check crlf-in-body +' + +test_expect_success 'an LF blank line after CRLF headers stays in the body' ' + split_check lf-in-body +' + +test_expect_success 'a status line is taken off the headers' ' + split_check status +' + +test_expect_success 'an empty body survives, as a redirect leaves one' ' + split_check empty-body +' + +test_expect_success 'output with no blank line at all is all headers' ' + split_check no-blank-line ' test_done |
