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/core/browser/data_reduction_proxy_inte
rceptor.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_inte
rceptor.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 // Tests the mechanics of proxy bypass work with a "real" server. For tests | 253 // Tests the mechanics of proxy bypass work with a "real" server. For tests |
254 // that cover every imaginable response that could trigger a bypass, see: | 254 // that cover every imaginable response that could trigger a bypass, see: |
255 // DataReductionProxyProtocolTest. | 255 // DataReductionProxyProtocolTest. |
256 net::TestDelegate delegate; | 256 net::TestDelegate delegate; |
257 std::unique_ptr<net::URLRequest> request(context().CreateRequest( | 257 std::unique_ptr<net::URLRequest> request(context().CreateRequest( |
258 direct().GetURL("/block10.html"), net::DEFAULT_PRIORITY, &delegate)); | 258 direct().GetURL("/block10.html"), net::DEFAULT_PRIORITY, &delegate)); |
259 request->Start(); | 259 request->Start(); |
260 EXPECT_TRUE(request->is_pending()); | 260 EXPECT_TRUE(request->is_pending()); |
261 base::RunLoop().Run(); | 261 base::RunLoop().Run(); |
262 | 262 |
263 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 263 EXPECT_EQ(net::OK, delegate.request_status()); |
264 EXPECT_EQ(net::OK, request->status().error()); | |
265 EXPECT_EQ("hello", delegate.data_received()); | 264 EXPECT_EQ("hello", delegate.data_received()); |
266 } | 265 } |
267 | 266 |
268 TEST_F(DataReductionProxyInterceptorWithServerTest, TestNoBypass) { | 267 TEST_F(DataReductionProxyInterceptorWithServerTest, TestNoBypass) { |
269 net::TestDelegate delegate; | 268 net::TestDelegate delegate; |
270 std::unique_ptr<net::URLRequest> request(context().CreateRequest( | 269 std::unique_ptr<net::URLRequest> request(context().CreateRequest( |
271 direct().GetURL("/noblock.html"), net::DEFAULT_PRIORITY, &delegate)); | 270 direct().GetURL("/noblock.html"), net::DEFAULT_PRIORITY, &delegate)); |
272 request->Start(); | 271 request->Start(); |
273 EXPECT_TRUE(request->is_pending()); | 272 EXPECT_TRUE(request->is_pending()); |
274 base::RunLoop().Run(); | 273 base::RunLoop().Run(); |
275 | 274 |
276 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 275 EXPECT_EQ(net::OK, delegate.request_status()); |
277 EXPECT_EQ(net::OK, request->status().error()); | |
278 EXPECT_EQ("hello", delegate.data_received()); | 276 EXPECT_EQ("hello", delegate.data_received()); |
279 } | 277 } |
280 | 278 |
281 class DataReductionProxyInterceptorEndToEndTest : public testing::Test { | 279 class DataReductionProxyInterceptorEndToEndTest : public testing::Test { |
282 public: | 280 public: |
283 DataReductionProxyInterceptorEndToEndTest() | 281 DataReductionProxyInterceptorEndToEndTest() |
284 : context_(true), context_storage_(&context_) {} | 282 : context_(true), context_storage_(&context_) {} |
285 | 283 |
286 ~DataReductionProxyInterceptorEndToEndTest() override {} | 284 ~DataReductionProxyInterceptorEndToEndTest() override {} |
287 | 285 |
(...skipping 19 matching lines...) Expand all Loading... |
307 // Creates a URLRequest using the test's TestURLRequestContext and executes | 305 // Creates a URLRequest using the test's TestURLRequestContext and executes |
308 // it. Returns the created URLRequest. | 306 // it. Returns the created URLRequest. |
309 std::unique_ptr<net::URLRequest> CreateAndExecuteRequest(const GURL& url) { | 307 std::unique_ptr<net::URLRequest> CreateAndExecuteRequest(const GURL& url) { |
310 std::unique_ptr<net::URLRequest> request( | 308 std::unique_ptr<net::URLRequest> request( |
311 context_.CreateRequest(url, net::IDLE, &delegate_)); | 309 context_.CreateRequest(url, net::IDLE, &delegate_)); |
312 request->Start(); | 310 request->Start(); |
313 drp_test_context_->RunUntilIdle(); | 311 drp_test_context_->RunUntilIdle(); |
314 return request; | 312 return request; |
315 } | 313 } |
316 | 314 |
317 const net::TestDelegate& delegate() const { | 315 const net::TestDelegate& delegate() const { return delegate_; } |
318 return delegate_; | |
319 } | |
320 | 316 |
321 net::MockClientSocketFactory* mock_socket_factory() { | 317 net::MockClientSocketFactory* mock_socket_factory() { |
322 return &mock_socket_factory_; | 318 return &mock_socket_factory_; |
323 } | 319 } |
324 | 320 |
325 TestDataReductionProxyConfig* config() const { | 321 TestDataReductionProxyConfig* config() const { |
326 return drp_test_context_->config(); | 322 return drp_test_context_->config(); |
327 } | 323 } |
328 | 324 |
329 net::ProxyServer origin() const { | 325 net::ProxyServer origin() const { |
(...skipping 19 matching lines...) Expand all Loading... |
349 MockRead(kBody.c_str()), | 345 MockRead(kBody.c_str()), |
350 MockRead(net::SYNCHRONOUS, net::OK), | 346 MockRead(net::SYNCHRONOUS, net::OK), |
351 }; | 347 }; |
352 net::StaticSocketDataProvider socket_data_provider( | 348 net::StaticSocketDataProvider socket_data_provider( |
353 mock_reads, arraysize(mock_reads), nullptr, 0); | 349 mock_reads, arraysize(mock_reads), nullptr, 0); |
354 mock_socket_factory()->AddSocketDataProvider(&socket_data_provider); | 350 mock_socket_factory()->AddSocketDataProvider(&socket_data_provider); |
355 | 351 |
356 std::unique_ptr<net::URLRequest> request = | 352 std::unique_ptr<net::URLRequest> request = |
357 CreateAndExecuteRequest(GURL("http://foo.com")); | 353 CreateAndExecuteRequest(GURL("http://foo.com")); |
358 | 354 |
359 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 355 EXPECT_EQ(net::OK, delegate().request_status()); |
360 EXPECT_EQ(200, request->GetResponseCode()); | 356 EXPECT_EQ(200, request->GetResponseCode()); |
361 EXPECT_EQ(kBody, delegate().data_received()); | 357 EXPECT_EQ(kBody, delegate().data_received()); |
362 EXPECT_EQ(origin().host_port_pair().ToString(), | 358 EXPECT_EQ(origin().host_port_pair().ToString(), |
363 request->proxy_server().ToString()); | 359 request->proxy_server().ToString()); |
364 } | 360 } |
365 | 361 |
366 TEST_F(DataReductionProxyInterceptorEndToEndTest, RedirectWithoutRetry) { | 362 TEST_F(DataReductionProxyInterceptorEndToEndTest, RedirectWithoutRetry) { |
367 // The redirect comes through the proxy and should not be retried. | 363 // The redirect comes through the proxy and should not be retried. |
368 MockRead redirect_mock_reads[] = { | 364 MockRead redirect_mock_reads[] = { |
369 MockRead("HTTP/1.1 302 Found\r\n" | 365 MockRead("HTTP/1.1 302 Found\r\n" |
(...skipping 13 matching lines...) Expand all Loading... |
383 MockRead(kBody.c_str()), | 379 MockRead(kBody.c_str()), |
384 MockRead(net::SYNCHRONOUS, net::OK), | 380 MockRead(net::SYNCHRONOUS, net::OK), |
385 }; | 381 }; |
386 net::StaticSocketDataProvider response_socket_data_provider( | 382 net::StaticSocketDataProvider response_socket_data_provider( |
387 response_mock_reads, arraysize(response_mock_reads), nullptr, 0); | 383 response_mock_reads, arraysize(response_mock_reads), nullptr, 0); |
388 mock_socket_factory()->AddSocketDataProvider(&response_socket_data_provider); | 384 mock_socket_factory()->AddSocketDataProvider(&response_socket_data_provider); |
389 | 385 |
390 std::unique_ptr<net::URLRequest> request = | 386 std::unique_ptr<net::URLRequest> request = |
391 CreateAndExecuteRequest(GURL("http://foo.com")); | 387 CreateAndExecuteRequest(GURL("http://foo.com")); |
392 | 388 |
393 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 389 EXPECT_EQ(net::OK, delegate().request_status()); |
394 EXPECT_EQ(200, request->GetResponseCode()); | 390 EXPECT_EQ(200, request->GetResponseCode()); |
395 EXPECT_EQ(kBody, delegate().data_received()); | 391 EXPECT_EQ(kBody, delegate().data_received()); |
396 EXPECT_EQ(origin().host_port_pair().ToString(), | 392 EXPECT_EQ(origin().host_port_pair().ToString(), |
397 request->proxy_server().ToString()); | 393 request->proxy_server().ToString()); |
398 // The redirect should have been processed and followed normally. | 394 // The redirect should have been processed and followed normally. |
399 EXPECT_EQ(1, delegate().received_redirect_count()); | 395 EXPECT_EQ(1, delegate().received_redirect_count()); |
400 } | 396 } |
401 | 397 |
402 TEST_F(DataReductionProxyInterceptorEndToEndTest, ResponseWithBypassAndRetry) { | 398 TEST_F(DataReductionProxyInterceptorEndToEndTest, ResponseWithBypassAndRetry) { |
403 // The first try gives a bypass. | 399 // The first try gives a bypass. |
(...skipping 14 matching lines...) Expand all Loading... |
418 MockRead(kBody.c_str()), | 414 MockRead(kBody.c_str()), |
419 MockRead(net::SYNCHRONOUS, net::OK), | 415 MockRead(net::SYNCHRONOUS, net::OK), |
420 }; | 416 }; |
421 net::StaticSocketDataProvider retry_socket_data_provider( | 417 net::StaticSocketDataProvider retry_socket_data_provider( |
422 retry_mock_reads, arraysize(retry_mock_reads), nullptr, 0); | 418 retry_mock_reads, arraysize(retry_mock_reads), nullptr, 0); |
423 mock_socket_factory()->AddSocketDataProvider(&retry_socket_data_provider); | 419 mock_socket_factory()->AddSocketDataProvider(&retry_socket_data_provider); |
424 | 420 |
425 std::unique_ptr<net::URLRequest> request = | 421 std::unique_ptr<net::URLRequest> request = |
426 CreateAndExecuteRequest(GURL("http://foo.com")); | 422 CreateAndExecuteRequest(GURL("http://foo.com")); |
427 | 423 |
428 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 424 EXPECT_EQ(net::OK, delegate().request_status()); |
429 EXPECT_EQ(200, request->GetResponseCode()); | 425 EXPECT_EQ(200, request->GetResponseCode()); |
430 EXPECT_EQ(kBody, delegate().data_received()); | 426 EXPECT_EQ(kBody, delegate().data_received()); |
431 EXPECT_FALSE(request->was_fetched_via_proxy()); | 427 EXPECT_FALSE(request->was_fetched_via_proxy()); |
432 // The bypassed response should have been intercepted before the response was | 428 // The bypassed response should have been intercepted before the response was |
433 // processed, so only the final response after the retry should have been | 429 // processed, so only the final response after the retry should have been |
434 // processed. | 430 // processed. |
435 EXPECT_EQ(1, delegate().response_started_count()); | 431 EXPECT_EQ(1, delegate().response_started_count()); |
436 } | 432 } |
437 | 433 |
438 TEST_F(DataReductionProxyInterceptorEndToEndTest, RedirectWithBypassAndRetry) { | 434 TEST_F(DataReductionProxyInterceptorEndToEndTest, RedirectWithBypassAndRetry) { |
(...skipping 26 matching lines...) Expand all Loading... |
465 socket_data_providers.push_back( | 461 socket_data_providers.push_back( |
466 base::MakeUnique<net::StaticSocketDataProvider>(mock_reads, 3, nullptr, | 462 base::MakeUnique<net::StaticSocketDataProvider>(mock_reads, 3, nullptr, |
467 0)); | 463 0)); |
468 mock_socket_factory()->AddSocketDataProvider( | 464 mock_socket_factory()->AddSocketDataProvider( |
469 socket_data_providers.back().get()); | 465 socket_data_providers.back().get()); |
470 } | 466 } |
471 | 467 |
472 std::unique_ptr<net::URLRequest> request = | 468 std::unique_ptr<net::URLRequest> request = |
473 CreateAndExecuteRequest(GURL("http://foo.com")); | 469 CreateAndExecuteRequest(GURL("http://foo.com")); |
474 | 470 |
475 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 471 EXPECT_EQ(net::OK, delegate().request_status()); |
476 EXPECT_EQ(200, request->GetResponseCode()); | 472 EXPECT_EQ(200, request->GetResponseCode()); |
477 EXPECT_EQ(kBody, delegate().data_received()); | 473 EXPECT_EQ(kBody, delegate().data_received()); |
478 EXPECT_FALSE(request->was_fetched_via_proxy()); | 474 EXPECT_FALSE(request->was_fetched_via_proxy()); |
479 | 475 |
480 // Each of the redirects should have been intercepted before being followed. | 476 // Each of the redirects should have been intercepted before being followed. |
481 EXPECT_EQ(0, delegate().received_redirect_count()); | 477 EXPECT_EQ(0, delegate().received_redirect_count()); |
482 EXPECT_EQ(std::vector<GURL>(1, GURL("http://foo.com")), request->url_chain()); | 478 EXPECT_EQ(std::vector<GURL>(1, GURL("http://foo.com")), request->url_chain()); |
483 } | 479 } |
484 | 480 |
485 TEST_F(DataReductionProxyInterceptorEndToEndTest, RedirectChainToHttps) { | 481 TEST_F(DataReductionProxyInterceptorEndToEndTest, RedirectChainToHttps) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 std::vector<GURL> expected_url_chain; | 516 std::vector<GURL> expected_url_chain; |
521 expected_url_chain.push_back(GURL("http://music.google.com")); | 517 expected_url_chain.push_back(GURL("http://music.google.com")); |
522 expected_url_chain.push_back(GURL("http://play.google.com")); | 518 expected_url_chain.push_back(GURL("http://play.google.com")); |
523 expected_url_chain.push_back(GURL("https://play.google.com")); | 519 expected_url_chain.push_back(GURL("https://play.google.com")); |
524 EXPECT_EQ(expected_url_chain, request->url_chain()); | 520 EXPECT_EQ(expected_url_chain, request->url_chain()); |
525 } | 521 } |
526 | 522 |
527 } // namespace | 523 } // namespace |
528 | 524 |
529 } // namespace data_reduction_proxy | 525 } // namespace data_reduction_proxy |
OLD | NEW |