Using HTTP in Modern C++

Testing HTTP Code Locally

How can I test my HTTP code locally without making real network requests?

Abstract art representing computer programming

When developing HTTP code, it's useful to be able to test it without needing to send real requests over the internet each time. There are a few ways we can do this:

Option 1: Use a local web server like Apache or Nginx to serve responses to HTTP requests sent to localhost or We can configure it to return specific responses for testing.

Option 2: Use a tool like Postman or Insomnia to manually send requests to our code running locally. These let us craft any arbitrary request.

Option 3: Use an online service like that generates HTTP responses we can control via the URL path and query parameters. For example:

cpr::Url URL{
cpr::Response r = cpr::Get(URL);

This will return a 200 OK response we can use for testing. We can change the URL to /status/404 to get a 404 Not Found response, etc.

Option 4: Create a mock or fake version of the cpr::Get function that returns hard-coded cpr::Response objects without making a network request:

cpr::Response Get(cpr::Url& url,
                  cpr::Parameters& params) {
  cpr::Response r;
  r.status_code = 200;
  r.text = "Dummy response text";
  return r;

Using techniques like these we can test our HTTP logic locally in a controlled way before deploying it to make real network requests.

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

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