httpmockserver
HTTP Mock Server

HTTP mock server allows you to test HTTP clients easily.

Basic information, such as requirements and installation steps, can be found at Readme.md file.

Classes provided

Helper functions

First steps

To create a simple test, you need to follow these steps:

  1. Derive class httpmock::MockServer and implement httpmock::MockServer::responseHandler(). This method is invoked on incomming requests.
  2. Start a server (several choices)
  3. Implement the test interacting with mockserver listening on localhost. Mockserver port can be obtained using httpmock::MockServer::getPort() method, or with following code if Google Test Environment is in use:
    // Mock is class derived from httpmock::IMockServer.
    ::testing::Environment *const env = ::testing::AddGlobalTestEnvironment(
    httpmock::createMockServerEnvironment<Mock>(serverStartingPortNumber));
    int port = mock_server_env->getMock()->getPort();

Request handling

Method httpmock::MockServer::responseHandler() is responsible for request handling. Request arguments are passed as method arguments, which contains request path, method type, headers, POST data and URL arguments.

After processing request, the httpmock::MockServer::Response has to be returned. It contains response body and status.

Custom headers can be added into the response object using httpmock::MockServer::Response::addHeader() method, passing key-value httpmock::MockServer::Header object instances.

httpmock::TestEnvironment
Definition: test_environment.h:20
httpmock::TestEnvironment::getMock
const HTTPMock & getMock() const
Definition: test_environment.h:50