Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / http / http_transaction_test_util.h
blobb8663c6e14382b7dd440a77d36c8dc52a89b3efb
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_
6 #define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_
8 #include "net/http/http_transaction.h"
10 #include <string>
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/load_flags.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_log.h"
20 #include "net/base/request_priority.h"
21 #include "net/base/test_completion_callback.h"
22 #include "net/disk_cache/disk_cache.h"
23 #include "net/http/http_cache.h"
24 #include "net/http/http_request_info.h"
25 #include "net/http/http_response_headers.h"
26 #include "net/http/http_response_info.h"
28 namespace net {
29 class HttpRequestHeaders;
30 class IOBuffer;
31 struct HttpRequestInfo;
34 //-----------------------------------------------------------------------------
35 // mock transaction data
37 // these flags may be combined to form the test_mode field
38 enum {
39 TEST_MODE_NORMAL = 0,
40 TEST_MODE_SYNC_NET_START = 1 << 0,
41 TEST_MODE_SYNC_NET_READ = 1 << 1,
42 TEST_MODE_SYNC_CACHE_START = 1 << 2,
43 TEST_MODE_SYNC_CACHE_READ = 1 << 3,
44 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4,
45 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ |
46 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ |
47 TEST_MODE_SYNC_CACHE_WRITE),
48 TEST_MODE_SLOW_READ = 1 << 5
51 typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request,
52 std::string* response_status,
53 std::string* response_headers,
54 std::string* response_data);
56 struct MockTransaction {
57 const char* url;
58 const char* method;
59 // If |request_time| is unspecified, the current time will be used.
60 base::Time request_time;
61 const char* request_headers;
62 int load_flags;
63 const char* status;
64 const char* response_headers;
65 // If |response_time| is unspecified, the current time will be used.
66 base::Time response_time;
67 const char* data;
68 int test_mode;
69 MockTransactionHandler handler;
70 net::CertStatus cert_status;
71 // Value returned by MockNetworkTransaction::Start (potentially
72 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
73 net::Error return_code;
76 extern const MockTransaction kSimpleGET_Transaction;
77 extern const MockTransaction kSimplePOST_Transaction;
78 extern const MockTransaction kTypicalGET_Transaction;
79 extern const MockTransaction kETagGET_Transaction;
80 extern const MockTransaction kRangeGET_Transaction;
82 // returns the mock transaction for the given URL
83 const MockTransaction* FindMockTransaction(const GURL& url);
85 // Add/Remove a mock transaction that can be accessed via FindMockTransaction.
86 // There can be only one MockTransaction associated with a given URL.
87 void AddMockTransaction(const MockTransaction* trans);
88 void RemoveMockTransaction(const MockTransaction* trans);
90 struct ScopedMockTransaction : MockTransaction {
91 ScopedMockTransaction() {
92 AddMockTransaction(this);
94 explicit ScopedMockTransaction(const MockTransaction& t)
95 : MockTransaction(t) {
96 AddMockTransaction(this);
98 ~ScopedMockTransaction() {
99 RemoveMockTransaction(this);
103 //-----------------------------------------------------------------------------
104 // mock http request
106 class MockHttpRequest : public net::HttpRequestInfo {
107 public:
108 explicit MockHttpRequest(const MockTransaction& t);
111 //-----------------------------------------------------------------------------
112 // use this class to test completely consuming a transaction
114 class TestTransactionConsumer {
115 public:
116 TestTransactionConsumer(net::RequestPriority priority,
117 net::HttpTransactionFactory* factory);
118 virtual ~TestTransactionConsumer();
120 void Start(const net::HttpRequestInfo* request,
121 const net::BoundNetLog& net_log);
123 bool is_done() const { return state_ == DONE; }
124 int error() const { return error_; }
126 const net::HttpResponseInfo* response_info() const {
127 return trans_->GetResponseInfo();
129 const std::string& content() const { return content_; }
131 private:
132 enum State {
133 IDLE,
134 STARTING,
135 READING,
136 DONE
139 void DidStart(int result);
140 void DidRead(int result);
141 void DidFinish(int result);
142 void Read();
144 void OnIOComplete(int result);
146 State state_;
147 scoped_ptr<net::HttpTransaction> trans_;
148 std::string content_;
149 scoped_refptr<net::IOBuffer> read_buf_;
150 int error_;
152 static int quit_counter_;
155 //-----------------------------------------------------------------------------
156 // mock network layer
158 class MockNetworkLayer;
160 // This transaction class inspects the available set of mock transactions to
161 // find data for the request URL. It supports IO operations that complete
162 // synchronously or asynchronously to help exercise different code paths in the
163 // HttpCache implementation.
164 class MockNetworkTransaction
165 : public net::HttpTransaction,
166 public base::SupportsWeakPtr<MockNetworkTransaction> {
167 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
168 public:
169 MockNetworkTransaction(net::RequestPriority priority,
170 MockNetworkLayer* factory);
171 virtual ~MockNetworkTransaction();
173 virtual int Start(const net::HttpRequestInfo* request,
174 const net::CompletionCallback& callback,
175 const net::BoundNetLog& net_log) OVERRIDE;
177 virtual int RestartIgnoringLastError(
178 const net::CompletionCallback& callback) OVERRIDE;
180 virtual int RestartWithCertificate(
181 net::X509Certificate* client_cert,
182 const net::CompletionCallback& callback) OVERRIDE;
184 virtual int RestartWithAuth(
185 const net::AuthCredentials& credentials,
186 const net::CompletionCallback& callback) OVERRIDE;
188 virtual bool IsReadyToRestartForAuth() OVERRIDE;
190 virtual int Read(net::IOBuffer* buf, int buf_len,
191 const net::CompletionCallback& callback) OVERRIDE;
193 virtual void StopCaching() OVERRIDE;
195 virtual bool GetFullRequestHeaders(
196 net::HttpRequestHeaders* headers) const OVERRIDE;
198 virtual int64 GetTotalReceivedBytes() const OVERRIDE;
200 virtual void DoneReading() OVERRIDE;
202 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE;
204 virtual net::LoadState GetLoadState() const OVERRIDE;
206 virtual net::UploadProgress GetUploadProgress() const OVERRIDE;
208 virtual void SetQuicServerInfo(
209 net::QuicServerInfo* quic_server_info) OVERRIDE;
211 virtual bool GetLoadTimingInfo(
212 net::LoadTimingInfo* load_timing_info) const OVERRIDE;
214 virtual void SetPriority(net::RequestPriority priority) OVERRIDE;
216 virtual void SetWebSocketHandshakeStreamCreateHelper(
217 CreateHelper* create_helper) OVERRIDE;
219 virtual void SetBeforeNetworkStartCallback(
220 const BeforeNetworkStartCallback& callback) OVERRIDE;
222 virtual void SetBeforeProxyHeadersSentCallback(
223 const BeforeProxyHeadersSentCallback& callback) OVERRIDE;
225 virtual int ResumeNetworkStart() OVERRIDE;
227 CreateHelper* websocket_handshake_stream_create_helper() {
228 return websocket_handshake_stream_create_helper_;
230 net::RequestPriority priority() const { return priority_; }
231 const net::HttpRequestInfo* request() const { return request_; }
233 private:
234 int StartInternal(const net::HttpRequestInfo* request,
235 const net::CompletionCallback& callback,
236 const net::BoundNetLog& net_log);
237 void CallbackLater(const net::CompletionCallback& callback, int result);
238 void RunCallback(const net::CompletionCallback& callback, int result);
240 const net::HttpRequestInfo* request_;
241 net::HttpResponseInfo response_;
242 std::string data_;
243 int data_cursor_;
244 int test_mode_;
245 net::RequestPriority priority_;
246 CreateHelper* websocket_handshake_stream_create_helper_;
247 base::WeakPtr<MockNetworkLayer> transaction_factory_;
248 int64 received_bytes_;
250 // NetLog ID of the fake / non-existent underlying socket used by the
251 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
252 // be initialized.
253 unsigned int socket_log_id_;
255 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
259 class MockNetworkLayer : public net::HttpTransactionFactory,
260 public base::SupportsWeakPtr<MockNetworkLayer> {
261 public:
262 MockNetworkLayer();
263 virtual ~MockNetworkLayer();
265 int transaction_count() const { return transaction_count_; }
266 bool done_reading_called() const { return done_reading_called_; }
267 bool stop_caching_called() const { return stop_caching_called_; }
268 void TransactionDoneReading();
269 void TransactionStopCaching();
271 // Returns the last priority passed to CreateTransaction, or
272 // DEFAULT_PRIORITY if it hasn't been called yet.
273 net::RequestPriority last_create_transaction_priority() const {
274 return last_create_transaction_priority_;
277 // Returns the last transaction created by
278 // CreateTransaction. Returns a NULL WeakPtr if one has not been
279 // created yet, or the last transaction has been destroyed, or
280 // ClearLastTransaction() has been called and a new transaction
281 // hasn't been created yet.
282 base::WeakPtr<MockNetworkTransaction> last_transaction() {
283 return last_transaction_;
286 // Makes last_transaction() return NULL until the next transaction
287 // is created.
288 void ClearLastTransaction() {
289 last_transaction_.reset();
292 // net::HttpTransactionFactory:
293 virtual int CreateTransaction(
294 net::RequestPriority priority,
295 scoped_ptr<net::HttpTransaction>* trans) OVERRIDE;
296 virtual net::HttpCache* GetCache() OVERRIDE;
297 virtual net::HttpNetworkSession* GetSession() OVERRIDE;
299 private:
300 int transaction_count_;
301 bool done_reading_called_;
302 bool stop_caching_called_;
303 net::RequestPriority last_create_transaction_priority_;
304 base::WeakPtr<MockNetworkTransaction> last_transaction_;
307 //-----------------------------------------------------------------------------
308 // helpers
310 // read the transaction completely
311 int ReadTransaction(net::HttpTransaction* trans, std::string* result);
313 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_