Package: mockallan
Mockallan is a lightweight HTTP server mock for CI and testing environments.HighlightsCommand-line interface for CI and testing environments.Configurable default and per-endpoint responses.Assert expected requests performed by the system under test based on the endpoint and the request body.Match the request body in assertions based ontext/plain message matchingJSON message matchingJSON schema validationXML schema validationRegular expression matchingRequest history enables robust assertion capabilities.Concise codebase focusing on simplicity.API naming inspired by theMockclass from the Pythonunittest.mockstandard library.RequirementsPython >= 3.10InstallationMockallan is available onPyPI. Install it using pip.pipinstallmockallanGetting StartedRunmockallan.pypythonmockallan.py
Listeningon0.0.0.0:8080Run the system under test.You can usecurlto simulate a request performed by the system under test. For example, if we expect our system under test to perform aPOST /orders/order_e2b9/products, run the followingcurlcommand.curl-s-XPOSThttp://localhost:8080/orders/order_e2b9/products--data'{"product_id": "foo","description": "bar","amount": 1}'Mockallan will reply with the factory default response.{"status":"200","message":"This is mockallan factory default response."}Make assertions on the expected request.curl-XGET"http://localhost:8080/assert-called?method=POST&path=/orders/order_e2b9/products"If the assertion request returns 200 then everything went fine.{"status":200,"type":"assertion-success","title":"Assertion request GET /assert-called succeeded","detail":"POST /orders/order_e2b9/products called 1 times."}If it returns 409 then the assertion failed and the system under test did not behave as expected.{"status":409,"type":"assertion-error","title":"Assertion request GET /assert-called failed","detail":"Expected POST /orders/order_e2b9/products to be called 1 times. Called 0 times."}Using Configurable Stub ResponsesCreate a stub configuration JSON file or use thestub_config.jsonprovided in this repository.E.g.{"endpoints":[{"request":{"method":"POST","path":"/orders/order_e2b9/products"},"response":{"status_code":200,"headers":{"Content-type":"application/json"},"body":{"status":"200","message":"This is the configured response for POST /orders/order_e2b9/products"}}}]}Runmockallan.pyand provide the JSON file.pythonmockallan.py-cstub_config.jsonRun the system under test or simulate the request performed by it. The mock will reply with the configured response forPOST /orders/order_e2b9/products.Make assertions on the expected request.curl-XGET"http://localhost:8080/assert-called?method=POST&path=/orders/order_e2b9/products"If the assertion request returns 200 then everything went fine. If it returns 409 then the assertion failed and the system under test did not behave as expected.Using/assert-called-withAnd/assert-called-once-withLet's explore additional validation options with thePOST /assert-called-withandPOST /assert-called-once-withendpoints. The body message provided in these requests corresponds to atext/plainmessageJSON messageJSON schemaXML schemaor a regular expressionto match as shown in the following sections.JSON Schema Validation AssertionsAddContent-Type: application/schema+jsonto thePOST /assert-called-withorPOST /assert-called-once-withrequest and place the JSON schema message in the body.E.g.curl-XPOST--header'Content-Type: application/json+schema'\"http://localhost:8080/assert-called-with?method=POST&path=/orders/order_e2b9/products"\--data'{"$schema": "http://json-schema.org/draft-07/schema#","type": "object","properties": {"orderNumber": {"type": "string"},"products": {"type": "array","items": {"type": "object","properties": {"productId": {"type": "string"},"quantity": {"type": "integer","minimum": 1}},"required": ["productId", "quantity"]}}},"required": ["orderNumber", "products"]}'XML Schema Validation AssertionsAddContent-Type: application/xmlto thePOST /assert-called-withorPOST /assert-called-once-withrequest and place the XML schema message in the body.E.g.curl-XPOST--header'Content-Type: application/xml'\"http://localhost:8080/assert-called-with?method=POST&path=/orders/order_e2b9/products"\--data'<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="order"><xs:complexType><xs:sequence><xs:element name="orderNumber" type="xs:string"/><xs:element name="products" type="productListType"/></xs:sequence></xs:complexType></xs:element><xs:complexType name="productListType"><xs:sequence><xs:element name="product" type="productType" minOccurs="0" maxOccurs="unbounded"/></xs:sequence></xs:complexType><xs:complexType name="productType"><xs:sequence><xs:element name="productId" type="xs:string"/><xs:element name="quantity" type="xs:integer"/></xs:sequence></xs:complexType></xs:schema>'Regex Validation AssertionsAdd the custom headerX-Mockallan-Validator: regexto thePOST /assert-called-withorPOST /assert-called-once-withrequest and place the regular expression in the body.E.g.curl-XPOST--header'X-Mockallan-Validator: regex'\"http://localhost:8080/assert-called-with?method=POST&path=/orders/order_e2b9/products"\--data'{"orderNumber":"\w+","products":\[\{"productId":"\w+","quantity":\d+}(,\{"productId":"\w+","quantity":\d+\})*\]}'Stub Configuration APIThe Stub Configuration API allows the test client to configure the mock at runtime.MethodPathQuery ParamsRequest BodyStatusResponse BodyPUT/configure-JSON stub configuration204; 400-GET/configure--200JSON stub configurationAssertion APIThe Assertion API allows for the validation of expected requests.MethodPathQuery ParamsRequest BodyStatusResponse BodyGET/assert-calledmethod, path-200 OK; 400; 409Assertion success or error messageGET/assert-called-oncemethod, path-200 OK; 400; 409Assertion success or error messagePOST/assert-called-withmethod, pathJSON object, JSON schema, XML schema or regex200 OK; 400; 409Assertion success or error messagePOST/assert-called-once-withmethod, pathJSON object, JSON schema, XML schema, regex or message body200 OK; 400; 409Assertion success or error messageGET/request-body--200 OK; 409The request body that the mock was last called withGET/request-body-list--200 OKList of all the requests made to the mock in sequenceGET/request-count--200 OKRequest countNamingStub Configuration API and Assertion API naming are inspired by classMockfrom the standard python packageunittest.mock.ContributingFound a bug, facing some unexpected quirks, or got ideas for making this project better? Don't hesitate toraise an issue. Your help is appreciated.LicenseThis project is licensed under the terms of the MIT license.Related Projectsmockallan-docker- Containerized lightweight HTTP server mock.mockallan-python-client- Mockallan python client class.