OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/url_request/url_request_http_job.h" |
| 6 |
| 7 #include <cstddef> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "net/base/auth.h" |
| 12 #include "net/http/http_transaction_factory.h" |
| 13 #include "net/http/http_transaction_unittest.h" |
| 14 #include "net/url_request/url_request_status.h" |
| 15 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace net { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Inherit from URLRequestHttpJob to expose the priority and some |
| 23 // other hidden functions. |
| 24 class TestURLRequestHttpJob : public URLRequestHttpJob { |
| 25 public: |
| 26 explicit TestURLRequestHttpJob(URLRequest* request) |
| 27 : URLRequestHttpJob(request, NULL, |
| 28 request->context()->http_user_agent_settings()) {} |
| 29 |
| 30 RequestPriority GetPriority() const { |
| 31 return request_info_.priority; |
| 32 } |
| 33 |
| 34 using URLRequestJob::GetStatus; |
| 35 using URLRequestHttpJob::DestroyTransaction; |
| 36 using URLRequestHttpJob::RestartTransactionWithAuth; |
| 37 using URLRequestHttpJob::OnStartCompleted; |
| 38 using URLRequestHttpJob::Start; |
| 39 using URLRequestHttpJob::SetPriority; |
| 40 |
| 41 protected: |
| 42 virtual ~TestURLRequestHttpJob() {} |
| 43 }; |
| 44 |
| 45 class URLRequestHttpJobTest : public ::testing::Test { |
| 46 protected: |
| 47 URLRequestHttpJobTest() { |
| 48 context_.set_http_transaction_factory(&network_layer_); |
| 49 } |
| 50 |
| 51 MockNetworkLayer network_layer_; |
| 52 TestURLRequestContext context_; |
| 53 }; |
| 54 |
| 55 // Make sure that URLRequestHttpJob passes on its priority updates to |
| 56 // its transaction. |
| 57 TEST_F(URLRequestHttpJobTest, Priority) { |
| 58 net::TestDelegate d; |
| 59 net::TestURLRequest req(GURL("http://www.example.com"), &d, &context_, NULL); |
| 60 |
| 61 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req)); |
| 62 EXPECT_FALSE(network_layer_.last_transaction()); |
| 63 EXPECT_EQ(LOWEST, job->GetPriority()); |
| 64 |
| 65 req.SetPriority(MEDIUM); |
| 66 // The priority doesn't propagate to the job until Start() is called. |
| 67 EXPECT_EQ(LOWEST, job->GetPriority()); |
| 68 |
| 69 job->Start(); |
| 70 ASSERT_TRUE(network_layer_.last_transaction()); |
| 71 EXPECT_EQ(MEDIUM, job->GetPriority()); |
| 72 EXPECT_EQ(MEDIUM, network_layer_.last_transaction()->priority()); |
| 73 |
| 74 job->SetPriority(HIGHEST); |
| 75 EXPECT_EQ(HIGHEST, job->GetPriority()); |
| 76 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); |
| 77 } |
| 78 |
| 79 // Make sure that URLRequestHttpJob passes on its priority updates to |
| 80 // newly-created transactions. |
| 81 TEST_F(URLRequestHttpJobTest, PriorityNewTransaction) { |
| 82 net::TestDelegate d; |
| 83 net::TestURLRequest req(GURL("http://www.example.com"), &d, &context_, NULL); |
| 84 |
| 85 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req)); |
| 86 EXPECT_FALSE(network_layer_.last_transaction()); |
| 87 |
| 88 job->Start(); |
| 89 EXPECT_TRUE(job->GetStatus().is_io_pending()); |
| 90 |
| 91 job->SetPriority(HIGHEST); |
| 92 EXPECT_EQ(HIGHEST, job->GetPriority()); |
| 93 EXPECT_TRUE(network_layer_.last_transaction()); |
| 94 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); |
| 95 |
| 96 MessageLoop::current()->RunUntilIdle(); |
| 97 EXPECT_FALSE(job->GetStatus().is_io_pending()); |
| 98 |
| 99 job->DestroyTransaction(); |
| 100 network_layer_.ClearLastTransaction(); |
| 101 |
| 102 // Create a new transaction. |
| 103 job->RestartTransactionWithAuth(AuthCredentials()); |
| 104 EXPECT_TRUE(network_layer_.last_transaction()); |
| 105 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); |
| 106 } |
| 107 |
| 108 } // namespace |
| 109 |
| 110 } // namespace net |
OLD | NEW |