Skip to content

Commit

Permalink
Write test for mockup, #16
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrdad1373pedramfar committed Jun 27, 2018
1 parent 9a0d29d commit bc0ce3d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
21 changes: 21 additions & 0 deletions bddrest/mockup_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import contextlib
import threading
from wsgiref.simple_server import WSGIRequestHandler, WSGIServer


@contextlib.contextmanager
def http_server(app=None, handler_class=WSGIRequestHandler,
server_class=WSGIServer, bind=('localhost', 8082)):

server = server_class(bind, handler_class)
if app:
assert isinstance(server, WSGIServer)
server.set_app(app)
thread = threading.Thread(
target=server.serve_forever, name='sa-media test server.', daemon=True
)
thread.start()
url = f'http://{server.server_address[0]}:{server.server_address[1]}'
yield server, url
server.shutdown()
thread.join()
27 changes: 27 additions & 0 deletions bddrest/tests/test_mockup_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest

import requests
from nanohttp import Controller, html, Application

from bddrest.mockup_server import http_server


class App(Application):
def __init__(self):
super().__init__(root=Root())


class Root(Controller):
@html
def index(self):
return 'test'


class MockUpServerTestCase(unittest.TestCase):
with http_server(app=App(), bind=('localhost', 8087)) as (server, url):
response = requests.get(url)
assert response.content == b'test'


if __name__ == '__main__':
unittest.main()
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@


dependencies = [
'nanohttp',
'webtest',
'pyyaml',
'argcomplete'
'argcomplete',
'requests'
]


Expand Down

0 comments on commit bc0ce3d

Please sign in to comment.