xref: /aosp_15_r20/external/cronet/net/third_party/uri_template/uri_template_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /*
2  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
3  * \license @{
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * @}
18  */
19 
20 #include "net/third_party/uri_template/uri_template.h"
21 
22 #include <memory>
23 #include <string>
24 
25 #include "testing/gtest/include/gtest/gtest.h"
26 
27 using std::string;
28 
29 namespace uri_template {
30 namespace {
31 
32 std::unordered_map<string, string> parameters_ = {
33     {"var", "value"},
34     {"hello", "Hello World!"},
35     {"path", "/foo/bar"},
36     {"empty", ""},
37     {"x", "1024"},
38     {"y", "768"},
39     {"percent", "%31"},
40     {"bad_percent", "%1"},
41     {"escaped", " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\x80\xFF"}};
42 
CheckExpansion(const string & uri_template,const string & expected_expansion,bool expected_validity=true,const std::set<string> * expected_vars=nullptr)43 void CheckExpansion(const string& uri_template,
44                     const string& expected_expansion,
45                     bool expected_validity = true,
46                     const std::set<string>* expected_vars = nullptr) {
47   string result;
48   std::set<string> vars_found;
49   EXPECT_EQ(expected_validity,
50             Expand(uri_template, parameters_, &result, &vars_found));
51   EXPECT_EQ(expected_expansion, result);
52   if (expected_vars) {
53     EXPECT_EQ(*expected_vars, vars_found);
54   }
55 }
56 
57 class UriTemplateTest : public testing::Test {};
58 
TEST_F(UriTemplateTest,TestLevel1Templates)59 TEST_F(UriTemplateTest, TestLevel1Templates) {
60   CheckExpansion("{var}", "value");
61   CheckExpansion("{hello}", "Hello%20World%21");
62   CheckExpansion("{percent}", "%2531");
63   CheckExpansion("{escaped}",
64                  "%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%3A%3B%3C%3D%3E%"
65                  "3F%40%5B%5C%5D%5E_%60%7B%7C%7D~%80%FF");
66 }
67 
TEST_F(UriTemplateTest,TestLevel2Templates)68 TEST_F(UriTemplateTest, TestLevel2Templates) {
69   // Reserved string expansion
70   CheckExpansion("{+var}", "value");
71   CheckExpansion("{+hello}", "Hello%20World!");
72   CheckExpansion("{+percent}", "%31");
73   CheckExpansion("{+bad_percent}", "%251");
74   CheckExpansion(
75       "{+escaped}",
76       "%20!%22#$%25&'()*+,-./:;%3C=%3E?@[%5C]%5E_%60%7B%7C%7D~%80%FF");
77   CheckExpansion("{+path}/here", "/foo/bar/here");
78   CheckExpansion("here?ref={+path}", "here?ref=/foo/bar");
79   // Fragment expansion
80   CheckExpansion("X{#var}", "X#value");
81   CheckExpansion("X{#hello}", "X#Hello%20World!");
82 }
83 
TEST_F(UriTemplateTest,TestLevel3Templates)84 TEST_F(UriTemplateTest, TestLevel3Templates) {
85   // String expansion with multiple variables
86   CheckExpansion("map?{x,y}", "map?1024,768");
87   CheckExpansion("{x,hello,y}", "1024,Hello%20World%21,768");
88   // Reserved expansion with multiple variables
89   CheckExpansion("{+x,hello,y}", "1024,Hello%20World!,768");
90   CheckExpansion("{+path,x}/here", "/foo/bar,1024/here");
91   // Fragment expansion with multiple variables
92   CheckExpansion("{#x,hello,y}", "#1024,Hello%20World!,768");
93   CheckExpansion("{#path,x}/here", "#/foo/bar,1024/here");
94   // Label expansion, dot-prefixed
95   CheckExpansion("X{.var}", "X.value");
96   CheckExpansion("X{.x,y}", "X.1024.768");
97   // Path segments, slash-prefixed
98   CheckExpansion("{/var}", "/value");
99   CheckExpansion("{/var,x}/here", "/value/1024/here");
100   // Path-style parameters, semicolon-prefixed
101   CheckExpansion("{;x,y}", ";x=1024;y=768");
102   CheckExpansion("{;x,y,empty}", ";x=1024;y=768;empty");
103   // Form-style query, ampersand-separated
104   CheckExpansion("{?x,y}", "?x=1024&y=768");
105   CheckExpansion("{?x,y,empty}", "?x=1024&y=768&empty=");
106   // Form-style query continuation
107   CheckExpansion("?fixed=yes{&x}", "?fixed=yes&x=1024");
108   CheckExpansion("{&x,y,empty}", "&x=1024&y=768&empty=");
109 }
110 
TEST_F(UriTemplateTest,TestMalformed)111 TEST_F(UriTemplateTest, TestMalformed) {
112   CheckExpansion("{", "", false);
113   CheckExpansion("map?{x", "", false);
114   CheckExpansion("map?{x,{y}", "", false);
115   CheckExpansion("map?{x,y}}", "", false);
116   CheckExpansion("map?{{x,y}}", "", false);
117 }
118 
TEST_F(UriTemplateTest,TestVariableSet)119 TEST_F(UriTemplateTest, TestVariableSet) {
120   std::set<string> expected_vars = {};
121   CheckExpansion("map?{z}", "map?", true, &expected_vars);
122   CheckExpansion("map{?z}", "map", true, &expected_vars);
123   expected_vars = {"empty"};
124   CheckExpansion("{empty}", "", true, &expected_vars);
125   expected_vars = {"x", "y"};
126   CheckExpansion("map?{x,y}", "map?1024,768", true, &expected_vars);
127   CheckExpansion("map?{x,z,y}", "map?1024,768", true, &expected_vars);
128   CheckExpansion("map{?x,z,y}", "map?x=1024&y=768", true, &expected_vars);
129   expected_vars = {"y", "path"};
130   CheckExpansion("{+path}{/z}{?y}&k=24", "/foo/bar?y=768&k=24", true,
131                  &expected_vars);
132   CheckExpansion("{y}{+path}", "768/foo/bar", true, &expected_vars);
133 }
134 
135 }  // namespace
136 }  // namespace uri_template
137