wiremock_multipart/
lib.rs

1//! wiremock-multipart adds matchers for use with [wiremock](https://crates.io/crates/wiremock)
2//! to check multipart characteristics of requests.
3//!
4//! ## How to install
5//! Add `wiremock-multipart` to your dev-dependencies:
6//! ```toml
7//! [dev-dependencies]
8//! # ...
9//! wiremock-multipart = "0.1"
10//! ```
11//!
12//! ## Getting started
13//!
14//! ```rust
15//! use wiremock::{MockServer, Mock, ResponseTemplate};
16//! use wiremock::matchers::method;
17//! use wiremock_multipart::prelude::*;
18//!
19//! #[async_std::main]
20//! async fn main() {
21//!     // Start a background HTTP server on a random local port
22//!     let mock_server = MockServer::start().await;
23//!
24//!     // Arrange the behaviour of the MockServer adding a Mock
25//!     Mock::given(method("POST"))
26//!         .and(NumberOfParts(2))
27//!         .respond_with(ResponseTemplate::new(200))
28//!         // Mounting the mock on the mock server - it's now effective!
29//!         .mount(&mock_server)
30//!         .await;
31//!
32//!     // if we now send a multipart/form-data request with two parts to it, the request
33//!     // will match and return 200.
34//! }
35//! ```
36
37#[cfg(test)]
38extern crate indoc;
39extern crate lazy_regex;
40#[cfg(test)]
41extern crate maplit;
42extern crate wiremock;
43
44pub mod matchers;
45mod part;
46mod request_utils;
47
48pub use part::Part;
49pub use request_utils::{MultipartContentType, RequestUtils};
50
51pub mod prelude {
52    pub use crate::matchers::*;
53}
54
55#[cfg(test)]
56mod test_utils {
57    use maplit::hashmap;
58    use std::collections::HashMap;
59    use std::str::FromStr;
60
61    use wiremock::http::{HeaderName, HeaderValue, Method, Url};
62    use wiremock::Request;
63
64    pub fn name(name: &'static str) -> HeaderName {
65        HeaderName::from_str(name).unwrap()
66    }
67
68    pub fn values(val: &'static str) -> HeaderValue {
69        HeaderValue::from_str(val).unwrap()
70    }
71
72    pub fn request(headers: impl IntoIterator<Item = (HeaderName, HeaderValue)>) -> Request {
73        requestb(headers, vec![])
74    }
75
76    pub fn requestb(
77        headers: impl IntoIterator<Item = (HeaderName, HeaderValue)>,
78        body: Vec<u8>,
79    ) -> Request {
80        Request {
81            url: Url::from_str("http://localhost").unwrap(),
82            method: Method::POST,
83            headers: headers.into_iter().collect(),
84            body,
85        }
86    }
87
88    pub fn multipart_header() -> HashMap<HeaderName, HeaderValue> {
89        hashmap! {
90            name("content-type") => values("multipart/form-data; boundary=xyz"),
91        }
92    }
93}