OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" | 5 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" |
6 | 6 |
7 #include "net/http/http_response_headers.h" | 7 #include "net/http/http_response_headers.h" |
8 #include "net/proxy/proxy_service.h" | 8 #include "net/proxy/proxy_service.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // Transform "normal"-looking headers (\n-separated) to the appropriate | 13 // Transform "normal"-looking headers (\n-separated) to the appropriate |
14 // input format for ParseRawHeaders (\0-separated). | 14 // input format for ParseRawHeaders (\0-separated). |
15 void HeadersToRaw(std::string* headers) { | 15 void HeadersToRaw(std::string* headers) { |
16 std::replace(headers->begin(), headers->end(), '\n', '\0'); | 16 std::replace(headers->begin(), headers->end(), '\n', '\0'); |
17 if (!headers->empty()) | 17 if (!headers->empty()) |
18 *headers += '\0'; | 18 *headers += '\0'; |
19 } | 19 } |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 namespace data_reduction_proxy { | 23 namespace data_reduction_proxy { |
24 | 24 |
25 class DataReductionProxyHeadersTest : public testing::Test {}; | 25 class DataReductionProxyHeadersTest : public testing::Test {}; |
26 | 26 |
| 27 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyActionValue) { |
| 28 const struct { |
| 29 const char* headers; |
| 30 std::string action_key; |
| 31 bool expected_result; |
| 32 std::string expected_action_value; |
| 33 } tests[] = { |
| 34 { "HTTP/1.1 200 OK\n" |
| 35 "Content-Length: 999\n", |
| 36 "a", |
| 37 false, |
| 38 "", |
| 39 }, |
| 40 { "HTTP/1.1 200 OK\n" |
| 41 "connection: keep-alive\n" |
| 42 "Content-Length: 999\n", |
| 43 "a", |
| 44 false, |
| 45 "", |
| 46 }, |
| 47 { "HTTP/1.1 200 OK\n" |
| 48 "connection: keep-alive\n" |
| 49 "Chrome-Proxy: bypass=86400\n" |
| 50 "Content-Length: 999\n", |
| 51 "bypass", |
| 52 true, |
| 53 "86400", |
| 54 }, |
| 55 { "HTTP/1.1 200 OK\n" |
| 56 "connection: keep-alive\n" |
| 57 "Chrome-Proxy: bypass86400\n" |
| 58 "Content-Length: 999\n", |
| 59 "bypass", |
| 60 false, |
| 61 "", |
| 62 }, |
| 63 { "HTTP/1.1 200 OK\n" |
| 64 "connection: keep-alive\n" |
| 65 "Chrome-Proxy: bypass=0\n" |
| 66 "Content-Length: 999\n", |
| 67 "bypass", |
| 68 true, |
| 69 "0", |
| 70 }, |
| 71 { "HTTP/1.1 200 OK\n" |
| 72 "connection: keep-alive\n" |
| 73 "Chrome-Proxy: bypass=1500\n" |
| 74 "Chrome-Proxy: bypass=86400\n" |
| 75 "Content-Length: 999\n", |
| 76 "bypass", |
| 77 true, |
| 78 "1500", |
| 79 }, |
| 80 { "HTTP/1.1 200 OK\n" |
| 81 "connection: keep-alive\n" |
| 82 "Chrome-Proxy: block=1500, block=3600\n" |
| 83 "Content-Length: 999\n", |
| 84 "block", |
| 85 true, |
| 86 "1500", |
| 87 }, |
| 88 { "HTTP/1.1 200 OK\n" |
| 89 "connection: proxy-bypass\n" |
| 90 "Chrome-Proxy: key=123 \n" |
| 91 "Content-Length: 999\n", |
| 92 "key", |
| 93 true, |
| 94 "123", |
| 95 }, |
| 96 { "HTTP/1.1 200 OK\n" |
| 97 "connection: proxy-bypass\n" |
| 98 "Chrome-Proxy: key= \n" |
| 99 "Content-Length: 999\n", |
| 100 "key", |
| 101 true, |
| 102 "", |
| 103 }, |
| 104 }; |
| 105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 106 std::string headers(tests[i].headers); |
| 107 HeadersToRaw(&headers); |
| 108 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 109 new net::HttpResponseHeaders(headers)); |
| 110 |
| 111 std::string action_value; |
| 112 bool has_action_key = GetDataReductionProxyActionValue( |
| 113 parsed, |
| 114 tests[i].action_key, |
| 115 &action_value); |
| 116 EXPECT_EQ(tests[i].expected_result, has_action_key); |
| 117 if (has_action_key) { |
| 118 EXPECT_EQ(tests[i].expected_action_value, action_value); |
| 119 } |
| 120 } |
| 121 } |
| 122 |
27 TEST_F(DataReductionProxyHeadersTest, GetProxyBypassInfo) { | 123 TEST_F(DataReductionProxyHeadersTest, GetProxyBypassInfo) { |
28 const struct { | 124 const struct { |
29 const char* headers; | 125 const char* headers; |
30 bool expected_result; | 126 bool expected_result; |
31 int64 expected_retry_delay; | 127 int64 expected_retry_delay; |
32 bool expected_bypass_all; | 128 bool expected_bypass_all; |
33 } tests[] = { | 129 } tests[] = { |
34 { "HTTP/1.1 200 OK\n" | 130 { "HTTP/1.1 200 OK\n" |
35 "Content-Length: 999\n", | 131 "Content-Length: 999\n", |
36 false, | 132 false, |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 data_reduction_proxy_info.bypass_duration.InSeconds()); | 284 data_reduction_proxy_info.bypass_duration.InSeconds()); |
189 EXPECT_EQ(tests[i].expected_bypass_all, | 285 EXPECT_EQ(tests[i].expected_bypass_all, |
190 data_reduction_proxy_info.bypass_all); | 286 data_reduction_proxy_info.bypass_all); |
191 } | 287 } |
192 } | 288 } |
193 | 289 |
194 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) { | 290 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) { |
195 const struct { | 291 const struct { |
196 const char* headers; | 292 const char* headers; |
197 bool expected_result; | 293 bool expected_result; |
| 294 bool expected_is_the_last; |
198 } tests[] = { | 295 } tests[] = { |
199 { "HTTP/1.1 200 OK\n" | 296 { "HTTP/1.1 200 OK\n" |
200 "Via: 1.1 Chrome-Proxy\n", | 297 "Via: 1.1 Chrome-Proxy\n", |
201 false, | 298 false, |
| 299 false, |
202 }, | 300 }, |
203 { "HTTP/1.1 200 OK\n" | 301 { "HTTP/1.1 200 OK\n" |
204 "Via: 1\n", | 302 "Via: 1\n", |
205 false, | 303 false, |
| 304 false, |
206 }, | 305 }, |
207 { "HTTP/1.1 200 OK\n" | 306 { "HTTP/1.1 200 OK\n" |
208 "Via: 1.1 Chrome-Compression-Proxy\n", | 307 "Via: 1.1 Chrome-Compression-Proxy\n", |
209 true, | 308 true, |
| 309 true, |
210 }, | 310 }, |
211 { "HTTP/1.1 200 OK\n" | 311 { "HTTP/1.1 200 OK\n" |
212 "Via: 1.0 Chrome-Compression-Proxy\n", | 312 "Via: 1.0 Chrome-Compression-Proxy\n", |
213 true, | 313 true, |
| 314 true, |
214 }, | 315 }, |
215 { "HTTP/1.1 200 OK\n" | 316 { "HTTP/1.1 200 OK\n" |
216 "Via: 1.1 Foo-Bar, 1.1 Chrome-Compression-Proxy\n", | 317 "Via: 1.1 Foo-Bar, 1.1 Chrome-Compression-Proxy\n", |
217 true, | 318 true, |
| 319 true, |
218 }, | 320 }, |
219 { "HTTP/1.1 200 OK\n" | 321 { "HTTP/1.1 200 OK\n" |
220 "Via: 1.1 Chrome-Compression-Proxy, 1.1 Bar-Foo\n", | 322 "Via: 1.1 Chrome-Compression-Proxy, 1.1 Bar-Foo\n", |
221 true, | 323 true, |
| 324 false, |
222 }, | 325 }, |
223 { "HTTP/1.1 200 OK\n" | 326 { "HTTP/1.1 200 OK\n" |
224 "Via: 1.1 chrome-compression-proxy\n", | 327 "Via: 1.1 chrome-compression-proxy\n", |
225 false, | 328 false, |
| 329 false, |
226 }, | 330 }, |
227 { "HTTP/1.1 200 OK\n" | 331 { "HTTP/1.1 200 OK\n" |
228 "Via: 1.1 Foo-Bar\n" | 332 "Via: 1.1 Foo-Bar\n" |
229 "Via: 1.1 Chrome-Compression-Proxy\n", | 333 "Via: 1.1 Chrome-Compression-Proxy\n", |
230 true, | 334 true, |
| 335 true, |
| 336 }, |
| 337 { "HTTP/1.1 200 OK\n" |
| 338 "Via: 1.1 Chrome-Compression-Proxy\n" |
| 339 "Via: 1.1 Foo-Bar\n", |
| 340 true, |
| 341 false, |
231 }, | 342 }, |
232 { "HTTP/1.1 200 OK\n" | 343 { "HTTP/1.1 200 OK\n" |
233 "Via: 1.1 Chrome-Proxy\n", | 344 "Via: 1.1 Chrome-Proxy\n", |
234 false, | 345 false, |
| 346 false, |
235 }, | 347 }, |
236 { "HTTP/1.1 200 OK\n" | 348 { "HTTP/1.1 200 OK\n" |
237 "Via: 1.1 Chrome Compression Proxy\n", | 349 "Via: 1.1 Chrome Compression Proxy\n", |
238 true, | 350 true, |
| 351 true, |
239 }, | 352 }, |
240 { "HTTP/1.1 200 OK\n" | 353 { "HTTP/1.1 200 OK\n" |
241 "Via: 1.1 Foo-Bar, 1.1 Chrome Compression Proxy\n", | 354 "Via: 1.1 Foo-Bar, 1.1 Chrome Compression Proxy\n", |
242 true, | 355 true, |
| 356 true, |
243 }, | 357 }, |
244 { "HTTP/1.1 200 OK\n" | 358 { "HTTP/1.1 200 OK\n" |
245 "Via: 1.1 Chrome Compression Proxy, 1.1 Bar-Foo\n", | 359 "Via: 1.1 Chrome Compression Proxy, 1.1 Bar-Foo\n", |
246 true, | 360 true, |
| 361 false, |
247 }, | 362 }, |
248 { "HTTP/1.1 200 OK\n" | 363 { "HTTP/1.1 200 OK\n" |
249 "Via: 1.1 chrome compression proxy\n", | 364 "Via: 1.1 chrome compression proxy\n", |
250 false, | 365 false, |
| 366 false, |
251 }, | 367 }, |
252 { "HTTP/1.1 200 OK\n" | 368 { "HTTP/1.1 200 OK\n" |
253 "Via: 1.1 Foo-Bar\n" | 369 "Via: 1.1 Foo-Bar\n" |
254 "Via: 1.1 Chrome Compression Proxy\n", | 370 "Via: 1.1 Chrome Compression Proxy\n", |
255 true, | 371 true, |
| 372 true, |
| 373 }, |
| 374 { "HTTP/1.1 200 OK\n" |
| 375 "Via: 1.1 Chrome Compression Proxy\n" |
| 376 "Via: 1.1 Foo-Bar\n", |
| 377 true, |
| 378 false, |
256 }, | 379 }, |
257 }; | 380 }; |
| 381 bool has, is_the_last; |
| 382 std::string headers; |
| 383 scoped_refptr<net::HttpResponseHeaders> parsed; |
258 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 384 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
259 std::string headers(tests[i].headers); | 385 headers = tests[i].headers; |
260 HeadersToRaw(&headers); | 386 HeadersToRaw(&headers); |
261 scoped_refptr<net::HttpResponseHeaders> parsed( | 387 parsed = new net::HttpResponseHeaders(headers); |
262 new net::HttpResponseHeaders(headers)); | |
263 | 388 |
264 EXPECT_EQ(tests[i].expected_result, | 389 has = HasDataReductionProxyViaHeader(parsed, &is_the_last); |
265 HasDataReductionProxyViaHeader(parsed)); | 390 EXPECT_EQ(tests[i].expected_result, has); |
| 391 if (has) { |
| 392 EXPECT_EQ(tests[i].expected_is_the_last, is_the_last); |
| 393 } |
266 } | 394 } |
| 395 |
| 396 has = HasDataReductionProxyViaHeader(parsed, NULL); |
| 397 EXPECT_EQ(tests[ARRAYSIZE_UNSAFE(tests) - 1].expected_result, has); |
267 } | 398 } |
268 | 399 |
269 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { | 400 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { |
270 const struct { | 401 const struct { |
271 const char* headers; | 402 const char* headers; |
272 net::ProxyService::DataReductionProxyBypassType expected_result; | 403 net::ProxyService::DataReductionProxyBypassType expected_result; |
273 } tests[] = { | 404 } tests[] = { |
274 { "HTTP/1.1 200 OK\n" | 405 { "HTTP/1.1 200 OK\n" |
275 "Chrome-Proxy: bypass=0\n" | 406 "Chrome-Proxy: bypass=0\n" |
276 "Via: 1.1 Chrome-Compression-Proxy\n", | 407 "Via: 1.1 Chrome-Compression-Proxy\n", |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 std::string headers(tests[i].headers); | 497 std::string headers(tests[i].headers); |
367 HeadersToRaw(&headers); | 498 HeadersToRaw(&headers); |
368 scoped_refptr<net::HttpResponseHeaders> parsed( | 499 scoped_refptr<net::HttpResponseHeaders> parsed( |
369 new net::HttpResponseHeaders(headers)); | 500 new net::HttpResponseHeaders(headers)); |
370 DataReductionProxyInfo chrome_proxy_info; | 501 DataReductionProxyInfo chrome_proxy_info; |
371 EXPECT_EQ(tests[i].expected_result, | 502 EXPECT_EQ(tests[i].expected_result, |
372 GetDataReductionProxyBypassType(parsed, &chrome_proxy_info)); | 503 GetDataReductionProxyBypassType(parsed, &chrome_proxy_info)); |
373 } | 504 } |
374 } | 505 } |
375 } // namespace data_reduction_proxy | 506 } // namespace data_reduction_proxy |
OLD | NEW |