|
4 | 4 | #include "TestCommon.h" |
5 | 5 | #include "TestRestRequestHandler.h" |
6 | 6 | #include <Rest/RestClient.h> |
| 7 | +#include <Rest/RestInformationCache.h> |
7 | 8 | #include <Rest/Schema/IRestClient.h> |
| 9 | +#include <Rest/Schema/InformationResponseDeserializer.h> |
8 | 10 | #include <AppInstallerVersions.h> |
9 | 11 | #include <AppInstallerErrors.h> |
10 | 12 | #include <AppInstallerRuntime.h> |
@@ -488,3 +490,182 @@ TEST_CASE("RestClientCreate_1.7_Success", "[RestSource]") |
488 | 490 | REQUIRE(information.Authentication.MicrosoftEntraIdInfo->Resource == "GUID"); |
489 | 491 | REQUIRE(information.Authentication.MicrosoftEntraIdInfo->Scope == "test"); |
490 | 492 | } |
| 493 | + |
| 494 | +// Simulate the msstore cache round trip using real world data. |
| 495 | +TEST_CASE("RestInformationCache_RoundTrip", "[RestInformationCache]") |
| 496 | +{ |
| 497 | + Settings::Stream{ Settings::Stream::RestInformationCache }.Remove(); |
| 498 | + |
| 499 | + std::wstring endpoint = L"https://test-url-com/information"; |
| 500 | + CacheControlPolicy cacheControl{ L"public, max-age=77287" }; |
| 501 | + auto response = web::json::value::parse( |
| 502 | +R"delimiter({ |
| 503 | + "$type": "Microsoft.Marketplace.Storefront.StoreEdgeFD.BusinessLogic.Response.PackageMetadata.PackageMetadataResponse, StoreEdgeFD", |
| 504 | + "Data": { |
| 505 | + "$type": "Microsoft.Marketplace.Storefront.StoreEdgeFD.BusinessLogic.Response.PackageMetadata.PackageMetadataData, StoreEdgeFD", |
| 506 | + "SourceIdentifier": "StoreEdgeFD", |
| 507 | + "SourceAgreements": { |
| 508 | + "$type": "Microsoft.Marketplace.Storefront.StoreEdgeFD.BusinessLogic.Response.PackageMetadata.SourceAgreements, StoreEdgeFD", |
| 509 | + "AgreementsIdentifier": "StoreEdgeFD", |
| 510 | + "Agreements": [ |
| 511 | + { |
| 512 | + "$type": "Microsoft.Marketplace.Storefront.StoreEdgeFD.BusinessLogic.Response.PackageManifest.AgreementDetail, StoreEdgeFD", |
| 513 | + "AgreementLabel": "Terms of Transaction", |
| 514 | + "AgreementUrl": "https://aka.ms/microsoft-store-terms-of-transaction" |
| 515 | + } |
| 516 | + ] |
| 517 | + }, |
| 518 | + "ServerSupportedVersions": [ "1.0.0", "1.1.0", "1.6.0" ], |
| 519 | + "RequiredQueryParameters": [ "market" ], |
| 520 | + "RequiredPackageMatchFields": [ "market" ] |
| 521 | + } |
| 522 | +})delimiter"); |
| 523 | + |
| 524 | + RestInformationCache cache; |
| 525 | + cache.Cache(endpoint, {}, {}, cacheControl, response); |
| 526 | + auto cachedValue = cache.Get(endpoint, {}, {}); |
| 527 | + |
| 528 | + REQUIRE(cachedValue.has_value()); |
| 529 | + |
| 530 | + InformationResponseDeserializer deserializer; |
| 531 | + const auto expected = deserializer.Deserialize(response); |
| 532 | + const auto& actual = cachedValue.value(); |
| 533 | + |
| 534 | + REQUIRE(expected.SourceIdentifier == actual.SourceIdentifier); |
| 535 | + REQUIRE(expected.SourceAgreementsIdentifier == actual.SourceAgreementsIdentifier); |
| 536 | + REQUIRE(expected.SourceAgreements.size() == actual.SourceAgreements.size()); |
| 537 | + REQUIRE(1 == actual.SourceAgreements.size()); |
| 538 | + REQUIRE(expected.SourceAgreements[0].Label == actual.SourceAgreements[0].Label); |
| 539 | + REQUIRE(expected.SourceAgreements[0].Text == actual.SourceAgreements[0].Text); |
| 540 | + REQUIRE(expected.SourceAgreements[0].Url == actual.SourceAgreements[0].Url); |
| 541 | + |
| 542 | + REQUIRE(expected.ServerSupportedVersions.size() == actual.ServerSupportedVersions.size()); |
| 543 | + for (const auto& expectedVersion : expected.ServerSupportedVersions) |
| 544 | + { |
| 545 | + REQUIRE(std::find(actual.ServerSupportedVersions.begin(), actual.ServerSupportedVersions.end(), expectedVersion) != actual.ServerSupportedVersions.end()); |
| 546 | + } |
| 547 | + |
| 548 | + REQUIRE(expected.RequiredQueryParameters.size() == actual.RequiredQueryParameters.size()); |
| 549 | + REQUIRE(1 == actual.RequiredQueryParameters.size()); |
| 550 | + REQUIRE(expected.RequiredQueryParameters[0] == actual.RequiredQueryParameters[0]); |
| 551 | + |
| 552 | + REQUIRE(expected.RequiredPackageMatchFields.size() == actual.RequiredPackageMatchFields.size()); |
| 553 | + REQUIRE(1 == actual.RequiredPackageMatchFields.size()); |
| 554 | + REQUIRE(expected.RequiredPackageMatchFields[0] == actual.RequiredPackageMatchFields[0]); |
| 555 | +} |
| 556 | + |
| 557 | +web::json::value CreateInformationResponse(std::string_view identifier) |
| 558 | +{ |
| 559 | + std::ostringstream stream; |
| 560 | + stream << R"({ "Data": { "SourceIdentifier": ")" << identifier << R"(", "ServerSupportedVersions": [ "1.0.0" ] } })"; |
| 561 | + |
| 562 | + return web::json::value::parse(stream.str()); |
| 563 | +} |
| 564 | + |
| 565 | +TEST_CASE("RestInformationCache_Get", "[RestInformationCache]") |
| 566 | +{ |
| 567 | + Settings::Stream{ Settings::Stream::RestInformationCache }.Remove(); |
| 568 | + |
| 569 | + std::wstring endpoint1 = L"https://test-url1-com/information"; |
| 570 | + std::wstring endpoint2 = L"https://test-url2-com/information"; |
| 571 | + std::wstring endpointNotPresent = L"https://test-url-not-present-com/information"; |
| 572 | + std::string header = "Header"; |
| 573 | + std::string caller = "Caller"; |
| 574 | + std::string publicEndpoint1Identifier = "Identifier1"; |
| 575 | + std::string privateEndpoint1Identifier = "Identifier2"; |
| 576 | + std::string privateEndpoint2Identifier = "Identifier3"; |
| 577 | + auto publicEndpoint1Response = CreateInformationResponse(publicEndpoint1Identifier); |
| 578 | + auto privateEndpoint1Response = CreateInformationResponse(privateEndpoint1Identifier); |
| 579 | + auto privateEndpoint2Response = CreateInformationResponse(privateEndpoint2Identifier); |
| 580 | + |
| 581 | + RestInformationCache cache; |
| 582 | + |
| 583 | + // Cache: |
| 584 | + // 1. public and private for same endpoint |
| 585 | + cache.Cache(endpoint1, header, caller, { L"public" }, publicEndpoint1Response); |
| 586 | + cache.Cache(endpoint1, header, caller, {}, privateEndpoint1Response); |
| 587 | + // 2. another endpoint with private data (same headers) |
| 588 | + cache.Cache(endpoint2, header, caller, {}, privateEndpoint2Response); |
| 589 | + |
| 590 | + SECTION("Same headers prefers private") |
| 591 | + { |
| 592 | + auto cachedValue = cache.Get(endpoint1, header, caller); |
| 593 | + REQUIRE(cachedValue.has_value()); |
| 594 | + REQUIRE(privateEndpoint1Identifier == cachedValue->SourceIdentifier); |
| 595 | + } |
| 596 | + SECTION("Different headers falls back to public") |
| 597 | + { |
| 598 | + auto cachedValue = cache.Get(endpoint1, "Different", "Different"); |
| 599 | + REQUIRE(cachedValue.has_value()); |
| 600 | + REQUIRE(publicEndpoint1Identifier == cachedValue->SourceIdentifier); |
| 601 | + } |
| 602 | + SECTION("Second endpoint") |
| 603 | + { |
| 604 | + auto cachedValue = cache.Get(endpoint2, header, caller); |
| 605 | + REQUIRE(cachedValue.has_value()); |
| 606 | + REQUIRE(privateEndpoint2Identifier == cachedValue->SourceIdentifier); |
| 607 | + } |
| 608 | + SECTION("Second endpoint different headers") |
| 609 | + { |
| 610 | + auto cachedValue = cache.Get(endpoint2, {}, {}); |
| 611 | + REQUIRE(!cachedValue.has_value()); |
| 612 | + } |
| 613 | + SECTION("Missing endpoint") |
| 614 | + { |
| 615 | + auto cachedValue = cache.Get(endpointNotPresent, header, caller); |
| 616 | + REQUIRE(!cachedValue.has_value()); |
| 617 | + } |
| 618 | +} |
| 619 | + |
| 620 | +TEST_CASE("RestInformationCache_Cache_NoStore", "[RestInformationCache]") |
| 621 | +{ |
| 622 | + Settings::Stream{ Settings::Stream::RestInformationCache }.Remove(); |
| 623 | + |
| 624 | + std::wstring endpoint = L"https://test-url-com/information"; |
| 625 | + |
| 626 | + RestInformationCache cache; |
| 627 | + cache.Cache(endpoint, {}, {}, { L"no-store" }, CreateInformationResponse("Identifier")); |
| 628 | + |
| 629 | + auto cachedValue = cache.Get(endpoint, {}, {}); |
| 630 | + REQUIRE(!cachedValue.has_value()); |
| 631 | +} |
| 632 | + |
| 633 | +TEST_CASE("RestInformationCache_Cache_Expiration", "[RestInformationCache]") |
| 634 | +{ |
| 635 | + Settings::Stream{ Settings::Stream::RestInformationCache }.Remove(); |
| 636 | + |
| 637 | + std::wstring endpoint = L"https://test-url-com/information"; |
| 638 | + |
| 639 | + RestInformationCache cache; |
| 640 | + cache.Cache(endpoint, {}, {}, { L"max-age=2" }, CreateInformationResponse("Identifier")); |
| 641 | + |
| 642 | + auto cachedValue = cache.Get(endpoint, {}, {}); |
| 643 | + REQUIRE(cachedValue.has_value()); |
| 644 | + |
| 645 | + std::this_thread::sleep_for(5s); |
| 646 | + |
| 647 | + cachedValue = cache.Get(endpoint, {}, {}); |
| 648 | + REQUIRE(!cachedValue.has_value()); |
| 649 | +} |
| 650 | + |
| 651 | +TEST_CASE("RestInformationCache_Cache_Overwrite", "[RestInformationCache]") |
| 652 | +{ |
| 653 | + Settings::Stream{ Settings::Stream::RestInformationCache }.Remove(); |
| 654 | + |
| 655 | + std::wstring endpoint = L"https://test-url-com/information"; |
| 656 | + std::string identifier1 = "Identifier1"; |
| 657 | + std::string identifier2 = "Identifier2"; |
| 658 | + |
| 659 | + RestInformationCache cache; |
| 660 | + cache.Cache(endpoint, {}, {}, {}, CreateInformationResponse(identifier1)); |
| 661 | + |
| 662 | + auto cachedValue = cache.Get(endpoint, {}, {}); |
| 663 | + REQUIRE(cachedValue.has_value()); |
| 664 | + REQUIRE(identifier1 == cachedValue->SourceIdentifier); |
| 665 | + |
| 666 | + cache.Cache(endpoint, {}, {}, {}, CreateInformationResponse(identifier2)); |
| 667 | + |
| 668 | + cachedValue = cache.Get(endpoint, {}, {}); |
| 669 | + REQUIRE(cachedValue.has_value()); |
| 670 | + REQUIRE(identifier2 == cachedValue->SourceIdentifier); |
| 671 | +} |
0 commit comments