Using HTTP in Modern C++

Mocking HTTP Calls in Unit Tests

How can I unit test code that makes HTTP requests without actually making network calls?

Abstract art representing computer programming

Unit testing is an important part of building reliable software in any language, including C++. But testing code that makes HTTP requests can be tricky - we don't want our tests to actually send network traffic or rely on a particular server being available.

The solution is to use mocking or faking to replace the real HTTP calls with simulated versions that always return a pre-configured response.

There are a number of C++ mocking libraries available that support this, such as GoogleMock or FakeIt. But even without a mocking library, we can apply the basic principle.

Here's an example of how we might structure our code to allow mocking of cpr::Get calls in unit tests:

#include <cpr/cpr.h>
#include <functional>
#include <iostream>

using HttpGetFunc = std::function<cpr::Response(
  cpr::Url, cpr::Parameters)>;

int getNumberOfThings(HttpGetFunc getter) {
  cpr::Url url{"http://www.example.com/things"};
  cpr::Parameters params{{"type", "widgets"}};

  cpr::Response r = getter(url, params);

  if (r.status_code == 200) {
    return std::stoi(r.text);
  } else {
    return -1;
  }
}

cpr::Response cprGet(cpr::Url url,
  cpr::Parameters params) {
  return cpr::Get(url, params);
}

int main() {
  int num = getNumberOfThings(cprGet);
  std::cout << "There are " << num
    << " things.\n";
}

Here the actual business logic is in the getNumberOfThings function. But instead of calling cpr::Get directly, it accepts a getter parameter, which is a function matching the signature of cpr::Get.

In the "production" code path, we call getNumberOfThings(cpr::Get), passing the real cpr::Get function.

But in unit tests, we can pass a mock or fake function that returns a predefined response without making any network calls:

#include <gtest/gtest.h>

cpr::Response mockGet(cpr::Url url, cpr::Parameters params) {
  cpr::Response fakeResponse;
  fakeResponse.status_code = 200;
  fakeResponse.text = "42";
  return fakeResponse;
}

TEST(ThingsTest, GetNumberOfThings) {
  int num = getNumberOfThings(mockGet);
  ASSERT_EQ(num, 42);
}

Our mockGet function always returns a 200 OK response with the text "42", regardless of the URL or parameters passed to it. This allows us to test that getNumberOfThings parses the response correctly without needing any real network access.

Of course, this is a simplified example. In a real application, we'd probably want to check that getNumberOfThings constructs the expected URL and parameters. And we'd want to test error cases by having the mock function return different status codes.

But the principle is the same - by abstracting the HTTP call behind a function interface, we can easily substitute mocks or fakes in tests to avoid relying on network access and make our tests faster and more reliable.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved