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