winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

HttpClientHelper.cpp (3377B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestCommon.h"
      5 #include "TestRestRequestHandler.h"
      6 #include <AppInstallerErrors.h>
      7 #include <AppInstallerRuntime.h>
      8 #include <AppInstallerStrings.h>
      9 #include <winget/Certificates.h>
     10 #include <winget/HttpClientHelper.h>
     11 #include <CertificateResources.h>
     12 
     13 using namespace AppInstaller::Http;
     14 using namespace AppInstaller::Runtime;
     15 using namespace AppInstaller::Utility;
     16 using namespace AppInstaller::Certificates;
     17 
     18 TEST_CASE("ExtractJsonResponse_UnsupportedMimeType", "[RestSource][RestSearch]")
     19 {
     20     HttpClientHelper helper{ GetTestRestRequestHandler(web::http::status_codes::OK, L"", web::http::details::mime_types::text_plain) };
     21     REQUIRE_THROWS_HR(helper.HandleGet(L"https://testUri"), APPINSTALLER_CLI_ERROR_RESTAPI_UNSUPPORTED_MIME_TYPE);
     22 }
     23 
     24 TEST_CASE("ValidateAndExtractResponse_ServiceUnavailable", "[RestSource]")
     25 {
     26     HttpClientHelper helper{ GetTestRestRequestHandler(web::http::status_codes::ServiceUnavailable) };
     27     REQUIRE_THROWS_HR(helper.HandleGet(L"https://testUri"), APPINSTALLER_CLI_ERROR_SERVICE_UNAVAILABLE);
     28 }
     29 
     30 TEST_CASE("ValidateAndExtractResponse_NotFound", "[RestSource]")
     31 {
     32     HttpClientHelper helper{ GetTestRestRequestHandler(web::http::status_codes::NotFound) };
     33     REQUIRE_THROWS_HR(helper.HandleGet(L"https://testUri"), APPINSTALLER_CLI_ERROR_RESTAPI_ENDPOINT_NOT_FOUND);
     34 }
     35 
     36 TEST_CASE("EnsureDefaultUserAgent", "[RestSource]")
     37 {
     38     HttpClientHelper helper{ GetTestRestRequestHandler([](const web::http::http_request& request)
     39         {
     40             auto itr = request.headers().find(web::http::header_names::user_agent);
     41             if (itr != request.headers().end() &&
     42                 itr->second.find(ConvertToUTF16(GetClientVersion())) != utility::string_t::npos &&
     43                 itr->second.find(ConvertToUTF16(GetPackageVersion())) != utility::string_t::npos)
     44             {
     45                 return web::http::status_codes::OK;
     46             }
     47             else
     48             {
     49                 return web::http::status_codes::BadRequest;
     50             }
     51         }) };
     52 
     53     SECTION("GET")
     54     {
     55         REQUIRE_NOTHROW(helper.HandleGet(L"https://testUri"));
     56     }
     57     SECTION("POST")
     58     {
     59         REQUIRE_NOTHROW(helper.HandlePost(L"https://testUri", {}));
     60     }
     61 }
     62 
     63 TEST_CASE("HttpClientHelper_PinningConfiguration", "[RestSource]")
     64 {
     65     // Create the Store chain config
     66     PinningChain chain;
     67     auto chainElement = chain.Root();
     68     chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_ROOT_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::PublicKey);
     69     chainElement = chainElement.Next();
     70     chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_INTERMEDIATE_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
     71     chainElement = chainElement.Next();
     72     chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_LEAF_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
     73 
     74     PinningConfiguration config;
     75     config.AddChain(chain);
     76 
     77     HttpClientHelper helper;
     78     helper.SetPinningConfiguration(config);
     79 
     80     REQUIRE_THROWS_HR(helper.HandleGet(L"https://github.com"), APPINSTALLER_CLI_ERROR_PINNED_CERTIFICATE_MISMATCH);
     81 }