| 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 #ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ | 5 #ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ |
| 6 #define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ | 6 #define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ |
| 7 | 7 |
| 8 #include "net/http/http_transaction.h" | 8 #include "net/http/http_transaction.h" |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/string16.h" | 15 #include "base/string16.h" |
| 15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 16 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 18 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
| 19 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
| 20 #include "net/disk_cache/disk_cache.h" | 21 #include "net/disk_cache/disk_cache.h" |
| 21 #include "net/http/http_cache.h" | 22 #include "net/http/http_cache.h" |
| 22 #include "net/http/http_request_info.h" | 23 #include "net/http/http_request_info.h" |
| 23 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 147 |
| 147 //----------------------------------------------------------------------------- | 148 //----------------------------------------------------------------------------- |
| 148 // mock network layer | 149 // mock network layer |
| 149 | 150 |
| 150 class MockNetworkLayer; | 151 class MockNetworkLayer; |
| 151 | 152 |
| 152 // This transaction class inspects the available set of mock transactions to | 153 // This transaction class inspects the available set of mock transactions to |
| 153 // find data for the request URL. It supports IO operations that complete | 154 // find data for the request URL. It supports IO operations that complete |
| 154 // synchronously or asynchronously to help exercise different code paths in the | 155 // synchronously or asynchronously to help exercise different code paths in the |
| 155 // HttpCache implementation. | 156 // HttpCache implementation. |
| 156 class MockNetworkTransaction : public net::HttpTransaction { | 157 class MockNetworkTransaction |
| 158 : public net::HttpTransaction, |
| 159 public base::SupportsWeakPtr<MockNetworkTransaction> { |
| 157 public: | 160 public: |
| 158 MockNetworkTransaction(net::RequestPriority priority, | 161 MockNetworkTransaction(net::RequestPriority priority, |
| 159 MockNetworkLayer* factory); | 162 MockNetworkLayer* factory); |
| 160 virtual ~MockNetworkTransaction(); | 163 virtual ~MockNetworkTransaction(); |
| 161 | 164 |
| 162 virtual int Start(const net::HttpRequestInfo* request, | 165 virtual int Start(const net::HttpRequestInfo* request, |
| 163 const net::CompletionCallback& callback, | 166 const net::CompletionCallback& callback, |
| 164 const net::BoundNetLog& net_log) OVERRIDE; | 167 const net::BoundNetLog& net_log) OVERRIDE; |
| 165 | 168 |
| 166 virtual int RestartIgnoringLastError( | 169 virtual int RestartIgnoringLastError( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 185 | 188 |
| 186 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE; | 189 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE; |
| 187 | 190 |
| 188 virtual net::LoadState GetLoadState() const OVERRIDE; | 191 virtual net::LoadState GetLoadState() const OVERRIDE; |
| 189 | 192 |
| 190 virtual net::UploadProgress GetUploadProgress() const OVERRIDE; | 193 virtual net::UploadProgress GetUploadProgress() const OVERRIDE; |
| 191 | 194 |
| 192 virtual bool GetLoadTimingInfo( | 195 virtual bool GetLoadTimingInfo( |
| 193 net::LoadTimingInfo* load_timing_info) const OVERRIDE; | 196 net::LoadTimingInfo* load_timing_info) const OVERRIDE; |
| 194 | 197 |
| 198 virtual void SetPriority(net::RequestPriority priority) OVERRIDE; |
| 199 |
| 200 net::RequestPriority priority() const { return priority_; } |
| 201 |
| 195 private: | 202 private: |
| 196 void CallbackLater(const net::CompletionCallback& callback, int result); | 203 void CallbackLater(const net::CompletionCallback& callback, int result); |
| 197 void RunCallback(const net::CompletionCallback& callback, int result); | 204 void RunCallback(const net::CompletionCallback& callback, int result); |
| 198 | 205 |
| 199 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_; | 206 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_; |
| 200 net::HttpResponseInfo response_; | 207 net::HttpResponseInfo response_; |
| 201 std::string data_; | 208 std::string data_; |
| 202 int data_cursor_; | 209 int data_cursor_; |
| 203 int test_mode_; | 210 int test_mode_; |
| 211 net::RequestPriority priority_; |
| 204 base::WeakPtr<MockNetworkLayer> transaction_factory_; | 212 base::WeakPtr<MockNetworkLayer> transaction_factory_; |
| 205 }; | 213 }; |
| 206 | 214 |
| 207 class MockNetworkLayer : public net::HttpTransactionFactory, | 215 class MockNetworkLayer : public net::HttpTransactionFactory, |
| 208 public base::SupportsWeakPtr<MockNetworkLayer> { | 216 public base::SupportsWeakPtr<MockNetworkLayer> { |
| 209 public: | 217 public: |
| 210 MockNetworkLayer(); | 218 MockNetworkLayer(); |
| 211 virtual ~MockNetworkLayer(); | 219 virtual ~MockNetworkLayer(); |
| 212 | 220 |
| 213 int transaction_count() const { return transaction_count_; } | 221 int transaction_count() const { return transaction_count_; } |
| 214 bool done_reading_called() const { return done_reading_called_; } | 222 bool done_reading_called() const { return done_reading_called_; } |
| 215 void TransactionDoneReading(); | 223 void TransactionDoneReading(); |
| 216 | 224 |
| 225 // Returns the last priority passed to CreateTransaction, or |
| 226 // DEFAULT_PRIORITY if it hasn't been called yet. |
| 227 net::RequestPriority last_create_transaction_priority() const { |
| 228 return last_create_transaction_priority_; |
| 229 } |
| 230 |
| 231 // Returns the last transaction created by |
| 232 // CreateTransaction. Returns a NULL WeakPtr if one has not been |
| 233 // created yet, or the last transaction has been destroyed, or |
| 234 // ClearLastTransaction() has been called and a new transaction |
| 235 // hasn't been created yet. |
| 236 base::WeakPtr<MockNetworkTransaction> last_transaction() { |
| 237 return last_transaction_; |
| 238 } |
| 239 |
| 240 // Makes last_transaction() return NULL until the next transaction |
| 241 // is created. |
| 242 void ClearLastTransaction() { |
| 243 last_transaction_.reset(); |
| 244 } |
| 245 |
| 217 // net::HttpTransactionFactory: | 246 // net::HttpTransactionFactory: |
| 218 virtual int CreateTransaction( | 247 virtual int CreateTransaction( |
| 219 net::RequestPriority priority, | 248 net::RequestPriority priority, |
| 220 scoped_ptr<net::HttpTransaction>* trans, | 249 scoped_ptr<net::HttpTransaction>* trans, |
| 221 net::HttpTransactionDelegate* delegate) OVERRIDE; | 250 net::HttpTransactionDelegate* delegate) OVERRIDE; |
| 222 virtual net::HttpCache* GetCache() OVERRIDE; | 251 virtual net::HttpCache* GetCache() OVERRIDE; |
| 223 virtual net::HttpNetworkSession* GetSession() OVERRIDE; | 252 virtual net::HttpNetworkSession* GetSession() OVERRIDE; |
| 224 | 253 |
| 225 private: | 254 private: |
| 226 int transaction_count_; | 255 int transaction_count_; |
| 227 bool done_reading_called_; | 256 bool done_reading_called_; |
| 257 net::RequestPriority last_create_transaction_priority_; |
| 258 base::WeakPtr<MockNetworkTransaction> last_transaction_; |
| 228 }; | 259 }; |
| 229 | 260 |
| 230 //----------------------------------------------------------------------------- | 261 //----------------------------------------------------------------------------- |
| 231 // helpers | 262 // helpers |
| 232 | 263 |
| 233 // read the transaction completely | 264 // read the transaction completely |
| 234 int ReadTransaction(net::HttpTransaction* trans, std::string* result); | 265 int ReadTransaction(net::HttpTransaction* trans, std::string* result); |
| 235 | 266 |
| 236 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ | 267 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ |
| OLD | NEW |