An API mocking tool
Project description
PyMoq is a tool for mocking HTTP services.
Installation
pip install PyMoq
Usage
By default PyMoq runs on 8080 TCP port, it can be changed:
mock = pymoq.Mock(port=8090)
Stub can be created by setting path and http method (GET is the default).
mock.create_stub('/books', method='post')
It is possible to set path as regex.
mock.create_stub('/books/[0-9]+', method='put')
For each stub a response can be configured with headers and http status code. Headers has to be dictionary with header name as its key.
mock.create_stub('/books', method='post').response('...', headers={'name': 'value'}, http_status=201)
Example test
import unittest import requests from pymoq import pymoq class ExampleUsageTestCase(unittest.TestCase): def test_direct_usage(self): content = '{"author": "John Doe", "title": "Lorem ipsum dolor sit amet", "id": 1}' headers = { 'content-type': 'application/json; charset=utf-8', 'location': 'http://localhost:8090/books/1' } mock = pymoq.Mock(port=8090) mock.create_stub('/books', method='post').response(content, headers=headers, http_status=201) with mock.run(): response = requests.post('http://localhost:8090/books', data={"author": "John Doe", "title": "Lorem ipsum dolor sit amet"}) self.assertEqual(response.status_code, 201) self.assertEqual(response.headers['content-type'], 'application/json; charset=utf-8') self.assertEqual(response.headers['location'], 'http://localhost:8090/books/1') self.assertEqual(response.text, content)
Configure with JSON file
A mock can be configured with json file. A file should looks like:
[{ "request": { "url": "/books", "method": "post" }, "response": { "content": {"author": "John Doe", "title": "Lorem ipsum dolor sit amet", "id": 1}, "headers": { "content-type": "application/json; charset=utf-8", "location": "http://localhost:8080/books/1" }, "httpStatus": 201 } }]
Example test
import unittest import requests from pymoq import pymoq class JsonConfigTestCase(unittest.TestCase): def test_load_config_from_file(self): mock = pymoq.Mock() mock.load('config.json') with mock.run(): response = requests.post('http://localhost:8080/books', data={"author": "John Doe", "title": "Lorem ipsum dolor sit amet"}) self.assertEqual(response.status_code, 201) self.assertEqual(response.headers['content-type'], 'application/json; charset=utf-8') self.assertEqual(response.headers['location'], 'http://localhost:8080/books/1') content = response.json() self.assertEqual(content['author'], 'John Doe') self.assertEqual(content['title'], 'Lorem ipsum dolor sit amet') self.assertEqual(content['id'], 1)
Request verifications
PyMoq can be used to validate requests.
Example test
import unittest import requests from pymoq import pymoq class RequestVerificationTestCase(unittest.TestCase): def test_request(self): mock = pymoq.Mock() stub = mock.create_stub('/books', method='post') with mock.run(): requests.post('http://localhost:8080/books', json={'firstName': 'John', 'lastName': 'Doe'} headers={'content-type': 'application/json'}) stub.assert_requested_once() stub.assert_requested_with_header('content-type', 'application/json') stub.assert_requested_body_contains('Doe')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
PyMoq-0.3.2.tar.gz
(4.7 kB
view details)
Built Distribution
PyMoq-0.3.2-py3-none-any.whl
(6.2 kB
view details)
File details
Details for the file PyMoq-0.3.2.tar.gz
.
File metadata
- Download URL: PyMoq-0.3.2.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 411cc8d63fa5868c801ee22a9e3a2064a019dcbe0b8352ef7f948457593344b4 |
|
MD5 | 01e4df936d7746772b6f5d9bdd662377 |
|
BLAKE2b-256 | 9b9dd22a6134373cda4233c01f3fe41f9e1ac3e57fe2069c9864d3ec9696c17e |
File details
Details for the file PyMoq-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: PyMoq-0.3.2-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0e8d19424f61667bdb8911a5eb91d8fb2810b337c6a31929f3485d19135f70d |
|
MD5 | 24dbd0ec89ba107037e36fad254166bf |
|
BLAKE2b-256 | 6fcd9c1a621e8a705b7f7f2a45ced9f463d7dbd5ce1f3717dea00bae23bb717e |