2019-06-29 11:01:22 -07:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from pathlib import Path
|
|
|
|
from subprocess import Popen, check_call
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
|
2022-12-20 17:06:38 -08:00
|
|
|
import requests
|
2019-06-29 11:01:22 -07:00
|
|
|
|
|
|
|
|
2022-12-20 17:06:38 -08:00
|
|
|
@contextmanager
|
2019-06-29 11:01:22 -07:00
|
|
|
def grasp_test_server(capture_file: Path, port: str, template=None):
|
|
|
|
server = str((Path(__file__).parent / 'grasp_server.py').absolute())
|
|
|
|
cmdline = [
|
|
|
|
server, '--port', port, '--path', str(capture_file),
|
|
|
|
]
|
|
|
|
if template is not None:
|
|
|
|
cmdline.extend(['--template', template])
|
2022-12-20 17:06:38 -08:00
|
|
|
with Popen(cmdline) as p:
|
|
|
|
try:
|
|
|
|
yield p
|
|
|
|
finally:
|
|
|
|
p.kill()
|
2019-06-29 11:01:22 -07:00
|
|
|
|
|
|
|
|
2022-12-20 17:06:38 -08:00
|
|
|
def test_server(tmp_path: Path) -> None:
|
2019-06-29 11:01:22 -07:00
|
|
|
cfile = tmp_path / 'test-capture.org'
|
2019-12-09 13:03:33 -08:00
|
|
|
PORT = '17890'
|
2019-06-29 11:01:22 -07:00
|
|
|
|
|
|
|
def send(url: str, title: str):
|
2022-12-20 17:06:38 -08:00
|
|
|
r = requests.post(
|
|
|
|
f'http://localhost:{PORT}',
|
|
|
|
json = {
|
|
|
|
'url': url,
|
|
|
|
'title': title,
|
|
|
|
'selection': None,
|
|
|
|
'comment': None,
|
|
|
|
'tag_str': None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert r.status_code == 200, r
|
2019-06-29 11:01:22 -07:00
|
|
|
|
|
|
|
|
|
|
|
with grasp_test_server(capture_file=cfile, port=PORT):
|
|
|
|
sleep(0.5) # startup
|
|
|
|
|
|
|
|
send(url='twitter.com', title='Twitter')
|
|
|
|
sleep(0.5)
|
|
|
|
|
|
|
|
assert 'twitter.com' in cfile.read_text()
|
|
|
|
# TODO er... wonder how nulls are converted into Nones..
|
|
|
|
# assert 'null' not in cfile.read_text()
|
|
|
|
|