{"id":19409723,"url":"https://github.com/seznam/httpmockserver","last_synced_at":"2025-04-24T10:32:08.099Z","repository":{"id":47118309,"uuid":"102713663","full_name":"seznam/httpmockserver","owner":"seznam","description":"C++ HTTP mock server for client tests.","archived":false,"fork":false,"pushed_at":"2021-08-09T15:21:20.000Z","size":720,"stargazers_count":30,"open_issues_count":5,"forks_count":11,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-04-11T15:28:48.164Z","etag":null,"topics":["cpp","mock","mockserver","testing","tests"],"latest_commit_sha":null,"homepage":"https://seznam.github.io/httpmockserver","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seznam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-07T08:45:35.000Z","updated_at":"2024-03-30T10:28:37.000Z","dependencies_parsed_at":"2022-09-09T08:22:11.264Z","dependency_job_id":null,"html_url":"https://github.com/seznam/httpmockserver","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Fhttpmockserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Fhttpmockserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Fhttpmockserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seznam%2Fhttpmockserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seznam","download_url":"https://codeload.github.com/seznam/httpmockserver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223948496,"owners_count":17230132,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","mock","mockserver","testing","tests"],"created_at":"2024-11-10T12:12:57.696Z","updated_at":"2024-11-10T12:12:58.344Z","avatar_url":"https://github.com/seznam.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C++ HTTP mock server\n\nLibrary for easier HTTP clients testing using simply defined HTTP mock server.\n\n## Dependencies\n\n  * [libmicrohttpd](https://www.gnu.org/software/libmicrohttpd/) (package `libmicrohttpd-dev` on debian based systems).\n  * [cpr](https://github.com/whoshuu/cpr) (tests only, downloaded as git submodules by default).\n  * [gtest](https://github.com/google/googletest) (tests only, downloaded as git submodules by default).\n  * CMake build system.\n  * C++11 compiler (G++ 4.9 tested).\n\n### CMake options\n\nCPR and GTest libraries are by default downloaded as git submodules, but it can be used with system-wide libraries\nwhen following cmake options are used:\n\n  * `-DUSE_SYSTEM_CPR=ON` - Use libcpr from system, do not build it from submodule. Default OFF.\n    * `-DUSE_SYSTEM_CURL=ON` - Use libcurl for libcpr from system, do not build it on its own. Default OFF.\n  * `-DUSE_SYSTEM_GTEST=ON` - Use Google Test from system, do not build it from submodule. Default OFF.\n  * `-DUSE_ALL_SYSTEM_LIBS=ON` - Use both libcpr and GTest libraries from system paths. Default OFF.\n\n## Build\n\nIf you are not using CPR and GTest from system, it is necessary to initialize git submodules:\n\n```sh\ngit submodule update --init --recursive\n```\n\nRun following commands to build the library:\n\n```sh\nmkdir build \u0026\u0026 cd build\ncmake ..\nmake\n```\n\nCustom installation directory can be specified using `cmake -DCMAKE_INSTALL_PREFIX:PATH=some/path` option.\nBy default the `/usr/local/` will be used on most systems.\nInstallation of the library and headers into defined prefix path is possible with `make install` command.\n\n## How to use it\n\nHere is the quick sample how to use mock server in GTests:\n\n```c++\n#include \u003cgtest/gtest.h\u003e\n#include \u003cstring\u003e\n#include \u003chttpmockserver/mock_server.h\u003e\n#include \u003chttpmockserver/test_environment.h\u003e\n\n\nclass HTTPMock: public httpmock::MockServer {\n  public:\n    /// Create HTTP server on port 9200\n    explicit HTTPMock(int port = 9200): MockServer(port) {}\n  private:\n\n    /// Handler called by MockServer on HTTP request.\n    Response responseHandler(\n            const std::string \u0026url,\n            const std::string \u0026method,\n            const std::string \u0026data,\n            const std::vector\u003cUrlArg\u003e \u0026urlArguments,\n            const std::vector\u003cHeader\u003e \u0026headers)\n    {\n        if (method == \"POST\" \u0026\u0026 matchesPrefix(url, \"/example\")) {\n            // Do something and return response\n            return Response(500, \"Fake HTTP response\");\n        }\n        // Return \"URI not found\" for the undefined methods\n        return Response(404, \"Not Found\");\n    }\n\n    /// Return true if \\p url starts with \\p str.\n    bool matchesPrefix(const std::string \u0026url, const std::string \u0026str) const {\n        return url.substr(0, str.size()) == str;\n    }\n};\n\n\nTEST(MyTest, dummyTest) {\n    // Here should be implementation of test case using HTTP server.\n    // HTTP requests are processed by HTTPMock::responseHandler(...)\n    // I. e.: when HTTP POST request is sent on localhost:9200/example, then\n    // response with status code 500 and body \"Fake HTTP response\" is returned.\n}\n\n\nint main(int argc, char *argv[]) {\n    ::testing::InitGoogleTest(\u0026argc, argv);\n    ::testing::AddGlobalTestEnvironment(new httpmock::TestEnvironment\u003cHTTPMock\u003e());\n    return RUN_ALL_TESTS();\n}\n```\n\nIf you do not want to rely on hardcoded port number, you can use port searching\nclass. Searching for unused port is done in 1000 iterations by default.\n\n```c++\n// ... class HTTPMock is same as above\n\n/// Server started in the main().\nstatic httpmock::TestEnvironment\u003chttpmock::MockServerHolder\u003e* mock_server_env = nullptr;\n\nTEST(MyTest, dummyTest) {\n    assert(nullptr != mock_server_env);\n    const int port = mock_server_env-\u003egetMock()-\u003egetPort();\n    // Here should be implementation of test case using HTTP server.\n    // HTTP requests are processed by HTTPMock::responseHandler(...)\n    // I. e.: when HTTP POST request is sent on localhost:$port/example, then\n    // response with status code 500 and body \"Fake HTTP response\" is returned.\n}\n\nint main(int argc, char *argv[]) {\n    ::testing::InitGoogleTest(\u0026argc, argv);\n    // startup the server for the tests\n    ::testing::Environment * const env = ::testing::AddGlobalTestEnvironment(\n            httpmock::createMockServerEnvironment\u003cHTTPMock\u003e(9200));\n    // set global env pointer\n    mock_server_env\n        = dynamic_cast\u003chttpmock::TestEnvironment\u003chttpmock::MockServerHolder\u003e *\u003e(env);\n    return RUN_ALL_TESTS();\n}\n```\n\n# License\n\nLibrary is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseznam%2Fhttpmockserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseznam%2Fhttpmockserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseznam%2Fhttpmockserver/lists"}