Skip to content

Commit

Permalink
Remove mocks when the server is dropped, not when the mock is dropped
Browse files Browse the repository at this point in the history
  • Loading branch information
lipanski committed Mar 12, 2023
1 parent ac9042d commit 68c5629
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 211 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn test_something() {
let url = server.url();

// Create a mock
let m = server.mock("GET", "/hello")
let mock = server.mock("GET", "/hello")
.with_status(201)
.with_header("content-type", "text/plain")
.with_header("x-api-key", "1234")
Expand All @@ -56,7 +56,7 @@ fn test_something() {
// `content-type: text/plain` header and the body "world".

// You can use `Mock::assert` to verify that your mock was called
m.assert();
mock.assert();
}
```

Expand All @@ -71,15 +71,15 @@ Use **matchers** to handle requests to the same endpoint in a different way:
fn test_something() {
let mut server = mockito::Server::new();

let m1 = server.mock("GET", "/greetings")
server.mock("GET", "/greetings")
.match_header("content-type", "application/json")
.match_body(mockito::Matcher::PartialJsonString(
"{\"greeting\": \"hello\"}".to_string(),
))
.with_body("hello json")
.create();

let m2 = server.mock("GET", "/greetings")
server.mock("GET", "/greetings")
.match_header("content-type", "application/text")
.match_body(mockito::Matcher::Regex("greeting=hello".to_string()))
.with_body("hello text")
Expand Down
95 changes: 53 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! let url = server.url();
//!
//! // Create a mock
//! let _m = server.mock("GET", "/hello")
//! let mock = server.mock("GET", "/hello")
//! .with_status(201)
//! .with_header("content-type", "text/plain")
//! .with_header("x-api-key", "1234")
Expand All @@ -47,7 +47,7 @@
//! // `content-type: text/plain` header and the body "world".
//!
//! // You can use `Mock::assert` to verify that your mock was called
//! // m.assert();
//! // mock.assert();
//! }
//! }
//! ```
Expand All @@ -65,15 +65,15 @@
//! fn test_something() {
//! let mut server = mockito::Server::new();
//!
//! let m1 = server.mock("GET", "/greetings")
//! server.mock("GET", "/greetings")
//! .match_header("content-type", "application/json")
//! .match_body(mockito::Matcher::PartialJsonString(
//! "{\"greeting\": \"hello\"}".to_string(),
//! ))
//! .with_body("hello json")
//! .create();
//!
//! let m2 = server.mock("GET", "/greetings")
//! server.mock("GET", "/greetings")
//! .match_header("content-type", "application/text")
//! .match_body(mockito::Matcher::Regex("greeting=hello".to_string()))
//! .with_body("hello text")
Expand Down Expand Up @@ -123,28 +123,26 @@
//!
//! # Lifetime
//!
//! Just like any Rust object, a mock is available only through its lifetime. You'll want to assign
//! the mocks to variables in order to extend and control their lifetime.
//!
//! Avoid using the underscore matcher when creating your mocks, as in `let _ = mock("GET", "/")`.
//! This will end your mock's lifetime immediately. You can still use the underscore to prefix your variable
//! names in an assignment, but don't limit it to just this one character.
//!
//! ## Example
//! A mock is available only throughout the lifetime of the server. Once the server goes
//! out of scope, all mocks defined on that server are removed:
//!
//! ```
//! let mut s = mockito::Server::new();
//! let _m1 = s.mock("GET", "/long").with_body("hello").create();
//! let address;
//!
//! {
//! let _m2 = s.mock("GET", "/short").with_body("hi").create();
//! let mut s = mockito::Server::new();
//! address = s.host_with_port();
//!
//! // Requests to GET /short will be mocked til here
//! s.mock("GET", "/").with_body("hi").create();
//!
//! // Requests to `address` will be responded with "hi" til here
//! }
//!
//! // Requests to GET /long will be mocked til here
//! // Requests to `address` will fail as of this point
//! ```
//!
//! You can remove individual mocks earlier by calling `Mock::remove`.
//!
//! # Async
//!
//! Mockito comes with both a sync and an async interface.
Expand All @@ -155,6 +153,7 @@
//! - `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 the following error:
Expand Down Expand Up @@ -182,7 +181,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Matched only calls to GET /hello
//! let _m = s.mock("GET", "/hello").create();
//! s.mock("GET", "/hello").create();
//! ```
//!
//! You can also match the path partially, by using a regular expression:
Expand All @@ -193,7 +192,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Will match calls to GET /hello/1 and GET /hello/2
//! let _m = s.mock("GET",
//! s.mock("GET",
//! mockito::Matcher::Regex(r"^/hello/(1|2)$".to_string())
//! ).create();
//! ```
Expand All @@ -206,7 +205,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Will match any GET request
//! let _m = s.mock("GET", mockito::Matcher::Any).create();
//! s.mock("GET", mockito::Matcher::Any).create();
//! ```
//!
//! # Matching by query
Expand All @@ -221,21 +220,21 @@
//!
//! // This will match requests containing the URL-encoded
//! // query parameter `greeting=good%20day`
//! let _m1 = s.mock("GET", "/test")
//! s.mock("GET", "/test")
//! .match_query(mockito::Matcher::UrlEncoded("greeting".into(), "good day".into()))
//! .create();
//!
//! // This will match requests containing the URL-encoded
//! // query parameters `hello=world` and `greeting=good%20day`
//! let _m2 = s.mock("GET", "/test")
//! s.mock("GET", "/test")
//! .match_query(mockito::Matcher::AllOf(vec![
//! mockito::Matcher::UrlEncoded("hello".into(), "world".into()),
//! mockito::Matcher::UrlEncoded("greeting".into(), "good day".into())
//! ]))
//! .create();
//!
//! // You can achieve similar results with the regex matcher
//! let _m3 = s.mock("GET", "/test")
//! s.mock("GET", "/test")
//! .match_query(mockito::Matcher::Regex("hello=world".into()))
//! .create();
//! ```
Expand All @@ -251,7 +250,7 @@
//! let mut s = mockito::Server::new();
//!
//! // This will perform a full match against the query part
//! let _m = s.mock("GET", "/test?hello=world").create();
//! s.mock("GET", "/test?hello=world").create();
//! ```
//!
//! # Matching by header
Expand All @@ -263,12 +262,12 @@
//! ```
//! let mut s = mockito::Server::new();
//!
//! let _m1 = s.mock("GET", "/hello")
//! s.mock("GET", "/hello")
//! .match_header("content-type", "application/json")
//! .with_body(r#"{"hello": "world"}"#)
//! .create();
//!
//! let _m2 = s.mock("GET", "/hello")
//! s.mock("GET", "/hello")
//! .match_header("content-type", "text/plain")
//! .with_body("world")
//! .create();
Expand All @@ -284,7 +283,7 @@
//! ```
//! let mut s = mockito::Server::new();
//!
//! let _m = s.mock("GET", "/hello")
//! s.mock("GET", "/hello")
//! .match_header("content-type", mockito::Matcher::Regex(r".*json.*".to_string()))
//! .with_body(r#"{"hello": "world"}"#)
//! .create();
Expand All @@ -297,7 +296,7 @@
//! ```
//! let mut s = mockito::Server::new();
//!
//! let _m = s.mock("GET", "/hello")
//! s.mock("GET", "/hello")
//! .match_header("content-type", mockito::Matcher::Any)
//! .with_body("something")
//! .create();
Expand All @@ -314,7 +313,7 @@
//! ```
//! let mut s = mockito::Server::new();
//!
//! let _m = s.mock("GET", "/hello")
//! s.mock("GET", "/hello")
//! .match_header("authorization", mockito::Matcher::Missing)
//! .with_body("no authorization header")
//! .create();
Expand All @@ -336,7 +335,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Will match requests to POST / whenever the request body is "hello"
//! let _m = s.mock("POST", "/").match_body("hello").create();
//! s.mock("POST", "/").match_body("hello").create();
//! ```
//!
//! Or you can match the body by using a regular expression:
Expand All @@ -347,7 +346,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Will match requests to POST / whenever the request body *contains* the word "hello" (e.g. "hello world")
//! let _m = s.mock("POST", "/").match_body(
//! s.mock("POST", "/").match_body(
//! mockito::Matcher::Regex("hello".to_string())
//! ).create();
//! ```
Expand All @@ -364,7 +363,7 @@
//! # fn main() {
//! let mut s = mockito::Server::new();
//! // Will match requests to POST / whenever the request body matches the json object
//! let _m = s.mock("POST", "/").match_body(mockito::Matcher::Json(json!({"hello": "world"}))).create();
//! s.mock("POST", "/").match_body(mockito::Matcher::Json(json!({"hello": "world"}))).create();
//! # }
//! ```
//!
Expand All @@ -375,7 +374,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Will match requests to POST / whenever the request body matches the json object
//! let _m = s.mock("POST", "/")
//! s.mock("POST", "/")
//! .match_body(
//! mockito::Matcher::JsonString(r#"{"hello": "world"}"#.to_string())
//! )
Expand All @@ -393,7 +392,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Will match requests to POST / whenever the request body is either `hello=world` or `{"hello":"world"}`
//! let _m = s.mock("POST", "/")
//! s.mock("POST", "/")
//! .match_body(
//! mockito::Matcher::AnyOf(vec![
//! mockito::Matcher::Exact("hello=world".to_string()),
Expand All @@ -414,7 +413,7 @@
//! let mut s = mockito::Server::new();
//!
//! // Will match requests to POST / whenever the request body contains both `hello` and `world`
//! let _m = s.mock("POST", "/")
//! s.mock("POST", "/")
//! .match_body(
//! mockito::Matcher::AllOf(vec![
//! mockito::Matcher::Regex("hello".to_string()),
Expand Down Expand Up @@ -607,23 +606,35 @@
//!
//! # Cleaning up
//!
//! As mentioned earlier, mocks are cleaned up at the end of their normal Rust lifetime. However,
//! you can always use the `reset` method to clean up *all* the mocks registered so far.
//!
//! ## Example
//! As mentioned earlier, mocks are cleaned up whenever the server goes out of scope. If you
//! need to remove them earlier, you can call `Server::reset` to remove all mocks registered
//! so far:
//!
//! ```
//! let mut s = mockito::Server::new();
//!
//! let _m1 = s.mock("GET", "/1").create();
//! let _m2 = s.mock("GET", "/2").create();
//! let _m3 = s.mock("GET", "/3").create();
//! s.mock("GET", "/1").create();
//! s.mock("GET", "/2").create();
//! s.mock("GET", "/3").create();
//!
//! s.reset();
//!
//! // Nothing is mocked at this point
//! ```
//!
//! ...or you can call `Mock::reset` to remove a single mock:
//!
//! ```
//! let mut s = mockito::Server::new();
//!
//! let m1 = s.mock("GET", "/1").create();
//! let m2 = s.mock("GET", "/2").create();
//!
//! m1.remove();
//!
//! // Only m2 is available at this point
//! ```
//!
//! # Debug
//!
//! Mockito uses the `env_logger` crate under the hood to provide useful debugging information.
Expand Down
Loading

0 comments on commit 68c5629

Please sign in to comment.