diff options
author | Richard Walters <rwalters@digitalstirling.com> | 2018-07-04 19:21:55 -0700 |
---|---|---|
committer | Richard Walters <rwalters@digitalstirling.com> | 2018-07-04 19:21:55 -0700 |
commit | ea3d03583c4cd2443220eddfedefcea51b7121c5 (patch) | |
tree | d0cc4120a80501c05f06b8682cbb8169d6c9cab2 /test | |
parent | d95bcc094d5102cbc1b625b69bf6378e3fb730a8 (diff) |
Fix requirements
Query and fragment may be empty but present in a URI.
Handle this in the same way that port is handled: include
a flag for each of query and fragment, to allow an
empty but present query/fragment.
Diffstat (limited to 'test')
-rw-r--r-- | test/src/UriTests.cpp | 96 |
1 files changed, 71 insertions, 25 deletions
diff --git a/test/src/UriTests.cpp b/test/src/UriTests.cpp index 9dbff55..75fbba3 100644 --- a/test/src/UriTests.cpp +++ b/test/src/UriTests.cpp @@ -764,34 +764,38 @@ TEST(UriTests, GenerateString) { bool hasPort; uint16_t port; std::vector< std::string > path; + bool hasQuery; std::string query; + bool hasFragment; std::string fragment; std::string expectedUriString; }; const std::vector< TestVector > testVectors{ - {"http", "bob", "www.example.com", true, 8080, {"", "abc", "def"}, "foobar", "ch2", "http://bob@www.example.com:8080/abc/def?foobar#ch2"}, - {"http", "bob", "www.example.com", true, 0, {}, "foobar", "ch2", "http://bob@www.example.com:0?foobar#ch2"}, - {"", "", "example.com", false, 0, {}, "bar", "", "//example.com?bar"}, - {"", "", "example.com", false, 0, {}, "", "", "//example.com"}, - {"", "", "example.com", false, 0, {""}, "", "", "//example.com/"}, - {"", "", "example.com", false, 0, {"", "xyz"}, "", "", "//example.com/xyz"}, - {"", "", "example.com", false, 0, {"", "xyz", ""}, "", "", "//example.com/xyz/"}, - {"", "", "", false, 0, {""}, "", "", "/"}, - {"", "", "", false, 0, {"", "xyz"}, "", "", "/xyz"}, - {"", "", "", false, 0, {"", "xyz", ""}, "", "", "/xyz/"}, - {"", "", "", false, 0, {}, "", "", ""}, - {"", "", "", false, 0, {"xyz"}, "", "", "xyz"}, - {"", "", "", false, 0, {"xyz", ""}, "", "", "xyz/"}, - {"", "", "", false, 0, {}, "bar", "", "?bar"}, - {"http", "", "", false, 0, {}, "bar", "", "http:?bar"}, - {"http", "", "", false, 0, {}, "", "", "http:"}, - {"http", "", "::1", false, 0, {}, "", "", "http://[::1]"}, - {"http", "", "::1.2.3.4", false, 0, {}, "", "", "http://[::1.2.3.4]"}, - {"http", "", "1.2.3.4", false, 0, {}, "", "", "http://1.2.3.4"}, - {"", "", "", false, 0, {}, "", "", ""}, - {"http", "bob", "", false, 0, {}, "foobar", "", "http://bob@?foobar"}, - {"", "bob", "", false, 0, {}, "foobar", "", "//bob@?foobar"}, - {"", "bob", "", false, 0, {}, "", "", "//bob@"}, + {"http", "bob", "www.example.com", true, 8080, {"", "abc", "def"}, true, "foobar", true, "ch2", "http://bob@www.example.com:8080/abc/def?foobar#ch2"}, + {"http", "bob", "www.example.com", true, 0, {}, true, "foobar", true, "ch2", "http://bob@www.example.com:0?foobar#ch2"}, + {"http", "bob", "www.example.com", true, 0, {}, true, "foobar", true, "", "http://bob@www.example.com:0?foobar#"}, + {"", "", "example.com", false, 0, {}, true, "bar", false, "", "//example.com?bar"}, + {"", "", "example.com", false, 0, {}, true, "" , false, "", "//example.com?"}, + {"", "", "example.com", false, 0, {}, false, "", false, "", "//example.com"}, + {"", "", "example.com", false, 0, {""}, false, "", false, "", "//example.com/"}, + {"", "", "example.com", false, 0, {"", "xyz"}, false, "", false, "", "//example.com/xyz"}, + {"", "", "example.com", false, 0, {"", "xyz", ""}, false, "", false, "", "//example.com/xyz/"}, + {"", "", "", false, 0, {""}, false, "", false, "", "/"}, + {"", "", "", false, 0, {"", "xyz"}, false, "", false, "", "/xyz"}, + {"", "", "", false, 0, {"", "xyz", ""}, false, "", false, "", "/xyz/"}, + {"", "", "", false, 0, {}, false, "", false, "", ""}, + {"", "", "", false, 0, {"xyz"}, false, "", false, "", "xyz"}, + {"", "", "", false, 0, {"xyz", ""}, false, "", false, "", "xyz/"}, + {"", "", "", false, 0, {}, true, "bar", false, "", "?bar"}, + {"http", "", "", false, 0, {}, true, "bar", false, "", "http:?bar"}, + {"http", "", "", false, 0, {}, false, "", false, "", "http:"}, + {"http", "", "::1", false, 0, {}, false, "", false, "", "http://[::1]"}, + {"http", "", "::1.2.3.4", false, 0, {}, false, "", false, "", "http://[::1.2.3.4]"}, + {"http", "", "1.2.3.4", false, 0, {}, false, "", false, "", "http://1.2.3.4"}, + {"", "", "", false, 0, {}, false, "", false, "", ""}, + {"http", "bob", "", false, 0, {}, true, "foobar", false, "", "http://bob@?foobar"}, + {"", "bob", "", false, 0, {}, true, "foobar", false, "", "//bob@?foobar"}, + {"", "bob", "", false, 0, {}, false, "", false, "", "//bob@"}, }; size_t index = 0; for (const auto& testVector : testVectors) { @@ -805,10 +809,52 @@ TEST(UriTests, GenerateString) { uri.ClearPort(); } uri.SetPath(testVector.path); - uri.SetQuery(testVector.query); - uri.SetFragment(testVector.fragment); + if (testVector.hasQuery) { + uri.SetQuery(testVector.query); + } else { + uri.ClearQuery(); + } + if (testVector.hasFragment) { + uri.SetFragment(testVector.fragment); + } else { + uri.ClearFragment(); + } const auto actualUriString = uri.GenerateString(); ASSERT_EQ(testVector.expectedUriString, actualUriString) << index; ++index; } } + +TEST(UriTests, FragmentEmptyButPresent) { + Uri::Uri uri; + ASSERT_TRUE(uri.ParseFromString("http://example.com#")); + ASSERT_TRUE(uri.HasFragment()); + ASSERT_EQ("", uri.GetFragment()); + ASSERT_EQ("http://example.com/#", uri.GenerateString()); + uri.ClearFragment(); + ASSERT_EQ("http://example.com/", uri.GenerateString()); + ASSERT_FALSE(uri.HasFragment()); + ASSERT_TRUE(uri.ParseFromString("http://example.com")); + ASSERT_FALSE(uri.HasFragment()); + uri.SetFragment(""); + ASSERT_TRUE(uri.HasFragment()); + ASSERT_EQ("", uri.GetFragment()); + ASSERT_EQ("http://example.com/#", uri.GenerateString()); +} + +TEST(UriTests, QueryEmptyButPresent) { + Uri::Uri uri; + ASSERT_TRUE(uri.ParseFromString("http://example.com?")); + ASSERT_TRUE(uri.HasQuery()); + ASSERT_EQ("", uri.GetQuery()); + ASSERT_EQ("http://example.com/?", uri.GenerateString()); + uri.ClearQuery(); + ASSERT_EQ("http://example.com/", uri.GenerateString()); + ASSERT_FALSE(uri.HasQuery()); + ASSERT_TRUE(uri.ParseFromString("http://example.com")); + ASSERT_FALSE(uri.HasQuery()); + uri.SetQuery(""); + ASSERT_TRUE(uri.HasQuery()); + ASSERT_EQ("", uri.GetQuery()); + ASSERT_EQ("http://example.com/?", uri.GenerateString()); +} |