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 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
16 #include "net/base/auth.h" | 16 #include "net/base/auth.h" |
17 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
18 #include "net/base/test_data_directory.h" | 18 #include "net/base/test_data_directory.h" |
19 #include "net/cookies/cookie_store_test_helpers.h" | 19 #include "net/cookies/cookie_store_test_helpers.h" |
20 #include "net/http/http_server_properties_impl.h" | |
20 #include "net/http/http_transaction_factory.h" | 21 #include "net/http/http_transaction_factory.h" |
21 #include "net/http/http_transaction_test_util.h" | 22 #include "net/http/http_transaction_test_util.h" |
22 #include "net/socket/socket_test_util.h" | 23 #include "net/socket/socket_test_util.h" |
23 #include "net/test/cert_test_util.h" | 24 #include "net/test/cert_test_util.h" |
24 #include "net/url_request/url_request.h" | 25 #include "net/url_request/url_request.h" |
25 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
26 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
27 #include "net/websockets/websocket_handshake_stream_base.h" | 28 #include "net/websockets/websocket_handshake_stream_base.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 24 matching lines...) Expand all Loading... | |
54 | 55 |
55 class URLRequestHttpJobTest : public ::testing::Test { | 56 class URLRequestHttpJobTest : public ::testing::Test { |
56 protected: | 57 protected: |
57 URLRequestHttpJobTest() | 58 URLRequestHttpJobTest() |
58 : req_(context_.CreateRequest(GURL("http://www.example.com"), | 59 : req_(context_.CreateRequest(GURL("http://www.example.com"), |
59 DEFAULT_PRIORITY, | 60 DEFAULT_PRIORITY, |
60 &delegate_)) { | 61 &delegate_)) { |
61 context_.set_http_transaction_factory(&network_layer_); | 62 context_.set_http_transaction_factory(&network_layer_); |
62 } | 63 } |
63 | 64 |
64 bool TransactionAcceptsSdchEncoding() { | 65 bool TransactionAcceptsSpecificEncoding(const std::string encoding) { |
65 base::WeakPtr<MockNetworkTransaction> transaction( | 66 base::WeakPtr<MockNetworkTransaction> transaction( |
66 network_layer_.last_transaction()); | 67 network_layer_.last_transaction()); |
67 EXPECT_TRUE(transaction); | 68 EXPECT_TRUE(transaction); |
68 if (!transaction) return false; | 69 if (!transaction) return false; |
69 | 70 |
70 const HttpRequestInfo* request_info = transaction->request(); | 71 const HttpRequestInfo* request_info = transaction->request(); |
71 EXPECT_TRUE(request_info); | 72 EXPECT_TRUE(request_info); |
72 if (!request_info) return false; | 73 if (!request_info) return false; |
73 | 74 |
74 std::string encoding_headers; | 75 std::string encoding_headers; |
75 bool get_success = request_info->extra_headers.GetHeader( | 76 bool get_success = request_info->extra_headers.GetHeader( |
76 "Accept-Encoding", &encoding_headers); | 77 "Accept-Encoding", &encoding_headers); |
77 EXPECT_TRUE(get_success); | 78 EXPECT_TRUE(get_success); |
78 if (!get_success) return false; | 79 if (!get_success) return false; |
79 | 80 |
80 // This check isn't wrapped with EXPECT* macros because different | 81 // This check isn't wrapped with EXPECT* macros because different |
81 // results from this function may be expected in different tests. | 82 // results from this function may be expected in different tests. |
82 for (const std::string& token : | 83 for (const std::string& token : |
83 base::SplitString(encoding_headers, ", ", base::KEEP_WHITESPACE, | 84 base::SplitString(encoding_headers, ", ", base::KEEP_WHITESPACE, |
84 base::SPLIT_WANT_NONEMPTY)) { | 85 base::SPLIT_WANT_NONEMPTY)) { |
85 if (base::EqualsCaseInsensitiveASCII(token, "sdch")) | 86 if (base::EqualsCaseInsensitiveASCII(token, encoding)) |
86 return true; | 87 return true; |
87 } | 88 } |
88 return false; | 89 return false; |
89 } | 90 } |
90 | 91 |
92 bool TransactionAcceptsSdchEncoding() { | |
93 return TransactionAcceptsSpecificEncoding("sdch"); | |
94 } | |
95 | |
96 bool TransactionAcceptsBrotliEncoding() { | |
97 return TransactionAcceptsSpecificEncoding("br"); | |
98 } | |
99 | |
91 void EnableSdch() { | 100 void EnableSdch() { |
92 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); | 101 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); |
93 } | 102 } |
94 | 103 |
104 void EnableBrotli() { | |
105 HttpNetworkSession::Params params; | |
106 params.enable_brotli = true; | |
107 params.http_server_properties = http_server_properties_.GetWeakPtr(); | |
108 network_session_.reset(new HttpNetworkSession(params)); | |
109 network_layer_.set_network_session(network_session_.get()); | |
xunjieli
2015/12/02 18:26:31
These two tests should really follow other URLRequ
eustas
2015/12/03 12:32:07
Nice idea. Done!
| |
110 } | |
111 | |
112 HttpServerPropertiesImpl http_server_properties_; | |
113 scoped_ptr<HttpNetworkSession> network_session_; | |
95 MockNetworkLayer network_layer_; | 114 MockNetworkLayer network_layer_; |
96 TestURLRequestContext context_; | 115 TestURLRequestContext context_; |
97 TestDelegate delegate_; | 116 TestDelegate delegate_; |
98 scoped_ptr<URLRequest> req_; | 117 scoped_ptr<URLRequest> req_; |
99 }; | 118 }; |
100 | 119 |
101 class URLRequestHttpJobWithMockSocketsTest : public ::testing::Test { | 120 class URLRequestHttpJobWithMockSocketsTest : public ::testing::Test { |
102 protected: | 121 protected: |
103 URLRequestHttpJobWithMockSocketsTest() | 122 URLRequestHttpJobWithMockSocketsTest() |
104 : context_(new TestURLRequestContext(true)) { | 123 : context_(new TestURLRequestContext(true)) { |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
622 // Confirm we don't advertise SDCH encoding in the case of a POST. | 641 // Confirm we don't advertise SDCH encoding in the case of a POST. |
623 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { | 642 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { |
624 EnableSdch(); | 643 EnableSdch(); |
625 req_->set_method("POST"); | 644 req_->set_method("POST"); |
626 scoped_refptr<TestURLRequestHttpJob> job( | 645 scoped_refptr<TestURLRequestHttpJob> job( |
627 new TestURLRequestHttpJob(req_.get())); | 646 new TestURLRequestHttpJob(req_.get())); |
628 job->Start(); | 647 job->Start(); |
629 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); | 648 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); |
630 } | 649 } |
631 | 650 |
651 TEST_F(URLRequestHttpJobTest, NoBrotliAdvertisementOverHttp) { | |
652 EnableBrotli(); | |
653 req_->set_method("GET"); // Redundant with default. | |
654 scoped_refptr<TestURLRequestHttpJob> job( | |
655 new TestURLRequestHttpJob(req_.get())); | |
656 job->Start(); | |
657 EXPECT_FALSE(TransactionAcceptsBrotliEncoding()); | |
658 } | |
659 | |
660 TEST_F(URLRequestHttpJobTest, BrotliAdvertisement) { | |
661 EnableBrotli(); | |
662 req_ = context_.CreateRequest(GURL("https://www.example.com"), | |
663 DEFAULT_PRIORITY, &delegate_); | |
664 req_->set_method("GET"); // Redundant with default. | |
665 scoped_refptr<TestURLRequestHttpJob> job( | |
666 new TestURLRequestHttpJob(req_.get())); | |
667 job->Start(); | |
668 EXPECT_TRUE(TransactionAcceptsBrotliEncoding()); | |
669 } | |
670 | |
632 // This base class just serves to set up some things before the TestURLRequest | 671 // This base class just serves to set up some things before the TestURLRequest |
633 // constructor is called. | 672 // constructor is called. |
634 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { | 673 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { |
635 protected: | 674 protected: |
636 URLRequestHttpJobWebSocketTestBase() : socket_data_(nullptr, 0, nullptr, 0), | 675 URLRequestHttpJobWebSocketTestBase() : socket_data_(nullptr, 0, nullptr, 0), |
637 context_(true) { | 676 context_(true) { |
638 // A Network Delegate is required for the WebSocketHandshakeStreamBase | 677 // A Network Delegate is required for the WebSocketHandshakeStreamBase |
639 // object to be passed on to the HttpNetworkTransaction. | 678 // object to be passed on to the HttpNetworkTransaction. |
640 context_.set_network_delegate(&network_delegate_); | 679 context_.set_network_delegate(&network_delegate_); |
641 | 680 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
789 req_->SetLoadFlags(LOAD_DISABLE_CACHE); | 828 req_->SetLoadFlags(LOAD_DISABLE_CACHE); |
790 job->Start(); | 829 job->Start(); |
791 base::RunLoop().RunUntilIdle(); | 830 base::RunLoop().RunUntilIdle(); |
792 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 831 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
793 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 832 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
794 } | 833 } |
795 | 834 |
796 } // namespace | 835 } // namespace |
797 | 836 |
798 } // namespace net | 837 } // namespace net |
OLD | NEW |