39 releases

0.8.0-beta.0 Sep 13, 2021
0.7.19 Jul 11, 2021
0.7.16 Mar 14, 2021
0.7.12 Dec 31, 2020
0.2.0 Jul 13, 2016

#35 in Testing

Download history 200/week @ 2021-06-05 220/week @ 2021-06-12 279/week @ 2021-06-19 237/week @ 2021-06-26 150/week @ 2021-07-03 142/week @ 2021-07-10 300/week @ 2021-07-17 117/week @ 2021-07-24 169/week @ 2021-07-31 78/week @ 2021-08-07 137/week @ 2021-08-14 198/week @ 2021-08-21 162/week @ 2021-08-28 469/week @ 2021-09-04 473/week @ 2021-09-11 122/week @ 2021-09-18

883 downloads per month
Used in 7 crates (6 directly)

MIT license

1MB
24K SLoC

Pact Mock Server library

This library implements the in-process mock server for matching HTTP requests and generating responses from a pact file. It implements the V3 Pact specification.

Online rust docs

For an example of calling these functions, have a look at the JavaScript reference.

All the exported functions using C bindings for controlling the mock server are now found in the pact_mock_server_ffi module.

create_mock_server

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

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

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 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

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 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

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.

Dependencies

~28MB
~673K SLoC