From 67a3e4bcd9b6d79f5fec75f946bd48d20f469a03 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 3 May 2020 09:12:41 +0300 Subject: Change: force scheme, host in request URLs to point at test server --- ewww.md | 3 ++- ewww.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ewww.md b/ewww.md index fa0aa4f..7d2ed8d 100644 --- a/ewww.md +++ b/ewww.md @@ -94,7 +94,8 @@ then I get status code 200 ~~~ ~~~{#minimal.yaml .file .yaml} -{} +hosts: + - example.com ~~~ diff --git a/ewww.py b/ewww.py index 19511fe..a7ff930 100644 --- a/ewww.py +++ b/ewww.py @@ -6,6 +6,7 @@ import os import re import subprocess import time +import urllib.parse import yaml @@ -30,6 +31,15 @@ def _write(filename, content): open(filename, 'w').write(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): + c = urllib.parse.urlparse(url) + host = c[1] + c = ('http', 'localhost:{}'.format(ctx['port'])) + c[2:] + return urllib.parse.urlunparse(c), host + + ############################################################################# # The actual step functions. @@ -40,15 +50,19 @@ def fixme(*args, **kwargs): # Start server using named configuration file. def start_server(ctx, filename=None): - _write(filename, get_file(filename).decode('UTF-8')) + config = get_file(filename).decode('UTF-8') + ctx['config'] = yaml.safe_load(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_exit(ctx, 0) def request(ctx, method=None, url=None): - _run(ctx, ['curl', '-sv', '-X', method, url]) + url, host = _url(ctx, url) + _run(ctx, ['curl', '-sv', '-X', method, '-HHost: {}'.format(host), url]) _run_exit(ctx, 0) def status_code_is(ctx, code=None): -- cgit v1.2.1