Mock Transport API¶
For high-level usage see the testing guide.
MockResponse¶
A lightweight response spec used by MockTransport handlers.
You can pick one of content, text, json and stream.
Passing more than one raises ValueError — there's no good way to
reconcile multiple bodies.
Parameters¶
status_code:
HTTP status code, e.g. 200, 404.
headers:
Any headers-like input (dict, list of pairs, Headers).
content:
Raw bytes body. Content-Length is set automatically.
text:
Unicode body; encoded as UTF-8. Content-Type defaults to
text/plain; charset=utf-8.
json:
Any JSON-serialisable object. Content-Type defaults to
application/json. Use the sentinel MockResponse.NO_BODY
(the default) to distinguish "no JSON" from json=None.
stream:
Async iterable yielding bytes. Use this to simulate streamed or
chunked-encoded bodies. Content-Length is not auto-computed.
http_version:
e.g. "HTTP/1.1" or "HTTP/2".
Source code in hyperhttp/mock.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
Attributes¶
__slots__ = ('status_code', 'http_version', '_headers', '_body', '_stream')
class-attribute
instance-attribute
¶
_body = body
instance-attribute
¶
_headers = hdrs
instance-attribute
¶
_stream = stream
instance-attribute
¶
http_version = http_version
instance-attribute
¶
status_code = int(status_code)
instance-attribute
¶
Functions¶
__init__(status_code=200, *, headers=None, content=None, text=None, json=_UNSET, stream=None, http_version='HTTP/1.1')
¶
Source code in hyperhttp/mock.py
__repr__()
¶
_build_raw()
¶
Source code in hyperhttp/mock.py
MockTransport¶
Bases: Transport
In-memory transport for tests.
Pass any of:
- a callable
handler(request) -> MockResponse(sync or async); - a single
MockResponse— every request gets the same reply; - a sequence
[MockResponse, ...]— popped left-to-right per call. Runs out →IndexError(loud failure beats silent reuse); - a sequence that cycles — wrap it with
itertools.cyclefirst if you want repeats.
A handler may also raise any hyperhttp exception (or OSError)
to simulate transport-level failures, which flows through the client's
retry / circuit-breaker machinery exactly as the real thing would.
Source code in hyperhttp/mock.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
Attributes¶
_calls = []
instance-attribute
¶
_closed = False
instance-attribute
¶
_handler = _normalise_handler(handler)
instance-attribute
¶
call_count
property
¶
calls
property
¶
List of every Request this transport has served (FIFO).
closed
property
¶
host_port
property
¶
http_version = 'HTTP/1.1'
class-attribute
instance-attribute
¶
in_flight
property
¶
last_request
property
¶
max_concurrent
property
¶
reusable
property
¶
Functions¶
__init__(handler)
¶
aclose()
async
¶
handle_request(*, method, url, headers, body, timeout)
async
¶
Source code in hyperhttp/mock.py
Router¶
Method + path-prefix dispatcher.
Tiny on purpose — the point of MockTransport is that you write your
own handler. This helper just exists so the 90 % case ("GET /users
returns 200, everything else 404") is a one-liner:
router = Router()
router.get("/users", lambda req: MockResponse(200, json=[...]))
router.post("/users", lambda req: MockResponse(201))
router.route("GET", "/health", lambda req: MockResponse(204))
mock = MockTransport(router)
Matching is exact on (method, url.path). For anything fancier
(wildcards, path-parameters), dispatch inside your own handler.
Unmatched requests return MockResponse(404).