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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! 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 V2 Pact specification
//! (https://github.com/pact-foundation/pact-specification/tree/version-2).
//!
//! There are a number of exported functions using C bindings for controlling the mock server. These can be used in any
//! language that supports C bindings.
//!
//! ## [create_mock_server](fn.create_mock_server.html)
//!
//! External interface to create a mock server. A pointer to the pact JSON as a C string is passed in,
//! as well as the port for the mock server to run on. A value of 0 for the port will result in a
//! port being allocated by the operating system. The port of the mock server is returned.
//!
//! ## [mock_server_matched](fn.mock_server_matched.html)
//!
//! Simple function that returns a boolean value given the port number of the mock service. This value will be true if all
//! the expectations of the pact that the mock server was created with have been met. It will return false if any request did
//! not match, an un-recognised request was received or an expected request was not received.
//!
//! ## [mock_server_mismatches](fn.mock_server_mismatches.html)
//!
//! This returns all the mismatches, un-expected requests and missing requests in JSON format, given the port number of the
//! mock server.
//!
//! **IMPORTANT NOTE:** The JSON string for the result is allocated on the rust heap, and will have to be freed once the
//! code using the mock server is complete. The [`cleanup_mock_server`](fn.cleanup_mock_server.html) function is provided for this purpose. If the mock
//! server is not cleaned up properly, this will result in memory leaks as the rust heap will not be reclaimed.
//!
//! ## [cleanup_mock_server](fn.cleanup_mock_server.html)
//!
//! This function will try terminate the mock server with the given port number and cleanup any memory allocated for it by
//! the [`mock_server_mismatches`](fn.mock_server_mismatches.html) function. Returns `true`, unless a mock server with the given port number does not exist,
//! or the function fails in some way.
//!
//! **NOTE:** Although `close()` on the listerner for the mock server is called, this does not currently work and the
//! listerner will continue handling requests. In this case, it will always return a 501 once the mock server has been
//! cleaned up.
//!
//! ## [write_pact_file](fn.write_pact_file.html)
//!
//! External interface to trigger a mock server to write out its pact file. This function should
//! be called if all the consumer tests have passed. The directory to write the file to is passed
//! as the second parameter. If a NULL pointer is passed, the current working directory is used.
//!
//! Returns 0 if the pact file was successfully written. Returns a positive code if the file can
//! not be written, or there is no mock server running on that port or the function panics.

#![warn(missing_docs)]

#[macro_use] extern crate log;
#[macro_use] extern crate p_macro;
#[macro_use] extern crate maplit;
#[macro_use] extern crate lazy_static;
extern crate libc;
#[macro_use] extern crate pact_matching;
extern crate rustc_serialize;
extern crate env_logger;
#[macro_use] extern crate hyper;
extern crate uuid;
#[macro_use] extern crate itertools;

use libc::{c_char, int32_t};
use std::ffi::CStr;
use std::ffi::CString;
use std::str;
use std::panic::catch_unwind;
use pact_matching::models::{Pact, Interaction, Request, OptionalBody};
use pact_matching::models::parse_query_string;
use pact_matching::Mismatch;
use rustc_serialize::json::{self, Json, ToJson};
use std::collections::{BTreeMap, HashMap};
use std::thread;
use std::sync::Mutex;
use std::sync::mpsc::channel;
use std::io::{self, Read, Write};
use std::path::PathBuf;
use hyper::server::{Server, Listening};
use hyper::status::StatusCode;
use hyper::header::{Headers, ContentType, AccessControlAllowOrigin, ContentLength};
use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value};
use hyper::uri::RequestUri;
use uuid::Uuid;
use itertools::Itertools;

/// Enum to define a match result
#[derive(Debug, Clone, PartialEq)]
pub enum MatchResult {
    /// Match result where the request was sucessfully matched
    RequestMatch(Interaction),
    /// Match result where there were a number of mismatches
    RequestMismatch(Interaction, Vec<Mismatch>),
    /// Match result where the request was not expected
    RequestNotFound(Request),
    /// Match result where an expected request was not received
    MissingRequest(Interaction)
}

impl MatchResult {
    /// Returns the match key for this mismatch
    pub fn match_key(&self) -> String {
        match self {
            &MatchResult::RequestMatch(_) => s!("Request-Matched"),
            &MatchResult::RequestMismatch(_, _) => s!("Request-Mismatch"),
            &MatchResult::RequestNotFound(_) => s!("Unexpected-Request"),
            &MatchResult::MissingRequest(_) => s!("Missing-Request")
        }
    }

    /// Returns true if this match result is a `RequestMatch`
    pub fn matched(&self) -> bool {
        match self {
            &MatchResult::RequestMatch(_) => true,
            _ => false
        }
    }

    /// Converts this match result to a `Json` struct
    pub fn to_json(&self) -> Json {
        match self {
            &MatchResult::RequestMatch(_) => Json::Object(btreemap!{ s!("type") => s!("request-match").to_json() }),
            &MatchResult::RequestMismatch(ref interaction, ref mismatches) => mismatches_to_json(&interaction.request, mismatches),
            &MatchResult::RequestNotFound(ref req) => Json::Object(btreemap!{
                s!("type") => s!("request-not-found").to_json(),
                s!("method") => req.method.to_json(),
                s!("path") => req.path.to_json(),
                s!("request") => req.to_json()
            }),
            &MatchResult::MissingRequest(ref interaction) => Json::Object(btreemap!{
                s!("type") => s!("missing-request").to_json(),
                s!("method") => interaction.request.method.to_json(),
                s!("path") => interaction.request.path.to_json(),
                s!("request") => interaction.request.to_json()
            })
        }
    }
}

fn mismatches_to_json(request: &Request, mismatches: &Vec<Mismatch>) -> Json {
    Json::Object(btreemap!{
        s!("type") => s!("request-mismatch").to_json(),
        s!("method") => request.method.to_json(),
        s!("path") => request.path.to_json(),
        s!("mismatches") => Json::Array(mismatches.iter().map(|m| m.to_json()).collect())
    })
}

/// Struct to represent a mock server
pub struct MockServer {
    /// Mock server unique ID
    pub id: String,
    /// Port the mock server is running on
    pub port: i32,
    /// Address of the server implementing the `Listening` trait
    pub server: u64,
    /// List of all match results for requests this mock server has received
    pub matches: Vec<MatchResult>,
    /// List of resources that need to be cleaned up when the mock server completes
    pub resources: Vec<CString>,
    /// Pact that this mock server is based on
    pub pact: Pact
}

impl MockServer {
    /// Creates a new mock server with the given ID and pact
    pub fn new(id: String, pact: &Pact) -> MockServer {
        MockServer { id: id.clone(), port: -1, server: 0, matches: vec![], resources: vec![],
            pact : pact.clone() }
    }

    /// Sets the port that the mock server is listening on
    pub fn port(&mut self, port: i32) {
        self.port = port;
    }

    /// Sets the address of the server implementing the `Listening` trait
    pub fn server(&mut self, server: &Listening) {
        let p = server as *const Listening;
        self.server = p as u64;
    }

    /// Converts this mock server to a `Json` struct
    pub fn to_json(&self) -> Json {
        Json::Object(btreemap!{
            s!("id") => Json::String(self.id.clone()),
            s!("port") => Json::U64(self.port as u64),
            s!("provider") => Json::String(self.pact.provider.name.clone()),
            s!("status") => Json::String(if self.mismatches().is_empty() {
                    s!("ok")
                } else {
                    s!("error")
                }
            )
        })
    }

    /// Returns all the mismatches that have occured with this mock server
    pub fn mismatches(&self) -> Vec<MatchResult> {
        let mismatches = self.matches.iter()
            .filter(|m| !m.matched())
            .map(|m| m.clone());
        let interactions: Vec<&Interaction> = self.matches.iter().map(|m| {
            match *m {
                MatchResult::RequestMatch(ref interaction) => Some(interaction),
                MatchResult::RequestMismatch(ref interaction, _) => Some(interaction),
                MatchResult::RequestNotFound(_) => None,
                MatchResult::MissingRequest(_) => None
            }
        }).filter(|o| o.is_some()).map(|o| o.unwrap()).collect();
        let missing = self.pact.interactions.iter()
            .filter(|i| !interactions.contains(i))
            .map(|i| MatchResult::MissingRequest(i.clone()));
        mismatches.chain(missing).collect()
    }

    /// Mock server writes it pact out to the provided directory
    pub fn write_pact(&self, output_path: &Option<String>) -> io::Result<()> {
        let pact_file_name = self.pact.default_file_name();
        let filename = match *output_path {
            Some(ref path) => {
                let mut path = PathBuf::from(path);
                path.push(pact_file_name);
                path
            },
            None => PathBuf::from(pact_file_name)
        };
        info!("Writing pact out to '{}'", filename.display());
        match self.pact.write_pact(filename.as_path()) {
            Ok(_) => Ok(()),
            Err(err) => {
                warn!("Failed to write pact to file - {}", err);
                Err(err)
            }
        }
    }

    /// Returns the URL of the mock server
    pub fn url(&self) -> String {
        format!("http://localhost:{}", self.port)
    }
}

impl PartialEq for MockServer {
    fn eq(&self, other: &MockServer) -> bool {
        self.id == other.id
    }
}

lazy_static! {
    static ref MOCK_SERVERS: Mutex<BTreeMap<String, Box<MockServer>>> = Mutex::new(BTreeMap::new());
}

fn match_request(req: &Request, interactions: &Vec<Interaction>) -> MatchResult {
    let match_results = interactions
        .into_iter()
        .map(|i| (i.clone(), pact_matching::match_request(i.request.clone(), req.clone())))
        .sorted_by(|i1, i2| {
            let list1 = i1.1.clone().into_iter().map(|m| m.mismatch_type()).unique().count();
            let list2 = i2.1.clone().into_iter().map(|m| m.mismatch_type()).unique().count();
            Ord::cmp(&list1, &list2)
        });
    match match_results.first() {
        Some(res) => {
            if res.1.is_empty() {
                MatchResult::RequestMatch(res.0.clone())
            } else if method_or_path_mismatch(&res.1) {
                MatchResult::RequestNotFound(req.clone())
            } else {
                MatchResult::RequestMismatch(res.0.clone(), res.1.clone())
            }
        },
        None => MatchResult::RequestNotFound(req.clone())
    }
}

fn method_or_path_mismatch(mismatches: &Vec<Mismatch>) -> bool {
    let mismatch_types: Vec<String> = mismatches.iter()
        .map(|mismatch| mismatch.mismatch_type())
        .collect();
    mismatch_types.contains(&s!("MethodMismatch")) || mismatch_types.contains(&s!("PathMismatch"))
}

fn extract_path(uri: &RequestUri) -> String {
    match uri {
        &RequestUri::AbsolutePath(ref s) => s!(s.splitn(2, "?").next().unwrap_or("/")),
        &RequestUri::AbsoluteUri(ref url) => s!(url.path()),
        _ => uri.to_string()
    }
}

fn extract_query_string(uri: &RequestUri) -> Option<HashMap<String, Vec<String>>> {
    match uri {
        &RequestUri::AbsolutePath(ref s) => {
            if s.contains("?") {
                match s.splitn(2, "?").last() {
                    Some(q) => parse_query_string(&s!(q)),
                    None => None
                }
            } else {
                None
            }
        },
        &RequestUri::AbsoluteUri(ref url) => match url.query() {
            Some(q) => parse_query_string(&s!(q)),
            None => None
        },
        _ => None
    }
}

fn extract_headers(headers: &Headers) -> Option<HashMap<String, String>> {
    if headers.len() > 0 {
        Some(headers.iter().map(|h| (s!(h.name()), h.value_string()) ).collect())
    } else {
        None
    }
}

fn extract_body(req: &mut hyper::server::Request) -> OptionalBody {
    let mut buffer = String::new();
    match req.read_to_string(&mut buffer) {
        Ok(size) => if size > 0 {
                OptionalBody::Present(buffer)
            } else {
                OptionalBody::Empty
            },
        Err(err) => {
            warn!("Failed to read request body: {}", err);
            OptionalBody::Empty
        }
    }
}

fn hyper_request_to_pact_request(req: &mut hyper::server::Request) -> Request {
    Request {
        method: req.method.to_string(),
        path: extract_path(&req.uri),
        query: extract_query_string(&req.uri),
        headers: extract_headers(&req.headers),
        body: extract_body(req),
        matching_rules: None
    }
}

fn error_body(req: &Request, error: &String) -> String {
    let body = hashmap!{ "error" => format!("{} : {:?}", error, req) };
    let json = json::encode(&body).unwrap();
    json.clone()
}

fn insert_new_mock_server(id: &String, pact: &Pact) {
    MOCK_SERVERS.lock().unwrap().insert(id.clone(), Box::new(MockServer::new(id.clone(), pact)));
}

fn update_mock_server<R>(id: &String, f: &Fn(&mut MockServer) -> R) -> Option<R> {
    match MOCK_SERVERS.lock().unwrap().get_mut(id) {
        Some(mock_server) => Some(f(mock_server)),
        _ => None
    }
}

fn update_mock_server_by_port<R>(port: i32, f: &Fn(&mut MockServer) -> R) -> Option<R> {
    let mut map = MOCK_SERVERS.lock().unwrap();
    match map.iter_mut().find(|ms| ms.1.port == port ) {
        Some(mock_server) => Some(f(mock_server.1)),
        None => None
    }
}

fn record_result(id: &String, match_result: &MatchResult) {
    update_mock_server(id, &|mock_server: &mut MockServer| {
        mock_server.matches.push(match_result.clone());
    });
}

/// Starts a mock server with the given ID, pact and port number. The ID needs to be unique. A port
/// number of 0 will result in an auto-allocated port by the operating system. Returns the port
/// that the mock server is running on wrapped in a `Result`.
///
/// # Errors
///
/// An error with a message will be returned in the following conditions:
///
/// - If a mock server is not able to be started
pub fn start_mock_server(id: String, pact: Pact, port: i32) -> Result<i32, String> {
    insert_new_mock_server(&id, &pact);
    let (out_tx, out_rx) = channel();
    let (in_tx, in_rx) = channel();
    in_tx.send((id.clone(), pact, port)).unwrap();
    thread::spawn(move || {
        let (mock_server_id, pact, port) = in_rx.recv().unwrap();
        let server = Server::http(format!("0.0.0.0:{}", port).as_str()).unwrap();
        let server_result = server.handle(move |mut req: hyper::server::Request, mut res: hyper::server::Response| {
            debug!("--> Hyper request to mock server {}", mock_server_id);
            match lookup_mock_server(mock_server_id.clone(), &|_| ()) {
                None => {
                    warn!("Mock server {} has been shutdown", mock_server_id);
                    *res.status_mut() = StatusCode::NotImplemented;
                    res.headers_mut().set_raw("X-Pact", vec!["Mock server has been shut down".as_bytes().to_vec()]);
                },
                Some(_) => {
                    debug!("Creating pact request from hyper request");
                    let req = hyper_request_to_pact_request(&mut req);
                    info!("Received request {:?}", req);
                    let match_result = match_request(&req, &pact.interactions);
                    record_result(&mock_server_id, &match_result);
                    match match_result {
                        MatchResult::RequestMatch(ref interaction) => {
                            info!("Request matched, sending response {:?}", interaction.response);
                            *res.status_mut() = StatusCode::from_u16(interaction.response.status);
                            res.headers_mut().set(AccessControlAllowOrigin::Any);
                            match interaction.response.headers {
                                Some(ref headers) => {
                                    for (k, v) in headers.clone() {
                                        res.headers_mut().set_raw(k, vec![v.into_bytes()]);
                                    }
                                },
                                None => ()
                            }
                            match interaction.response.body {
                                OptionalBody::Present(ref body) => {
                                    res.send(body.as_bytes()).unwrap();
                                },
                                _ => ()
                            }
                        },
                        _ => {
                            *res.status_mut() = StatusCode::InternalServerError;
                            res.headers_mut().set(
                                ContentType(Mime(TopLevel::Application, SubLevel::Json,
                                                 vec![(Attr::Charset, Value::Utf8)]))
                            );
                            res.headers_mut().set(AccessControlAllowOrigin::Any);
                            res.headers_mut().set_raw("X-Pact", vec![match_result.match_key().as_bytes().to_vec()]);
                            let body = error_body(&req, &match_result.match_key());
                            res.headers_mut().set(ContentLength(body.as_bytes().len() as u64));
                            let mut res = res.start().unwrap();
                            res.write_all(body.as_bytes()).unwrap();
                        }
                    }
                }
            }
        });

        match server_result {
            Ok(ref server) => {
                let port = server.socket.port() as i32;
                info!("Mock Provider Server started on port {}", port);
                update_mock_server(&id, &|mock_server| {
                    mock_server.port(port);
                    mock_server.server(server);
                });
                out_tx.send(Ok(port)).unwrap();
            },
            Err(e) => {
                error!("Could not start server: {}", e);
                out_tx.send(Err(format!("Could not start server: {}", e))).unwrap();
            }
        }
    });

    out_rx.recv().unwrap()
}

/// Looks up the mock server by ID, and passes it into the given closure. The result of the
/// closure is returned wrapped in an `Option`. If no mock server is found with that ID, `None`
/// is returned.
pub fn lookup_mock_server<R>(id: String, f: &Fn(&MockServer) -> R) -> Option<R> {
    debug!("Looking up mock server with ID {}", id);
    let map = MOCK_SERVERS.lock().unwrap();
    match map.get(&id) {
        Some(ref mock_server) => {
            debug!("Found mock server, invoking function ...");
            Some(f(mock_server))
        },
        None => {
            debug!("Did not find mock server");
            None
        }
    }
}

/// Looks up the mock server by port number, and passes it into the given closure. The result of the
/// closure is returned wrapped in an `Option`. If no mock server is found with that port number, `None`
/// is returned.
pub fn lookup_mock_server_by_port<R>(mock_server_port: i32, f: &Fn(&MockServer) -> R) -> Option<R> {
    let map = MOCK_SERVERS.lock().unwrap();
    match map.iter().find(|ms| ms.1.port == mock_server_port ) {
        Some(mock_server) => Some(f(mock_server.1)),
        None => None
    }
}

/// Iterates through all the mock servers, passing each one to the given closure.
pub fn iterate_mock_servers(f: &mut FnMut(&String, &MockServer)) {
    let map = MOCK_SERVERS.lock().unwrap();
    for (key, value) in map.iter() {
        f(key, value);
    }
}

fn cleanup_mock_server_impl(mock_server: &mut MockServer) -> String {
    mock_server.resources.clear();
    if mock_server.server > 0 {
        let server_raw = mock_server.server as *mut Listening;
        let mut server_ref = unsafe { &mut *server_raw };
        server_ref.close().unwrap();
    }
    mock_server.id.clone()
}

/// Shuts and cleans up the mock server with the given id. Returns true if a mock server was
/// found, false otherwise.
///
/// **NOTE:** Although `close()` on the listerner for the mock server is called, this does not
/// currently work and the listerner will continue handling requests. In this
/// case, it will always return a 404 once the mock server has been cleaned up.
pub fn shutdown_mock_server(id: &String) -> bool {
    debug!("Shutting down mock server with ID {}", id);
    let id_result = update_mock_server(id, &cleanup_mock_server_impl);

    match id_result {
        Some(ref id) => {
            MOCK_SERVERS.lock().unwrap().remove(id);
            true
        },
        None => false
    }
}

/// Shuts and cleans up the mock server with the given port. Returns true if a mock server was
/// found, false otherwise.
///
/// **NOTE:** Although `close()` on the listerner for the mock server is called, this does not
/// currently work and the listerner will continue handling requests. In this
/// case, it will always return a 404 once the mock server has been cleaned up.
pub fn shutdown_mock_server_by_port(port: i32) -> bool {
    debug!("Shutting down mock server with port {}", port);
    let id_result = update_mock_server_by_port(port, &cleanup_mock_server_impl);

    match id_result {
        Some(ref id) => {
            MOCK_SERVERS.lock().unwrap().remove(id);
            true
        },
        None => false
    }
}

/// External interface to create a mock server. A pointer to the pact JSON as a C string is passed in,
/// as well as the port for the mock server to run on. A value of 0 for the port will result in a
/// port being allocated by the operating system. The port of the mock server is returned.
///
/// # Errors
///
/// Errors are returned as negative values.
///
/// | Error | Description |
/// |-------|-------------|
/// | -1 | A null pointer was received |
/// | -2 | The pact JSON could not be parsed |
/// | -3 | The mock server could not be started |
/// | -4 | The method paniced |
///
#[no_mangle]
pub extern fn create_mock_server(pact_str: *const c_char, port: int32_t) -> int32_t {
    env_logger::init().unwrap_or(());

    let result = catch_unwind(|| {
        let c_str = unsafe {
            if pact_str.is_null() {
                error!("Got a null pointer instead of pact json");
                return -1;
            }
            CStr::from_ptr(pact_str)
        };

        let pact_json = str::from_utf8(c_str.to_bytes()).unwrap();
        let result = Json::from_str(pact_json);
        match result {
            Ok(pact_json) => {
                let pact = Pact::from_json(&s!("<create_mock_server>"), &pact_json);
                match start_mock_server(Uuid::new_v4().simple().to_string(), pact, port) {
                    Ok(mock_server) => mock_server as i32,
                    Err(msg) => {
                        error!("Could not start mock server: {}", msg);
                        -3
                    }
                }
            },
            Err(err) => {
                error!("Could not parse pact json: {}", err);
                -2
            }
        }
    });

    match result {
        Ok(val) => val,
        Err(cause) => {
            error!("Caught a general panic: {:?}", cause);
            -4
        }
    }
}

/// External interface to check if a mock server has matched all its requests. The port number is
/// passed in, and if all requests have been matched, true is returned. False is returned if there
/// is no mock server on the given port, or if any request has not been successfully matched, or
/// the method panics.
#[no_mangle]
pub extern fn mock_server_matched(mock_server_port: int32_t) -> bool {
    let result = catch_unwind(|| {
        lookup_mock_server_by_port(mock_server_port, &|mock_server| {
            mock_server.mismatches().is_empty()
        }).unwrap_or(false)
    });

    match result {
        Ok(val) => val,
        Err(cause) => {
            error!("Caught a general panic: {:?}", cause);
            false
        }
    }
}

/// External interface to get all the mismatches from a mock server. The port number of the mock
/// server is passed in, and a pointer to a C string with the mismatches in JSON format is
/// returned.
///
/// **NOTE:** The JSON string for the result is allocated on the heap, and will have to be freed
/// once the code using the mock server is complete. The [`cleanup_mock_server`](fn.cleanup_mock_server.html) function is
/// provided for this purpose.
///
/// # Errors
///
/// If there is no mock server with the provided port number, or the function panics, a NULL
/// pointer will be returned. Don't try to dereference it, it will not end well for you.
///
#[no_mangle]
pub extern fn mock_server_mismatches(mock_server_port: int32_t) -> *mut c_char {
    let result = catch_unwind(|| {
        let result = update_mock_server_by_port(mock_server_port, &|ref mut mock_server| {
            let mismatches = mock_server.mismatches().iter()
                .map(|mismatch| mismatch.to_json() )
                .collect::<Vec<Json>>();
            let json = Json::Array(mismatches);
            let s = CString::new(json.to_string()).unwrap();
            let p = s.as_ptr();
            mock_server.resources.push(s);
            p
        });
        match result {
            Some(p) => p as *mut _,
            None => 0 as *mut _
        }
    });

    match result {
        Ok(val) => val,
        Err(cause) => {
            error!("Caught a general panic: {:?}", cause);
            0 as *mut _
        }
    }
}

/// External interface to cleanup a mock server. This function will try terminate the mock server
/// with the given port number and cleanup any memory allocated for it. Returns true, unless a
/// mock server with the given port number does not exist, or the function panics.
///
/// **NOTE:** Although `close()` on the listerner for the mock server is called, this does not
/// currently work and the listerner will continue handling requests. In this
/// case, it will always return a 404 once the mock server has been cleaned up.
#[no_mangle]
pub extern fn cleanup_mock_server(mock_server_port: int32_t) -> bool {
    let result = catch_unwind(|| {
        shutdown_mock_server_by_port(mock_server_port)
    });

    match result {
        Ok(val) => val,
        Err(cause) => {
            error!("Caught a general panic: {:?}", cause);
            false
        }
    }
}

/// External interface to trigger a mock server to write out its pact file. This function should
/// be called if all the consumer tests have passed. The directory to write the file to is passed
/// as the second parameter. If a NULL pointer is passed, the current working directory is used.
///
/// Returns 0 if the pact file was successfully written. Returns a positive code if the file can
/// not be written, or there is no mock server running on that port or the function panics.
///
/// # Errors
///
/// Errors are returned as positive values.
///
/// | Error | Description |
/// |-------|-------------|
/// | 1 | A general panic was caught |
/// | 2 | The pact file was not able to be written |
/// | 3 | A mock server with the provided port was not found |
#[no_mangle]
pub extern fn write_pact_file(mock_server_port: int32_t, directory: *const c_char) -> int32_t {
    let result = catch_unwind(|| {
        let dir = unsafe {
            if directory.is_null() {
                warn!("Directory to write to is NULL, defaulting to the current working directory");
                None
            } else {
                let c_str = CStr::from_ptr(directory);
                let dir_str = str::from_utf8(c_str.to_bytes()).unwrap();
                if dir_str.is_empty() {
                    None
                } else {
                    Some(s!(dir_str))
                }
            }
        };

        lookup_mock_server_by_port(mock_server_port, &|mock_server| {
            match mock_server.write_pact(&dir) {
                Ok(_) => 0,
                Err(err) => {
                    error!("Failed to write pact to file - {}", err);
                    2
                }
            }
        }).unwrap_or(3)
    });

    match result {
        Ok(val) => val,
        Err(cause) => {
            error!("Caught a general panic: {:?}", cause);
            1
        }
    }
}

#[cfg(test)]
#[macro_use(expect)]
extern crate expectest;

#[cfg(test)]
extern crate quickcheck;

#[cfg(test)]
mod tests;