You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/gunit.h"
  28. #include "talk/base/httpcommon-inl.h"
  29. #include "talk/base/httpcommon.h"
  30. namespace talk_base {
  31. #define TEST_PROTOCOL "http://"
  32. #define TEST_HOST "www.google.com"
  33. #define TEST_PATH "/folder/file.html"
  34. #define TEST_QUERY "?query=x&attr=y"
  35. #define TEST_URL TEST_PROTOCOL TEST_HOST TEST_PATH TEST_QUERY
  36. TEST(Url, DecomposesUrls) {
  37. Url<char> url(TEST_URL);
  38. EXPECT_TRUE(url.valid());
  39. EXPECT_FALSE(url.secure());
  40. EXPECT_STREQ(TEST_HOST, url.host().c_str());
  41. EXPECT_EQ(80, url.port());
  42. EXPECT_STREQ(TEST_PATH, url.path().c_str());
  43. EXPECT_STREQ(TEST_QUERY, url.query().c_str());
  44. EXPECT_STREQ(TEST_HOST, url.address().c_str());
  45. EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
  46. EXPECT_STREQ(TEST_URL, url.url().c_str());
  47. }
  48. TEST(Url, ComposesUrls) {
  49. // Set in constructor
  50. Url<char> url(TEST_PATH TEST_QUERY, TEST_HOST, 80);
  51. EXPECT_TRUE(url.valid());
  52. EXPECT_FALSE(url.secure());
  53. EXPECT_STREQ(TEST_HOST, url.host().c_str());
  54. EXPECT_EQ(80, url.port());
  55. EXPECT_STREQ(TEST_PATH, url.path().c_str());
  56. EXPECT_STREQ(TEST_QUERY, url.query().c_str());
  57. EXPECT_STREQ(TEST_HOST, url.address().c_str());
  58. EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
  59. EXPECT_STREQ(TEST_URL, url.url().c_str());
  60. url.clear();
  61. EXPECT_FALSE(url.valid());
  62. EXPECT_FALSE(url.secure());
  63. EXPECT_STREQ("", url.host().c_str());
  64. EXPECT_EQ(80, url.port());
  65. EXPECT_STREQ("/", url.path().c_str());
  66. EXPECT_STREQ("", url.query().c_str());
  67. // Set component-wise
  68. url.set_host(TEST_HOST);
  69. url.set_port(80);
  70. url.set_path(TEST_PATH);
  71. url.set_query(TEST_QUERY);
  72. EXPECT_TRUE(url.valid());
  73. EXPECT_FALSE(url.secure());
  74. EXPECT_STREQ(TEST_HOST, url.host().c_str());
  75. EXPECT_EQ(80, url.port());
  76. EXPECT_STREQ(TEST_PATH, url.path().c_str());
  77. EXPECT_STREQ(TEST_QUERY, url.query().c_str());
  78. EXPECT_STREQ(TEST_HOST, url.address().c_str());
  79. EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
  80. EXPECT_STREQ(TEST_URL, url.url().c_str());
  81. }
  82. TEST(Url, EnsuresNonEmptyPath) {
  83. Url<char> url(TEST_PROTOCOL TEST_HOST);
  84. EXPECT_TRUE(url.valid());
  85. EXPECT_STREQ("/", url.path().c_str());
  86. url.clear();
  87. EXPECT_STREQ("/", url.path().c_str());
  88. url.set_path("");
  89. EXPECT_STREQ("/", url.path().c_str());
  90. url.clear();
  91. EXPECT_STREQ("/", url.path().c_str());
  92. url.set_full_path("");
  93. EXPECT_STREQ("/", url.path().c_str());
  94. }
  95. TEST(Url, GetQueryAttributes) {
  96. Url<char> url(TEST_URL);
  97. std::string value;
  98. EXPECT_TRUE(url.get_attribute("query", &value));
  99. EXPECT_STREQ("x", value.c_str());
  100. value.clear();
  101. EXPECT_TRUE(url.get_attribute("attr", &value));
  102. EXPECT_STREQ("y", value.c_str());
  103. value.clear();
  104. EXPECT_FALSE(url.get_attribute("Query", &value));
  105. EXPECT_TRUE(value.empty());
  106. }
  107. TEST(HttpResponseData, parseLeaderHttp1_0) {
  108. static const char kResponseString[] = "HTTP/1.0 200 OK";
  109. HttpResponseData response;
  110. EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
  111. sizeof(kResponseString) - 1));
  112. EXPECT_EQ(HVER_1_0, response.version);
  113. EXPECT_EQ(200U, response.scode);
  114. }
  115. TEST(HttpResponseData, parseLeaderHttp1_1) {
  116. static const char kResponseString[] = "HTTP/1.1 200 OK";
  117. HttpResponseData response;
  118. EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
  119. sizeof(kResponseString) - 1));
  120. EXPECT_EQ(HVER_1_1, response.version);
  121. EXPECT_EQ(200U, response.scode);
  122. }
  123. TEST(HttpResponseData, parseLeaderHttpUnknown) {
  124. static const char kResponseString[] = "HTTP 200 OK";
  125. HttpResponseData response;
  126. EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
  127. sizeof(kResponseString) - 1));
  128. EXPECT_EQ(HVER_UNKNOWN, response.version);
  129. EXPECT_EQ(200U, response.scode);
  130. }
  131. TEST(HttpResponseData, parseLeaderHttpFailure) {
  132. static const char kResponseString[] = "HTTP/1.1 503 Service Unavailable";
  133. HttpResponseData response;
  134. EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
  135. sizeof(kResponseString) - 1));
  136. EXPECT_EQ(HVER_1_1, response.version);
  137. EXPECT_EQ(503U, response.scode);
  138. }
  139. TEST(HttpResponseData, parseLeaderHttpInvalid) {
  140. static const char kResponseString[] = "Durrrrr, what's HTTP?";
  141. HttpResponseData response;
  142. EXPECT_EQ(HE_PROTOCOL, response.parseLeader(kResponseString,
  143. sizeof(kResponseString) - 1));
  144. }
  145. } // namespace talk_base