[][src]Crate pact_mock_server

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

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_ffi

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_ffi

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_ffi

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

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

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.

Structs

MockServer

Struct to represent a mock server

Enums

MatchResult

Enum to define a match result

MockServerError

Mock server errors

WritePactFileErr

Write Pact File Errors

Functions

cleanup_mock_server_ffi

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.

create_mock_server

Creates a mock server. Requires the pact JSON as a string 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.

create_mock_server_ffi

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.

iterate_mock_servers

Iterates through all the mock servers, passing each one to the given closure.

lookup_mock_server

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.

lookup_mock_server_by_port

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.

mock_server_matched

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

mock_server_matched_ffi

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.

mock_server_mismatches

Gets all the mismatches from a mock server. The port number of the mock server is passed in, and the results are returned in JSON format as a String.

mock_server_mismatches_ffi

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.

shutdown_mock_server

Shuts and cleans up the mock server with the given id. Returns true if a mock server was found, false otherwise.

shutdown_mock_server_by_port

Shuts and cleans up the mock server with the given port. Returns true if a mock server was found, false otherwise.

start_mock_server

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.

write_pact_file

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 None is passed in, the current working directory is used.

write_pact_file_ffi

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.