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
#![warn(missing_docs)]
#[cfg_attr(test, macro_use)] extern crate pact_matching;
extern crate serde_json;
extern crate hyper;
extern crate futures;
extern crate tokio;
#[macro_use] extern crate log;
extern crate uuid;
extern crate itertools;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate maplit;
pub mod matching;
pub mod mock_server;
pub mod server_manager;
mod hyper_server;
use pact_matching::models::Pact;
use pact_matching::s;
use std::sync::Mutex;
use serde_json::json;
use uuid::Uuid;
use server_manager::ServerManager;
pub enum MockServerError {
InvalidPactJson,
MockServerFailedToStart
}
lazy_static! {
pub static ref MANAGER: Mutex<Option<ServerManager>> = Mutex::new(Option::None);
}
pub fn start_mock_server(id: String, pact: Pact, addr: std::net::SocketAddr) -> Result<i32, String> {
MANAGER.lock().unwrap()
.get_or_insert_with(ServerManager::new)
.start_mock_server_with_addr(id, pact, addr)
.map(|addr| addr.port() as i32)
}
pub extern fn create_mock_server(pact_json: &str, addr: std::net::SocketAddr) -> Result<i32, MockServerError> {
match serde_json::from_str(pact_json) {
Ok(pact_json) => {
let pact = Pact::from_json(&s!("<create_mock_server>"), &pact_json);
start_mock_server(Uuid::new_v4().simple().to_string(), pact, addr)
.map_err(|err| {
error!("Could not start mock server: {}", err);
MockServerError::MockServerFailedToStart
})
},
Err(err) => {
error!("Could not parse pact json: {}", err);
Err(MockServerError::InvalidPactJson)
}
}
}
pub extern fn mock_server_matched(mock_server_port: i32) -> bool {
MANAGER.lock().unwrap()
.get_or_insert_with(ServerManager::new)
.find_mock_server_by_port_mut(mock_server_port as u16, &|mock_server| {
mock_server.mismatches().is_empty()
})
.unwrap_or(false)
}
pub extern fn mock_server_mismatches(mock_server_port: i32) -> Option<std::string::String> {
MANAGER.lock().unwrap()
.get_or_insert_with(ServerManager::new)
.find_mock_server_by_port_mut(mock_server_port as u16, &|mock_server| {
let mismatches = mock_server.mismatches().iter()
.map(|mismatch| mismatch.to_json() )
.collect::<Vec<serde_json::Value>>();
json!(mismatches).to_string()
})
}
pub enum WritePactFileErr {
IOError,
NoMockServer
}
pub extern fn write_pact_file(mock_server_port: i32, directory: Option<String>) -> Result<(), WritePactFileErr> {
let opt_result = MANAGER.lock().unwrap()
.get_or_insert_with(ServerManager::new)
.find_mock_server_by_port_mut(mock_server_port as u16, &|mock_server| {
mock_server.write_pact(&directory)
.map(|_| ())
.map_err(|err| {
error!("Failed to write pact to file - {}", err);
WritePactFileErr::IOError
})
});
match opt_result {
Some(result) => result,
None => {
error!("No mock server running on port {}", mock_server_port);
Err(WritePactFileErr::NoMockServer)
}
}
}
#[cfg(test)]
#[macro_use(expect)]
extern crate expectest;
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
mod tests;