| 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/memory/scoped_ptr.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "net/base/auth.h" | 14 #include "net/base/auth.h" |
| 15 #include "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
| 16 #include "net/base/test_data_directory.h" |
| 16 #include "net/http/http_transaction_factory.h" | 17 #include "net/http/http_transaction_factory.h" |
| 17 #include "net/http/http_transaction_test_util.h" | 18 #include "net/http/http_transaction_test_util.h" |
| 18 #include "net/socket/socket_test_util.h" | 19 #include "net/socket/socket_test_util.h" |
| 20 #include "net/test/cert_test_util.h" |
| 19 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
| 20 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
| 21 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
| 22 #include "net/websockets/websocket_handshake_stream_base.h" | 24 #include "net/websockets/websocket_handshake_stream_base.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 26 | 28 |
| 27 namespace net { | 29 namespace net { |
| 28 | 30 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 void EnableSdch() { | 88 void EnableSdch() { |
| 87 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); | 89 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); |
| 88 } | 90 } |
| 89 | 91 |
| 90 MockNetworkLayer network_layer_; | 92 MockNetworkLayer network_layer_; |
| 91 TestURLRequestContext context_; | 93 TestURLRequestContext context_; |
| 92 TestDelegate delegate_; | 94 TestDelegate delegate_; |
| 93 scoped_ptr<URLRequest> req_; | 95 scoped_ptr<URLRequest> req_; |
| 94 }; | 96 }; |
| 95 | 97 |
| 98 class URLRequestHttpJobBackoffTest : public ::testing::Test { |
| 99 protected: |
| 100 URLRequestHttpJobBackoffTest() : context_(new TestURLRequestContext(true)) { |
| 101 context_->set_client_socket_factory(&socket_factory_); |
| 102 context_->set_network_delegate(&network_delegate_); |
| 103 context_->set_backoff_manager(&manager_); |
| 104 context_->Init(); |
| 105 } |
| 106 |
| 107 MockClientSocketFactory socket_factory_; |
| 108 TestNetworkDelegate network_delegate_; |
| 109 URLRequestBackoffManager manager_; |
| 110 scoped_ptr<TestURLRequestContext> context_; |
| 111 }; |
| 112 |
| 113 TEST_F(URLRequestHttpJobBackoffTest, BackoffHeader) { |
| 114 MockWrite writes[] = {MockWrite( |
| 115 "GET / HTTP/1.1\r\n" |
| 116 "Host: www.example.com\r\n" |
| 117 "Connection: keep-alive\r\n" |
| 118 "User-Agent:\r\n" |
| 119 "Accept-Encoding: gzip, deflate\r\n" |
| 120 "Accept-Language: en-us,fr\r\n\r\n")}; |
| 121 |
| 122 MockRead reads[] = {MockRead( |
| 123 "HTTP/1.1 200 OK\r\n" |
| 124 "Backoff: 3600\r\n" |
| 125 "Content-Length: 9\r\n\r\n"), |
| 126 MockRead("test.html")}; |
| 127 |
| 128 net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK); |
| 129 ssl_socket_data_provider.SetNextProto(kProtoHTTP11); |
| 130 ssl_socket_data_provider.cert = |
| 131 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"); |
| 132 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider); |
| 133 |
| 134 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 135 arraysize(writes)); |
| 136 socket_factory_.AddSocketDataProvider(&socket_data); |
| 137 |
| 138 TestDelegate delegate1; |
| 139 scoped_ptr<URLRequest> request1 = |
| 140 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY, |
| 141 &delegate1).Pass(); |
| 142 |
| 143 request1->Start(); |
| 144 ASSERT_TRUE(request1->is_pending()); |
| 145 base::RunLoop().Run(); |
| 146 |
| 147 EXPECT_TRUE(request1->status().is_success()); |
| 148 EXPECT_EQ("test.html", delegate1.data_received()); |
| 149 EXPECT_EQ(1, delegate1.received_before_network_start_count()); |
| 150 EXPECT_EQ(1, manager_.GetNumberOfEntriesForTests()); |
| 151 |
| 152 // Issue another request, and backoff logic should apply. |
| 153 TestDelegate delegate2; |
| 154 scoped_ptr<URLRequest> request2 = |
| 155 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY, |
| 156 &delegate2).Pass(); |
| 157 |
| 158 request2->Start(); |
| 159 ASSERT_TRUE(request2->is_pending()); |
| 160 base::RunLoop().Run(); |
| 161 |
| 162 EXPECT_FALSE(request2->status().is_success()); |
| 163 EXPECT_EQ(ERR_TEMPORARY_BACKOFF, request2->status().error()); |
| 164 EXPECT_EQ(0, delegate2.received_before_network_start_count()); |
| 165 } |
| 166 |
| 167 TEST_F(URLRequestHttpJobBackoffTest, BackoffHeaderNotSecure) { |
| 168 MockWrite writes[] = {MockWrite( |
| 169 "GET / HTTP/1.1\r\n" |
| 170 "Host: www.example.com\r\n" |
| 171 "Connection: keep-alive\r\n" |
| 172 "User-Agent:\r\n" |
| 173 "Accept-Encoding: gzip, deflate\r\n" |
| 174 "Accept-Language: en-us,fr\r\n\r\n")}; |
| 175 MockRead reads[] = {MockRead( |
| 176 "HTTP/1.1 200 OK\r\n" |
| 177 "Backoff: 3600\r\n" |
| 178 "Content-Length: 9\r\n\r\n"), |
| 179 MockRead("test.html")}; |
| 180 |
| 181 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 182 arraysize(writes)); |
| 183 socket_factory_.AddSocketDataProvider(&socket_data); |
| 184 |
| 185 TestDelegate delegate; |
| 186 scoped_ptr<URLRequest> request = |
| 187 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, |
| 188 &delegate).Pass(); |
| 189 |
| 190 request->Start(); |
| 191 ASSERT_TRUE(request->is_pending()); |
| 192 base::RunLoop().Run(); |
| 193 |
| 194 EXPECT_TRUE(request->status().is_success()); |
| 195 EXPECT_EQ("test.html", delegate.data_received()); |
| 196 EXPECT_EQ(1, delegate.received_before_network_start_count()); |
| 197 // Backoff logic does not apply to plain HTTP request. |
| 198 EXPECT_EQ(0, manager_.GetNumberOfEntriesForTests()); |
| 199 } |
| 200 |
| 201 TEST_F(URLRequestHttpJobBackoffTest, BackoffHeaderCachedResponse) { |
| 202 MockWrite writes[] = {MockWrite( |
| 203 "GET / HTTP/1.1\r\n" |
| 204 "Host: www.example.com\r\n" |
| 205 "Connection: keep-alive\r\n" |
| 206 "User-Agent:\r\n" |
| 207 "Accept-Encoding: gzip, deflate\r\n" |
| 208 "Accept-Language: en-us,fr\r\n\r\n")}; |
| 209 MockRead reads[] = {MockRead( |
| 210 "HTTP/1.1 200 OK\r\n" |
| 211 "Backoff: 3600\r\n" |
| 212 "Cache-Control: max-age=120\r\n" |
| 213 "Content-Length: 9\r\n\r\n"), |
| 214 MockRead("test.html")}; |
| 215 |
| 216 net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK); |
| 217 ssl_socket_data_provider.SetNextProto(kProtoHTTP11); |
| 218 ssl_socket_data_provider.cert = |
| 219 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"); |
| 220 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider); |
| 221 |
| 222 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 223 arraysize(writes)); |
| 224 socket_factory_.AddSocketDataProvider(&socket_data); |
| 225 |
| 226 TestDelegate delegate1; |
| 227 scoped_ptr<URLRequest> request1 = |
| 228 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY, |
| 229 &delegate1).Pass(); |
| 230 |
| 231 request1->Start(); |
| 232 ASSERT_TRUE(request1->is_pending()); |
| 233 base::RunLoop().Run(); |
| 234 |
| 235 EXPECT_TRUE(request1->status().is_success()); |
| 236 EXPECT_EQ("test.html", delegate1.data_received()); |
| 237 EXPECT_EQ(1, delegate1.received_before_network_start_count()); |
| 238 EXPECT_EQ(1, manager_.GetNumberOfEntriesForTests()); |
| 239 |
| 240 // Backoff logic does not apply to a second request, since it is fetched |
| 241 // from cache. |
| 242 TestDelegate delegate2; |
| 243 scoped_ptr<URLRequest> request2 = |
| 244 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY, |
| 245 &delegate2).Pass(); |
| 246 |
| 247 request2->Start(); |
| 248 ASSERT_TRUE(request2->is_pending()); |
| 249 base::RunLoop().Run(); |
| 250 EXPECT_TRUE(request2->was_cached()); |
| 251 EXPECT_TRUE(request2->status().is_success()); |
| 252 EXPECT_EQ(0, delegate2.received_before_network_start_count()); |
| 253 } |
| 254 |
| 96 // Make sure that SetPriority actually sets the URLRequestHttpJob's | 255 // Make sure that SetPriority actually sets the URLRequestHttpJob's |
| 97 // priority, both before and after start. | 256 // priority, both before and after start. |
| 98 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) { | 257 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) { |
| 99 scoped_refptr<TestURLRequestHttpJob> job( | 258 scoped_refptr<TestURLRequestHttpJob> job( |
| 100 new TestURLRequestHttpJob(req_.get())); | 259 new TestURLRequestHttpJob(req_.get())); |
| 101 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); | 260 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 102 | 261 |
| 103 job->SetPriority(LOWEST); | 262 job->SetPriority(LOWEST); |
| 104 EXPECT_EQ(LOWEST, job->priority()); | 263 EXPECT_EQ(LOWEST, job->priority()); |
| 105 | 264 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 req_->SetLoadFlags(LOAD_DISABLE_CACHE); | 502 req_->SetLoadFlags(LOAD_DISABLE_CACHE); |
| 344 job->Start(); | 503 job->Start(); |
| 345 base::RunLoop().RunUntilIdle(); | 504 base::RunLoop().RunUntilIdle(); |
| 346 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 505 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
| 347 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 506 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 348 } | 507 } |
| 349 | 508 |
| 350 } // namespace | 509 } // namespace |
| 351 | 510 |
| 352 } // namespace net | 511 } // namespace net |
| OLD | NEW |