server test: use requests instead of httpie

This commit is contained in:
Dima Gerasimov 2022-12-21 01:06:38 +00:00 committed by karlicoss
parent 4bc8f01122
commit 6ab099a330

View file

@ -4,16 +4,10 @@ from subprocess import Popen, check_call
from time import sleep from time import sleep
# ugh. import requests
@contextmanager @contextmanager
def killmepls(*args, **kwargs):
with Popen(*args, **kwargs) as p:
try:
yield p
finally:
p.kill()
def grasp_test_server(capture_file: Path, port: str, template=None): def grasp_test_server(capture_file: Path, port: str, template=None):
server = str((Path(__file__).parent / 'grasp_server.py').absolute()) server = str((Path(__file__).parent / 'grasp_server.py').absolute())
cmdline = [ cmdline = [
@ -21,23 +15,29 @@ def grasp_test_server(capture_file: Path, port: str, template=None):
] ]
if template is not None: if template is not None:
cmdline.extend(['--template', template]) cmdline.extend(['--template', template])
return killmepls(cmdline) with Popen(cmdline) as p:
try:
yield p
finally:
p.kill()
def test_server(tmp_path: Path): def test_server(tmp_path: Path) -> None:
cfile = tmp_path / 'test-capture.org' cfile = tmp_path / 'test-capture.org'
PORT = '17890' PORT = '17890'
def send(url: str, title: str): def send(url: str, title: str):
# TODO eh, not sure if there is anything easier than httpie r = requests.post(
check_call([ f'http://localhost:{PORT}',
'http', '--ignore-stdin', 'POST', f'http://localhost:{PORT}', json = {
f'url={url}', 'url': url,
f'title={title}', 'title': title,
'selection=null', 'selection': None,
'comment=null', 'comment': None,
'tag_str=null', 'tag_str': None,
]) },
)
assert r.status_code == 200, r
with grasp_test_server(capture_file=cfile, port=PORT): with grasp_test_server(capture_file=cfile, port=PORT):