1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/httpcommon-inl.h"
13 #include "webrtc/base/httpcommon.h"
14
15 namespace rtc {
16
17 #define TEST_PROTOCOL "http://"
18 #define TEST_HOST "www.google.com"
19 #define TEST_PATH "/folder/file.html"
20 #define TEST_QUERY "?query=x&attr=y"
21 #define TEST_URL TEST_PROTOCOL TEST_HOST TEST_PATH TEST_QUERY
22
TEST(Url,DecomposesUrls)23 TEST(Url, DecomposesUrls) {
24 Url<char> url(TEST_URL);
25 EXPECT_TRUE(url.valid());
26 EXPECT_FALSE(url.secure());
27 EXPECT_STREQ(TEST_HOST, url.host().c_str());
28 EXPECT_EQ(80, url.port());
29 EXPECT_STREQ(TEST_PATH, url.path().c_str());
30 EXPECT_STREQ(TEST_QUERY, url.query().c_str());
31 EXPECT_STREQ(TEST_HOST, url.address().c_str());
32 EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
33 EXPECT_STREQ(TEST_URL, url.url().c_str());
34 }
35
TEST(Url,ComposesUrls)36 TEST(Url, ComposesUrls) {
37 // Set in constructor
38 Url<char> url(TEST_PATH TEST_QUERY, TEST_HOST, 80);
39 EXPECT_TRUE(url.valid());
40 EXPECT_FALSE(url.secure());
41 EXPECT_STREQ(TEST_HOST, url.host().c_str());
42 EXPECT_EQ(80, url.port());
43 EXPECT_STREQ(TEST_PATH, url.path().c_str());
44 EXPECT_STREQ(TEST_QUERY, url.query().c_str());
45 EXPECT_STREQ(TEST_HOST, url.address().c_str());
46 EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
47 EXPECT_STREQ(TEST_URL, url.url().c_str());
48
49 url.clear();
50 EXPECT_FALSE(url.valid());
51 EXPECT_FALSE(url.secure());
52 EXPECT_STREQ("", url.host().c_str());
53 EXPECT_EQ(80, url.port());
54 EXPECT_STREQ("/", url.path().c_str());
55 EXPECT_STREQ("", url.query().c_str());
56
57 // Set component-wise
58 url.set_host(TEST_HOST);
59 url.set_port(80);
60 url.set_path(TEST_PATH);
61 url.set_query(TEST_QUERY);
62 EXPECT_TRUE(url.valid());
63 EXPECT_FALSE(url.secure());
64 EXPECT_STREQ(TEST_HOST, url.host().c_str());
65 EXPECT_EQ(80, url.port());
66 EXPECT_STREQ(TEST_PATH, url.path().c_str());
67 EXPECT_STREQ(TEST_QUERY, url.query().c_str());
68 EXPECT_STREQ(TEST_HOST, url.address().c_str());
69 EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
70 EXPECT_STREQ(TEST_URL, url.url().c_str());
71 }
72
TEST(Url,EnsuresNonEmptyPath)73 TEST(Url, EnsuresNonEmptyPath) {
74 Url<char> url(TEST_PROTOCOL TEST_HOST);
75 EXPECT_TRUE(url.valid());
76 EXPECT_STREQ("/", url.path().c_str());
77
78 url.clear();
79 EXPECT_STREQ("/", url.path().c_str());
80 url.set_path("");
81 EXPECT_STREQ("/", url.path().c_str());
82
83 url.clear();
84 EXPECT_STREQ("/", url.path().c_str());
85 url.set_full_path("");
86 EXPECT_STREQ("/", url.path().c_str());
87 }
88
TEST(Url,GetQueryAttributes)89 TEST(Url, GetQueryAttributes) {
90 Url<char> url(TEST_URL);
91 std::string value;
92 EXPECT_TRUE(url.get_attribute("query", &value));
93 EXPECT_STREQ("x", value.c_str());
94 value.clear();
95 EXPECT_TRUE(url.get_attribute("attr", &value));
96 EXPECT_STREQ("y", value.c_str());
97 value.clear();
98 EXPECT_FALSE(url.get_attribute("Query", &value));
99 EXPECT_TRUE(value.empty());
100 }
101
TEST(Url,SkipsUserAndPassword)102 TEST(Url, SkipsUserAndPassword) {
103 Url<char> url("https://mail.google.com:pwd@badsite.com:12345/asdf");
104 EXPECT_TRUE(url.valid());
105 EXPECT_TRUE(url.secure());
106 EXPECT_STREQ("badsite.com", url.host().c_str());
107 EXPECT_EQ(12345, url.port());
108 EXPECT_STREQ("/asdf", url.path().c_str());
109 EXPECT_STREQ("badsite.com:12345", url.address().c_str());
110 }
111
TEST(Url,SkipsUser)112 TEST(Url, SkipsUser) {
113 Url<char> url("https://mail.google.com@badsite.com:12345/asdf");
114 EXPECT_TRUE(url.valid());
115 EXPECT_TRUE(url.secure());
116 EXPECT_STREQ("badsite.com", url.host().c_str());
117 EXPECT_EQ(12345, url.port());
118 EXPECT_STREQ("/asdf", url.path().c_str());
119 EXPECT_STREQ("badsite.com:12345", url.address().c_str());
120 }
121
TEST(HttpResponseData,parseLeaderHttp1_0)122 TEST(HttpResponseData, parseLeaderHttp1_0) {
123 static const char kResponseString[] = "HTTP/1.0 200 OK";
124 HttpResponseData response;
125 EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
126 sizeof(kResponseString) - 1));
127 EXPECT_EQ(HVER_1_0, response.version);
128 EXPECT_EQ(200U, response.scode);
129 }
130
TEST(HttpResponseData,parseLeaderHttp1_1)131 TEST(HttpResponseData, parseLeaderHttp1_1) {
132 static const char kResponseString[] = "HTTP/1.1 200 OK";
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(200U, response.scode);
138 }
139
TEST(HttpResponseData,parseLeaderHttpUnknown)140 TEST(HttpResponseData, parseLeaderHttpUnknown) {
141 static const char kResponseString[] = "HTTP 200 OK";
142 HttpResponseData response;
143 EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
144 sizeof(kResponseString) - 1));
145 EXPECT_EQ(HVER_UNKNOWN, response.version);
146 EXPECT_EQ(200U, response.scode);
147 }
148
TEST(HttpResponseData,parseLeaderHttpFailure)149 TEST(HttpResponseData, parseLeaderHttpFailure) {
150 static const char kResponseString[] = "HTTP/1.1 503 Service Unavailable";
151 HttpResponseData response;
152 EXPECT_EQ(HE_NONE, response.parseLeader(kResponseString,
153 sizeof(kResponseString) - 1));
154 EXPECT_EQ(HVER_1_1, response.version);
155 EXPECT_EQ(503U, response.scode);
156 }
157
TEST(HttpResponseData,parseLeaderHttpInvalid)158 TEST(HttpResponseData, parseLeaderHttpInvalid) {
159 static const char kResponseString[] = "Durrrrr, what's HTTP?";
160 HttpResponseData response;
161 EXPECT_EQ(HE_PROTOCOL, response.parseLeader(kResponseString,
162 sizeof(kResponseString) - 1));
163 }
164
165 } // namespace rtc
166