| 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 <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 12 #include "net/base/auth.h" | 13 #include "net/base/auth.h" |
| 13 #include "net/base/request_priority.h" | 14 #include "net/base/request_priority.h" |
| 14 #include "net/http/http_transaction_factory.h" | 15 #include "net/http/http_transaction_factory.h" |
| 15 #include "net/http/http_transaction_test_util.h" | 16 #include "net/http/http_transaction_test_util.h" |
| 16 #include "net/socket/socket_test_util.h" | 17 #include "net/socket/socket_test_util.h" |
| 18 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 19 #include "net/websockets/websocket_handshake_stream_base.h" | 21 #include "net/websockets/websocket_handshake_stream_base.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 | 25 |
| 24 namespace net { | 26 namespace net { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 40 using URLRequestHttpJob::Kill; | 42 using URLRequestHttpJob::Kill; |
| 41 using URLRequestHttpJob::priority; | 43 using URLRequestHttpJob::priority; |
| 42 | 44 |
| 43 protected: | 45 protected: |
| 44 virtual ~TestURLRequestHttpJob() {} | 46 virtual ~TestURLRequestHttpJob() {} |
| 45 }; | 47 }; |
| 46 | 48 |
| 47 class URLRequestHttpJobTest : public ::testing::Test { | 49 class URLRequestHttpJobTest : public ::testing::Test { |
| 48 protected: | 50 protected: |
| 49 URLRequestHttpJobTest() | 51 URLRequestHttpJobTest() |
| 50 : req_(GURL("http://www.example.com"), | 52 : req_(context_.CreateRequest(GURL("http://www.example.com"), |
| 51 DEFAULT_PRIORITY, | 53 DEFAULT_PRIORITY, |
| 52 &delegate_, | 54 &delegate_, |
| 53 &context_) { | 55 NULL)) { |
| 54 context_.set_http_transaction_factory(&network_layer_); | 56 context_.set_http_transaction_factory(&network_layer_); |
| 55 } | 57 } |
| 56 | 58 |
| 57 bool TransactionAcceptsSdchEncoding() { | 59 bool TransactionAcceptsSdchEncoding() { |
| 58 base::WeakPtr<MockNetworkTransaction> transaction( | 60 base::WeakPtr<MockNetworkTransaction> transaction( |
| 59 network_layer_.last_transaction()); | 61 network_layer_.last_transaction()); |
| 60 EXPECT_TRUE(transaction); | 62 EXPECT_TRUE(transaction); |
| 61 if (!transaction) return false; | 63 if (!transaction) return false; |
| 62 | 64 |
| 63 const HttpRequestInfo* request_info = transaction->request(); | 65 const HttpRequestInfo* request_info = transaction->request(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 81 return false; | 83 return false; |
| 82 } | 84 } |
| 83 | 85 |
| 84 void EnableSdch() { | 86 void EnableSdch() { |
| 85 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); | 87 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); |
| 86 } | 88 } |
| 87 | 89 |
| 88 MockNetworkLayer network_layer_; | 90 MockNetworkLayer network_layer_; |
| 89 TestURLRequestContext context_; | 91 TestURLRequestContext context_; |
| 90 TestDelegate delegate_; | 92 TestDelegate delegate_; |
| 91 TestURLRequest req_; | 93 scoped_ptr<URLRequest> req_; |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 // Make sure that SetPriority actually sets the URLRequestHttpJob's | 96 // Make sure that SetPriority actually sets the URLRequestHttpJob's |
| 95 // priority, both before and after start. | 97 // priority, both before and after start. |
| 96 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) { | 98 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) { |
| 97 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 99 scoped_refptr<TestURLRequestHttpJob> job( |
| 100 new TestURLRequestHttpJob(req_.get())); |
| 98 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); | 101 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 99 | 102 |
| 100 job->SetPriority(LOWEST); | 103 job->SetPriority(LOWEST); |
| 101 EXPECT_EQ(LOWEST, job->priority()); | 104 EXPECT_EQ(LOWEST, job->priority()); |
| 102 | 105 |
| 103 job->SetPriority(LOW); | 106 job->SetPriority(LOW); |
| 104 EXPECT_EQ(LOW, job->priority()); | 107 EXPECT_EQ(LOW, job->priority()); |
| 105 | 108 |
| 106 job->Start(); | 109 job->Start(); |
| 107 EXPECT_EQ(LOW, job->priority()); | 110 EXPECT_EQ(LOW, job->priority()); |
| 108 | 111 |
| 109 job->SetPriority(MEDIUM); | 112 job->SetPriority(MEDIUM); |
| 110 EXPECT_EQ(MEDIUM, job->priority()); | 113 EXPECT_EQ(MEDIUM, job->priority()); |
| 111 } | 114 } |
| 112 | 115 |
| 113 // Make sure that URLRequestHttpJob passes on its priority to its | 116 // Make sure that URLRequestHttpJob passes on its priority to its |
| 114 // transaction on start. | 117 // transaction on start. |
| 115 TEST_F(URLRequestHttpJobTest, SetTransactionPriorityOnStart) { | 118 TEST_F(URLRequestHttpJobTest, SetTransactionPriorityOnStart) { |
| 116 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 119 scoped_refptr<TestURLRequestHttpJob> job( |
| 120 new TestURLRequestHttpJob(req_.get())); |
| 117 job->SetPriority(LOW); | 121 job->SetPriority(LOW); |
| 118 | 122 |
| 119 EXPECT_FALSE(network_layer_.last_transaction()); | 123 EXPECT_FALSE(network_layer_.last_transaction()); |
| 120 | 124 |
| 121 job->Start(); | 125 job->Start(); |
| 122 | 126 |
| 123 ASSERT_TRUE(network_layer_.last_transaction()); | 127 ASSERT_TRUE(network_layer_.last_transaction()); |
| 124 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 128 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 125 } | 129 } |
| 126 | 130 |
| 127 // Make sure that URLRequestHttpJob passes on its priority updates to | 131 // Make sure that URLRequestHttpJob passes on its priority updates to |
| 128 // its transaction. | 132 // its transaction. |
| 129 TEST_F(URLRequestHttpJobTest, SetTransactionPriority) { | 133 TEST_F(URLRequestHttpJobTest, SetTransactionPriority) { |
| 130 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 134 scoped_refptr<TestURLRequestHttpJob> job( |
| 135 new TestURLRequestHttpJob(req_.get())); |
| 131 job->SetPriority(LOW); | 136 job->SetPriority(LOW); |
| 132 job->Start(); | 137 job->Start(); |
| 133 ASSERT_TRUE(network_layer_.last_transaction()); | 138 ASSERT_TRUE(network_layer_.last_transaction()); |
| 134 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 139 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 135 | 140 |
| 136 job->SetPriority(HIGHEST); | 141 job->SetPriority(HIGHEST); |
| 137 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); | 142 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority()); |
| 138 } | 143 } |
| 139 | 144 |
| 140 // Make sure that URLRequestHttpJob passes on its priority updates to | 145 // Make sure that URLRequestHttpJob passes on its priority updates to |
| 141 // newly-created transactions after the first one. | 146 // newly-created transactions after the first one. |
| 142 TEST_F(URLRequestHttpJobTest, SetSubsequentTransactionPriority) { | 147 TEST_F(URLRequestHttpJobTest, SetSubsequentTransactionPriority) { |
| 143 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 148 scoped_refptr<TestURLRequestHttpJob> job( |
| 149 new TestURLRequestHttpJob(req_.get())); |
| 144 job->Start(); | 150 job->Start(); |
| 145 | 151 |
| 146 job->SetPriority(LOW); | 152 job->SetPriority(LOW); |
| 147 ASSERT_TRUE(network_layer_.last_transaction()); | 153 ASSERT_TRUE(network_layer_.last_transaction()); |
| 148 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 154 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 149 | 155 |
| 150 job->Kill(); | 156 job->Kill(); |
| 151 network_layer_.ClearLastTransaction(); | 157 network_layer_.ClearLastTransaction(); |
| 152 | 158 |
| 153 // Creates a second transaction. | 159 // Creates a second transaction. |
| 154 job->Start(); | 160 job->Start(); |
| 155 ASSERT_TRUE(network_layer_.last_transaction()); | 161 ASSERT_TRUE(network_layer_.last_transaction()); |
| 156 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); | 162 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority()); |
| 157 } | 163 } |
| 158 | 164 |
| 159 // Confirm we do advertise SDCH encoding in the case of a GET. | 165 // Confirm we do advertise SDCH encoding in the case of a GET. |
| 160 TEST_F(URLRequestHttpJobTest, SdchAdvertisementGet) { | 166 TEST_F(URLRequestHttpJobTest, SdchAdvertisementGet) { |
| 161 EnableSdch(); | 167 EnableSdch(); |
| 162 req_.set_method("GET"); // Redundant with default. | 168 req_->set_method("GET"); // Redundant with default. |
| 163 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 169 scoped_refptr<TestURLRequestHttpJob> job( |
| 170 new TestURLRequestHttpJob(req_.get())); |
| 164 job->Start(); | 171 job->Start(); |
| 165 EXPECT_TRUE(TransactionAcceptsSdchEncoding()); | 172 EXPECT_TRUE(TransactionAcceptsSdchEncoding()); |
| 166 } | 173 } |
| 167 | 174 |
| 168 // Confirm we don't advertise SDCH encoding in the case of a POST. | 175 // Confirm we don't advertise SDCH encoding in the case of a POST. |
| 169 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { | 176 TEST_F(URLRequestHttpJobTest, SdchAdvertisementPost) { |
| 170 EnableSdch(); | 177 EnableSdch(); |
| 171 req_.set_method("POST"); | 178 req_->set_method("POST"); |
| 172 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 179 scoped_refptr<TestURLRequestHttpJob> job( |
| 180 new TestURLRequestHttpJob(req_.get())); |
| 173 job->Start(); | 181 job->Start(); |
| 174 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); | 182 EXPECT_FALSE(TransactionAcceptsSdchEncoding()); |
| 175 } | 183 } |
| 176 | 184 |
| 177 // This base class just serves to set up some things before the TestURLRequest | 185 // This base class just serves to set up some things before the TestURLRequest |
| 178 // constructor is called. | 186 // constructor is called. |
| 179 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { | 187 class URLRequestHttpJobWebSocketTestBase : public ::testing::Test { |
| 180 protected: | 188 protected: |
| 181 URLRequestHttpJobWebSocketTestBase() : socket_data_(NULL, 0, NULL, 0), | 189 URLRequestHttpJobWebSocketTestBase() : socket_data_(NULL, 0, NULL, 0), |
| 182 context_(true) { | 190 context_(true) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 194 StaticSocketDataProvider socket_data_; | 202 StaticSocketDataProvider socket_data_; |
| 195 TestNetworkDelegate network_delegate_; | 203 TestNetworkDelegate network_delegate_; |
| 196 MockClientSocketFactory socket_factory_; | 204 MockClientSocketFactory socket_factory_; |
| 197 TestURLRequestContext context_; | 205 TestURLRequestContext context_; |
| 198 }; | 206 }; |
| 199 | 207 |
| 200 class URLRequestHttpJobWebSocketTest | 208 class URLRequestHttpJobWebSocketTest |
| 201 : public URLRequestHttpJobWebSocketTestBase { | 209 : public URLRequestHttpJobWebSocketTestBase { |
| 202 protected: | 210 protected: |
| 203 URLRequestHttpJobWebSocketTest() | 211 URLRequestHttpJobWebSocketTest() |
| 204 : req_(GURL("ws://www.example.com"), | 212 : req_(context_.CreateRequest(GURL("ws://www.example.com"), |
| 205 DEFAULT_PRIORITY, | 213 DEFAULT_PRIORITY, |
| 206 &delegate_, | 214 &delegate_, |
| 207 &context_) { | 215 NULL)) { |
| 208 // The TestNetworkDelegate expects a call to NotifyBeforeURLRequest before | 216 // The TestNetworkDelegate expects a call to NotifyBeforeURLRequest before |
| 209 // anything else happens. | 217 // anything else happens. |
| 210 GURL url("ws://localhost/"); | 218 GURL url("ws://localhost/"); |
| 211 TestCompletionCallback dummy; | 219 TestCompletionCallback dummy; |
| 212 network_delegate_.NotifyBeforeURLRequest(&req_, dummy.callback(), &url); | 220 network_delegate_.NotifyBeforeURLRequest( |
| 221 req_.get(), dummy.callback(), &url); |
| 213 } | 222 } |
| 214 | 223 |
| 215 TestDelegate delegate_; | 224 TestDelegate delegate_; |
| 216 TestURLRequest req_; | 225 scoped_ptr<URLRequest> req_; |
| 217 }; | 226 }; |
| 218 | 227 |
| 219 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { | 228 class MockCreateHelper : public WebSocketHandshakeStreamBase::CreateHelper { |
| 220 public: | 229 public: |
| 221 // GoogleMock does not appear to play nicely with move-only types like | 230 // GoogleMock does not appear to play nicely with move-only types like |
| 222 // scoped_ptr, so this forwarding method acts as a workaround. | 231 // scoped_ptr, so this forwarding method acts as a workaround. |
| 223 virtual WebSocketHandshakeStreamBase* CreateBasicStream( | 232 virtual WebSocketHandshakeStreamBase* CreateBasicStream( |
| 224 scoped_ptr<ClientSocketHandle> connection, | 233 scoped_ptr<ClientSocketHandle> connection, |
| 225 bool using_proxy) OVERRIDE { | 234 bool using_proxy) OVERRIDE { |
| 226 // Discard the arguments since we don't need them anyway. | 235 // Discard the arguments since we don't need them anyway. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 // Fake implementation of WebSocketHandshakeStreamBase method(s) | 309 // Fake implementation of WebSocketHandshakeStreamBase method(s) |
| 301 virtual scoped_ptr<WebSocketStream> Upgrade() OVERRIDE { | 310 virtual scoped_ptr<WebSocketStream> Upgrade() OVERRIDE { |
| 302 return scoped_ptr<WebSocketStream>(); | 311 return scoped_ptr<WebSocketStream>(); |
| 303 } | 312 } |
| 304 | 313 |
| 305 private: | 314 private: |
| 306 bool initialize_stream_was_called_; | 315 bool initialize_stream_was_called_; |
| 307 }; | 316 }; |
| 308 | 317 |
| 309 TEST_F(URLRequestHttpJobWebSocketTest, RejectedWithoutCreateHelper) { | 318 TEST_F(URLRequestHttpJobWebSocketTest, RejectedWithoutCreateHelper) { |
| 310 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 319 scoped_refptr<TestURLRequestHttpJob> job( |
| 320 new TestURLRequestHttpJob(req_.get())); |
| 311 job->Start(); | 321 job->Start(); |
| 312 base::RunLoop().RunUntilIdle(); | 322 base::RunLoop().RunUntilIdle(); |
| 313 EXPECT_EQ(URLRequestStatus::FAILED, req_.status().status()); | 323 EXPECT_EQ(URLRequestStatus::FAILED, req_->status().status()); |
| 314 EXPECT_EQ(ERR_DISALLOWED_URL_SCHEME, req_.status().error()); | 324 EXPECT_EQ(ERR_DISALLOWED_URL_SCHEME, req_->status().error()); |
| 315 } | 325 } |
| 316 | 326 |
| 317 TEST_F(URLRequestHttpJobWebSocketTest, CreateHelperPassedThrough) { | 327 TEST_F(URLRequestHttpJobWebSocketTest, CreateHelperPassedThrough) { |
| 318 scoped_refptr<TestURLRequestHttpJob> job(new TestURLRequestHttpJob(&req_)); | 328 scoped_refptr<TestURLRequestHttpJob> job( |
| 329 new TestURLRequestHttpJob(req_.get())); |
| 319 scoped_ptr<MockCreateHelper> create_helper( | 330 scoped_ptr<MockCreateHelper> create_helper( |
| 320 new ::testing::StrictMock<MockCreateHelper>()); | 331 new ::testing::StrictMock<MockCreateHelper>()); |
| 321 FakeWebSocketHandshakeStream* fake_handshake_stream( | 332 FakeWebSocketHandshakeStream* fake_handshake_stream( |
| 322 new FakeWebSocketHandshakeStream); | 333 new FakeWebSocketHandshakeStream); |
| 323 // Ownership of fake_handshake_stream is transferred when CreateBasicStream() | 334 // Ownership of fake_handshake_stream is transferred when CreateBasicStream() |
| 324 // is called. | 335 // is called. |
| 325 EXPECT_CALL(*create_helper, CreateBasicStreamMock()) | 336 EXPECT_CALL(*create_helper, CreateBasicStreamMock()) |
| 326 .WillOnce(Return(fake_handshake_stream)); | 337 .WillOnce(Return(fake_handshake_stream)); |
| 327 req_.SetUserData(WebSocketHandshakeStreamBase::CreateHelper::DataKey(), | 338 req_->SetUserData(WebSocketHandshakeStreamBase::CreateHelper::DataKey(), |
| 328 create_helper.release()); | 339 create_helper.release()); |
| 329 req_.SetLoadFlags(LOAD_DISABLE_CACHE); | 340 req_->SetLoadFlags(LOAD_DISABLE_CACHE); |
| 330 job->Start(); | 341 job->Start(); |
| 331 base::RunLoop().RunUntilIdle(); | 342 base::RunLoop().RunUntilIdle(); |
| 332 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_.status().status()); | 343 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
| 333 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 344 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 334 } | 345 } |
| 335 | 346 |
| 336 } // namespace | 347 } // namespace |
| 337 | 348 |
| 338 } // namespace net | 349 } // namespace net |
| OLD | NEW |