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> |
(...skipping 19 matching lines...) Expand all Loading... |
30 class IOBuffer; | 30 class IOBuffer; |
31 } | 31 } |
32 | 32 |
33 //----------------------------------------------------------------------------- | 33 //----------------------------------------------------------------------------- |
34 // mock transaction data | 34 // mock transaction data |
35 | 35 |
36 // these flags may be combined to form the test_mode field | 36 // these flags may be combined to form the test_mode field |
37 enum { | 37 enum { |
38 TEST_MODE_NORMAL = 0, | 38 TEST_MODE_NORMAL = 0, |
39 TEST_MODE_SYNC_NET_START = 1 << 0, | 39 TEST_MODE_SYNC_NET_START = 1 << 0, |
40 TEST_MODE_SYNC_NET_READ = 1 << 1, | 40 TEST_MODE_SYNC_NET_READ = 1 << 1, |
41 TEST_MODE_SYNC_CACHE_START = 1 << 2, | 41 TEST_MODE_SYNC_CACHE_START = 1 << 2, |
42 TEST_MODE_SYNC_CACHE_READ = 1 << 3, | 42 TEST_MODE_SYNC_CACHE_READ = 1 << 3, |
43 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4, | 43 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4, |
44 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ | | 44 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ | |
45 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ | | 45 TEST_MODE_SYNC_CACHE_START | |
| 46 TEST_MODE_SYNC_CACHE_READ | |
46 TEST_MODE_SYNC_CACHE_WRITE), | 47 TEST_MODE_SYNC_CACHE_WRITE), |
47 TEST_MODE_SLOW_READ = 1 << 5 | 48 TEST_MODE_SLOW_READ = 1 << 5 |
48 }; | 49 }; |
49 | 50 |
50 typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request, | 51 typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request, |
51 std::string* response_status, | 52 std::string* response_status, |
52 std::string* response_headers, | 53 std::string* response_headers, |
53 std::string* response_data); | 54 std::string* response_data); |
54 | 55 |
55 struct MockTransaction { | 56 struct MockTransaction { |
(...skipping 24 matching lines...) Expand all Loading... |
80 | 81 |
81 // returns the mock transaction for the given URL | 82 // returns the mock transaction for the given URL |
82 const MockTransaction* FindMockTransaction(const GURL& url); | 83 const MockTransaction* FindMockTransaction(const GURL& url); |
83 | 84 |
84 // Add/Remove a mock transaction that can be accessed via FindMockTransaction. | 85 // Add/Remove a mock transaction that can be accessed via FindMockTransaction. |
85 // There can be only one MockTransaction associated with a given URL. | 86 // There can be only one MockTransaction associated with a given URL. |
86 void AddMockTransaction(const MockTransaction* trans); | 87 void AddMockTransaction(const MockTransaction* trans); |
87 void RemoveMockTransaction(const MockTransaction* trans); | 88 void RemoveMockTransaction(const MockTransaction* trans); |
88 | 89 |
89 struct ScopedMockTransaction : MockTransaction { | 90 struct ScopedMockTransaction : MockTransaction { |
90 ScopedMockTransaction() { | 91 ScopedMockTransaction() { AddMockTransaction(this); } |
91 AddMockTransaction(this); | |
92 } | |
93 explicit ScopedMockTransaction(const MockTransaction& t) | 92 explicit ScopedMockTransaction(const MockTransaction& t) |
94 : MockTransaction(t) { | 93 : MockTransaction(t) { |
95 AddMockTransaction(this); | 94 AddMockTransaction(this); |
96 } | 95 } |
97 ~ScopedMockTransaction() { | 96 ~ScopedMockTransaction() { RemoveMockTransaction(this); } |
98 RemoveMockTransaction(this); | |
99 } | |
100 }; | 97 }; |
101 | 98 |
102 //----------------------------------------------------------------------------- | 99 //----------------------------------------------------------------------------- |
103 // mock http request | 100 // mock http request |
104 | 101 |
105 class MockHttpRequest : public net::HttpRequestInfo { | 102 class MockHttpRequest : public net::HttpRequestInfo { |
106 public: | 103 public: |
107 explicit MockHttpRequest(const MockTransaction& t); | 104 explicit MockHttpRequest(const MockTransaction& t); |
108 }; | 105 }; |
109 | 106 |
(...skipping 11 matching lines...) Expand all Loading... |
121 | 118 |
122 bool is_done() const { return state_ == DONE; } | 119 bool is_done() const { return state_ == DONE; } |
123 int error() const { return error_; } | 120 int error() const { return error_; } |
124 | 121 |
125 const net::HttpResponseInfo* response_info() const { | 122 const net::HttpResponseInfo* response_info() const { |
126 return trans_->GetResponseInfo(); | 123 return trans_->GetResponseInfo(); |
127 } | 124 } |
128 const std::string& content() const { return content_; } | 125 const std::string& content() const { return content_; } |
129 | 126 |
130 private: | 127 private: |
131 enum State { | 128 enum State { IDLE, STARTING, READING, DONE }; |
132 IDLE, | |
133 STARTING, | |
134 READING, | |
135 DONE | |
136 }; | |
137 | 129 |
138 void DidStart(int result); | 130 void DidStart(int result); |
139 void DidRead(int result); | 131 void DidRead(int result); |
140 void DidFinish(int result); | 132 void DidFinish(int result); |
141 void Read(); | 133 void Read(); |
142 | 134 |
143 void OnIOComplete(int result); | 135 void OnIOComplete(int result); |
144 | 136 |
145 State state_; | 137 State state_; |
146 scoped_ptr<net::HttpTransaction> trans_; | 138 scoped_ptr<net::HttpTransaction> trans_; |
(...skipping 10 matching lines...) Expand all Loading... |
157 class MockNetworkLayer; | 149 class MockNetworkLayer; |
158 | 150 |
159 // This transaction class inspects the available set of mock transactions to | 151 // This transaction class inspects the available set of mock transactions to |
160 // find data for the request URL. It supports IO operations that complete | 152 // find data for the request URL. It supports IO operations that complete |
161 // synchronously or asynchronously to help exercise different code paths in the | 153 // synchronously or asynchronously to help exercise different code paths in the |
162 // HttpCache implementation. | 154 // HttpCache implementation. |
163 class MockNetworkTransaction | 155 class MockNetworkTransaction |
164 : public net::HttpTransaction, | 156 : public net::HttpTransaction, |
165 public base::SupportsWeakPtr<MockNetworkTransaction> { | 157 public base::SupportsWeakPtr<MockNetworkTransaction> { |
166 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper; | 158 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper; |
| 159 |
167 public: | 160 public: |
168 MockNetworkTransaction(net::RequestPriority priority, | 161 MockNetworkTransaction(net::RequestPriority priority, |
169 MockNetworkLayer* factory); | 162 MockNetworkLayer* factory); |
170 virtual ~MockNetworkTransaction(); | 163 virtual ~MockNetworkTransaction(); |
171 | 164 |
172 virtual int Start(const net::HttpRequestInfo* request, | 165 virtual int Start(const net::HttpRequestInfo* request, |
173 const net::CompletionCallback& callback, | 166 const net::CompletionCallback& callback, |
174 const net::BoundNetLog& net_log) OVERRIDE; | 167 const net::BoundNetLog& net_log) OVERRIDE; |
175 | 168 |
176 virtual int RestartIgnoringLastError( | 169 virtual int RestartIgnoringLastError( |
177 const net::CompletionCallback& callback) OVERRIDE; | 170 const net::CompletionCallback& callback) OVERRIDE; |
178 | 171 |
179 virtual int RestartWithCertificate( | 172 virtual int RestartWithCertificate( |
180 net::X509Certificate* client_cert, | 173 net::X509Certificate* client_cert, |
181 const net::CompletionCallback& callback) OVERRIDE; | 174 const net::CompletionCallback& callback) OVERRIDE; |
182 | 175 |
183 virtual int RestartWithAuth( | 176 virtual int RestartWithAuth(const net::AuthCredentials& credentials, |
184 const net::AuthCredentials& credentials, | 177 const net::CompletionCallback& callback) OVERRIDE; |
185 const net::CompletionCallback& callback) OVERRIDE; | |
186 | 178 |
187 virtual bool IsReadyToRestartForAuth() OVERRIDE; | 179 virtual bool IsReadyToRestartForAuth() OVERRIDE; |
188 | 180 |
189 virtual int Read(net::IOBuffer* buf, int buf_len, | 181 virtual int Read(net::IOBuffer* buf, |
| 182 int buf_len, |
190 const net::CompletionCallback& callback) OVERRIDE; | 183 const net::CompletionCallback& callback) OVERRIDE; |
191 | 184 |
192 virtual void StopCaching() OVERRIDE; | 185 virtual void StopCaching() OVERRIDE; |
193 | 186 |
194 virtual bool GetFullRequestHeaders( | 187 virtual bool GetFullRequestHeaders( |
195 net::HttpRequestHeaders* headers) const OVERRIDE; | 188 net::HttpRequestHeaders* headers) const OVERRIDE; |
196 | 189 |
197 virtual int64 GetTotalReceivedBytes() const OVERRIDE; | 190 virtual int64 GetTotalReceivedBytes() const OVERRIDE; |
198 | 191 |
199 virtual void DoneReading() OVERRIDE; | 192 virtual void DoneReading() OVERRIDE; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 // CreateTransaction. Returns a NULL WeakPtr if one has not been | 264 // CreateTransaction. Returns a NULL WeakPtr if one has not been |
272 // created yet, or the last transaction has been destroyed, or | 265 // created yet, or the last transaction has been destroyed, or |
273 // ClearLastTransaction() has been called and a new transaction | 266 // ClearLastTransaction() has been called and a new transaction |
274 // hasn't been created yet. | 267 // hasn't been created yet. |
275 base::WeakPtr<MockNetworkTransaction> last_transaction() { | 268 base::WeakPtr<MockNetworkTransaction> last_transaction() { |
276 return last_transaction_; | 269 return last_transaction_; |
277 } | 270 } |
278 | 271 |
279 // Makes last_transaction() return NULL until the next transaction | 272 // Makes last_transaction() return NULL until the next transaction |
280 // is created. | 273 // is created. |
281 void ClearLastTransaction() { | 274 void ClearLastTransaction() { last_transaction_.reset(); } |
282 last_transaction_.reset(); | |
283 } | |
284 | 275 |
285 // net::HttpTransactionFactory: | 276 // net::HttpTransactionFactory: |
286 virtual int CreateTransaction( | 277 virtual int CreateTransaction( |
287 net::RequestPriority priority, | 278 net::RequestPriority priority, |
288 scoped_ptr<net::HttpTransaction>* trans) OVERRIDE; | 279 scoped_ptr<net::HttpTransaction>* trans) OVERRIDE; |
289 virtual net::HttpCache* GetCache() OVERRIDE; | 280 virtual net::HttpCache* GetCache() OVERRIDE; |
290 virtual net::HttpNetworkSession* GetSession() OVERRIDE; | 281 virtual net::HttpNetworkSession* GetSession() OVERRIDE; |
291 | 282 |
292 private: | 283 private: |
293 int transaction_count_; | 284 int transaction_count_; |
294 bool done_reading_called_; | 285 bool done_reading_called_; |
295 bool stop_caching_called_; | 286 bool stop_caching_called_; |
296 net::RequestPriority last_create_transaction_priority_; | 287 net::RequestPriority last_create_transaction_priority_; |
297 base::WeakPtr<MockNetworkTransaction> last_transaction_; | 288 base::WeakPtr<MockNetworkTransaction> last_transaction_; |
298 }; | 289 }; |
299 | 290 |
300 //----------------------------------------------------------------------------- | 291 //----------------------------------------------------------------------------- |
301 // helpers | 292 // helpers |
302 | 293 |
303 // read the transaction completely | 294 // read the transaction completely |
304 int ReadTransaction(net::HttpTransaction* trans, std::string* result); | 295 int ReadTransaction(net::HttpTransaction* trans, std::string* result); |
305 | 296 |
306 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ | 297 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ |
OLD | NEW |