Skip to content

Commit

Permalink
better usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
nyrkovalex committed Jun 9, 2016
1 parent b15ac0f commit 0acc329
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions README.rst
Expand Up @@ -2,19 +2,39 @@ Httpsrv
=======

Simple http server for API mocking during automated testing
Plays nicely with httpsrvvcr_ library for automated request recording


Example usage
-------------

A typical usage pattern would probably look like the one below.

Using requests_ library::

>>> import requests
>>> from httpsrv import Server
>>> server = Server(8080).start()
>>> server.on('GET', '/').text('hello')
>>> res = requests.get('http://localhost:8080')
>>> assert res.text == 'hello'
import unittest
import requests
from httpsrv import Server

server = Server(8080).start()

class MyTestCase(unittest.TestCase):
def setUp(self):
server.reset()

def test_should_get_hello(self):
# this means that server will respond once upon GET request
# further GET requests on this path will get 500
server.on('GET', '/').text('hello')
res = requests.get('http://localhost:8080')
assert res.text == 'hello'

def test_should_always_respond_to_options(self):
# this means that any OPTIONS request will get status 200
# such behavior is particulary useful when mocking preflight queries
server.always('OPTIONS').status(200)
res = requests.get('http://localhost:8080')
assert res.status_code == 200


Installation
Expand All @@ -25,10 +45,11 @@ Installation
pip install httpsrv


.. _requests: http://docs.python-requests.org/en/master/


Documentation
-------------

http://httpsrv.readthedocs.org


.. _requests: http://docs.python-requests.org/en/master/
.. _httpsrvvcr: https://httpsrvvcr.readthedocs.io/

0 comments on commit 0acc329

Please sign in to comment.