UrlBuilderTests.cpp (6016B)
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 "UrlBuilder.h" 8 9 #include <catch2/catch_test_macros.hpp> 10 11 #define TEST(...) TEST_CASE("[UrlBuilderTests] " __VA_ARGS__) 12 13 using namespace SFS::details; 14 using namespace SFS::test; 15 16 TEST("UrlBuilder") 17 { 18 ReportingHandler handler; 19 handler.SetLoggingCallback(LogCallbackToTest); 20 21 UrlBuilder builder(handler); 22 REQUIRE_THROWS_CODE_MSG(builder.GetUrl(), ConnectionUrlSetupFailed, "Curl URL error: No host part in the URL"); 23 24 builder.SetHost("www.example.com"); 25 REQUIRE_THROWS_CODE_MSG(builder.GetUrl(), ConnectionUrlSetupFailed, "Curl URL error: No scheme part in the URL"); 26 27 builder.SetScheme(Scheme::Https); 28 REQUIRE(builder.GetUrl() == "https://www.example.com/"); 29 30 SECTION("SetHost") 31 { 32 builder.SetHost("www.example2.com"); 33 REQUIRE(builder.GetUrl() == "https://www.example2.com/"); 34 35 REQUIRE_THROWS_CODE_MSG(builder.SetHost("+"), ConnectionUrlSetupFailed, "Curl URL error: Bad hostname"); 36 REQUIRE_THROWS_CODE_MSG(builder.SetHost("a&b"), ConnectionUrlSetupFailed, "Curl URL error: Bad hostname"); 37 REQUIRE_THROWS_CODE_MSG(builder.SetHost("a\nb"), ConnectionUrlSetupFailed, "Curl URL error: Bad hostname"); 38 REQUIRE_THROWS_CODE_MSG(builder.SetHost("a\tb"), ConnectionUrlSetupFailed, "Curl URL error: Bad hostname"); 39 40 REQUIRE_THROWS_CODE_MSG(builder.SetHost(""), InvalidArg, "Host must not empty"); 41 } 42 43 SECTION("SetPath") 44 { 45 builder.SetPath("index.html"); 46 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html"); 47 48 REQUIRE_THROWS_CODE_MSG(builder.SetPath(""), InvalidArg, "Path must not empty"); 49 50 builder.ResetPath(); 51 REQUIRE(builder.GetUrl() == "https://www.example.com/"); 52 } 53 54 SECTION("AppendPath") 55 { 56 builder.SetPath("index.html"); 57 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html"); 58 59 builder.AppendPathEncoded("index.html"); 60 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html/index.html"); 61 62 builder.AppendPath("a/"); 63 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html/index.html/a/"); 64 65 builder.AppendPath("b/"); 66 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html/index.html/a/b/"); 67 68 INFO("Encoding for append includes the / character"); 69 builder.AppendPathEncoded("c/"); 70 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html/index.html/a/b/c%2f"); 71 72 INFO("Calling SetPath() resets the path"); 73 builder.SetPath("index"); 74 REQUIRE(builder.GetUrl() == "https://www.example.com/index"); 75 76 REQUIRE_THROWS_CODE_MSG(builder.AppendPath(""), InvalidArg, "Path must not empty"); 77 } 78 79 SECTION("SetQuery, AppendQuery") 80 { 81 builder.SetQuery("key", "value"); 82 REQUIRE(builder.GetUrl() == "https://www.example.com/?key=value"); 83 84 builder.AppendQuery("key2", "value2"); 85 REQUIRE(builder.GetUrl() == "https://www.example.com/?key=value&key2=value2"); 86 87 builder.SetQuery("key2", "value2"); 88 REQUIRE(builder.GetUrl() == "https://www.example.com/?key2=value2"); 89 90 builder.AppendQuery("key3", "valu/e@2"); 91 REQUIRE(builder.GetUrl() == "https://www.example.com/?key2=value2&key3=valu%2fe%402"); 92 93 builder.SetQuery("ke$y4", "valu/e@3"); 94 REQUIRE(builder.GetUrl() == "https://www.example.com/?ke%24y4=valu%2fe%403"); 95 96 REQUIRE_THROWS_CODE_MSG(builder.SetQuery("", "value"), InvalidArg, "Query key and value must not empty"); 97 REQUIRE_THROWS_CODE_MSG(builder.SetQuery("key", ""), InvalidArg, "Query key and value must not empty"); 98 REQUIRE_THROWS_CODE_MSG(builder.SetQuery("", ""), InvalidArg, "Query key and value must not empty"); 99 100 REQUIRE_THROWS_CODE_MSG(builder.AppendQuery("", "value"), InvalidArg, "Query key and value must not empty"); 101 REQUIRE_THROWS_CODE_MSG(builder.AppendQuery("key", ""), InvalidArg, "Query key and value must not empty"); 102 REQUIRE_THROWS_CODE_MSG(builder.AppendQuery("", ""), InvalidArg, "Query key and value must not empty"); 103 104 builder.ResetQuery(); 105 REQUIRE(builder.GetUrl() == "https://www.example.com/"); 106 } 107 108 SECTION("SetUrl") 109 { 110 builder.SetUrl("https://www.example.com/index.html?key=value"); 111 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html?key=value"); 112 REQUIRE_THROWS_CODE_MSG(builder.SetUrl("https://www.+.com"), 113 ConnectionUrlSetupFailed, 114 "Curl URL error: Bad hostname"); 115 116 REQUIRE_THROWS_CODE_MSG(builder.SetUrl(""), InvalidArg, "Url must not empty"); 117 } 118 119 SECTION("SetScheme, SetHost, SetPath, SetQuery") 120 { 121 builder.SetScheme(Scheme::Https).SetHost("www.example.com").SetPath("index.html").SetQuery("key", "value"); 122 REQUIRE(builder.GetUrl() == "https://www.example.com/index.html?key=value"); 123 } 124 125 SECTION("SetScheme, SetHost, AppendPathEncoded, SetQuery") 126 { 127 builder.SetScheme(Scheme::Https) 128 .SetHost("www.example.com") 129 .AppendPathEncoded("index@.html") 130 .SetQuery("key", "value"); 131 REQUIRE(builder.GetUrl() == "https://www.example.com/index%40.html?key=value"); 132 } 133 134 SECTION("Constructor with URL") 135 { 136 UrlBuilder builder2("https://www.example.com/index.html?key=value", handler); 137 REQUIRE(builder2.GetUrl() == "https://www.example.com/index.html?key=value"); 138 139 REQUIRE_THROWS_CODE_MSG(UrlBuilder("https://www.+.com", handler), 140 ConnectionUrlSetupFailed, 141 "Curl URL error: Bad hostname"); 142 } 143 }