| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_response_body_drainer.h" | 5 #include "net/http/http_response_body_drainer.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "net/http/http_network_session.h" | 16 #include "net/http/http_network_session.h" |
| 17 #include "net/http/http_server_properties_impl.h" | 17 #include "net/http/http_server_properties_impl.h" |
| 18 #include "net/http/http_stream.h" | 18 #include "net/http/http_stream.h" |
| 19 #include "net/proxy/proxy_service.h" | 19 #include "net/proxy/proxy_service.h" |
| 20 #include "net/ssl/ssl_config_service_defaults.h" | 20 #include "net/ssl/ssl_config_service_defaults.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const int kMagicChunkSize = 1024; | 27 const int kMagicChunkSize = 1024; |
| 28 COMPILE_ASSERT( | 28 COMPILE_ASSERT((HttpResponseBodyDrainer::kDrainBodyBufferSize % |
| 29 (HttpResponseBodyDrainer::kDrainBodyBufferSize % kMagicChunkSize) == 0, | 29 kMagicChunkSize) == 0, |
| 30 chunk_size_needs_to_divide_evenly_into_buffer_size); | 30 chunk_size_needs_to_divide_evenly_into_buffer_size); |
| 31 | 31 |
| 32 class CloseResultWaiter { | 32 class CloseResultWaiter { |
| 33 public: | 33 public: |
| 34 CloseResultWaiter() | 34 CloseResultWaiter() |
| 35 : result_(false), | 35 : result_(false), have_result_(false), waiting_for_result_(false) {} |
| 36 have_result_(false), | |
| 37 waiting_for_result_(false) {} | |
| 38 | 36 |
| 39 int WaitForResult() { | 37 int WaitForResult() { |
| 40 CHECK(!waiting_for_result_); | 38 CHECK(!waiting_for_result_); |
| 41 while (!have_result_) { | 39 while (!have_result_) { |
| 42 waiting_for_result_ = true; | 40 waiting_for_result_ = true; |
| 43 base::MessageLoop::current()->Run(); | 41 base::MessageLoop::current()->Run(); |
| 44 waiting_for_result_ = false; | 42 waiting_for_result_ = false; |
| 45 } | 43 } |
| 46 return result_; | 44 return result_; |
| 47 } | 45 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 virtual bool CanFindEndOfResponse() const OVERRIDE { return true; } | 98 virtual bool CanFindEndOfResponse() const OVERRIDE { return true; } |
| 101 virtual bool IsConnectionReused() const OVERRIDE { return false; } | 99 virtual bool IsConnectionReused() const OVERRIDE { return false; } |
| 102 virtual void SetConnectionReused() OVERRIDE {} | 100 virtual void SetConnectionReused() OVERRIDE {} |
| 103 virtual bool IsConnectionReusable() const OVERRIDE { return false; } | 101 virtual bool IsConnectionReusable() const OVERRIDE { return false; } |
| 104 virtual int64 GetTotalReceivedBytes() const OVERRIDE { return 0; } | 102 virtual int64 GetTotalReceivedBytes() const OVERRIDE { return 0; } |
| 105 virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE {} | 103 virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE {} |
| 106 virtual void GetSSLCertRequestInfo( | 104 virtual void GetSSLCertRequestInfo( |
| 107 SSLCertRequestInfo* cert_request_info) OVERRIDE {} | 105 SSLCertRequestInfo* cert_request_info) OVERRIDE {} |
| 108 | 106 |
| 109 // Mocked API | 107 // Mocked API |
| 110 virtual int ReadResponseBody(IOBuffer* buf, int buf_len, | 108 virtual int ReadResponseBody(IOBuffer* buf, |
| 109 int buf_len, |
| 111 const CompletionCallback& callback) OVERRIDE; | 110 const CompletionCallback& callback) OVERRIDE; |
| 112 virtual void Close(bool not_reusable) OVERRIDE { | 111 virtual void Close(bool not_reusable) OVERRIDE { |
| 113 CHECK(!closed_); | 112 CHECK(!closed_); |
| 114 closed_ = true; | 113 closed_ = true; |
| 115 result_waiter_->set_result(not_reusable); | 114 result_waiter_->set_result(not_reusable); |
| 116 } | 115 } |
| 117 | 116 |
| 118 virtual HttpStream* RenewStreamForAuth() OVERRIDE { | 117 virtual HttpStream* RenewStreamForAuth() OVERRIDE { return NULL; } |
| 119 return NULL; | |
| 120 } | |
| 121 | 118 |
| 122 virtual bool IsResponseBodyComplete() const OVERRIDE { return is_complete_; } | 119 virtual bool IsResponseBodyComplete() const OVERRIDE { return is_complete_; } |
| 123 | 120 |
| 124 virtual bool IsSpdyHttpStream() const OVERRIDE { return false; } | 121 virtual bool IsSpdyHttpStream() const OVERRIDE { return false; } |
| 125 | 122 |
| 126 virtual bool GetLoadTimingInfo( | 123 virtual bool GetLoadTimingInfo( |
| 127 LoadTimingInfo* load_timing_info) const OVERRIDE { return false; } | 124 LoadTimingInfo* load_timing_info) const OVERRIDE { |
| 125 return false; |
| 126 } |
| 128 | 127 |
| 129 virtual void Drain(HttpNetworkSession*) OVERRIDE {} | 128 virtual void Drain(HttpNetworkSession*) OVERRIDE {} |
| 130 | 129 |
| 131 virtual void SetPriority(RequestPriority priority) OVERRIDE {} | 130 virtual void SetPriority(RequestPriority priority) OVERRIDE {} |
| 132 | 131 |
| 133 // Methods to tweak/observer mock behavior: | 132 // Methods to tweak/observer mock behavior: |
| 134 void set_stall_reads_forever() { stall_reads_forever_ = true; } | 133 void set_stall_reads_forever() { stall_reads_forever_ = true; } |
| 135 | 134 |
| 136 void set_num_chunks(int num_chunks) { num_chunks_ = num_chunks; } | 135 void set_num_chunks(int num_chunks) { num_chunks_ = num_chunks; } |
| 137 | 136 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 params.ssl_config_service = ssl_config_service_.get(); | 224 params.ssl_config_service = ssl_config_service_.get(); |
| 226 params.http_server_properties = http_server_properties_->GetWeakPtr(); | 225 params.http_server_properties = http_server_properties_->GetWeakPtr(); |
| 227 return new HttpNetworkSession(params); | 226 return new HttpNetworkSession(params); |
| 228 } | 227 } |
| 229 | 228 |
| 230 scoped_ptr<ProxyService> proxy_service_; | 229 scoped_ptr<ProxyService> proxy_service_; |
| 231 scoped_refptr<SSLConfigService> ssl_config_service_; | 230 scoped_refptr<SSLConfigService> ssl_config_service_; |
| 232 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_; | 231 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_; |
| 233 const scoped_refptr<HttpNetworkSession> session_; | 232 const scoped_refptr<HttpNetworkSession> session_; |
| 234 CloseResultWaiter result_waiter_; | 233 CloseResultWaiter result_waiter_; |
| 235 MockHttpStream* const mock_stream_; // Owned by |drainer_|. | 234 MockHttpStream* const mock_stream_; // Owned by |drainer_|. |
| 236 HttpResponseBodyDrainer* const drainer_; // Deletes itself. | 235 HttpResponseBodyDrainer* const drainer_; // Deletes itself. |
| 237 }; | 236 }; |
| 238 | 237 |
| 239 TEST_F(HttpResponseBodyDrainerTest, DrainBodySyncSingleOK) { | 238 TEST_F(HttpResponseBodyDrainerTest, DrainBodySyncSingleOK) { |
| 240 mock_stream_->set_num_chunks(1); | 239 mock_stream_->set_num_chunks(1); |
| 241 mock_stream_->set_sync(); | 240 mock_stream_->set_sync(); |
| 242 drainer_->Start(session_.get()); | 241 drainer_->Start(session_.get()); |
| 243 EXPECT_FALSE(result_waiter_.WaitForResult()); | 242 EXPECT_FALSE(result_waiter_.WaitForResult()); |
| 244 } | 243 } |
| 245 | 244 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 268 | 267 |
| 269 TEST_F(HttpResponseBodyDrainerTest, DrainBodySyncEmptyChunk) { | 268 TEST_F(HttpResponseBodyDrainerTest, DrainBodySyncEmptyChunk) { |
| 270 mock_stream_->set_num_chunks(4); | 269 mock_stream_->set_num_chunks(4); |
| 271 mock_stream_->set_sync(); | 270 mock_stream_->set_sync(); |
| 272 mock_stream_->set_is_last_chunk_zero_size(); | 271 mock_stream_->set_is_last_chunk_zero_size(); |
| 273 drainer_->Start(session_.get()); | 272 drainer_->Start(session_.get()); |
| 274 EXPECT_FALSE(result_waiter_.WaitForResult()); | 273 EXPECT_FALSE(result_waiter_.WaitForResult()); |
| 275 } | 274 } |
| 276 | 275 |
| 277 TEST_F(HttpResponseBodyDrainerTest, DrainBodySizeEqualsDrainBuffer) { | 276 TEST_F(HttpResponseBodyDrainerTest, DrainBodySizeEqualsDrainBuffer) { |
| 278 mock_stream_->set_num_chunks( | 277 mock_stream_->set_num_chunks(HttpResponseBodyDrainer::kDrainBodyBufferSize / |
| 279 HttpResponseBodyDrainer::kDrainBodyBufferSize / kMagicChunkSize); | 278 kMagicChunkSize); |
| 280 drainer_->Start(session_.get()); | 279 drainer_->Start(session_.get()); |
| 281 EXPECT_FALSE(result_waiter_.WaitForResult()); | 280 EXPECT_FALSE(result_waiter_.WaitForResult()); |
| 282 } | 281 } |
| 283 | 282 |
| 284 TEST_F(HttpResponseBodyDrainerTest, DrainBodyTimeOut) { | 283 TEST_F(HttpResponseBodyDrainerTest, DrainBodyTimeOut) { |
| 285 mock_stream_->set_num_chunks(2); | 284 mock_stream_->set_num_chunks(2); |
| 286 mock_stream_->set_stall_reads_forever(); | 285 mock_stream_->set_stall_reads_forever(); |
| 287 drainer_->Start(session_.get()); | 286 drainer_->Start(session_.get()); |
| 288 EXPECT_TRUE(result_waiter_.WaitForResult()); | 287 EXPECT_TRUE(result_waiter_.WaitForResult()); |
| 289 } | 288 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 317 | 316 |
| 318 TEST_F(HttpResponseBodyDrainerTest, StartWithNothingToDo) { | 317 TEST_F(HttpResponseBodyDrainerTest, StartWithNothingToDo) { |
| 319 mock_stream_->set_num_chunks(0); | 318 mock_stream_->set_num_chunks(0); |
| 320 drainer_->StartWithSize(session_.get(), 0); | 319 drainer_->StartWithSize(session_.get(), 0); |
| 321 EXPECT_FALSE(result_waiter_.WaitForResult()); | 320 EXPECT_FALSE(result_waiter_.WaitForResult()); |
| 322 } | 321 } |
| 323 | 322 |
| 324 } // namespace | 323 } // namespace |
| 325 | 324 |
| 326 } // namespace net | 325 } // namespace net |
| OLD | NEW |