Probably broke Win7 Tests (dbg)(6). http://build.chromium.org/p/chromium.win/builders...
[chromium-blink-merge.git] / net / http / http_transaction_test_util.h
blobd340fc9c05023732c0ddc4309fbae6cbbcab84b7
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;
33 //-----------------------------------------------------------------------------
34 // mock transaction data
36 // these flags may be combined to form the test_mode field
37 enum {
38 TEST_MODE_NORMAL = 0,
39 TEST_MODE_SYNC_NET_START = 1 << 0,
40 TEST_MODE_SYNC_NET_READ = 1 << 1,
41 TEST_MODE_SYNC_CACHE_START = 1 << 2,
42 TEST_MODE_SYNC_CACHE_READ = 1 << 3,
43 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4,
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 |
46 TEST_MODE_SYNC_CACHE_WRITE),
47 TEST_MODE_SLOW_READ = 1 << 5
50 typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request,
51 std::string* response_status,
52 std::string* response_headers,
53 std::string* response_data);
55 struct MockTransaction {
56 const char* url;
57 const char* method;
58 // If |request_time| is unspecified, the current time will be used.
59 base::Time request_time;
60 const char* request_headers;
61 int load_flags;
62 const char* status;
63 const char* response_headers;
64 // If |response_time| is unspecified, the current time will be used.
65 base::Time response_time;
66 const char* data;
67 int test_mode;
68 MockTransactionHandler handler;
69 net::CertStatus cert_status;
70 // Value returned by MockNetworkTransaction::Start (potentially
71 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
72 net::Error return_code;
75 extern const MockTransaction kSimpleGET_Transaction;
76 extern const MockTransaction kSimplePOST_Transaction;
77 extern const MockTransaction kTypicalGET_Transaction;
78 extern const MockTransaction kETagGET_Transaction;
79 extern const MockTransaction kRangeGET_Transaction;
81 // returns the mock transaction for the given URL
82 const MockTransaction* FindMockTransaction(const GURL& url);
84 // Add/Remove a mock transaction that can be accessed via FindMockTransaction.
85 // There can be only one MockTransaction associated with a given URL.
86 void AddMockTransaction(const MockTransaction* trans);
87 void RemoveMockTransaction(const MockTransaction* trans);
89 struct ScopedMockTransaction : MockTransaction {
90 ScopedMockTransaction() {
91 AddMockTransaction(this);
93 explicit ScopedMockTransaction(const MockTransaction& t)
94 : MockTransaction(t) {
95 AddMockTransaction(this);
97 ~ScopedMockTransaction() {
98 RemoveMockTransaction(this);
102 //-----------------------------------------------------------------------------
103 // mock http request
105 class MockHttpRequest : public net::HttpRequestInfo {
106 public:
107 explicit MockHttpRequest(const MockTransaction& t);
110 //-----------------------------------------------------------------------------
111 // use this class to test completely consuming a transaction
113 class TestTransactionConsumer {
114 public:
115 TestTransactionConsumer(net::RequestPriority priority,
116 net::HttpTransactionFactory* factory);
117 virtual ~TestTransactionConsumer();
119 void Start(const net::HttpRequestInfo* request,
120 const net::BoundNetLog& net_log);
122 bool is_done() const { return state_ == DONE; }
123 int error() const { return error_; }
125 const net::HttpResponseInfo* response_info() const {
126 return trans_->GetResponseInfo();
128 const std::string& content() const { return content_; }
130 private:
131 enum State {
132 IDLE,
133 STARTING,
134 READING,
135 DONE
138 void DidStart(int result);
139 void DidRead(int result);
140 void DidFinish(int result);
141 void Read();
143 void OnIOComplete(int result);
145 State state_;
146 scoped_ptr<net::HttpTransaction> trans_;
147 std::string content_;
148 scoped_refptr<net::IOBuffer> read_buf_;
149 int error_;
151 static int quit_counter_;
154 //-----------------------------------------------------------------------------
155 // mock network layer
157 class MockNetworkLayer;
159 // This transaction class inspects the available set of mock transactions to
160 // find data for the request URL. It supports IO operations that complete
161 // synchronously or asynchronously to help exercise different code paths in the
162 // HttpCache implementation.
163 class MockNetworkTransaction
164 : public net::HttpTransaction,
165 public base::SupportsWeakPtr<MockNetworkTransaction> {
166 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
167 public:
168 MockNetworkTransaction(net::RequestPriority priority,
169 MockNetworkLayer* factory);
170 virtual ~MockNetworkTransaction();
172 virtual int Start(const net::HttpRequestInfo* request,
173 const net::CompletionCallback& callback,
174 const net::BoundNetLog& net_log) OVERRIDE;
176 virtual int RestartIgnoringLastError(
177 const net::CompletionCallback& callback) OVERRIDE;
179 virtual int RestartWithCertificate(
180 net::X509Certificate* client_cert,
181 const net::CompletionCallback& callback) OVERRIDE;
183 virtual int RestartWithAuth(
184 const net::AuthCredentials& credentials,
185 const net::CompletionCallback& callback) OVERRIDE;
187 virtual bool IsReadyToRestartForAuth() OVERRIDE;
189 virtual int Read(net::IOBuffer* buf, int buf_len,
190 const net::CompletionCallback& callback) OVERRIDE;
192 virtual void StopCaching() OVERRIDE;
194 virtual bool GetFullRequestHeaders(
195 net::HttpRequestHeaders* headers) const OVERRIDE;
197 virtual int64 GetTotalReceivedBytes() const OVERRIDE;
199 virtual void DoneReading() OVERRIDE;
201 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE;
203 virtual net::LoadState GetLoadState() const OVERRIDE;
205 virtual net::UploadProgress GetUploadProgress() const OVERRIDE;
207 virtual void SetQuicServerInfo(
208 net::QuicServerInfo* quic_server_info) OVERRIDE;
210 virtual bool GetLoadTimingInfo(
211 net::LoadTimingInfo* load_timing_info) const OVERRIDE;
213 virtual void SetPriority(net::RequestPriority priority) OVERRIDE;
215 virtual void SetWebSocketHandshakeStreamCreateHelper(
216 CreateHelper* create_helper) OVERRIDE;
218 virtual void SetBeforeNetworkStartCallback(
219 const BeforeNetworkStartCallback& callback) OVERRIDE;
221 virtual void SetBeforeProxyHeadersSentCallback(
222 const BeforeProxyHeadersSentCallback& callback) OVERRIDE;
224 virtual int ResumeNetworkStart() OVERRIDE;
226 CreateHelper* websocket_handshake_stream_create_helper() {
227 return websocket_handshake_stream_create_helper_;
229 net::RequestPriority priority() const { return priority_; }
231 private:
232 int StartInternal(const net::HttpRequestInfo* request,
233 const net::CompletionCallback& callback,
234 const net::BoundNetLog& net_log);
235 void CallbackLater(const net::CompletionCallback& callback, int result);
236 void RunCallback(const net::CompletionCallback& callback, int result);
238 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
239 const net::HttpRequestInfo* request_;
240 net::HttpResponseInfo response_;
241 std::string data_;
242 int data_cursor_;
243 int test_mode_;
244 net::RequestPriority priority_;
245 CreateHelper* websocket_handshake_stream_create_helper_;
246 base::WeakPtr<MockNetworkLayer> transaction_factory_;
247 int64 received_bytes_;
249 // NetLog ID of the fake / non-existent underlying socket used by the
250 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
251 // be initialized.
252 unsigned int socket_log_id_;
255 class MockNetworkLayer : public net::HttpTransactionFactory,
256 public base::SupportsWeakPtr<MockNetworkLayer> {
257 public:
258 MockNetworkLayer();
259 virtual ~MockNetworkLayer();
261 int transaction_count() const { return transaction_count_; }
262 bool done_reading_called() const { return done_reading_called_; }
263 bool stop_caching_called() const { return stop_caching_called_; }
264 void TransactionDoneReading();
265 void TransactionStopCaching();
267 // Returns the last priority passed to CreateTransaction, or
268 // DEFAULT_PRIORITY if it hasn't been called yet.
269 net::RequestPriority last_create_transaction_priority() const {
270 return last_create_transaction_priority_;
273 // Returns the last transaction created by
274 // CreateTransaction. Returns a NULL WeakPtr if one has not been
275 // created yet, or the last transaction has been destroyed, or
276 // ClearLastTransaction() has been called and a new transaction
277 // hasn't been created yet.
278 base::WeakPtr<MockNetworkTransaction> last_transaction() {
279 return last_transaction_;
282 // Makes last_transaction() return NULL until the next transaction
283 // is created.
284 void ClearLastTransaction() {
285 last_transaction_.reset();
288 // net::HttpTransactionFactory:
289 virtual int CreateTransaction(
290 net::RequestPriority priority,
291 scoped_ptr<net::HttpTransaction>* trans) OVERRIDE;
292 virtual net::HttpCache* GetCache() OVERRIDE;
293 virtual net::HttpNetworkSession* GetSession() OVERRIDE;
295 private:
296 int transaction_count_;
297 bool done_reading_called_;
298 bool stop_caching_called_;
299 net::RequestPriority last_create_transaction_priority_;
300 base::WeakPtr<MockNetworkTransaction> last_transaction_;
303 //-----------------------------------------------------------------------------
304 // helpers
306 // read the transaction completely
307 int ReadTransaction(net::HttpTransaction* trans, std::string* result);
309 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_