| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
| 6 | 6 |
| 7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
| 8 #include <stdarg.h> | 8 #include <stdarg.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 6278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6289 EXPECT_EQ("hello world", response_data); | 6289 EXPECT_EQ("hello world", response_data); |
| 6290 | 6290 |
| 6291 // Empty the current queue. This is necessary because idle sockets are | 6291 // Empty the current queue. This is necessary because idle sockets are |
| 6292 // added to the connection pool asynchronously with a PostTask. | 6292 // added to the connection pool asynchronously with a PostTask. |
| 6293 base::RunLoop().RunUntilIdle(); | 6293 base::RunLoop().RunUntilIdle(); |
| 6294 | 6294 |
| 6295 // We now check to make sure the socket was added back to the pool. | 6295 // We now check to make sure the socket was added back to the pool. |
| 6296 EXPECT_EQ(1, GetIdleSocketCountInSSLSocketPool(session.get())); | 6296 EXPECT_EQ(1, GetIdleSocketCountInSSLSocketPool(session.get())); |
| 6297 } | 6297 } |
| 6298 | 6298 |
| 6299 // Grab a socket, use it, and put it back into the pool. Then, make |
| 6300 // low memory notification and ensure the socket pool is flushed. |
| 6301 TEST_P(HttpNetworkTransactionTest, FlushSocketPoolOnLowMemoryNotifications) { |
| 6302 HttpRequestInfo request; |
| 6303 request.method = "GET"; |
| 6304 request.url = GURL("http://www.example.org/"); |
| 6305 request.load_flags = 0; |
| 6306 |
| 6307 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 6308 |
| 6309 std::unique_ptr<HttpTransaction> trans( |
| 6310 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 6311 |
| 6312 MockRead data_reads[] = { |
| 6313 // A part of the response body is received with the response headers. |
| 6314 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 6315 // The rest of the response body is received in two parts. |
| 6316 MockRead("lo"), MockRead(" world"), |
| 6317 MockRead("junk"), // Should not be read!! |
| 6318 MockRead(SYNCHRONOUS, OK), |
| 6319 }; |
| 6320 |
| 6321 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6322 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 6323 |
| 6324 TestCompletionCallback callback; |
| 6325 |
| 6326 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6327 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 6328 |
| 6329 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6330 |
| 6331 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6332 ASSERT_TRUE(response); |
| 6333 EXPECT_TRUE(response->headers); |
| 6334 std::string status_line = response->headers->GetStatusLine(); |
| 6335 EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 6336 |
| 6337 // Make memory critical notification and ensure the transaction still has been |
| 6338 // operating right. |
| 6339 base::MemoryPressureListener::NotifyMemoryPressure( |
| 6340 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 6341 base::RunLoop().RunUntilIdle(); |
| 6342 |
| 6343 // Socket should not be flushed as long as it is not idle. |
| 6344 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 6345 |
| 6346 std::string response_data; |
| 6347 rv = ReadTransaction(trans.get(), &response_data); |
| 6348 EXPECT_THAT(rv, IsOk()); |
| 6349 EXPECT_EQ("hello world", response_data); |
| 6350 |
| 6351 // Empty the current queue. This is necessary because idle sockets are |
| 6352 // added to the connection pool asynchronously with a PostTask. |
| 6353 base::RunLoop().RunUntilIdle(); |
| 6354 |
| 6355 // We now check to make sure the socket was added back to the pool. |
| 6356 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 6357 |
| 6358 // Idle sockets should be flushed now. |
| 6359 base::MemoryPressureListener::NotifyMemoryPressure( |
| 6360 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 6361 base::RunLoop().RunUntilIdle(); |
| 6362 |
| 6363 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 6364 } |
| 6365 |
| 6366 // Grab an SSL socket, use it, and put it back into the pool. Then, make |
| 6367 // low memory notification and ensure the socket pool is flushed. |
| 6368 TEST_P(HttpNetworkTransactionTest, FlushSSLSocketPoolOnLowMemoryNotifications) { |
| 6369 HttpRequestInfo request; |
| 6370 request.method = "GET"; |
| 6371 request.url = GURL("https://www.example.org/"); |
| 6372 request.load_flags = 0; |
| 6373 |
| 6374 MockWrite data_writes[] = { |
| 6375 MockWrite("GET / HTTP/1.1\r\n" |
| 6376 "Host: www.example.org\r\n" |
| 6377 "Connection: keep-alive\r\n\r\n"), |
| 6378 }; |
| 6379 |
| 6380 MockRead data_reads[] = { |
| 6381 MockRead("HTTP/1.1 200 OK\r\n"), MockRead("Content-Length: 11\r\n\r\n"), |
| 6382 MockRead("hello world"), MockRead(ASYNC, ERR_CONNECTION_CLOSED)}; |
| 6383 |
| 6384 SSLSocketDataProvider ssl(ASYNC, OK); |
| 6385 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 6386 |
| 6387 StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 6388 arraysize(data_writes)); |
| 6389 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 6390 |
| 6391 TestCompletionCallback callback; |
| 6392 |
| 6393 std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 6394 std::unique_ptr<HttpTransaction> trans( |
| 6395 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 6396 |
| 6397 EXPECT_EQ(0, GetIdleSocketCountInSSLSocketPool(session.get())); |
| 6398 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6399 |
| 6400 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6401 |
| 6402 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6403 ASSERT_TRUE(response); |
| 6404 ASSERT_TRUE(response->headers); |
| 6405 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6406 |
| 6407 // Make memory critical notification and ensure the transaction still has been |
| 6408 // operating right. |
| 6409 base::MemoryPressureListener::NotifyMemoryPressure( |
| 6410 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 6411 base::RunLoop().RunUntilIdle(); |
| 6412 |
| 6413 EXPECT_EQ(0, GetIdleSocketCountInSSLSocketPool(session.get())); |
| 6414 |
| 6415 std::string response_data; |
| 6416 rv = ReadTransaction(trans.get(), &response_data); |
| 6417 EXPECT_THAT(rv, IsOk()); |
| 6418 EXPECT_EQ("hello world", response_data); |
| 6419 |
| 6420 // Empty the current queue. This is necessary because idle sockets are |
| 6421 // added to the connection pool asynchronously with a PostTask. |
| 6422 base::RunLoop().RunUntilIdle(); |
| 6423 |
| 6424 // We now check to make sure the socket was added back to the pool. |
| 6425 EXPECT_EQ(1, GetIdleSocketCountInSSLSocketPool(session.get())); |
| 6426 |
| 6427 // Make memory notification once again and ensure idle socket is closed. |
| 6428 base::MemoryPressureListener::NotifyMemoryPressure( |
| 6429 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 6430 base::RunLoop().RunUntilIdle(); |
| 6431 |
| 6432 EXPECT_EQ(0, GetIdleSocketCountInSSLSocketPool(session.get())); |
| 6433 } |
| 6434 |
| 6299 // Make sure that we recycle a socket after a zero-length response. | 6435 // Make sure that we recycle a socket after a zero-length response. |
| 6300 // http://crbug.com/9880 | 6436 // http://crbug.com/9880 |
| 6301 TEST_P(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { | 6437 TEST_P(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
| 6302 HttpRequestInfo request; | 6438 HttpRequestInfo request; |
| 6303 request.method = "GET"; | 6439 request.method = "GET"; |
| 6304 request.url = GURL( | 6440 request.url = GURL( |
| 6305 "http://www.example.org/csi?v=3&s=web&action=&" | 6441 "http://www.example.org/csi?v=3&s=web&action=&" |
| 6306 "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" | 6442 "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 6307 "e=17259,18167,19592,19773,19981,20133,20173,20233&" | 6443 "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 6308 "rt=prt.2642,ol.2649,xjs.2951"); | 6444 "rt=prt.2642,ol.2649,xjs.2951"); |
| (...skipping 9435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 15744 base::RunLoop().RunUntilIdle(); | 15880 base::RunLoop().RunUntilIdle(); |
| 15745 | 15881 |
| 15746 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy); | 15882 EXPECT_TRUE(trans.GetResponseInfo()->was_fetched_via_spdy); |
| 15747 HttpRequestHeaders headers; | 15883 HttpRequestHeaders headers; |
| 15748 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers)); | 15884 ASSERT_TRUE(trans.GetFullRequestHeaders(&headers)); |
| 15749 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding)); | 15885 EXPECT_TRUE(headers.HasHeader(HttpRequestHeaders::kTokenBinding)); |
| 15750 } | 15886 } |
| 15751 #endif // !defined(OS_IOS) | 15887 #endif // !defined(OS_IOS) |
| 15752 | 15888 |
| 15753 } // namespace net | 15889 } // namespace net |
| OLD | NEW |