From b9a9084bc317368fed5ddbdccdf38ffb84e937e9 Mon Sep 17 00:00:00 2001 From: mohadese-yousefi <45990057+mohadese-yousefi@users.noreply.github.com> Date: Sat, 2 Mar 2019 15:36:05 +0330 Subject: [PATCH] Feature/mockup (#61) * added easycli moudal to bddrest * Edited import order! * Migrated to easycli (#51) * added easycli moudal to bddrest * Edited import order! * Deleted def main * added command mockupserver. * Add mockup controller * Add mockupserver controller * Resolve conflict * Resolved conflict * v2.2.1 * Beautiful is better ugly. * Beautiful is better ugly. * Fix a conflict in rebase * Fix a conflict in rebase * Repr function added to call * Repr function added to call * Fix some bug * Add mockup * Fix a conflict in rebase * Changed test in test_cli * Changed test in test_cli * Changed cli test * Changed cli test --- bddrest/mockupserver/__init__.py | 26 +++++++++++++++++++++++++- bddrest/mockupserver/controller.py | 14 -------------- bddrest/tests/test_cli.py | 17 +++++------------ requirements-ci.txt | 1 + requirements-dev.txt | 1 - setup.py | 3 ++- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/bddrest/mockupserver/__init__.py b/bddrest/mockupserver/__init__.py index afc3660..d1876af 100644 --- a/bddrest/mockupserver/__init__.py +++ b/bddrest/mockupserver/__init__.py @@ -1,7 +1,7 @@ import sys +import json from easycli import SubCommand, Argument -#from bddrest.mockupserver.controller import MockupController class MockupServer(SubCommand): @@ -16,7 +16,31 @@ class MockupServer(SubCommand): ] def __call__(self, args): + from nanohttp import Controller, HTTPNotFound, context from ..authoring import Story with open(args.story) as story_file: story = Story.load(story_file) + class RootMockupController(Controller): + + def __init__(self): + self.stories = [] + + def add_story(self, story): + self.stories.append(story) + + def server(self, call): + for k, v in call.response.headers: + context.response_headers.add_header(k, v) + yield call.response.text.encode() + + def __call__(self, *remaining_paths): + calls = [story.base_call] + story.calls + for call in calls: + url = call.url.replace(':', '') + if set(url.strip('/').split('/')) == set(remaining_paths) : + return self.server(call) + raise HTTPNotFound() + + from nanohttp import quickstart + quickstart(RootMockupController()) diff --git a/bddrest/mockupserver/controller.py b/bddrest/mockupserver/controller.py index e5fc23b..e69de29 100644 --- a/bddrest/mockupserver/controller.py +++ b/bddrest/mockupserver/controller.py @@ -1,14 +0,0 @@ -from nanohttp.controllers import Controller - - -class MockupController(Controller): - - def __init__(self): - self.stories = [] - - def add_story(self, story): - self.stories.append(story) - - def __call__(self, *remaining_paths): - raise NotImplementedError - diff --git a/bddrest/tests/test_cli.py b/bddrest/tests/test_cli.py index 1496512..9eed444 100644 --- a/bddrest/tests/test_cli.py +++ b/bddrest/tests/test_cli.py @@ -1,5 +1,5 @@ -from bddcli import Command, when, stdout, status, stderr, Application +from bddcli import Given, when, stdout, status, stderr, Application from bddrest.tests.helpers import standard_files_mockup @@ -8,21 +8,15 @@ def test_document_cli(): - with Command( - app, - 'Pass document for generate markdown', - stdin=yamlstory, - arguments=['document'] - ): - assert status == 0 + with Given(app, ['document']): + when(stdin=yamlstory) assert stderr == '' + assert status == 0 assert stdout == expected_markdown def test_help(): - with Command(app, 'Without any pramater for getting help.'): - assert status == 0 - assert stderr == '' + with Given(app): assert stdout == expected_help @@ -55,7 +49,6 @@ def test_help(): id: None ''' - expected_markdown = '''\ ## Posting a book diff --git a/requirements-ci.txt b/requirements-ci.txt index dd7b30e..90c4169 100644 --- a/requirements-ci.txt +++ b/requirements-ci.txt @@ -1,4 +1,5 @@ coverage coveralls pytest-cov +bddcli >= 2, < 3 diff --git a/requirements-dev.txt b/requirements-dev.txt index fd0e279..870b88e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,4 @@ -r requirements-ci.txt -bddcli pytest-pudb ipython diff --git a/setup.py b/setup.py index ad978ff..c88fce2 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,8 @@ 'pyyaml', 'argcomplete', 'pytest >= 4.0.2', - 'easycli' + 'easycli', + 'nanohttp' ]