clortho

A very simple key/value server
git clone https://www.brianlane.com/git/clortho
Log | Files | Refs | README | LICENSE

commit d9b5a0f0572848e577c32838c55f70f3ba9128ba
parent 481fccdad2fea3696a76f4abed5f5f1c44d7f295
Author: Brian C. Lane <bcl@redhat.com>
Date:   Wed,  5 Apr 2017 12:09:46 -0700

Add testing of the clortho API

Diffstat:
A.travis.yml | 9+++++++++
Mrequirements.txt | 2++
Msrc/clortho.py | 5++++-
Asrc/clortho_test.py | 48++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml @@ -0,0 +1,9 @@ +language: python +python: + - "3.5" + - "3.6" + - "nightly" # currently points to 3.7-dev +# command to install dependencies +install: "pip install -r requirements.txt" +# command to run tests +script: pytest -v diff --git a/requirements.txt b/requirements.txt @@ -1 +1,3 @@ aiohttp +pytest +pytest-aiohttp diff --git a/src/clortho.py b/src/clortho.py @@ -88,13 +88,16 @@ async def set_key(request): return web.Response(text=text, status=status) -async def init(loop, host, port): +def setup_app(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/keystore/version', get_version) app.router.add_route('GET', '/keystore/info', show_info) app.router.add_route('GET', '/keystore/{key}', get_key) app.router.add_route('POST', '/keystore/{key}', set_key) + return app +async def init(loop, host, port): + app = setup_app(loop) srv = await loop.create_server(app.make_handler(), host, port) print("Server started at http://%s:%s" % (host, port)) return srv diff --git a/src/clortho_test.py b/src/clortho_test.py @@ -0,0 +1,48 @@ +#!/usr/bin/python3 + +import pytest +from aiohttp.test_utils import make_mocked_request + +from clortho import get_client, setup_app, VERSION + +@pytest.fixture +def cli(loop, test_client): + app = setup_app(loop) + return loop.run_until_complete(test_client(app)) + +async def test_version(cli): + resp = await cli.get("/keystore/version") + assert resp.status == 200 + assert await resp.text() == "version: %s" % VERSION + +async def test_post(cli): + resp = await cli.post("/keystore/test", data={"value": "test value"}) + assert resp.status == 200 + assert await resp.text() == "OK" + +async def test_get(cli): + resp = await cli.post("/keystore/test-get", data={"value": "test-get value"}) + assert resp.status == 200 + assert await resp.text() == "OK" + + resp = await cli.get("/keystore/test-get") + assert resp.status == 200 + assert await resp.text() == "test-get value" + +async def test_info(cli): + resp = await cli.get("/keystore/info") + assert resp.status == 200 + +def test_proxy_client(): + req = make_mocked_request("GET", "/keystore/version", headers={"X-Forwarded-For": "1.2.3.4, 5.6.7.8"}) + client = get_client(req) + assert client == "1.2.3.4" + +def test_client_peer(): + class MockTransport(object): + def get_extra_info(self, ignore): + return ("1.2.3.4", 65535) + + req = make_mocked_request("GET", "/keystore/version", transport=MockTransport()) + client = get_client(req) + assert client == "1.2.3.4"