1 // Copyright 2021 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "quiche/common/platform/api/quiche_url_utils.h"
6
7 #include <optional>
8 #include <set>
9 #include <string>
10
11 #include "absl/container/flat_hash_map.h"
12 #include "absl/container/flat_hash_set.h"
13 #include "quiche/common/platform/api/quiche_test.h"
14
15 namespace quiche {
16 namespace {
17
ValidateExpansion(const std::string & uri_template,const absl::flat_hash_map<std::string,std::string> & parameters,const std::string & expected_expansion,const absl::flat_hash_set<std::string> & expected_vars_found)18 void ValidateExpansion(
19 const std::string& uri_template,
20 const absl::flat_hash_map<std::string, std::string>& parameters,
21 const std::string& expected_expansion,
22 const absl::flat_hash_set<std::string>& expected_vars_found) {
23 absl::flat_hash_set<std::string> vars_found;
24 std::string target;
25 ASSERT_TRUE(
26 ExpandURITemplate(uri_template, parameters, &target, &vars_found));
27 EXPECT_EQ(expected_expansion, target);
28 EXPECT_EQ(vars_found, expected_vars_found);
29 }
30
TEST(QuicheUrlUtilsTest,Basic)31 TEST(QuicheUrlUtilsTest, Basic) {
32 ValidateExpansion("/{foo}/{bar}/", {{"foo", "123"}, {"bar", "456"}},
33 "/123/456/", {"foo", "bar"});
34 }
35
TEST(QuicheUrlUtilsTest,ExtraParameter)36 TEST(QuicheUrlUtilsTest, ExtraParameter) {
37 ValidateExpansion("/{foo}/{bar}/{baz}/", {{"foo", "123"}, {"bar", "456"}},
38 "/123/456//", {"foo", "bar"});
39 }
40
TEST(QuicheUrlUtilsTest,MissingParameter)41 TEST(QuicheUrlUtilsTest, MissingParameter) {
42 ValidateExpansion("/{foo}/{baz}/", {{"foo", "123"}, {"bar", "456"}}, "/123//",
43 {"foo"});
44 }
45
TEST(QuicheUrlUtilsTest,RepeatedParameter)46 TEST(QuicheUrlUtilsTest, RepeatedParameter) {
47 ValidateExpansion("/{foo}/{bar}/{foo}/", {{"foo", "123"}, {"bar", "456"}},
48 "/123/456/123/", {"foo", "bar"});
49 }
50
TEST(QuicheUrlUtilsTest,URLEncoding)51 TEST(QuicheUrlUtilsTest, URLEncoding) {
52 ValidateExpansion("/{foo}/{bar}/", {{"foo", "123"}, {"bar", ":"}},
53 "/123/%3A/", {"foo", "bar"});
54 }
55
ValidateUrlDecode(const std::string & input,const std::optional<std::string> & expected_output)56 void ValidateUrlDecode(const std::string& input,
57 const std::optional<std::string>& expected_output) {
58 std::optional<std::string> decode_result = AsciiUrlDecode(input);
59 if (!expected_output.has_value()) {
60 EXPECT_FALSE(decode_result.has_value());
61 return;
62 }
63 ASSERT_TRUE(decode_result.has_value());
64 EXPECT_EQ(decode_result.value(), expected_output);
65 }
66
TEST(QuicheUrlUtilsTest,DecodeNoChange)67 TEST(QuicheUrlUtilsTest, DecodeNoChange) {
68 ValidateUrlDecode("foobar", "foobar");
69 }
70
TEST(QuicheUrlUtilsTest,DecodeReplace)71 TEST(QuicheUrlUtilsTest, DecodeReplace) {
72 ValidateUrlDecode("%7Bfoobar%7D", "{foobar}");
73 }
74
TEST(QuicheUrlUtilsTest,DecodeFail)75 TEST(QuicheUrlUtilsTest, DecodeFail) { ValidateUrlDecode("%FF", std::nullopt); }
76
77 } // namespace
78 } // namespace quiche
79