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 {"ws://upgrade.test/", true, "wss://upgrade.test/"}, | |
649 {"http://upgrade.test:123/", true, "https://upgrade.test:123/"}, | |
650 {"ws://upgrade.test:123/", true, "wss://upgrade.test:123/"}, | |
651 {"http://no-upgrade.test/", false, "http://no-upgrade.test/"}, | |
652 {"ws://no-upgrade.test/", false, "ws://no-upgrade.test/"}, | |
653 {"http://no-upgrade.test:123/", false, "http://no-upgrade.test:123/"}, | |
654 {"ws://no-upgrade.test:123/", false, "ws://no-upgrade.test:123/"}, | |
655 }; | |
656 | |
657 for (const auto& test : cases) { | |
658 SCOPED_TRACE(test.url); | |
659 TestDelegate d; | |
660 TestNetworkDelegate network_delegate; | |
661 std::unique_ptr<URLRequest> r( | |
662 context_.CreateRequest(GURL(test.url), DEFAULT_PRIORITY, &d)); | |
663 | |
664 net_log_.Clear(); | |
665 r->Start(); | |
666 base::RunLoop().Run(); | |
667 | |
668 if (test.upgrade_expected) { | |
669 net::TestNetLogEntry::List entries; | |
670 net_log_.GetEntries(&entries); | |
671 int redirects = 0; | |
672 for (const auto& entry : entries) { | |
673 if (entry.type == net::NetLog::TYPE_URL_REQUEST_REDIRECT_JOB) { | |
674 redirects++; | |
675 std::string value; | |
676 EXPECT_TRUE(entry.GetStringValue("reason", &value)); | |
677 EXPECT_EQ("HSTS", value); | |
678 } | |
679 } | |
680 EXPECT_EQ(1, redirects); | |
681 EXPECT_EQ(1, d.received_redirect_count()); | |
682 EXPECT_EQ(2u, r->url_chain().size()); | |
683 } else { | |
684 EXPECT_EQ(0, d.received_redirect_count()); | |
685 EXPECT_EQ(1u, r->url_chain().size()); | |
686 } | |
687 EXPECT_EQ(GURL(test.url_expected), r->url()); | |
688 } | |
689 } | |
690 | |
628 class MockSdchObserver : public SdchObserver { | 691 class MockSdchObserver : public SdchObserver { |
629 public: | 692 public: |
630 MockSdchObserver() {} | 693 MockSdchObserver() {} |
631 MOCK_METHOD2(OnDictionaryAdded, | 694 MOCK_METHOD2(OnDictionaryAdded, |
632 void(const GURL& request_url, const std::string& server_hash)); | 695 void(const GURL& request_url, const std::string& server_hash)); |
633 MOCK_METHOD1(OnDictionaryRemoved, void(const std::string& server_hash)); | 696 MOCK_METHOD1(OnDictionaryRemoved, void(const std::string& server_hash)); |
634 MOCK_METHOD1(OnDictionaryUsed, void(const std::string& server_hash)); | 697 MOCK_METHOD1(OnDictionaryUsed, void(const std::string& server_hash)); |
635 MOCK_METHOD2(OnGetDictionary, | 698 MOCK_METHOD2(OnGetDictionary, |
636 void(const GURL& request_url, const GURL& dictionary_url)); | 699 void(const GURL& request_url, const GURL& dictionary_url)); |
637 MOCK_METHOD0(OnClearDictionaries, void()); | 700 MOCK_METHOD0(OnClearDictionaries, void()); |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
937 create_helper.release()); | 1000 create_helper.release()); |
938 req_->SetLoadFlags(LOAD_DISABLE_CACHE); | 1001 req_->SetLoadFlags(LOAD_DISABLE_CACHE); |
939 req_->Start(); | 1002 req_->Start(); |
940 base::RunLoop().RunUntilIdle(); | 1003 base::RunLoop().RunUntilIdle(); |
941 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 1004 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
942 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 1005 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
943 } | 1006 } |
944 | 1007 |
945 #endif // !defined(OS_IOS) | 1008 #endif // !defined(OS_IOS) |
946 | 1009 |
1010 class URLRequestHttpJobInternalRedirectTest : public ::testing::Test { | |
mmenke
2016/06/17 16:40:29
Remove this.
Mike West
2016/06/17 19:44:34
Ugh. Yes, thank you. :)
| |
1011 protected: | |
1012 URLRequestHttpJobInternalRedirectTest() | |
1013 : context_(new TestURLRequestContext(true)) { | |
1014 std::unique_ptr<HttpNetworkSession::Params> params( | |
1015 new HttpNetworkSession::Params); | |
1016 context_->set_enable_brotli(true); | |
1017 context_->set_http_network_session_params(std::move(params)); | |
1018 context_->set_client_socket_factory(&socket_factory_); | |
1019 context_->Init(); | |
1020 } | |
1021 | |
1022 MockClientSocketFactory socket_factory_; | |
1023 std::unique_ptr<TestURLRequestContext> context_; | |
1024 }; | |
1025 | |
947 } // namespace | 1026 } // namespace |
948 | 1027 |
949 } // namespace net | 1028 } // namespace net |
OLD | NEW |