API Reference¶
The mbtest.server module¶
- mbtest.server.mock_server(request, executable=PosixPath('node_modules/.bin/mb'), port=2525, *, timeout=5, debug=True, allow_injection=True, local_only=True, data_dir='.mbdb')[source]¶
Pytest fixture, making available a mock server, running one or more imposters, one for each domain being mocked.
Use in a pytest conftest.py fixture as follows:
@pytest.fixture(scope="session") def mock_server(request): return server.mock_server(request)
Test will look like:
def test_an_imposter(mock_server): imposter = Imposter(Stub(Predicate(path='/test'), Response(body='sausages')), record_requests=True) with mock_server(imposter) as s: r = requests.get(f"{imposter.url}/test") assert_that(r, is_response().with_status_code(200).and_body("sausages")) assert_that(s, had_request(path='/test', method="GET"))
- Parameters:
request (
FixtureRequest) – Request for a fixture from a test or fixture function.executable (
str|Path) – Alternate location for the Mountebank executable.port (
int) – Server port.timeout (
int) – specifies how long to wait for the Mountebank server to start.debug (
bool) – Start the server in debug mode, which records all requests. This needs to be True for thembtest.matchers.had_request()matcher to work.allow_injection (
bool) – Allow JavaScript injection. If True, local_only should also be True,as per Mountebank security.local_only (
bool) – Accept request only from localhost.data_dir (
str|None) – Persist all operations to disk, in this directory.
- Return type:
- Returns:
Mock server.
- class mbtest.server.MountebankServer(port, scheme='http', host='localhost', imposters_path='imposters')[source]¶
Allow addition of imposters to an already running Mountebank mock server.
Test will look like:
def test_an_imposter(mock_server): mb = MountebankServer(1234) imposter = Imposter(Stub(Predicate(path='/test'), Response(body='sausages')), record_requests=True) with mb(imposter): r = requests.get(f"{imposter.url}/test") assert_that(r, is_response().with_status_code(200).and_body("sausages")) assert_that(imposter, had_request(path='/test', method="GET"))
Imposters will be torn down when the with block is exited.
- Parameters:
- property server_url: URL¶
- query_all_imposters()[source]¶
Yield all imposters running on the server, including those defined elsewhere.
- get_replayable_imposter(imposter)[source]¶
Retrieve a replayable version of an imposter.
Calls
GET /imposters/{port}?replayable=true&removeProxies=true, which returns the imposter with all proxy responses captured as concrete stubs, ready to replay without an upstream service.
- class mbtest.server.ExecutingMountebankServer(executable=PosixPath('node_modules/.bin/mb'), port=2525, *, timeout=5, debug=True, allow_injection=True, local_only=True, data_dir='.mbdb')[source]¶
A Mountebank mock server, running one or more imposters, one for each domain being mocked.
Test will look like:
def test_an_imposter(mock_server): mb = ExecutingMountebankServer() imposter = Imposter(Stub(Predicate(path='/test'), Response(body='sausages')), record_requests=True) with mb(imposter) as s: r = requests.get(f"{imposter.url}/test") assert_that(r, is_response().with_status_code(200).and_body("sausages")) assert_that(s, had_request(path='/test', method="GET")) mb.close()
The mountebank server will be started when this class is instantiated, and needs to be closed if it’s not to be left running. Consider using the
mock_server()pytest fixture, which will take care of this for you.- Parameters:
executable (
str|Path) – Optional, alternate location for the Mountebank executable.port (
int) – Server port.timeout (
float) – How long to wait for the Mountebank server to start.debug (
bool) – Start the server in debug mode, which records all requests. This needs to be True for thembtest.matchers.had_request()matcher to work.allow_injection (
bool) –Allow JavaScript injection. If True, local_only should also be True,as per Mountebank security.
local_only (
bool) – Accept request only from localhost.data_dir (
str|None) – Persist all operations to disk, in this directory.
- start_lock = <unlocked _thread.lock object>¶
The mbtest.imposters.imposters module¶
- class mbtest.imposters.imposters.Imposter(stubs, port=None, *, protocol=Protocol.HTTP, name=None, default_response=None, key=None, cert=None, record_requests=True, mutual_auth=False, host=None, server_url=None)[source]¶
Represents a Mountebank imposter. Think of an imposter as a mock website, running a protocol, on a specific port. Required behaviors are specified using stubs.
- Parameters:
name (
str|None) – Imposter name - useful for interactive exploration of imposters on http://localhost:2525/impostersdefault_response (
HttpResponse|None) – The default response to send if no predicate matches.record_requests (
bool) – Record requests made against this imposter, so they can be asserted against later.mutual_auth (
bool) – Server will request a client certificate.
- class Protocol(*values)[source]¶
Imposter Protocol.
- HTTP = 'http'¶
- HTTPS = 'https'¶
- SMTP = 'smtp'¶
- TCP = 'tcp'¶
- default_response: HttpResponse | None = None¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- save(path)[source]¶
Save this imposter to a JSON file for later replay.
The saved file can be loaded with
from_file()and posted to a Mountebank server, or used directly with Mountebank’s--configfileoption.
- classmethod from_file(path)[source]¶
Load an imposter from a JSON file previously saved with
save().
- property configuration_url: URL¶
- query_all_stubs()[source]¶
Return all stubs running on the impostor, including those defined elsewhere.
- add_stubs(definition, index=None)[source]¶
Add one or more stubs to a running impostor.
- Return type:
- class mbtest.imposters.imposters.HttpRequest(method, path, query, headers, body=None)[source]¶
-
- property json: MutableMapping[str, str | int | float | bool | MutableSequence[JsonValue] | MutableMapping[str, JsonValue] | None] | None¶
The mbtest.imposters.stubs module¶
- class mbtest.imposters.stubs.Stub(predicates=None, responses=None)[source]¶
Represents a Mountebank stub. Think of a stub as a behavior, triggered by a matching predicate.
- Parameters:
predicates (
BasePredicate|Iterable[BasePredicate] |None) – Trigger this stub if one of these predicates matches the requestresponses (
BaseResponse|Iterable[BaseResponse] |None) – Use these response behaviors (in order)
- predicates: list[BasePredicate]¶
- responses: list[BaseResponse]¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.stubs.AddStub(stub=<factory>, index=None)[source]¶
Represents a Mountebank add stub request <http://localhost:2525/docs/api/overview#add-stub>. To add new stab to an existing imposter.
- Parameters:
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
The mbtest.imposters.predicates module¶
- class mbtest.imposters.predicates.BasePredicate[source]¶
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.predicates.Predicate(*, path=None, method=None, query=None, body=None, headers=None, xpath=None, jsonpath=None, form=None, operator=Operator.EQUALS, case_sensitive=True)[source]¶
Represents a Mountebank predicate. A predicate can be thought of as a trigger, which may or may not match a request.
- Parameters:
query (
Mapping[str,str|int|bool] |None) – Query arguments, keys and values.body (
str|MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None] |None) – Body text. Can be a string, or a JSON serialisable data structure.headers (
Mapping[str,str] |None) – Headers, keys and values.form (
Mapping[str,str] |None) – Form-encoded key-value pairs in the body.case_sensitive (
bool)
- class Method(*values)[source]¶
Predicate HTTP method.
- DELETE = 'DELETE'¶
- GET = 'GET'¶
- HEAD = 'HEAD'¶
- POST = 'POST'¶
- PUT = 'PUT'¶
- PATCH = 'PATCH'¶
- OPTIONS = 'OPTIONS'¶
- class Operator(*values)[source]¶
-
- EQUALS = 'equals'¶
- DEEP_EQUALS = 'deepEquals'¶
- CONTAINS = 'contains'¶
- STARTS_WITH = 'startsWith'¶
- ENDS_WITH = 'endsWith'¶
- MATCHES = 'matches'¶
- EXISTS = 'exists'¶
- body: str | MutableMapping[str, str | int | float | bool | MutableSequence[JsonValue] | MutableMapping[str, JsonValue] | None] | None = None¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.predicates.AndPredicate(left, right)[source]¶
- left: BasePredicate¶
- right: BasePredicate¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.predicates.OrPredicate(left, right)[source]¶
- left: BasePredicate¶
- right: BasePredicate¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.predicates.NotPredicate(inverted)[source]¶
- inverted: BasePredicate¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.predicates.TcpPredicate(data)[source]¶
Represents a Mountebank TCP predicate. A predicate can be thought of as a trigger, which may or may not match a request.
- Parameters:
data (
str) – Data to match the request.
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.predicates.InjectionPredicate(inject)[source]¶
Represents a Mountebank injection predicate. A predicate can be thought of as a trigger, which may or may not match a request.
Injection requires Mountebank version 2.0 or higher.
- Parameters:
inject (
str) – JavaScript function to inject.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
The mbtest.imposters.responses module¶
- class mbtest.imposters.responses.BaseResponse[source]¶
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.responses.HttpResponse(body='', status_code=HTTPStatus.OK, headers=None, mode=None)[source]¶
Represents a Mountebank HTTP response.
- Parameters:
body (
str|MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None] |Element|bytes) – Body text for response. Can be a string, bytes, an XML Element, or a JSON serialisable data structure.status_code (
HTTPStatus|int|str) – HTTP status code - preferhttp.HTTPStatusvalues.
- body: str | MutableMapping[str, str | int | float | bool | MutableSequence[JsonValue] | MutableMapping[str, JsonValue] | None] | Element | bytes = ''¶
- status_code: HTTPStatus | int | str = 200¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.responses.Response(*, body='', status_code=HTTPStatus.OK, wait=None, repeat=None, headers=None, mode=None, copy=None, decorate=None, lookup=None, shell_transform=None, http_response=None)[source]¶
Represents a Mountebank ‘is’ response behavior.
- Parameters:
body (
str|bytes|MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – Body text for response. Can be a string, or a JSON serialisable data structure.status_code (
HTTPStatus|int|str) – HTTP status code - preferhttp.HTTPStatusvalues.wait (
int|str|None) – Add latency, in ms.repeat (
int|None) – Repeat this many times before moving on to next response.decorate (
str|None) – Decorate behavior.shell_transform (
str|Iterable[str] |None) – shellTransform behaviorhttp_response (
HttpResponse|None) – HTTP Response Fields - use this or the body, status_code, headers and mode fields, not both.
- http_response: HttpResponse¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- property body: str | MutableMapping[str, str | int | float | bool | MutableSequence[JsonValue] | MutableMapping[str, JsonValue] | None] | Element | bytes¶
- property status_code: HTTPStatus | int | str¶
- class mbtest.imposters.responses.TcpResponse(data)[source]¶
-
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.responses.FaultResponse(fault)[source]¶
Represents a Mountebank fault response.
- Parameters:
fault (
Fault) – The fault to simulate.
- class Fault(*values)[source]¶
- CONNECTION_RESET_BY_PEER = 'CONNECTION_RESET_BY_PEER'¶
- RANDOM_DATA_THEN_CLOSE = 'RANDOM_DATA_THEN_CLOSE'¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.responses.Proxy(to, wait=None, inject_headers=None, mode=Mode.ONCE, predicate_generators=<factory>, decorate=None)[source]¶
Represents a Mountebank proxy.
- Parameters:
to (
URL|str) – The origin server, to which the request should proxy.
- class Mode(*values)[source]¶
Defines the replay behavior of the proxy.
- ONCE = 'proxyOnce'¶
- ALWAYS = 'proxyAlways'¶
- TRANSPARENT = 'proxyTransparent'¶
- predicate_generators: list[PredicateGenerator]¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.responses.PredicateGenerator(path=False, query=False, operator=Operator.EQUALS, case_sensitive=True)[source]¶
Represents a Mountebank predicate generator.
- Parameters:
path (
bool) – Include the path in the generated predicate.
- query: bool | MutableMapping[str, str | int | float | bool | MutableSequence[JsonValue] | MutableMapping[str, JsonValue] | None] = False¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.responses.InjectionResponse(inject)[source]¶
Represents a Mountebank injection response.
Injection requires Mountebank version 2.0 or higher.
- Parameters:
inject (
str) – JavaScript function to inject .
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
The mbtest.imposters.behaviors.copy module¶
- class mbtest.imposters.behaviors.copy.Copy(from_, into, using)[source]¶
Represents a copy behavior.
- Parameters:
from – The name of the request field to copy from, or, if the request field is an object, then an object specifying the path to the request field.
into (
str) – The token to replace in the response with the selected request value.using (
Using) – The configuration needed to select values from the response.
- from_: str | MutableMapping[str, str | int | float | bool | MutableSequence[JsonValue] | MutableMapping[str, JsonValue] | None]¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
The mbtest.imposters.behaviors.lookup module¶
- class mbtest.imposters.behaviors.lookup.Lookup(key, datasource_path, datasource_key_column, into)[source]¶
Represents a lookup behavior.
- Parameters:
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.behaviors.lookup.Key(from_, using, index=0)[source]¶
The information on how to select the key from the request.
- Parameters:
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
The mbtest.imposters.behaviors.using module¶
- class mbtest.imposters.behaviors.using.Using(selector)[source]¶
How to select values from the response.
- Parameters:
selector (
str) – The selector used to select the value(s) from the request.
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.behaviors.using.UsingRegex(selector, ignore_case=False, multiline=False)[source]¶
Select values from the response using a regular expression.
- Parameters:
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.behaviors.using.UsingXpath(selector, ns=None)[source]¶
Select values from the response using an xpath expression.
- Parameters:
selector (
str) – The selector used to select the value(s) from the request.ns (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None] |None) – The ns object maps namespace aliases to URLs
- ns: MutableMapping[str, str | int | float | bool | MutableSequence[JsonValue] | MutableMapping[str, JsonValue] | None] | None = None¶
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- class mbtest.imposters.behaviors.using.UsingJsonpath(selector)[source]¶
Select values from the response using a jsonpath expression.
- Parameters:
selector (
str) – The selector used to select the value(s) from the request.
- classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
The mbtest.matchers module¶
- mbtest.matchers.had_request(*, method=<IsAnything(ANYTHING)>, path=<IsAnything(ANYTHING)>, query=<IsAnything(ANYTHING)>, headers=<IsAnything(ANYTHING)>, body=<IsAnything(ANYTHING)>, times=<IsAnything(ANYTHING)>)[source]¶
Mountebank server has recorded call matching.
Build criteria with with_ and and_ methods:
assert_that(server, had_request().with_path(“/test”).and_method(“GET”))
Available attributes as per parameters.
- Parameters:
path (
furl|URL|str|Matcher[furl|URL|str]) – Request’s path matched…query (
Mapping[str,str] |Matcher[Mapping[str,str]]) – Request’s query matched…headers (
Mapping[str,str] |Matcher[Mapping[str,str]]) – Request’s headers matched…times (
int|Matcher[int]) – Request’s number of times called matched matched…
- Return type:
- class mbtest.matchers.HadRequest(*, method=<IsAnything(ANYTHING)>, path=<IsAnything(ANYTHING)>, query=<IsAnything(ANYTHING)>, headers=<IsAnything(ANYTHING)>, body=<IsAnything(ANYTHING)>, times=<IsAnything(ANYTHING)>)[source]¶
Mountebank server has recorded call matching
- Parameters:
path (
furl|URL|str|Matcher[furl|URL|str]) – Request’s path matched…query (
Mapping[str,str] |Matcher[Mapping[str,str]]) – Request’s query matched…headers (
Mapping[str,str] |Matcher[Mapping[str,str]]) – Request’s headers matched…times (
int|Matcher[int]) – Request’s number of times called matched matched…
- describe_to(description)[source]¶
Generates a description of the object.
The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.
- Parameters:
description (
Description) – The description to be built or appended to.- Return type:
- describe_mismatch(item, mismatch_description)[source]¶
Generates a description of why the matcher has not accepted the item.
The description will be part of a larger description of why a matching failed, so it should be concise.
This method assumes that
matches(item)isFalse, but will not check this.- Parameters:
item (
Imposter|MountebankServer) – The item that theMatcherhas rejected.mismatch_description (
Description) – The description to be built or appended to.
- Return type:
- with_method(method)[source]¶
Matches the HTTP method of recorded requests.
- and_method(method)¶
Matches the HTTP method of recorded requests.
- with_path(path)[source]¶
Matches the path of recorded requests.
- and_path(path)¶
Matches the path of recorded requests.
- and_query(query)¶
Matches the query parameters of recorded requests.
- and_headers(headers)¶
Matches the headers of recorded requests.
- with_body(body)[source]¶
Matches the raw body text of recorded requests.
- and_body(body)¶
Matches the raw body text of recorded requests.
- with_json(json)[source]¶
Matches the JSON payload of recorded requests.
- Parameters:
json (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None] |Matcher[MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]]) – Expected JSON object or a matcher for the JSON payload.- Return type:
- Returns:
This matcher instance for chaining.
- and_json(json)¶
Matches the JSON payload of recorded requests.
- Parameters:
json (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None] |Matcher[MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]]) – Expected JSON object or a matcher for the JSON payload.- Return type:
- Returns:
This matcher instance for chaining.
- with_times(times)[source]¶
Matches the count of recorded requests matching the criteria.
- mbtest.matchers.email_sent(from_=<IsAnything(ANYTHING)>, to=<IsAnything(ANYTHING)>, subject=<IsAnything(ANYTHING)>, body_text=<IsAnything(ANYTHING)>)[source]¶
Mountebank SMTP server was asked to sent email matching.
Build criteria with with_ and and_ methods:
assert_that(server, email_sent().with_body_text(“hello”).and_subject(“hi”))
Available attributes as per parameters.
- class mbtest.matchers.EmailSent(from_=<IsAnything(ANYTHING)>, to=<IsAnything(ANYTHING)>, subject=<IsAnything(ANYTHING)>, body_text=<IsAnything(ANYTHING)>)[source]¶
Mountebank SMTP server was asked to sent email matching.
- Parameters:
- describe_to(description)[source]¶
Generates a description of the object.
The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.
- Parameters:
description (
Description) – The description to be built or appended to.- Return type:
- describe_mismatch(item, mismatch_description)[source]¶
Generates a description of why the matcher has not accepted the item.
The description will be part of a larger description of why a matching failed, so it should be concise.
This method assumes that
matches(item)isFalse, but will not check this.- Parameters:
item (
Imposter|MountebankServer) – The item that theMatcherhas rejected.mismatch_description (
Description) – The description to be built or appended to.
- Return type:
- with_from_(from_)[source]¶
Matches the sender address of sent emails.
- Parameters:
from – Expected sender address or a matcher for the sender.
- Return type:
- Returns:
This matcher instance for chaining.
- and_from_(from_)¶
Matches the sender address of sent emails.
- Parameters:
from – Expected sender address or a matcher for the sender.
- Return type:
- Returns:
This matcher instance for chaining.
- and_to(to)¶
Matches the recipient addresses of sent emails.
- and_subject(subject)¶
Matches the subject line of sent emails.
The mbtest.imposters.base module¶
- class mbtest.imposters.base.JsonSerializable[source]¶
Object capable of being converted to a JSON serializable structure (using
as_structure()) or from such a structure ((usingfrom_structure()).- abstractmethod as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.
- abstractmethod classmethod from_structure(structure)[source]¶
Convert from a JSON serializable structure.
- Parameters:
structure (
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]) – JSON structure to be converted.- Return type:
- Returns:
Converted object.
- static add_if_true(dictionary, key, value)[source]¶
Add key/value to dictionary only if value is truthy.
- Return type:
- static as_json_object(value)[source]¶
Narrow a JsonValue value to a JsonObject (dict[str, JsonValue]).
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]
- class mbtest.imposters.base.Injecting(inject)[source]¶
-
- as_structure()[source]¶
Convert to a JSON serializable structure.
- Return type:
MutableMapping[str,str|int|float|bool|MutableSequence[JsonValue] |MutableMapping[str, JsonValue] |None]- Returns:
Structure suitable for JSON serialisation.