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
//! The `pact_mock_server` crate provides the in-process mock server for mocking HTTP requests
//! and generating responses based on a pact file. It implements the
//! [V3 Pact specification](https://github.com/pact-foundation/pact-specification/tree/version-3)
//! and [V4 Pact specification](https://github.com/pact-foundation/pact-specification/tree/version-4).
//!
//! ## Crate features
//! All features are enabled by default
//!
//! * `datetime`: Enables support of date and time expressions and generators.
//! * `xml`: Enables support for parsing XML documents.
//! * `plugins`: Enables support for using plugins.
//! * `multipart`: Enables support for MIME multipart bodies.
//! * `tls`: Enables support for mock servers using TLS. This will add the following dependencies: hyper-rustls, rustls, rustls-pemfile, tokio-rustls.
//!
//! ## Creating a mock server
//! Mock servers can be created by using the mock server builder in the `builder` package. The
//! builder can create both standard HTTP and HTTPS servers.
//!
//! The following example loads a Pact file, starts the mock server and then shuts it down later.
//! ```rust
//! # tokio_test::block_on(async {
//! use pact_models::prelude::{Pact, RequestResponsePact};
//! use pact_mock_server::builder::MockServerBuilder;
//!
//! // Setup a Pact file for the mock server
//! let pact_json = r#"
//! {
//! "provider": {
//! "name": "Example Provider"
//! },
//! "consumer": {
//! "name": "Example Consumer"
//! },
//! "interactions": [
//! {
//! "description": "a GET request",
//! "request": {
//! "method": "GET",
//! "path": "/path"
//! },
//! "response": {
//! "status": 200,
//! "headers": {
//! "Content-Type": "text/plain"
//! },
//! "body": "Hello from the mock server"
//! }
//! }
//! ]
//! }
//! "#;
//! let pact = RequestResponsePact::from_json(&"JSON sample".to_string(), &serde_json::from_str(pact_json)?)?;
//!
//! // Create the mock server. Note that the async version requires a Tokio runtime.
//! let mut mock_server = MockServerBuilder::new()
//! .bind_to("127.0.0.1:0")
//! .with_pact(pact.boxed())
//! .start()
//! .await?;
//!
//! // We can now make any requests to the mock server
//! let http_client = reqwest::Client::new();
//! let response = http_client.get(format!("http://127.0.0.1:{}/path", mock_server.port()).as_str())
//! .send()
//! .await?;
//! assert_eq!(response.text().await?, "Hello from the mock server");
//!
//! // Shut the mock server down. This will dispose of the running background tasks.
//! mock_server.shutdown()?;
//!
//! // Finally we can now check the status of the mock server.
//! assert_eq!(mock_server.all_matched(), true);
//!
//! # Ok::<(), anyhow::Error>(())
//! # });
//! ```
;
use Mutex;
use hashmap;
use ;
use task_local;
use ;
use crateServerManager;
task_local!
/// Mock server errors
lazy_static!
lazy_static!
/// Sets up all the core catalogue entries for mock servers
/// Write Pact File Errors