forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClientHelper.cpp
More file actions
91 lines (79 loc) · 3.76 KB
/
Copy pathHttpClientHelper.cpp
File metadata and controls
91 lines (79 loc) · 3.76 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include "TestRestRequestHandler.h"
#include <AppInstallerErrors.h>
#include <AppInstallerRuntime.h>
#include <AppInstallerStrings.h>
#include <winget/Certificates.h>
#include <winget/HttpClientHelper.h>
#include <CertificateResources.h>
#include <winget/JsonUtil.h>
using namespace AppInstaller::Http;
using namespace AppInstaller::Runtime;
using namespace AppInstaller::Utility;
using namespace AppInstaller::Certificates;
TEST_CASE("ExtractJsonResponse_UnsupportedMimeType", "[RestSource][RestSearch]")
{
HttpClientHelper helper{ GetTestRestRequestHandler(web::http::status_codes::OK, L"", web::http::details::mime_types::text_plain) };
REQUIRE_THROWS_HR(helper.HandleGet(L"https://testUri"), APPINSTALLER_CLI_ERROR_RESTAPI_UNSUPPORTED_MIME_TYPE);
}
TEST_CASE("ValidateAndExtractResponse_ServiceUnavailable", "[RestSource]")
{
HttpClientHelper helper{ GetTestRestRequestHandler(web::http::status_codes::ServiceUnavailable) };
REQUIRE_THROWS_HR(helper.HandleGet(L"https://testUri"), APPINSTALLER_CLI_ERROR_SERVICE_UNAVAILABLE);
}
TEST_CASE("ValidateAndExtractResponse_NotFound", "[RestSource]")
{
HttpClientHelper helper{ GetTestRestRequestHandler(web::http::status_codes::NotFound) };
REQUIRE_THROWS_HR(helper.HandleGet(L"https://testUri"), APPINSTALLER_CLI_ERROR_RESTAPI_ENDPOINT_NOT_FOUND);
}
TEST_CASE("EnsureDefaultUserAgent", "[RestSource]")
{
HttpClientHelper helper{ GetTestRestRequestHandler([](const web::http::http_request& request)
{
auto itr = request.headers().find(web::http::header_names::user_agent);
if (itr != request.headers().end() &&
itr->second.find(ConvertToUTF16(GetClientVersion())) != utility::string_t::npos &&
itr->second.find(ConvertToUTF16(GetPackageVersion())) != utility::string_t::npos)
{
return web::http::status_codes::OK;
}
else
{
return web::http::status_codes::BadRequest;
}
}) };
SECTION("GET")
{
REQUIRE_NOTHROW(helper.HandleGet(L"https://testUri"));
}
SECTION("POST")
{
REQUIRE_NOTHROW(helper.HandlePost(L"https://testUri", {}));
}
}
TEST_CASE("HttpClientHelper_PinningConfiguration", "[RestSource]")
{
// Create the Store chain config
PinningChain chain;
auto chainElement = chain.Root();
chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_ROOT_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::PublicKey);
chainElement = chainElement.Next();
chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_INTERMEDIATE_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
chainElement = chainElement.Next();
chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_LEAF_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
PinningConfiguration config;
config.AddChain(chain);
HttpClientHelper helper;
helper.SetPinningConfiguration(config);
REQUIRE_THROWS_HR(helper.HandleGet(L"https://github.com"), APPINSTALLER_CLI_ERROR_PINNED_CERTIFICATE_MISMATCH);
}
TEST_CASE("HttpClientHelper_CallerCharacters", "[RestSource]")
{
HttpClientHelper::HttpRequestHeaders headers;
headers.emplace(web::http::header_names::user_agent, AppInstaller::JSON::GetUtilityString(AppInstaller::Runtime::GetUserAgent("\xe6\xb5\x8b\xe8\xaf\x95")));
HttpClientHelper helper;
REQUIRE_THROWS_HR(helper.HandleGet(L"https://github.com", headers), APPINSTALLER_CLI_ERROR_RESTAPI_UNSUPPORTED_MIME_TYPE);
}