diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-07-19 10:28:21 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-07-19 10:50:12 +0300 |
commit | b2c24dd2def45f0f28c948163c61c16a45973b2b (patch) | |
tree | 49196448ddaaff0d2e095edd50cbbb02ad07725b | |
parent | afb4c816cc713e27cf44334ddce3608a345a4356 (diff) | |
download | ewww-b2c24dd2def45f0f28c948163c61c16a45973b2b.tar.gz |
refactor: extract functions for doing HTTP requests to its own module
-rw-r--r-- | ewww.md | 1 | ||||
-rw-r--r-- | ewww.py | 32 | ||||
-rw-r--r-- | ewww.yaml | 2 | ||||
-rw-r--r-- | http.py | 45 |
4 files changed, 49 insertions, 31 deletions
@@ -188,6 +188,7 @@ bindings: functions: - ewww.py - daemon.py + - http.py - runcmd.py classes: - scenario-disabled @@ -91,35 +91,7 @@ def stop_server(ctx): stop_daemon(ctx, "ewww") -# Make a HTTP request. +# Make an HTTP request. def request(ctx, method=None, url=None): url, host = _url(ctx, url) - print(url) - _run(ctx, ["curl", "-ksv", "-X", method, "-HHost: {}".format(host), url]) - _run_exit(ctx, 0) - - -# Check status code of latest HTTP request. -def status_code_is(ctx, code=None): - pattern = "\n< HTTP/2 {} ".format(code) - assert_eq(pattern in ctx["stderr"], True) - - -# Check a HTTP response header for latest request has a given value. -def http_header_is(ctx, header=None, value=None): - s = ctx["stderr"] - pattern = "\n< {}: {}".format(header, value) - 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") - if body != s: - print("stdout:", repr(s)) - prin("pattern:", repr(body)) - assert_eq(body, s) + http_request(ctx, host=host, method=method, url=url) @@ -15,7 +15,7 @@ function: fixme - then: I get status code {code} - function: status_code_is + function: http_status_code_is - then: 'header (?P<header>\S+) is "(?P<value>.+)"' regex: true @@ -0,0 +1,45 @@ +############################################################################# +# Some helpers to make HTTP requests and examine responses + +import json +import logging +import os +import random +import re +import shutil +import signal +import subprocess +import time +import urllib.parse + +import yaml + + +# Make an HTTP request. +def http_request(ctx, host=None, method=None, url=None): + logging.debug(f"Make HTTP request: {method} {url}") + runcmd(ctx, ["curl", "-ksv", "-X", method, f"-HHost: {host}", url]) + exit_code_is(ctx, 0) + + +# Check status code of latest HTTP request. +def http_status_code_is(ctx, code=None): + logging.debug(f"Verifying status code of previous HTTP request is {code}") + pattern = f"\n< HTTP/2 {code} " + assert_eq(pattern in ctx["stderr"], True) + + +# Check a HTTP response header for latest request has a given value. +def http_header_is(ctx, header=None, value=None): + logging.debug(f"Verifying response has header {header}: {value}") + s = ctx["stderr"] + pattern = f"\n< {header}: {value}" + 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): + logging.debug(f"Verifying response body is {body!r}") + s = ctx["stdout"] + body = body.encode("UTF8").decode("unicode-escape") + assert_eq(body, s) |