summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-07-19 10:28:21 +0300
committerLars Wirzenius <liw@liw.fi>2020-07-19 10:50:12 +0300
commitb2c24dd2def45f0f28c948163c61c16a45973b2b (patch)
tree49196448ddaaff0d2e095edd50cbbb02ad07725b
parentafb4c816cc713e27cf44334ddce3608a345a4356 (diff)
downloadewww-b2c24dd2def45f0f28c948163c61c16a45973b2b.tar.gz
refactor: extract functions for doing HTTP requests to its own module
-rw-r--r--ewww.md1
-rw-r--r--ewww.py32
-rw-r--r--ewww.yaml2
-rw-r--r--http.py45
4 files changed, 49 insertions, 31 deletions
diff --git a/ewww.md b/ewww.md
index 1623f1f..ed2f663 100644
--- a/ewww.md
+++ b/ewww.md
@@ -188,6 +188,7 @@ bindings:
functions:
- ewww.py
- daemon.py
+ - http.py
- runcmd.py
classes:
- scenario-disabled
diff --git a/ewww.py b/ewww.py
index 3eb6e97..76f27ac 100644
--- a/ewww.py
+++ b/ewww.py
@@ -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)
diff --git a/ewww.yaml b/ewww.yaml
index d1a13ab..08a13e4 100644
--- a/ewww.yaml
+++ b/ewww.yaml
@@ -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
diff --git a/http.py b/http.py
new file mode 100644
index 0000000..fd552a6
--- /dev/null
+++ b/http.py
@@ -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)