Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/url_request/url_request_http_job.h" | 5 #include "net/url_request/url_request_http_job.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 17 #include "net/base/auth.h" | 17 #include "net/base/auth.h" |
| 18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
| 19 #include "net/base/sdch_observer.h" | 19 #include "net/base/sdch_observer.h" |
| 20 #include "net/base/test_data_directory.h" | 20 #include "net/base/test_data_directory.h" |
| 21 #include "net/cookies/cookie_store_test_helpers.h" | 21 #include "net/cookies/cookie_store_test_helpers.h" |
| 22 #include "net/http/http_transaction_factory.h" | 22 #include "net/http/http_transaction_factory.h" |
| 23 #include "net/http/http_transaction_test_util.h" | 23 #include "net/http/http_transaction_test_util.h" |
| 24 #include "net/log/test_net_log.h" | |
| 25 #include "net/log/test_net_log_entry.h" | |
| 26 #include "net/log/test_net_log_util.h" | |
| 24 #include "net/socket/socket_test_util.h" | 27 #include "net/socket/socket_test_util.h" |
| 25 #include "net/test/cert_test_util.h" | 28 #include "net/test/cert_test_util.h" |
| 26 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
| 27 #include "net/url_request/url_request_job_factory_impl.h" | 30 #include "net/url_request/url_request_job_factory_impl.h" |
| 28 #include "net/url_request/url_request_status.h" | 31 #include "net/url_request/url_request_status.h" |
| 29 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
| 30 #include "net/websockets/websocket_handshake_stream_base.h" | 33 #include "net/websockets/websocket_handshake_stream_base.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 57 class URLRequestHttpJobTest : public ::testing::Test { | 60 class URLRequestHttpJobTest : public ::testing::Test { |
| 58 protected: | 61 protected: |
| 59 URLRequestHttpJobTest() : context_(true) { | 62 URLRequestHttpJobTest() : context_(true) { |
| 60 context_.set_http_transaction_factory(&network_layer_); | 63 context_.set_http_transaction_factory(&network_layer_); |
| 61 | 64 |
| 62 // The |test_job_factory_| takes ownership of the interceptor. | 65 // The |test_job_factory_| takes ownership of the interceptor. |
| 63 test_job_interceptor_ = new TestJobInterceptor(); | 66 test_job_interceptor_ = new TestJobInterceptor(); |
| 64 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( | 67 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
| 65 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); | 68 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); |
| 66 context_.set_job_factory(&test_job_factory_); | 69 context_.set_job_factory(&test_job_factory_); |
| 67 | 70 context_.set_net_log(&net_log_); |
| 68 context_.Init(); | 71 context_.Init(); |
| 69 | 72 |
| 70 req_ = context_.CreateRequest(GURL("http://www.example.com"), | 73 req_ = context_.CreateRequest(GURL("http://www.example.com"), |
| 71 DEFAULT_PRIORITY, &delegate_); | 74 DEFAULT_PRIORITY, &delegate_); |
| 72 } | 75 } |
| 73 | 76 |
| 74 bool TransactionAcceptsSdchEncoding() { | 77 bool TransactionAcceptsSdchEncoding() { |
| 75 base::WeakPtr<MockNetworkTransaction> transaction( | 78 base::WeakPtr<MockNetworkTransaction> transaction( |
| 76 network_layer_.last_transaction()); | 79 network_layer_.last_transaction()); |
| 77 EXPECT_TRUE(transaction); | 80 EXPECT_TRUE(transaction); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 103 } | 106 } |
| 104 | 107 |
| 105 MockNetworkLayer network_layer_; | 108 MockNetworkLayer network_layer_; |
| 106 | 109 |
| 107 // |test_job_interceptor_| is owned by |test_job_factory_|. | 110 // |test_job_interceptor_| is owned by |test_job_factory_|. |
| 108 TestJobInterceptor* test_job_interceptor_; | 111 TestJobInterceptor* test_job_interceptor_; |
| 109 URLRequestJobFactoryImpl test_job_factory_; | 112 URLRequestJobFactoryImpl test_job_factory_; |
| 110 | 113 |
| 111 TestURLRequestContext context_; | 114 TestURLRequestContext context_; |
| 112 TestDelegate delegate_; | 115 TestDelegate delegate_; |
| 116 TestNetLog net_log_; | |
| 113 std::unique_ptr<URLRequest> req_; | 117 std::unique_ptr<URLRequest> req_; |
| 114 }; | 118 }; |
| 115 | 119 |
| 116 class URLRequestHttpJobWithMockSocketsTest : public ::testing::Test { | 120 class URLRequestHttpJobWithMockSocketsTest : public ::testing::Test { |
| 117 protected: | 121 protected: |
| 118 URLRequestHttpJobWithMockSocketsTest() | 122 URLRequestHttpJobWithMockSocketsTest() |
| 119 : context_(new TestURLRequestContext(true)) { | 123 : context_(new TestURLRequestContext(true)) { |
| 120 context_->set_client_socket_factory(&socket_factory_); | 124 context_->set_client_socket_factory(&socket_factory_); |
| 121 context_->set_network_delegate(&network_delegate_); | 125 context_->set_network_delegate(&network_delegate_); |
| 122 context_->set_backoff_manager(&manager_); | 126 context_->set_backoff_manager(&manager_); |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 618 // Confirm we don't advertise SDCH encoding in the case of a POST. | 622 // Confirm we don't advertise SDCH encoding in the case of a POST. |
| 619 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { | 623 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { |
| 620 EnableSdch(); | 624 EnableSdch(); |
| 621 req_->set_method("POST"); | 625 req_->set_method("POST"); |
| 622 test_job_interceptor_->set_main_intercept_job( | 626 test_job_interceptor_->set_main_intercept_job( |
| 623 base::WrapUnique(new TestURLRequestHttpJob(req_.get()))); | 627 base::WrapUnique(new TestURLRequestHttpJob(req_.get()))); |
| 624 req_->Start(); | 628 req_->Start(); |
| 625 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); | 629 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); |
| 626 } | 630 } |
| 627 | 631 |
| 632 TEST_F(URLRequestHttpJobTest, HSTSInternalRedirectTest) { | |
| 633 // Setup HSTS state. | |
| 634 context_.transport_security_state()->AddHSTS( | |
| 635 "upgrade.test", base::Time::Now() + base::TimeDelta::FromSeconds(10), | |
| 636 true); | |
| 637 ASSERT_TRUE( | |
| 638 context_.transport_security_state()->ShouldUpgradeToSSL("upgrade.test")); | |
| 639 ASSERT_FALSE(context_.transport_security_state()->ShouldUpgradeToSSL( | |
| 640 "no-upgrade.test")); | |
| 641 | |
| 642 struct TestCase { | |
| 643 const char* url; | |
| 644 bool upgrade_expected; | |
| 645 const char* url_expected; | |
| 646 } cases[] = { | |
| 647 {"http://upgrade.test/", true, "https://upgrade.test/"}, | |
| 648 {"http://upgrade.test:123/", true, "https://upgrade.test:123/"}, | |
| 649 {"http://no-upgrade.test/", false, "http://no-upgrade.test/"}, | |
| 650 {"http://no-upgrade.test:123/", false, "http://no-upgrade.test:123/"}, | |
| 651 // iOS doesn't support websockets; see the comments above | |
| 652 // URLRequestHttpJobWebSocketTest for detail. | |
| 653 #if !defined(OS_IOS) | |
|
Mike West
2016/06/20 13:36:41
mmenke@: WebSockets aren't enabled on iOS. I'm dis
| |
| 654 {"ws://upgrade.test/", true, "wss://upgrade.test/"}, | |
| 655 {"ws://upgrade.test:123/", true, "wss://upgrade.test:123/"}, | |
| 656 {"ws://no-upgrade.test/", false, "ws://no-upgrade.test/"}, | |
| 657 {"ws://no-upgrade.test:123/", false, "ws://no-upgrade.test:123/"}, | |
| 658 #endif // !defined(OS_IOS) | |
| 659 }; | |
| 660 | |
| 661 for (const auto& test : cases) { | |
| 662 SCOPED_TRACE(test.url); | |
| 663 TestDelegate d; | |
| 664 TestNetworkDelegate network_delegate; | |
| 665 std::unique_ptr<URLRequest> r( | |
| 666 context_.CreateRequest(GURL(test.url), DEFAULT_PRIORITY, &d)); | |
| 667 | |
| 668 net_log_.Clear(); | |
| 669 r->Start(); | |
| 670 base::RunLoop().Run(); | |
| 671 | |
| 672 if (test.upgrade_expected) { | |
| 673 net::TestNetLogEntry::List entries; | |
| 674 net_log_.GetEntries(&entries); | |
| 675 int redirects = 0; | |
| 676 for (const auto& entry : entries) { | |
| 677 if (entry.type == net::NetLog::TYPE_URL_REQUEST_REDIRECT_JOB) { | |
| 678 redirects++; | |
| 679 std::string value; | |
| 680 EXPECT_TRUE(entry.GetStringValue("reason", &value)); | |
| 681 EXPECT_EQ("HSTS", value); | |
| 682 } | |
| 683 } | |
| 684 EXPECT_EQ(1, redirects); | |
| 685 EXPECT_EQ(1, d.received_redirect_count()); | |
| 686 EXPECT_EQ(2u, r->url_chain().size()); | |
| 687 } else { | |
| 688 EXPECT_EQ(0, d.received_redirect_count()); | |
| 689 EXPECT_EQ(1u, r->url_chain().size()); | |
| 690 } | |
| 691 EXPECT_EQ(GURL(test.url_expected), r->url()); | |
| 692 } | |
| 693 } | |
| 694 | |
| 628 class MockSdchObserver : public SdchObserver { | 695 class MockSdchObserver : public SdchObserver { |
| 629 public: | 696 public: |
| 630 MockSdchObserver() {} | 697 MockSdchObserver() {} |
| 631 MOCK_METHOD2(OnDictionaryAdded, | 698 MOCK_METHOD2(OnDictionaryAdded, |
| 632 void(const GURL& request_url, const std::string& server_hash)); | 699 void(const GURL& request_url, const std::string& server_hash)); |
| 633 MOCK_METHOD1(OnDictionaryRemoved, void(const std::string& server_hash)); | 700 MOCK_METHOD1(OnDictionaryRemoved, void(const std::string& server_hash)); |
| 634 MOCK_METHOD1(OnDictionaryUsed, void(const std::string& server_hash)); | 701 MOCK_METHOD1(OnDictionaryUsed, void(const std::string& server_hash)); |
| 635 MOCK_METHOD2(OnGetDictionary, | 702 MOCK_METHOD2(OnGetDictionary, |
| 636 void(const GURL& request_url, const GURL& dictionary_url)); | 703 void(const GURL& request_url, const GURL& dictionary_url)); |
| 637 MOCK_METHOD0(OnClearDictionaries, void()); | 704 MOCK_METHOD0(OnClearDictionaries, void()); |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 940 base::RunLoop().RunUntilIdle(); | 1007 base::RunLoop().RunUntilIdle(); |
| 941 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 1008 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
| 942 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 1009 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 943 } | 1010 } |
| 944 | 1011 |
| 945 #endif // !defined(OS_IOS) | 1012 #endif // !defined(OS_IOS) |
| 946 | 1013 |
| 947 } // namespace | 1014 } // namespace |
| 948 | 1015 |
| 949 } // namespace net | 1016 } // namespace net |
| OLD | NEW |