winget-cli

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

CustomHeader.cpp (7891B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestCommon.h"
      5 #include "TestHooks.h"
      6 #include "TestSettings.h"
      7 #include "TestSource.h"
      8 #include "TestRestRequestHandler.h"
      9 #include <Rest/Schema/1_1/Interface.h>
     10 #include <winget/JsonUtil.h>
     11 #include <Rest/RestClient.h>
     12 #include <winget/Settings.h>
     13 
     14 using namespace TestCommon;
     15 using namespace AppInstaller;
     16 using namespace AppInstaller::Http;
     17 using namespace AppInstaller::Settings;
     18 using namespace AppInstaller::Repository;
     19 using namespace AppInstaller::Repository::Rest;
     20 using namespace AppInstaller::Repository::Rest::Schema;
     21 using namespace AppInstaller::Repository::Rest::Schema::V1_0;
     22 
     23 namespace
     24 {
     25     utility::string_t CustomHeaderName = L"Windows-Package-Manager";
     26 
     27     constexpr std::string_view s_EmptySources = R"(
     28         Sources:
     29         )"sv;
     30 
     31     utility::string_t sampleSearchResponse = _XPLATSTR(
     32         R"delimiter({
     33             "Data" : [
     34                {
     35               "PackageIdentifier": "git.package",
     36               "PackageName": "package",
     37               "Publisher": "git",
     38               "Versions": [
     39                 {   "PackageVersion": "1.0.0" }]
     40             }]
     41         })delimiter");
     42 }
     43 
     44 // In RestClient.cpp tests
     45 extern RestClient CreateRestClient(
     46     const std::string& restApi,
     47     const std::optional<std::string>& customHeader,
     48     std::string_view caller,
     49     const Http::HttpClientHelper& helper,
     50     const Authentication::AuthenticationArguments& authArgs = {});
     51 
     52 TEST_CASE("RestClient_CustomHeader", "[RestSource][CustomHeader]")
     53 {
     54     utility::string_t sample = _XPLATSTR(
     55         R"delimiter({
     56             "Data" : {
     57               "SourceIdentifier": "Source123",
     58               "ServerSupportedVersions": [
     59                 "1.0.0",
     60                 "2.0.0"]
     61         }})delimiter");
     62 
     63     std::optional<std::string> customHeader = "Testing custom header";
     64     auto header = std::make_pair<>(CustomHeaderName, JSON::GetUtilityString(customHeader.value()));
     65     HttpClientHelper helper{ GetHeaderVerificationHandler(web::http::status_codes::OK, sample, header) };
     66     RestClient client = CreateRestClient(utility::conversions::to_utf8string("https://restsource.com/api"), customHeader, {}, helper);
     67     REQUIRE(client.GetSourceIdentifier() == "Source123");
     68 }
     69 
     70 TEST_CASE("RestSourceSearch_CustomHeader", "[RestSource][CustomHeader]")
     71 {
     72     utility::string_t customHeader = L"Testing custom header";
     73     auto header = std::make_pair<>(CustomHeaderName, customHeader);
     74     HttpClientHelper helper{ GetHeaderVerificationHandler(web::http::status_codes::OK, sampleSearchResponse, header) };
     75     std::unordered_map<utility::string_t, utility::string_t> headers;
     76     headers.emplace(CustomHeaderName, customHeader);
     77 
     78     V1_1::Interface v1_1{ "https://restsource.com/api", std::move(helper) , {}, headers};
     79     Schema::IRestClient::SearchResult searchResponse = v1_1.Search({});
     80     REQUIRE(searchResponse.Matches.size() == 1);
     81     Schema::IRestClient::Package package = searchResponse.Matches.at(0);
     82 }
     83 
     84 TEST_CASE("RestSourceSearch_WhitespaceCustomHeader", "[RestSource][CustomHeader]")
     85 {
     86     utility::string_t customHeader = L"    ";
     87     auto header = std::make_pair<>(CustomHeaderName, customHeader);
     88     HttpClientHelper helper{ GetHeaderVerificationHandler(web::http::status_codes::OK, sampleSearchResponse, header) };
     89     std::unordered_map<utility::string_t, utility::string_t> headers;
     90     headers.emplace(CustomHeaderName, customHeader);
     91 
     92     V1_1::Interface v1_1{ "https://restsource.com/api", std::move(helper), {}, headers };
     93     Schema::IRestClient::SearchResult searchResponse = v1_1.Search({});
     94     REQUIRE(searchResponse.Matches.size() == 1);
     95 }
     96 
     97 TEST_CASE("RestSourceSearch_NoCustomHeader", "[RestSource][CustomHeader]")
     98 {
     99     utility::string_t customHeader = L"    ";
    100     auto header = std::make_pair<>(CustomHeaderName, customHeader);
    101     HttpClientHelper helper{ GetHeaderVerificationHandler(web::http::status_codes::OK, sampleSearchResponse, header) };
    102     std::unordered_map<utility::string_t, utility::string_t> headers;
    103     headers.emplace(CustomHeaderName, customHeader);
    104 
    105     V1_1::Interface v1_1{ "https://restsource.com/api", std::move(helper), {}, {} };
    106     REQUIRE_THROWS_HR(v1_1.Search({}), APPINSTALLER_CLI_ERROR_RESTAPI_INTERNAL_ERROR);
    107 }
    108 
    109 TEST_CASE("RestSourceSearch_CustomHeaderExceedingSize", "[RestSource][CustomHeader]")
    110 {
    111     std::string customHeader = "This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. This is a custom header that is longer than 1024 characters. ";
    112     auto header = std::make_pair<>(CustomHeaderName, JSON::GetUtilityString(customHeader));
    113     HttpClientHelper helper{ GetHeaderVerificationHandler(web::http::status_codes::OK, sampleSearchResponse, header) };
    114 
    115     REQUIRE_THROWS_HR(CreateRestClient(utility::conversions::to_utf8string("https://restsource.com/api"), customHeader, {}, helper),
    116         APPINSTALLER_CLI_ERROR_CUSTOMHEADER_EXCEEDS_MAXLENGTH);
    117 }
    118 
    119 TEST_CASE("RestClient_CustomUserAgentHeader", "[RestSource][CustomHeader]")
    120 {
    121     utility::string_t sample = _XPLATSTR(
    122         R"delimiter({
    123             "Data" : {
    124               "SourceIdentifier": "Source123",
    125               "ServerSupportedVersions": [
    126                 "1.0.0",
    127                 "2.0.0"]
    128         }})delimiter");
    129 
    130     std::string testCaller = "TestCaller";
    131     auto header = std::make_pair<>(web::http::header_names::user_agent, JSON::GetUtilityString(Runtime::GetUserAgent(testCaller)));
    132     HttpClientHelper helper{ GetHeaderVerificationHandler(web::http::status_codes::OK, sample, header) };
    133     RestClient client = CreateRestClient(utility::conversions::to_utf8string("https://restsource.com/api"), {}, testCaller, helper);
    134     REQUIRE(client.GetSourceIdentifier() == "Source123");
    135 }
    136 
    137 TEST_CASE("RestClient_DefaultUserAgentHeader", "[RestSource][CustomHeader]")
    138 {
    139     utility::string_t sample = _XPLATSTR(
    140         R"delimiter({
    141             "Data" : {
    142               "SourceIdentifier": "Source123",
    143               "ServerSupportedVersions": [
    144                 "1.0.0",
    145                 "2.0.0"]
    146         }})delimiter");
    147 
    148     auto header = std::make_pair<>(web::http::header_names::user_agent, JSON::GetUtilityString(Runtime::GetDefaultUserAgent()));
    149     HttpClientHelper helper{ GetHeaderVerificationHandler(web::http::status_codes::OK, sample, header) };
    150     RestClient client = CreateRestClient(utility::conversions::to_utf8string("https://restsource.com/api"), {}, {}, helper);
    151     REQUIRE(client.GetSourceIdentifier() == "Source123");
    152 }