Update Rust crate mockito to v1
This MR contains the following updates:
Package | Type | Update | Change |
---|---|---|---|
mockito | dev-dependencies | major |
~0.32 -> ~1.0.0
|
Release Notes
lipanski/mockito
v1.0.0
Changes
- [Breaking] The legacy interface was removed in this version
-
[Breaking]
Mock::with_body_from_fn
was renamed toMock::with_chunked_body
- the former is still supported with a deprecation warning - Mocks are only cleared when the server is dropped, not when the mock is dropped - this means you don't have to assign mocks to variables any more (unless you want to call other methods on them)
- Introduced the
Mock::remove
andMock::remove_async
methods to remove mocks on demand
Major changes since 0.31
- Tests can now run in parallel
- Support for HTTP2
- An async interface for all actions (though the sync interface is also available)
- Mock multiple server/hosts at the same time
For a list of all the changes please check the release log.
Migrating to the new API
Legacy API:
let m1 = mockito::mock("GET", "/hello").with_body("hello").create();
let m2 = mockito::mock("GET", "/bye").with_body("bye").create();
// Use one of these to configure your client
let host = mockito:server_address();
let url = mockito::server_url();
New API:
let mut server = mockito::Server::new();
server.mock("GET", "/hello").with_body("hello").create();
server.mock("GET", "/bye").with_body("bye").create();
// Use one of these to configure your client
let host = server.host_with_port();
let url = server.url();
If you can't migrate to the new API in one go, consider using version 0.32.5, which supports both the legacy API as well as the new API.
Migrating to the async API
In order to write async tests, you'll need to use the _async
methods:
Server::new_async
Mock::create_async
Mock::assert_async
Mock::matched_async
Mock::remove_async
Server::reset_async
...otherwise your tests will not compile and you'll see this error:
Cannot block the current thread from within a runtime. This happens because a function attempted
to block the current thread while the thread is being used to drive asynchronous tasks.
Example:
#[tokio::test]
async fn test_simple_route_mock_async() {
let mut server = Server::new_async().await;
let m1 = server.mock("GET", "/a").with_body("aaa").create_async().await;
let m2 = server.mock("GET", "/b").with_body("bbb").create_async().await;
let (m1, m2) = futures::join!(m1, m2);
// You can use `Mock::assert_async` to verify that your mock was called
// m1.assert_async().await;
// m2.assert_async().await;
}
v0.32.5
- Implement and enable a new server pool and get rid of the deadpool dependency
- Replace the mpsc mock/server communication with
Arc
(more reliable in this case) - Split sync & async paths more clearly
v0.32.4
-
Introduce
Mock::with_body_from_request
which allows setting the response body dynamically, based on theRequest
object - Small performance improvement: replace the state
Mutex
with anRwLock
- Small fixes to the documentation (thanks @konstin)
v0.32.3
- Various fixes addressing hanging/flickering tests
-
Server::new
andServer::new_async
now return aServerGuard
object which dereferences toServer
- this is only relevant if you need to assign a type to your server - The server pool has been disabled and will be brought back in a future release
v0.32.2
- Prevent the Mock destructor from hanging in some cases
- Updates to the docs, clarifying the usage of
_async
methods
v0.32.1
Configuration
-
If you want to rebase/retry this MR, check this box
This MR has been generated by Renovate Bot.