winget-cli

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

SFSUrlBuilderTests.cpp (4680B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 
      4 #include "../../util/SFSExceptionMatcher.h"
      5 #include "../../util/TestHelper.h"
      6 #include "ReportingHandler.h"
      7 #include "SFSUrlBuilder.h"
      8 
      9 #include <catch2/catch_test_macros.hpp>
     10 
     11 #define TEST(...) TEST_CASE("[SFSUrlBuilderTests] " __VA_ARGS__)
     12 
     13 using namespace SFS::details;
     14 using namespace SFS::test;
     15 
     16 const std::string c_accountId = "accountId";
     17 const std::string c_instanceId = "instanceId";
     18 const std::string c_nameSpace = "nameSpace";
     19 
     20 TEST("SFSUrlBuilder")
     21 {
     22     ReportingHandler handler;
     23     handler.SetLoggingCallback(LogCallbackToTest);
     24 
     25     SECTION("CreateFromAccountId()")
     26     {
     27         SECTION("ASCII strings")
     28         {
     29             SFSUrlBuilder builder(c_accountId, c_instanceId, c_nameSpace, handler);
     30             REQUIRE(builder.GetUrl() == "https://accountId.api.cdp.microsoft.com/");
     31 
     32             REQUIRE(
     33                 builder.GetLatestVersionUrl("product") ==
     34                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId/namespaces/nameSpace/names/product/versions/latest?action=select");
     35 
     36             REQUIRE(
     37                 builder.GetLatestVersionBatchUrl() ==
     38                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId/namespaces/nameSpace/names?action=BatchUpdates");
     39 
     40             REQUIRE(
     41                 builder.GetSpecificVersionUrl("product", "version") ==
     42                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId/namespaces/nameSpace/names/product/versions/version");
     43 
     44             REQUIRE(
     45                 builder.GetDownloadInfoUrl("product", "version") ==
     46                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId/namespaces/nameSpace/names/product/versions/version/files?action=GenerateDownloadInfo");
     47         }
     48 
     49         SECTION("Non-ASCII strings")
     50         {
     51             REQUIRE_THROWS_CODE_MSG(SFSUrlBuilder("a&b", c_instanceId, c_nameSpace, handler),
     52                                     ConnectionUrlSetupFailed,
     53                                     "Curl URL error: Bad hostname");
     54 
     55             REQUIRE_THROWS_CODE_MSG(SFSUrlBuilder("a\nb", c_instanceId, c_nameSpace, handler),
     56                                     ConnectionUrlSetupFailed,
     57                                     "Curl URL error: Bad hostname");
     58 
     59             REQUIRE_THROWS_CODE_MSG(SFSUrlBuilder("a\tb", c_instanceId, c_nameSpace, handler),
     60                                     ConnectionUrlSetupFailed,
     61                                     "Curl URL error: Bad hostname");
     62 
     63             SFSUrlBuilder builder(c_accountId, "instanceId@", "namespace+", handler);
     64             REQUIRE(builder.GetUrl() == "https://accountId.api.cdp.microsoft.com/");
     65 
     66             REQUIRE(
     67                 builder.GetLatestVersionUrl("pr$duct") ==
     68                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId%40/namespaces/namespace%2b/names/pr%24duct/versions/latest?action=select");
     69 
     70             REQUIRE(
     71                 builder.GetLatestVersionBatchUrl() ==
     72                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId%40/namespaces/namespace%2b/names?action=BatchUpdates");
     73 
     74             REQUIRE(
     75                 builder.GetSpecificVersionUrl("pr$duct", "versi/n") ==
     76                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId%40/namespaces/namespace%2b/names/pr%24duct/versions/versi%2fn");
     77 
     78             REQUIRE(
     79                 builder.GetDownloadInfoUrl("pr$duct", "versi/n") ==
     80                 "https://accountId.api.cdp.microsoft.com/api/v2/contents/instanceId%40/namespaces/namespace%2b/names/pr%24duct/versions/versi%2fn/files?action=GenerateDownloadInfo");
     81         }
     82     }
     83 
     84     SECTION("CreateFromCustomUrl()")
     85     {
     86         SFSUrlBuilder builder(SFSCustomUrl("http://www.example.com"), c_instanceId, c_nameSpace, handler);
     87         REQUIRE(builder.GetUrl() == "http://www.example.com/");
     88 
     89         SFSUrlBuilder builder2(SFSCustomUrl("http://www.example2.com"), c_instanceId, c_nameSpace, handler);
     90         REQUIRE(builder2.GetUrl() == "http://www.example2.com/");
     91 
     92         REQUIRE_THROWS_CODE_MSG(SFSUrlBuilder(SFSCustomUrl("http://www.+.com"), c_instanceId, c_nameSpace, handler),
     93                                 ConnectionUrlSetupFailed,
     94                                 "Curl URL error: Bad hostname");
     95 
     96         REQUIRE_THROWS_CODE_MSG(SFSUrlBuilder(SFSCustomUrl("example"), c_instanceId, c_nameSpace, handler),
     97                                 ConnectionUrlSetupFailed,
     98                                 "Curl URL error: Bad scheme");
     99     }
    100 }