uri_template_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "net/third_party/uri_template/uri_template.h"
  20. #include <memory>
  21. #include <string>
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. using std::string;
  24. namespace uri_template {
  25. namespace {
  26. std::unordered_map<string, string> parameters_ = {
  27. {"var", "value"},
  28. {"hello", "Hello World!"},
  29. {"path", "/foo/bar"},
  30. {"empty", ""},
  31. {"x", "1024"},
  32. {"y", "768"},
  33. {"percent", "%31"},
  34. {"bad_percent", "%1"},
  35. {"escaped", " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\x80\xFF"}};
  36. void CheckExpansion(const string& uri_template,
  37. const string& expected_expansion,
  38. bool expected_validity = true,
  39. const std::set<string>* expected_vars = nullptr) {
  40. string result;
  41. std::set<string> vars_found;
  42. EXPECT_EQ(expected_validity,
  43. Expand(uri_template, parameters_, &result, &vars_found));
  44. EXPECT_EQ(expected_expansion, result);
  45. if (expected_vars) {
  46. EXPECT_EQ(*expected_vars, vars_found);
  47. }
  48. }
  49. class UriTemplateTest : public testing::Test {};
  50. TEST_F(UriTemplateTest, TestLevel1Templates) {
  51. CheckExpansion("{var}", "value");
  52. CheckExpansion("{hello}", "Hello%20World%21");
  53. CheckExpansion("{percent}", "%2531");
  54. CheckExpansion("{escaped}",
  55. "%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%3A%3B%3C%3D%3E%"
  56. "3F%40%5B%5C%5D%5E_%60%7B%7C%7D~%80%FF");
  57. }
  58. TEST_F(UriTemplateTest, TestLevel2Templates) {
  59. // Reserved string expansion
  60. CheckExpansion("{+var}", "value");
  61. CheckExpansion("{+hello}", "Hello%20World!");
  62. CheckExpansion("{+percent}", "%31");
  63. CheckExpansion("{+bad_percent}", "%251");
  64. CheckExpansion(
  65. "{+escaped}",
  66. "%20!%22#$%25&'()*+,-./:;%3C=%3E?@[%5C]%5E_%60%7B%7C%7D~%80%FF");
  67. CheckExpansion("{+path}/here", "/foo/bar/here");
  68. CheckExpansion("here?ref={+path}", "here?ref=/foo/bar");
  69. // Fragment expansion
  70. CheckExpansion("X{#var}", "X#value");
  71. CheckExpansion("X{#hello}", "X#Hello%20World!");
  72. }
  73. TEST_F(UriTemplateTest, TestLevel3Templates) {
  74. // String expansion with multiple variables
  75. CheckExpansion("map?{x,y}", "map?1024,768");
  76. CheckExpansion("{x,hello,y}", "1024,Hello%20World%21,768");
  77. // Reserved expansion with multiple variables
  78. CheckExpansion("{+x,hello,y}", "1024,Hello%20World!,768");
  79. CheckExpansion("{+path,x}/here", "/foo/bar,1024/here");
  80. // Fragment expansion with multiple variables
  81. CheckExpansion("{#x,hello,y}", "#1024,Hello%20World!,768");
  82. CheckExpansion("{#path,x}/here", "#/foo/bar,1024/here");
  83. // Label expansion, dot-prefixed
  84. CheckExpansion("X{.var}", "X.value");
  85. CheckExpansion("X{.x,y}", "X.1024.768");
  86. // Path segments, slash-prefixed
  87. CheckExpansion("{/var}", "/value");
  88. CheckExpansion("{/var,x}/here", "/value/1024/here");
  89. // Path-style parameters, semicolon-prefixed
  90. CheckExpansion("{;x,y}", ";x=1024;y=768");
  91. CheckExpansion("{;x,y,empty}", ";x=1024;y=768;empty");
  92. // Form-style query, ampersand-separated
  93. CheckExpansion("{?x,y}", "?x=1024&y=768");
  94. CheckExpansion("{?x,y,empty}", "?x=1024&y=768&empty=");
  95. // Form-style query continuation
  96. CheckExpansion("?fixed=yes{&x}", "?fixed=yes&x=1024");
  97. CheckExpansion("{&x,y,empty}", "&x=1024&y=768&empty=");
  98. }
  99. TEST_F(UriTemplateTest, TestMalformed) {
  100. CheckExpansion("{", "", false);
  101. CheckExpansion("map?{x", "", false);
  102. CheckExpansion("map?{x,{y}", "", false);
  103. CheckExpansion("map?{x,y}}", "", false);
  104. CheckExpansion("map?{{x,y}}", "", false);
  105. }
  106. TEST_F(UriTemplateTest, TestVariableSet) {
  107. std::set<string> expected_vars = {};
  108. CheckExpansion("map?{z}", "map?", true, &expected_vars);
  109. CheckExpansion("map{?z}", "map", true, &expected_vars);
  110. expected_vars = {"empty"};
  111. CheckExpansion("{empty}", "", true, &expected_vars);
  112. expected_vars = {"x", "y"};
  113. CheckExpansion("map?{x,y}", "map?1024,768", true, &expected_vars);
  114. CheckExpansion("map?{x,z,y}", "map?1024,768", true, &expected_vars);
  115. CheckExpansion("map{?x,z,y}", "map?x=1024&y=768", true, &expected_vars);
  116. expected_vars = {"y", "path"};
  117. CheckExpansion("{+path}{/z}{?y}&k=24", "/foo/bar?y=768&k=24", true,
  118. &expected_vars);
  119. CheckExpansion("{y}{+path}", "768/foo/bar", true, &expected_vars);
  120. }
  121. } // namespace
  122. } // namespace uri_template