http_mock 1.0.0 copy "http_mock: ^1.0.0" to clipboard
http_mock: ^1.0.0 copied to clipboard

A package:http client that can mock server responses.

http_mock #

Pub build status

A package:http client that can mock server responses.

Usage #

Instead of sending requests over a network, the MockClient class is also a server in itself. It uses the cross-platform angel_route library to provide you with a routing solution that supports path parameters and middleware out-of-the-box.

All your handlers just need to accept a MockHttpContext. If they return true, then other handlers will be allowed to run (effectively accomplishing a middleware concept). Otherwise, the return value will be processed.

You can return:

  • a Response or StreamedResponse
  • null or nothing - do not write anything else to the server
  • Anything else - will be serialized via JSON.encode
var client = new http.MockClient();

client.router
  ..get('/hello/:name', (http.MockHttpContext ctx) {
    // Return a plain response
    return new http.Response('Hello, ${ctx.request.params["name"]}!', 200);
  })
  ..all('*', (http.MockHttpContext ctx) {
    return new http.Response('404 Not Found', 404);
  });

// And then, in a test...
var response = await client.get('/hello/world');
expect(response.body, 'Hello, world!');

0
likes
25
pub points
7%
popularity

Publisher

unverified uploader

A package:http client that can mock server responses.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

angel_route, http

More

Packages that depend on http_mock