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 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 | 45 |
| 46 using URLRequestHttpJob::SetPriority; | 46 using URLRequestHttpJob::SetPriority; |
| 47 using URLRequestHttpJob::Start; | 47 using URLRequestHttpJob::Start; |
| 48 using URLRequestHttpJob::Kill; | 48 using URLRequestHttpJob::Kill; |
| 49 using URLRequestHttpJob::priority; | 49 using URLRequestHttpJob::priority; |
| 50 | 50 |
| 51 protected: | 51 protected: |
| 52 ~TestURLRequestHttpJob() override {} | 52 ~TestURLRequestHttpJob() override {} |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 bool AcceptsEncoding(const HttpRequestHeaders& headers, | |
| 56 const std::string encoding) { | |
| 57 std::string encoding_headers; | |
| 58 bool get_success = headers.GetHeader("Accept-Encoding", &encoding_headers); | |
| 59 EXPECT_TRUE(get_success); | |
| 60 if (!get_success) | |
| 61 return false; | |
| 62 | |
| 63 // This check isn't wrapped with EXPECT* macros because different | |
| 64 // results from this function may be expected in different tests. | |
| 65 for (const std::string& token : | |
| 66 base::SplitString(encoding_headers, ", ", base::KEEP_WHITESPACE, | |
| 67 base::SPLIT_WANT_NONEMPTY)) { | |
| 68 if (base::EqualsCaseInsensitiveASCII(token, encoding)) | |
| 69 return true; | |
| 70 } | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 55 class URLRequestHttpJobTest : public ::testing::Test { | 74 class URLRequestHttpJobTest : public ::testing::Test { |
| 56 protected: | 75 protected: |
| 57 URLRequestHttpJobTest() | 76 URLRequestHttpJobTest() |
| 58 : req_(context_.CreateRequest(GURL("http://www.example.com"), | 77 : req_(context_.CreateRequest(GURL("http://www.example.com"), |
| 59 DEFAULT_PRIORITY, | 78 DEFAULT_PRIORITY, |
| 60 &delegate_)) { | 79 &delegate_)) { |
| 61 context_.set_http_transaction_factory(&network_layer_); | 80 context_.set_http_transaction_factory(&network_layer_); |
| 62 } | 81 } |
| 63 | 82 |
| 64 bool TransactionAcceptsSdchEncoding() { | 83 bool TransactionAcceptsSdchEncoding() { |
| 65 base::WeakPtr<MockNetworkTransaction> transaction( | 84 base::WeakPtr<MockNetworkTransaction> transaction( |
| 66 network_layer_.last_transaction()); | 85 network_layer_.last_transaction()); |
| 67 EXPECT_TRUE(transaction); | 86 EXPECT_TRUE(transaction); |
| 68 if (!transaction) return false; | 87 if (!transaction) return false; |
| 69 | 88 |
| 70 const HttpRequestInfo* request_info = transaction->request(); | 89 const HttpRequestInfo* request_info = transaction->request(); |
| 71 EXPECT_TRUE(request_info); | 90 EXPECT_TRUE(request_info); |
| 72 if (!request_info) return false; | 91 if (!request_info) return false; |
| 73 | 92 |
| 74 std::string encoding_headers; | 93 return AcceptsEncoding(request_info->extra_headers, "sdch"); |
| 75 bool get_success = request_info->extra_headers.GetHeader( | |
| 76 "Accept-Encoding", &encoding_headers); | |
| 77 EXPECT_TRUE(get_success); | |
| 78 if (!get_success) return false; | |
| 79 | |
| 80 // This check isn't wrapped with EXPECT* macros because different | |
| 81 // results from this function may be expected in different tests. | |
| 82 for (const std::string& token : | |
| 83 base::SplitString(encoding_headers, ", ", base::KEEP_WHITESPACE, | |
| 84 base::SPLIT_WANT_NONEMPTY)) { | |
| 85 if (base::EqualsCaseInsensitiveASCII(token, "sdch")) | |
| 86 return true; | |
| 87 } | |
| 88 return false; | |
| 89 } | 94 } |
| 90 | 95 |
| 91 void EnableSdch() { | 96 void EnableSdch() { |
| 92 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); | 97 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); |
| 93 } | 98 } |
| 94 | 99 |
| 95 MockNetworkLayer network_layer_; | 100 MockNetworkLayer network_layer_; |
| 96 TestURLRequestContext context_; | 101 TestURLRequestContext context_; |
| 97 TestDelegate delegate_; | 102 TestDelegate delegate_; |
| 98 scoped_ptr<URLRequest> req_; | 103 scoped_ptr<URLRequest> req_; |
| (...skipping 523 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. | 627 // Confirm we don't advertise SDCH encoding in the case of a POST. |
| 623 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { | 628 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { |
| 624 EnableSdch(); | 629 EnableSdch(); |
| 625 req_->set_method("POST"); | 630 req_->set_method("POST"); |
| 626 scoped_refptr<TestURLRequestHttpJob> job( | 631 scoped_refptr<TestURLRequestHttpJob> job( |
| 627 new TestURLRequestHttpJob(req_.get())); | 632 new TestURLRequestHttpJob(req_.get())); |
| 628 job->Start(); | 633 job->Start(); |
| 629 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); | 634 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); |
| 630 } | 635 } |
| 631 | 636 |
| 637 class URLRequestHttpJobWithBrotliSupportTest : public ::testing::Test { | |
| 638 protected: | |
| 639 URLRequestHttpJobWithBrotliSupportTest() | |
| 640 : context_(new TestURLRequestContext(true)) { | |
| 641 scoped_ptr<HttpNetworkSession::Params> params( | |
| 642 new HttpNetworkSession::Params); | |
| 643 params->enable_brotli = true; | |
| 644 context_->set_http_network_session_params(params.Pass()); | |
| 645 context_->set_network_delegate(&network_delegate_); | |
| 646 context_->Init(); | |
| 647 } | |
| 648 | |
| 649 bool AcceptsBrotliEncoding() { | |
| 650 return AcceptsEncoding(network_delegate_.last_headers(), "br"); | |
|
xunjieli
2015/12/04 15:21:19
Let's not add "last_headers" to TestNetworkDelegat
eustas
2015/12/04 16:14:31
Great idea! Thank you. Done.
| |
| 651 } | |
| 652 | |
| 653 void RunRequest(GURL url) { | |
|
xunjieli
2015/12/04 15:21:19
nit: const GURL&
eustas
2015/12/04 16:14:31
Done.
| |
| 654 scoped_ptr<URLRequest> req( | |
| 655 context_->CreateRequest(url, DEFAULT_PRIORITY, &delegate_)); | |
| 656 scoped_refptr<TestURLRequestHttpJob> job( | |
| 657 new TestURLRequestHttpJob(req.get())); | |
| 658 TestCompletionCallback dummy; | |
| 659 network_delegate_.NotifyBeforeURLRequest(req.get(), dummy.callback(), &url); | |
| 660 job->Start(); | |
| 661 base::RunLoop().RunUntilIdle(); | |
| 662 } | |
| 663 | |
| 664 scoped_ptr<TestURLRequestContext> context_; | |
|
xunjieli
2015/12/04 15:21:19
nit: add "private" before these variables.
eustas
2015/12/04 16:14:32
Done.
| |
| 665 TestNetworkDelegate network_delegate_; | |
| 666 TestDelegate delegate_; | |
| 667 }; | |
| 668 | |
| 669 TEST_F(URLRequestHttpJobWithBrotliSupportTest, NoBrotliAdvertisementOverHttp) { | |
| 670 RunRequest(GURL("http://www.example.com")); | |
| 671 EXPECT_FALSE(AcceptsBrotliEncoding()); | |
| 672 } | |
| 673 | |
| 674 TEST_F(URLRequestHttpJobWithBrotliSupportTest, BrotliAdvertisement) { | |
| 675 RunRequest(GURL("https://www.example.com")); | |
| 676 EXPECT_TRUE(AcceptsBrotliEncoding()); | |
| 677 } | |
| 678 | |
| 632 // This base class just serves to set up some things before the TestURLRequest | 679 // This base class just serves to set up some things before the TestURLRequest |
| 633 // constructor is called. | 680 // constructor is called. |
| 634 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { | 681 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { |
| 635 protected: | 682 protected: |
| 636 URLRequestHttpJobWebSocketTestBase() : socket_data_(nullptr, 0, nullptr, 0), | 683 URLRequestHttpJobWebSocketTestBase() : socket_data_(nullptr, 0, nullptr, 0), |
| 637 context_(true) { | 684 context_(true) { |
| 638 // A Network Delegate is required for the WebSocketHandshakeStreamBase | 685 // A Network Delegate is required for the WebSocketHandshakeStreamBase |
| 639 // object to be passed on to the HttpNetworkTransaction. | 686 // object to be passed on to the HttpNetworkTransaction. |
| 640 context_.set_network_delegate(&network_delegate_); | 687 context_.set_network_delegate(&network_delegate_); |
| 641 | 688 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 789 req_->SetLoadFlags(LOAD_DISABLE_CACHE); | 836 req_->SetLoadFlags(LOAD_DISABLE_CACHE); |
| 790 job->Start(); | 837 job->Start(); |
| 791 base::RunLoop().RunUntilIdle(); | 838 base::RunLoop().RunUntilIdle(); |
| 792 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 839 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
| 793 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 840 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 794 } | 841 } |
| 795 | 842 |
| 796 } // namespace | 843 } // namespace |
| 797 | 844 |
| 798 } // namespace net | 845 } // namespace net |
| OLD | NEW |