From 6c5153b4feea64f588a6cff65aa1630278476983 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sat, 29 Jun 2019 16:18:09 +0100 Subject: [PATCH] test for server --- .circleci/config.yml | 1 + server/grasp_server.py | 47 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7288d44..3f49f1d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -58,6 +58,7 @@ jobs: # TODO preserve apt cache.. - run: sudo apt install python3-pip - run: python3 -m pip install --user mypy pylint pytest + - run: sudo apt install httpie # only used for tests - run: name: pylint diff --git a/server/grasp_server.py b/server/grasp_server.py index 2e5d5c0..df2530d 100755 --- a/server/grasp_server.py +++ b/server/grasp_server.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 import argparse from http.server import BaseHTTPRequestHandler, HTTPServer import json @@ -122,6 +122,7 @@ def run(port: str, capture_path: str, template: str): logger.info(f"Starting httpd on port {port}") httpd.serve_forever() + def setup_parser(p): p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension') p.add_argument('--path', type=str, default='~/capture.org', help='File to capture into') @@ -129,6 +130,50 @@ def setup_parser(p): {as_org.__doc__} """) + +def test_server(tmp_path: Path): + from subprocess import Popen, check_call + from time import sleep + from contextlib import contextmanager + + # ugh. + @contextmanager + def killmepls(*args, **kwargs): + with Popen(*args, **kwargs) as p: + try: + yield p + finally: + p.kill() + + + cfile = tmp_path / 'test-capture.org' + PORT = '17777' + + def send(url: str, title: str): + # TODO eh, not sure if there is anything easier than httpie + check_call([ + 'http', '--ignore-stdin', 'POST', f'http://localhost:{PORT}', + f'url={url}', + f'title={title}', + 'selection=null', + 'comment=null', + 'tag_str=null', + ]) + + + with killmepls([ + __file__, '--port', PORT, '--path', cfile, + ]) as s: + 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() + + def main(): logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')