/ src / common / UnitTests-CommonUtils / HttpClient.Tests.cpp
HttpClient.Tests.cpp
  1  #include "pch.h"
  2  #include "TestHelpers.h"
  3  #include <HttpClient.h>
  4  
  5  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  6  
  7  namespace UnitTestsCommonUtils
  8  {
  9      TEST_CLASS(HttpClientTests)
 10      {
 11      public:
 12          // Note: Network tests may fail in offline environments
 13          // These tests are designed to verify the API doesn't crash
 14  
 15          TEST_METHOD(HttpClient_DefaultConstruction)
 16          {
 17              http::HttpClient client;
 18              // Should not crash
 19              Assert::IsTrue(true);
 20          }
 21  
 22          TEST_METHOD(HttpClient_Request_InvalidUri_ReturnsEmpty)
 23          {
 24              http::HttpClient client;
 25  
 26              try
 27              {
 28                  // Invalid URI should not crash, may throw or return empty
 29                  auto result = client.request(winrt::Windows::Foundation::Uri(L"invalid://not-a-valid-uri"));
 30                  // If we get here, result may be empty or contain error
 31              }
 32              catch (...)
 33              {
 34                  // Exception is acceptable for invalid URI
 35              }
 36              Assert::IsTrue(true);
 37          }
 38  
 39          TEST_METHOD(HttpClient_Download_InvalidUri_DoesNotCrash)
 40          {
 41              http::HttpClient client;
 42              TestHelpers::TempFile tempFile;
 43  
 44              try
 45              {
 46                  auto result = client.download(
 47                      winrt::Windows::Foundation::Uri(L"https://invalid.invalid.invalid"),
 48                      tempFile.path());
 49                  // May return false or throw
 50              }
 51              catch (...)
 52              {
 53                  // Exception is acceptable for invalid/unreachable URI
 54              }
 55              Assert::IsTrue(true);
 56          }
 57  
 58          TEST_METHOD(HttpClient_Download_WithCallback_DoesNotCrash)
 59          {
 60              http::HttpClient client;
 61              TestHelpers::TempFile tempFile;
 62              std::atomic<int> callbackCount{ 0 };
 63  
 64              try
 65              {
 66                  auto result = client.download(
 67                      winrt::Windows::Foundation::Uri(L"https://invalid.invalid.invalid"),
 68                      tempFile.path(),
 69                      [&callbackCount]([[maybe_unused]] float progress) {
 70                          callbackCount++;
 71                      });
 72              }
 73              catch (...)
 74              {
 75                  // Exception is acceptable
 76              }
 77              Assert::IsTrue(true);
 78          }
 79  
 80          TEST_METHOD(HttpClient_Download_EmptyPath_DoesNotCrash)
 81          {
 82              http::HttpClient client;
 83  
 84              try
 85              {
 86                  auto result = client.download(
 87                      winrt::Windows::Foundation::Uri(L"https://example.com"),
 88                      L"");
 89              }
 90              catch (...)
 91              {
 92                  // Exception is acceptable for empty path
 93              }
 94              Assert::IsTrue(true);
 95          }
 96  
 97          // These tests require network access and may be skipped in offline environments
 98          TEST_METHOD(HttpClient_Request_ValidUri_ReturnsResult)
 99          {
100              // Skip this test in most CI environments
101              // Only run manually to verify network functionality
102              http::HttpClient client;
103  
104              try
105              {
106                  // Use a reliable, fast-responding URL
107                  auto result = client.request(winrt::Windows::Foundation::Uri(L"https://www.microsoft.com"));
108                  // Result may or may not be successful depending on network
109              }
110              catch (...)
111              {
112                  // Network errors are acceptable in test environment
113              }
114              Assert::IsTrue(true);
115          }
116  
117          // Thread safety test (doesn't require network)
118          TEST_METHOD(HttpClient_MultipleInstances_DoNotCrash)
119          {
120              std::vector<std::unique_ptr<http::HttpClient>> clients;
121  
122              for (int i = 0; i < 10; ++i)
123              {
124                  clients.push_back(std::make_unique<http::HttpClient>());
125              }
126  
127              // All clients should coexist without crashing
128              Assert::AreEqual(static_cast<size_t>(10), clients.size());
129          }
130  
131          TEST_METHOD(HttpClient_ConcurrentConstruction_DoesNotCrash)
132          {
133              std::vector<std::thread> threads;
134              std::atomic<int> successCount{ 0 };
135  
136              for (int i = 0; i < 5; ++i)
137              {
138                  threads.emplace_back([&successCount]() {
139                      http::HttpClient client;
140                      successCount++;
141                  });
142              }
143  
144              for (auto& t : threads)
145              {
146                  t.join();
147              }
148  
149              Assert::AreEqual(5, successCount.load());
150          }
151      };
152  }