1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 167 168 169 170 171 172 173 174 175 176 177 178 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
use crate::diff;
use crate::matcher::{Matcher, PathAndQueryMatcher};
use crate::response::{Body, Response};
use crate::server::RemoteMock;
use crate::server::State;
use crate::Request;
use crate::{Error, ErrorKind};
use hyper::header::HeaderName;
use hyper::HeaderMap;
use hyper::StatusCode;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::convert::Into;
use std::fmt;
use std::io;
use std::ops::Drop;
use std::path::Path;
use std::string::ToString;
use std::sync::Arc;
use std::sync::RwLock;
#[allow(missing_docs)]
pub trait IntoHeaderName {
#[track_caller]
fn into_header_name(self) -> HeaderName;
}
impl IntoHeaderName for String {
fn into_header_name(self) -> HeaderName {
HeaderName::try_from(self)
.map_err(|_| Error::new(ErrorKind::InvalidHeaderName))
.unwrap()
}
}
impl IntoHeaderName for &String {
fn into_header_name(self) -> HeaderName {
HeaderName::try_from(self)
.map_err(|_| Error::new(ErrorKind::InvalidHeaderName))
.unwrap()
}
}
impl IntoHeaderName for &str {
fn into_header_name(self) -> HeaderName {
HeaderName::try_from(self)
.map_err(|_| Error::new(ErrorKind::InvalidHeaderName))
.unwrap()
}
}
impl IntoHeaderName for HeaderName {
fn into_header_name(self) -> HeaderName {
self
}
}
impl IntoHeaderName for &HeaderName {
fn into_header_name(self) -> HeaderName {
self.into()
}
}
#[derive(Clone, Debug)]
pub struct InnerMock {
pub(crate) id: String,
pub(crate) method: String,
pub(crate) path: PathAndQueryMatcher,
pub(crate) headers: HeaderMap<Matcher>,
pub(crate) body: Matcher,
pub(crate) response: Response,
pub(crate) hits: usize,
pub(crate) expected_hits_at_least: Option<usize>,
pub(crate) expected_hits_at_most: Option<usize>,
}
impl fmt::Display for InnerMock {
#[allow(deprecated)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut formatted = String::new();
formatted.push_str("\r\n");
formatted.push_str(&self.method);
formatted.push(' ');
formatted.push_str(&self.path.to_string());
for (key, value) in &self.headers {
formatted.push_str(key.as_str());
formatted.push_str(": ");
formatted.push_str(&value.to_string());
formatted.push_str("\r\n");
}
match self.body {
Matcher::Exact(ref value)
| Matcher::JsonString(ref value)
| Matcher::PartialJsonString(ref value)
| Matcher::Regex(ref value) => {
formatted.push_str(value);
formatted.push_str("\r\n");
}
Matcher::Binary(_) => {
formatted.push_str("(binary)\r\n");
}
Matcher::Json(ref json_obj) | Matcher::PartialJson(ref json_obj) => {
formatted.push_str(&json_obj.to_string());
formatted.push_str("\r\n")
}
Matcher::UrlEncoded(ref field, ref value) => {
formatted.push_str(field);
formatted.push('=');
formatted.push_str(value);
}
Matcher::Missing => formatted.push_str("(missing)\r\n"),
Matcher::AnyOf(..) => formatted.push_str("(any of)\r\n"),
Matcher::AllOf(..) => formatted.push_str("(all of)\r\n"),
Matcher::Any => {}
}
f.write_str(&formatted)
}
}
impl PartialEq for InnerMock {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.method == other.method
&& self.path == other.path
&& self.headers == other.headers
&& self.body == other.body
&& self.response == other.response
&& self.hits == other.hits
}
}
///
/// Stores information about a mocked request. Should be initialized via `Server::mock()`.
///
#[derive(Debug)]
pub struct Mock {
state: Arc<RwLock<State>>,
inner: InnerMock,
/// Used to warn of mocks missing a `.create()` call. See issue #112
created: bool,
assert_on_drop: bool,
}
impl Mock {
pub(crate) fn new<P: Into<Matcher>>(
state: Arc<RwLock<State>>,
method: &str,
path: P,
assert_on_drop: bool,
) -> Mock {
let inner = InnerMock {
id: thread_rng()
.sample_iter(&Alphanumeric)
.map(char::from)
.take(24)
.collect(),
method: method.to_owned().to_uppercase(),
path: PathAndQueryMatcher::Unified(path.into()),
headers: HeaderMap::<Matcher>::default(),
body: Matcher::Any,
response: Response::default(),
hits: 0,
expected_hits_at_least: None,
expected_hits_at_most: None,
};
Self {
state,
inner,
created: false,
assert_on_drop,
}
}
///
/// Allows matching against the query part when responding with a mock.
///
/// Note that you can also specify the query as part of the path argument
/// in a `mock` call, in which case an exact match will be performed.
/// Any future calls of `Mock#match_query` will override the query matcher.
///
/// ## Example
///
/// ```
/// use mockito::Matcher;
///
/// let mut s = mockito::Server::new();
///
/// // This will match requests containing the URL-encoded
/// // query parameter `greeting=good%20day`
/// s.mock("GET", "/test")
/// .match_query(Matcher::UrlEncoded("greeting".into(), "good day".into()))
/// .create();
///
/// // This will match requests containing the URL-encoded
/// // query parameters `hello=world` and `greeting=good%20day`
/// s.mock("GET", "/test")
/// .match_query(Matcher::AllOf(vec![
/// Matcher::UrlEncoded("hello".into(), "world".into()),
/// Matcher::UrlEncoded("greeting".into(), "good day".into())
/// ]))
/// .create();
///
/// // You can achieve similar results with the regex matcher
/// s.mock("GET", "/test")
/// .match_query(Matcher::Regex("hello=world".into()))
/// .create();
/// ```
///
pub fn match_query<M: Into<Matcher>>(mut self, query: M) -> Self {
let new_path = match &self.inner.path {
PathAndQueryMatcher::Unified(matcher) => {
PathAndQueryMatcher::Split(Box::new(matcher.clone()), Box::new(query.into()))
}
PathAndQueryMatcher::Split(path, _) => {
PathAndQueryMatcher::Split(path.clone(), Box::new(query.into()))
}
};
self.inner.path = new_path;
self
}
///
/// Allows matching a particular request header when responding with a mock.
///
/// When matching a request, the field letter case is ignored.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/").match_header("content-type", "application/json");
/// ```
///
/// Like most other `Mock` methods, it allows chanining:
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/")
/// .match_header("content-type", "application/json")
/// .match_header("authorization", "password");
/// ```
///
#[track_caller]
pub fn match_header<T: IntoHeaderName, M: Into<Matcher>>(mut self, field: T, value: M) -> Self {
self.inner
.headers
.append(field.into_header_name(), value.into());
self
}
///
/// Allows matching a particular request body when responding with a mock.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("POST", "/").match_body(r#"{"hello": "world"}"#).with_body("json").create();
/// s.mock("POST", "/").match_body("hello=world").with_body("form").create();
///
/// // Requests passing `{"hello": "world"}` inside the body will be responded with "json".
/// // Requests passing `hello=world` inside the body will be responded with "form".
///
/// // Create a temporary file
/// use std::env;
/// use std::fs::File;
/// use std::io::Write;
/// use std::path::Path;
/// use rand;
/// use rand::Rng;
///
/// let random_bytes: Vec<u8> = (0..1024).map(|_| rand::random::<u8>()).collect();
///
/// let mut tmp_file = env::temp_dir();
/// tmp_file.push("test_file.txt");
/// let mut f_write = File::create(tmp_file.clone()).unwrap();
/// f_write.write_all(random_bytes.as_slice()).unwrap();
/// let mut f_read = File::open(tmp_file.clone()).unwrap();
///
///
/// // the following are equivalent ways of defining a mock matching
/// // a binary payload
/// s.mock("POST", "/").match_body(tmp_file.as_path()).create();
/// s.mock("POST", "/").match_body(random_bytes).create();
/// s.mock("POST", "/").match_body(&mut f_read).create();
/// ```
///
pub fn match_body<M: Into<Matcher>>(mut self, body: M) -> Self {
self.inner.body = body.into();
self
}
///
/// Sets the status code of the mock response. The default status code is 200.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/").with_status(201);
/// ```
///
#[track_caller]
pub fn with_status(mut self, status: usize) -> Self {
self.inner.response.status = StatusCode::from_u16(status as u16)
.map_err(|_| Error::new_with_context(ErrorKind::InvalidStatusCode, status))
.unwrap();
self
}
///
/// Sets a header of the mock response.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/").with_header("content-type", "application/json");
/// ```
///
pub fn with_header<T: IntoHeaderName>(mut self, field: T, value: &str) -> Self {
self.inner
.response
.headers
.append(field.into_header_name(), value.to_owned());
self
}
///
/// Sets the body of the mock response. Its `Content-Length` is handled automatically.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/").with_body("hello world");
/// ```
///
pub fn with_body<StrOrBytes: AsRef<[u8]>>(mut self, body: StrOrBytes) -> Self {
self.inner.response.body = Body::Bytes(body.as_ref().to_owned());
self
}
///
/// Sets the body of the mock response dynamically. The response will use chunked transfer encoding.
///
/// The callback function will be called only once. You can sleep in between calls to the
/// writer to simulate delays between the chunks. The callback function can also return an
/// error after any number of writes in order to abort the response.
///
/// The function must be thread-safe. If it's a closure, it can't be borrowing its context.
/// Use `move` closures and `Arc` to share any data.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/").with_chunked_body(|w| w.write_all(b"hello world"));
/// ```
///
pub fn with_chunked_body(
mut self,
callback: impl Fn(&mut dyn io::Write) -> io::Result<()> + Send + Sync + 'static,
) -> Self {
self.inner.response.body = Body::FnWithWriter(Arc::new(callback));
self
}
///
/// **DEPRECATED:** Replaced by `Mock::with_chunked_body`.
///
#[deprecated(since = "1.0.0", note = "Use `Mock::with_chunked_body` instead")]
pub fn with_body_from_fn(
self,
callback: impl Fn(&mut dyn io::Write) -> io::Result<()> + Send + Sync + 'static,
) -> Self {
self.with_chunked_body(callback)
}
///
/// Sets the body of the mock response dynamically while exposing the request object.
///
/// You can use this method to provide a custom reponse body for every incoming request.
///
/// The function must be thread-safe. If it's a closure, it can't be borrowing its context.
/// Use `move` closures and `Arc` to share any data.
///
/// ### Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// let _m = s.mock("GET", mockito::Matcher::Any).with_body_from_request(|request| {
/// if request.path() == "/bob" {
/// "hello bob".into()
/// } else if request.path() == "/alice" {
/// "hello alice".into()
/// } else {
/// "hello world".into()
/// }
/// });
/// ```
///
pub fn with_body_from_request(
mut self,
callback: impl Fn(&Request) -> Vec<u8> + Send + Sync + 'static,
) -> Self {
self.inner.response.body = Body::FnWithRequest(Arc::new(callback));
self
}
///
/// Sets the body of the mock response from the contents of a file stored under `path`.
/// Its `Content-Length` is handled automatically.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/").with_body_from_file("tests/files/simple.http");
/// ```
///
#[track_caller]
pub fn with_body_from_file(mut self, path: impl AsRef<Path>) -> Self {
self.inner.response.body = Body::Bytes(
std::fs::read(path)
.map_err(|_| Error::new(ErrorKind::FileNotFound))
.unwrap(),
);
self
}
///
/// Sets the expected amount of requests that this mock is supposed to receive.
/// This is only enforced when calling the `assert` method.
/// Defaults to 1 request.
///
#[allow(clippy::missing_const_for_fn)]
pub fn expect(mut self, hits: usize) -> Self {
self.inner.expected_hits_at_least = Some(hits);
self.inner.expected_hits_at_most = Some(hits);
self
}
///
/// Sets the minimum amount of requests that this mock is supposed to receive.
/// This is only enforced when calling the `assert` method.
///
pub fn expect_at_least(mut self, hits: usize) -> Self {
self.inner.expected_hits_at_least = Some(hits);
if self.inner.expected_hits_at_most.is_some()
&& self.inner.expected_hits_at_most < self.inner.expected_hits_at_least
{
self.inner.expected_hits_at_most = None;
}
self
}
///
/// Sets the maximum amount of requests that this mock is supposed to receive.
/// This is only enforced when calling the `assert` method.
///
pub fn expect_at_most(mut self, hits: usize) -> Self {
self.inner.expected_hits_at_most = Some(hits);
if self.inner.expected_hits_at_least.is_some()
&& self.inner.expected_hits_at_least > self.inner.expected_hits_at_most
{
self.inner.expected_hits_at_least = None;
}
self
}
///
/// Asserts that the expected amount of requests (defaults to 1 request) were performed.
///
#[track_caller]
pub fn assert(&self) {
let mutex = self.state.clone();
let state = mutex.read().unwrap();
if let Some(hits) = state.get_mock_hits(self.inner.id.clone()) {
let matched = self.matched_hits(hits);
let message = if !matched {
let last_request = state.get_last_unmatched_request();
self.build_assert_message(hits, last_request)
} else {
String::default()
};
assert!(matched, "{}", message)
} else {
panic!("could not retrieve enough information about the remote mock")
}
}
///
/// Same as `Mock::assert` but async.
///
pub async fn assert_async(&self) {
let mutex = self.state.clone();
let state = mutex.read().unwrap();
if let Some(hits) = state.get_mock_hits(self.inner.id.clone()) {
let matched = self.matched_hits(hits);
let message = if !matched {
let last_request = state.get_last_unmatched_request();
self.build_assert_message(hits, last_request)
} else {
String::default()
};
assert!(matched, "{}", message)
} else {
panic!("could not retrieve enough information about the remote mock")
}
}
///
/// Returns whether the expected amount of requests (defaults to 1) were performed.
///
pub fn matched(&self) -> bool {
let mutex = self.state.clone();
let state = mutex.read().unwrap();
let Some(hits) = state.get_mock_hits(self.inner.id.clone()) else {
return false;
};
self.matched_hits(hits)
}
///
/// Same as `Mock::matched` but async.
///
pub async fn matched_async(&self) -> bool {
let mutex = self.state.clone();
let state = mutex.read().unwrap();
let Some(hits) = state.get_mock_hits(self.inner.id.clone()) else {
return false;
};
self.matched_hits(hits)
}
///
/// Registers the mock to the server - your mock will be served only after calling this method.
///
/// ## Example
///
/// ```
/// let mut s = mockito::Server::new();
///
/// s.mock("GET", "/").with_body("hello world").create();
/// ```
///
pub fn create(mut self) -> Mock {
let remote_mock = RemoteMock::new(self.inner.clone());
let state = self.state.clone();
let mut state = state.write().unwrap();
state.mocks.push(remote_mock);
self.created = true;
self
}
///
/// Same as `Mock::create` but async.
///
pub async fn create_async(mut self) -> Mock {
let remote_mock = RemoteMock::new(self.inner.clone());
let state = self.state.clone();
let mut state = state.write().unwrap();
state.mocks.push(remote_mock);
self.created = true;
self
}
///
/// Removes the mock from the server.
///
pub fn remove(&self) {
let mutex = self.state.clone();
let mut state = mutex.write().unwrap();
state.remove_mock(self.inner.id.clone());
}
///
/// Same as `Mock::remove` but async.
///
pub async fn remove_async(&self) {
let mutex = self.state.clone();
let mut state = mutex.write().unwrap();
state.remove_mock(self.inner.id.clone());
}
fn matched_hits(&self, hits: usize) -> bool {
match (
self.inner.expected_hits_at_least,
self.inner.expected_hits_at_most,
) {
(Some(min), Some(max)) => hits >= min && hits <= max,
(Some(min), None) => hits >= min,
(None, Some(max)) => hits <= max,
(None, None) => hits == 1,
}
}
fn build_assert_message(&self, hits: usize, last_request: Option<String>) -> String {
let mut message = match (
self.inner.expected_hits_at_least,
self.inner.expected_hits_at_most,
) {
(Some(min), Some(max)) if min == max => format!(
"\n> Expected {} request(s) to:\n{}\n...but received {}\n\n",
min, self, hits
),
(Some(min), Some(max)) => format!(
"\n> Expected between {} and {} request(s) to:\n{}\n...but received {}\n\n",
min, max, self, hits
),
(Some(min), None) => format!(
"\n> Expected at least {} request(s) to:\n{}\n...but received {}\n\n",
min, self, hits
),
(None, Some(max)) => format!(
"\n> Expected at most {} request(s) to:\n{}\n...but received {}\n\n",
max, self, hits
),
(None, None) => format!(
"\n> Expected 1 request(s) to:\n{}\n...but received {}\n\n",
self, hits
),
};
if let Some(last_request) = last_request {
message.push_str(&format!(
"> The last unmatched request was:\n{}\n",
last_request
));
let difference = diff::compare(&self.to_string(), &last_request);
message.push_str(&format!("> Difference:\n{}\n", difference));
}
message
}
}
impl Drop for Mock {
fn drop(&mut self) {
if !self.created {
log::warn!("Missing .create() call on mock {}", self);
}
if self.assert_on_drop {
self.assert();
}
}
}
impl fmt::Display for Mock {
#[allow(deprecated)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut formatted = String::new();
formatted.push_str(&self.inner.to_string());
f.write_str(&formatted)
}
}
impl PartialEq for Mock {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}