http_server_mock/types
Core data types shared across all modules.
Most of these types are produced by the builder modules (matcher,
response, stub) and consumed by the server or verify. You generally
interact with them through those builders rather than constructing them
directly.
Types
Describes how the request body must look for a stub to match.
pub type BodyMatcher {
AnyBody
ExactBody(String)
ContainsBody(String)
JsonBody(String)
}
Constructors
-
AnyBodyAny body is accepted (including an empty body).
-
ExactBody(String)The body must be exactly equal to the given string.
-
ContainsBody(String)The body must contain the given string as a substring.
-
JsonBody(String)The body must be semantically equal to the given JSON string (whitespace and key order are ignored).
A request that the mock server received and recorded.
Returned by http_server_mock.recorded_requests and used with the verify
module to assert that expected requests were made.
pub type RecordedRequest {
RecordedRequest(
id: String,
method: http.Method,
path: String,
query: option.Option(String),
headers: dict.Dict(String, String),
body: String,
timestamp_ms: Int,
matched_stub_id: option.Option(String),
)
}
Constructors
-
RecordedRequest( id: String, method: http.Method, path: String, query: option.Option(String), headers: dict.Dict(String, String), body: String, timestamp_ms: Int, matched_stub_id: option.Option(String), )Arguments
- id
-
Unique identifier for this recorded request.
- query
-
The raw query string, without the leading
?.Noneif absent. - headers
-
All request headers, with names normalised to lowercase.
- timestamp_ms
-
Unix timestamp in milliseconds when the request was received.
- matched_stub_id
-
The ID of the stub that matched this request, or
Noneif no stub matched (in which case the server returned 404).
The full set of conditions an incoming request must satisfy for a stub to
be selected. Conditions that are None or empty match anything.
Build one using the matcher module rather than constructing it directly.
pub type RequestMatcher {
RequestMatcher(
method: option.Option(http.Method),
path: option.Option(StringMatcher),
query_params: List(#(String, StringMatcher)),
headers: List(#(String, StringMatcher)),
body: BodyMatcher,
)
}
Constructors
-
RequestMatcher( method: option.Option(http.Method), path: option.Option(StringMatcher), query_params: List(#(String, StringMatcher)), headers: List(#(String, StringMatcher)), body: BodyMatcher, )
The body of a stub response.
pub type ResponseBody {
StringBody(String)
RawJsonBody(String)
BytesBody(BitArray)
NoBody
}
Constructors
-
StringBody(String)A plain text string body.
-
RawJsonBody(String)A raw JSON string body (sent as-is, without re-serialisation).
-
BytesBody(BitArray)A binary body.
-
NoBodyNo body — the response has an empty body.
Describes the HTTP response the server sends when a stub is matched.
Build one using the response module rather than constructing it directly.
pub type ResponseDefinition {
ResponseDefinition(
status: Int,
headers: List(#(String, String)),
body: ResponseBody,
delay_ms: option.Option(Int),
)
}
Constructors
-
ResponseDefinition( status: Int, headers: List(#(String, String)), body: ResponseBody, delay_ms: option.Option(Int), )
The state of a named scenario, used to model stateful request sequences.
Set via stub.in_scenario, stub.when_state_is, and
stub.then_transition_to rather than constructing this directly.
pub type ScenarioState {
ScenarioState(
name: String,
required_state: option.Option(String),
new_state: option.Option(String),
)
}
Constructors
-
ScenarioState( name: String, required_state: option.Option(String), new_state: option.Option(String), )Arguments
- name
-
The name of the scenario this stub belongs to.
- required_state
-
The scenario state that must be active for this stub to match.
Nonemeans the stub is active only before the scenario has started. - new_state
-
The scenario state to transition to after this stub is matched.
Nonemeans the scenario state does not change.
Describes how a single string field of a request (path, header value, query parameter value) must look for a stub to match.
pub type StringMatcher {
Exact(String)
Contains(String)
Prefix(String)
Suffix(String)
AnyString
}
Constructors
-
Exact(String)The field must be exactly equal to the given string.
-
Contains(String)The field must contain the given string as a substring.
-
Prefix(String)The field must start with the given string.
-
Suffix(String)The field must end with the given string.
-
AnyStringAny value is accepted (the field is not checked).
A stub pairs a RequestMatcher with a ResponseDefinition.
Build one using stub.new rather than constructing this directly.
pub type Stub {
Stub(
id: String,
priority: Int,
matcher: RequestMatcher,
response: ResponseDefinition,
scenario: option.Option(ScenarioState),
)
}
Constructors
-
Stub( id: String, priority: Int, matcher: RequestMatcher, response: ResponseDefinition, scenario: option.Option(ScenarioState), )Arguments
- id
-
Unique identifier for this stub. Used in
RecordedRequest.matched_stub_idand the/__admin/stubslisting. - priority
-
Lower values take precedence when multiple stubs match the same request.