Mock web server for testing web clients
Project description
This tool provides an in-process WSGI server on an ephemeral port. It is intended for use in unit tests, when the system under test makes outgoing HTTP connections that cannot easily be mocked.
Usage
First, create a WSGI application that represents the fake web server you want to create. This is simpler than it seems; for example:
def simple_app(environ, start_response): status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) return ['Hello, world!\n']
Next, activate the test server. There are several ways to do this, but all of them produce a port number.
As a context manager:
from mockweb import mock_server def test_web_request(): with mock_server(simple_app) as port: my_client.get_greeting('http://127.0.0.1:{}'.format(port))
As a decorator:
from mockweb import mock_server @mock_server(simple_app) def test_web_request(port): my_client.get_greeting('http://127.0.0.1:{}'.format(port))
Or manually started and stopped:
from mockweb import mock_server @mock_server def test_web_request(): server = mock_server(simple_app) # ... server.start() my_client.get_greeting('http://127.0.0.1:{}'.format(port)) server.stop()
In the latter case, be careful to stop the server.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file webmock-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: webmock-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a56dc2c62d4c0748007f5865ac4d5bf5c8a1b8e8f8d850e103da266e26dfbea2 |
|
MD5 | 417b2ee786186c7c2f4b9d9a2bacca2d |
|
BLAKE2b-256 | 89ed24f950c5f23c6550e01f05cfb14d497af4a78821e846449700b2ef02a1ce |