MockHttpTransport class
A fake HTTP connection for tests: it answers requests with responses prepared in advance, without any network, and records every request sent.
It replaces the real HttpTransport under the code being tested,
typically a Feign client, so a test decides what the remote service
answers (data, errors, failures, event streams) and checks what the
client sent. Prepare an answer with when for a method and a path.
Several answers prepared for the same request are used in order, and
the last one repeats. A request without a prepared answer throws a
StateError that lists the prepared ones.
// The catalog service a Feign client calls is played by the mock.
final catalog = MockHttpTransport()
..when(HttpMethod.get, '/books/978-0')
.respond(200, json: {'isbn': '978-0', 'title': 'Dune'})
..when(HttpMethod.get, '/books/000').respond(404)
..when(HttpMethod.get, '/flaky')
.fail(HttpTransportException('down')); // to test retries
final app = springBootTest(
springApplication,
overrides: [BeanOverride<HttpTransport>(catalog)],
);
test('finds a book', () async {
final book = await app.getBean<CatalogClient>().find('978-0');
expect(book.title, 'Dune');
expect(catalog.lastRequest.headers['Accept'], 'application/json');
});
Spring (Java):
MockRestServiceServer server =
MockRestServiceServer.bindTo(builder).build();
server.expect(requestTo("/books/978-0"))
.andRespond(withSuccess("{\"title\":\"Dune\"}",
MediaType.APPLICATION_JSON));
- Implemented types
Constructors
- MockHttpTransport()
- A mock with no prepared answer yet; add them with when.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- lastRequest → HttpRequest
-
The last request sent, to check its headers or body; throws a
StateErrorwhen none was sent.no setter -
requests
→ List<
HttpRequest> -
Every request sent so far, in order, including those that had no
prepared answer.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
) → void -
Releases the transport's connections. Call it when the transport is
no longer needed (the Feign starter does when the application stops);
do not use the transport afterwards.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void - Forgets the prepared answers and the recorded requests, for example between two tests that share the mock.
-
send(
HttpRequest request) → Future< HttpResponse> -
Sends
requestand reads the whole response into memory.override -
stream(
HttpRequest request) → Future< StreamedHttpResponse> -
Sends
requestand completes as soon as the status and headers have arrived. The body is then read from StreamedHttpResponse.body as it comes.override -
toString(
) → String -
A string representation of this object.
inherited
-
when(
HttpMethod method, String path) → MockResponseBuilder -
Starts preparing the answer to
methodrequests forpath; finish it with one of the methods of the returned MockResponseBuilder.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited