diff options
Diffstat (limited to 'ewww.py')
-rw-r--r-- | ewww.py | 43 |
1 files changed, 32 insertions, 11 deletions
@@ -3,6 +3,7 @@ import json import os +import random import re import subprocess import time @@ -23,9 +24,16 @@ def _run(ctx, argv): # Check that latest call of _run ended with an specific exit code. def _run_exit(ctx, expected): + if ctx['exit'] != expected: + print('ctx:', ctx.as_dict()) assert_eq(ctx['exit'], expected) +# Name of Rust binary, debug-build. +def _binary(name): + return os.path.abspath(os.path.join(srcdir, 'target', 'debug', name)) + + # Write a file with given content. def _write(filename, content): open(filename, 'w').write(content) @@ -34,9 +42,10 @@ def _write(filename, content): # Construct a URL that points to server running on localhost by # replacing the actual scheme and host with ones that work for test. def _url(ctx, url): + port = ctx['config']['port'] c = urllib.parse.urlparse(url) host = c[1] - c = ('http', 'localhost:{}'.format(ctx['port'])) + c[2:] + c = ('http', 'localhost:{}'.format(port)) + c[2:] return urllib.parse.urlunparse(c), host @@ -57,15 +66,25 @@ def create_file(ctx, filename=None, content=None): # Start server using named configuration file. def start_server(ctx, filename=None): config = get_file(filename).decode('UTF-8') - ctx['config'] = yaml.safe_load(config) + config = yaml.safe_load(config) + config['port'] = random.randint(2000, 30000) + ctx['config'] = config + config = yaml.safe_dump(config) _write(filename, config) - pid = os.path.abspath('ewww.pid') - bin = os.path.join(srcdir, 'target', 'debug', 'ewww') - _run(ctx, ['/usr/sbin/daemonize', '-p', pid, bin, filename]) - ctx['pid'] = open(pid).read() - ctx['port'] = 3030 + + _run(ctx, [ + '/usr/sbin/daemonize', + '-c', os.getcwd(), + '-p', 'ewww.pid', + '-o', 'ewww.stdout', + '-e', 'ewww.stderr', + _binary('ewww'), + filename + ]) _run_exit(ctx, 0) + ctx['pid'] = open('ewww.pid').read() + # Make a HTTP request. def request(ctx, method=None, url=None): url, host = _url(ctx, url) @@ -81,14 +100,16 @@ def status_code_is(ctx, code=None): def http_header_is(ctx, header=None, value=None): s = ctx['stderr'] pattern = '\n< {}: {}'.format(header, value) - print('stderr:', repr(s)) - print('pattern:', repr(pattern)) + if pattern not in s: + print('stderr:', repr(s)) + print('pattern:', repr(pattern)) assert_eq(pattern in s, True) # Check a HTTP body response for latest request has a given value. def http_body_is(ctx, body=None): s = ctx['stdout'] body = body.encode('UTF8').decode('unicode-escape') - print('stdout:', repr(s)) - print('pattern:', repr(body)) + if body != s: + print('stdout:', repr(s)) + prin('pattern:', repr(body)) assert_eq(body, s) |