3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/base/gunit.h"
29 #include "talk/base/httpcommon-inl.h"
30 #include "talk/base/httpcommon.h"
34 #define TEST_PROTOCOL "http://"
35 #define TEST_HOST "www.google.com"
36 #define TEST_PATH "/folder/file.html"
37 #define TEST_QUERY "?query=x&attr=y"
38 #define TEST_URL TEST_PROTOCOL TEST_HOST TEST_PATH TEST_QUERY
40 TEST(Url, DecomposesUrls) {
41 Url<char> url(TEST_URL);
42 EXPECT_TRUE(url.valid());
43 EXPECT_FALSE(url.secure());
44 EXPECT_STREQ(TEST_HOST, url.host().c_str());
45 EXPECT_EQ(80, url.port());
46 EXPECT_STREQ(TEST_PATH, url.path().c_str());
47 EXPECT_STREQ(TEST_QUERY, url.query().c_str());
48 EXPECT_STREQ(TEST_HOST, url.address().c_str());
49 EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
50 EXPECT_STREQ(TEST_URL, url.url().c_str());
53 TEST(Url, ComposesUrls) {
55 Url<char> url(TEST_PATH TEST_QUERY, TEST_HOST, 80);
56 EXPECT_TRUE(url.valid());
57 EXPECT_FALSE(url.secure());
58 EXPECT_STREQ(TEST_HOST, url.host().c_str());
59 EXPECT_EQ(80, url.port());
60 EXPECT_STREQ(TEST_PATH, url.path().c_str());
61 EXPECT_STREQ(TEST_QUERY, url.query().c_str());
62 EXPECT_STREQ(TEST_HOST, url.address().c_str());
63 EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
64 EXPECT_STREQ(TEST_URL, url.url().c_str());
67 EXPECT_FALSE(url.valid());
68 EXPECT_FALSE(url.secure());
69 EXPECT_STREQ("", url.host().c_str());
70 EXPECT_EQ(80, url.port());
71 EXPECT_STREQ("/", url.path().c_str());
72 EXPECT_STREQ("", url.query().c_str());
75 url.set_host(TEST_HOST);
77 url.set_path(TEST_PATH);
78 url.set_query(TEST_QUERY);
79 EXPECT_TRUE(url.valid());
80 EXPECT_FALSE(url.secure());
81 EXPECT_STREQ(TEST_HOST, url.host().c_str());
82 EXPECT_EQ(80, url.port());
83 EXPECT_STREQ(TEST_PATH, url.path().c_str());
84 EXPECT_STREQ(TEST_QUERY, url.query().c_str());
85 EXPECT_STREQ(TEST_HOST, url.address().c_str());
86 EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
87 EXPECT_STREQ(TEST_URL, url.url().c_str());
90 TEST(Url, EnsuresNonEmptyPath) {
91 Url<char> url(TEST_PROTOCOL TEST_HOST);
92 EXPECT_TRUE(url.valid());
93 EXPECT_STREQ("/", url.path().c_str());
96 EXPECT_STREQ("/", url.path().c_str());
98 EXPECT_STREQ("/", url.path().c_str());
101 EXPECT_STREQ("/", url.path().c_str());
102 url.set_full_path("");
103 EXPECT_STREQ("/", url.path().c_str());
106 TEST(Url, GetQueryAttributes) {
107 Url<char> url(TEST_URL);
109 EXPECT_TRUE(url.get_attribute("query", &value));
110 EXPECT_STREQ("x", value.c_str());
112 EXPECT_TRUE(url.get_attribute("attr", &value));
113 EXPECT_STREQ("y", value.c_str());
115 EXPECT_FALSE(url.get_attribute("Query", &value));
116 EXPECT_TRUE(value.empty());
119 TEST(Url, SkipsUserAndPassword) {
120 Url<char> url("https://mail.google.com:pwd@badsite.com:12345/asdf");
121 EXPECT_TRUE(url.valid());
122 EXPECT_TRUE(url.secure());
123 EXPECT_STREQ("badsite.com", url.host().c_str());
124 EXPECT_EQ(12345, url.port());
125 EXPECT_STREQ("/asdf", url.path().c_str());
126 EXPECT_STREQ("badsite.com:12345", url.address().c_str());
129 TEST(Url, SkipsUser) {
130 Url<char> url("https://mail.google.com@badsite.com:12345/asdf");
131 EXPECT_TRUE(url.valid());
132 EXPECT_TRUE(url.secure());
133 EXPECT_STREQ("badsite.com", url.host().c_str());
134 EXPECT_EQ(12345, url.port());
135 EXPECT_STREQ("/asdf", url.path().c_str());
136 EXPECT_STREQ("badsite.com:12345", url.address().c_str());
139 TEST(HttpResponseData, parseLeaderHttp1_0) {
140 static const char kResponseString[] = "HTTP/1.0 200 OK";
141 HttpResponseData response;
142 EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
143 sizeof(kResponseString) - 1));
144 EXPECT_EQ(HVER_1_0, response.version);
145 EXPECT_EQ(200U, response.scode);
148 TEST(HttpResponseData, parseLeaderHttp1_1) {
149 static const char kResponseString[] = "HTTP/1.1 200 OK";
150 HttpResponseData response;
151 EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
152 sizeof(kResponseString) - 1));
153 EXPECT_EQ(HVER_1_1, response.version);
154 EXPECT_EQ(200U, response.scode);
157 TEST(HttpResponseData, parseLeaderHttpUnknown) {
158 static const char kResponseString[] = "HTTP 200 OK";
159 HttpResponseData response;
160 EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
161 sizeof(kResponseString) - 1));
162 EXPECT_EQ(HVER_UNKNOWN, response.version);
163 EXPECT_EQ(200U, response.scode);
166 TEST(HttpResponseData, parseLeaderHttpFailure) {
167 static const char kResponseString[] = "HTTP/1.1 503 Service Unavailable";
168 HttpResponseData response;
169 EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
170 sizeof(kResponseString) - 1));
171 EXPECT_EQ(HVER_1_1, response.version);
172 EXPECT_EQ(503U, response.scode);
175 TEST(HttpResponseData, parseLeaderHttpInvalid) {
176 static const char kResponseString[] = "Durrrrr, what's HTTP?";
177 HttpResponseData response;
178 EXPECT_EQ(HE_PROTOCOL, response.parseLeader(kResponseString,
179 sizeof(kResponseString) - 1));
182 } // namespace talk_base