add flag to stage improved conic tolerance
[chromium-blink-merge.git] / net / http / http_transaction_test_util.h
blobce3bb97818626edef3a16e7c840216f7d499b240
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 ~MockNetworkTransaction() override;
173 int Start(const net::HttpRequestInfo* request,
174 const net::CompletionCallback& callback,
175 const net::BoundNetLog& net_log) override;
177 int RestartIgnoringLastError(
178 const net::CompletionCallback& callback) override;
180 int RestartWithCertificate(net::X509Certificate* client_cert,
181 const net::CompletionCallback& callback) override;
183 int RestartWithAuth(const net::AuthCredentials& credentials,
184 const net::CompletionCallback& callback) override;
186 bool IsReadyToRestartForAuth() override;
188 int Read(net::IOBuffer* buf,
189 int buf_len,
190 const net::CompletionCallback& callback) override;
192 void StopCaching() override;
194 bool GetFullRequestHeaders(net::HttpRequestHeaders* headers) const override;
196 int64 GetTotalReceivedBytes() const override;
198 void DoneReading() override;
200 const net::HttpResponseInfo* GetResponseInfo() const override;
202 net::LoadState GetLoadState() const override;
204 net::UploadProgress GetUploadProgress() const override;
206 void SetQuicServerInfo(net::QuicServerInfo* quic_server_info) override;
208 bool GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
210 void SetPriority(net::RequestPriority priority) override;
212 void SetWebSocketHandshakeStreamCreateHelper(
213 CreateHelper* create_helper) override;
215 void SetBeforeNetworkStartCallback(
216 const BeforeNetworkStartCallback& callback) override;
218 void SetBeforeProxyHeadersSentCallback(
219 const BeforeProxyHeadersSentCallback& callback) override;
221 int ResumeNetworkStart() override;
223 CreateHelper* websocket_handshake_stream_create_helper() {
224 return websocket_handshake_stream_create_helper_;
226 net::RequestPriority priority() const { return priority_; }
227 const net::HttpRequestInfo* request() const { return request_; }
229 private:
230 int StartInternal(const net::HttpRequestInfo* request,
231 const net::CompletionCallback& callback,
232 const net::BoundNetLog& net_log);
233 void CallbackLater(const net::CompletionCallback& callback, int result);
234 void RunCallback(const net::CompletionCallback& callback, int result);
236 const net::HttpRequestInfo* request_;
237 net::HttpResponseInfo response_;
238 std::string data_;
239 int data_cursor_;
240 int test_mode_;
241 net::RequestPriority priority_;
242 CreateHelper* websocket_handshake_stream_create_helper_;
243 base::WeakPtr<MockNetworkLayer> transaction_factory_;
244 int64 received_bytes_;
246 // NetLog ID of the fake / non-existent underlying socket used by the
247 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
248 // be initialized.
249 unsigned int socket_log_id_;
251 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
255 class MockNetworkLayer : public net::HttpTransactionFactory,
256 public base::SupportsWeakPtr<MockNetworkLayer> {
257 public:
258 MockNetworkLayer();
259 ~MockNetworkLayer() override;
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 int CreateTransaction(net::RequestPriority priority,
290 scoped_ptr<net::HttpTransaction>* trans) override;
291 net::HttpCache* GetCache() override;
292 net::HttpNetworkSession* GetSession() override;
294 // The caller must guarantee that |clock| will outlive this object.
295 void SetClock(base::Clock* clock);
296 base::Clock* clock() const { return clock_; }
298 // The current time (will use clock_ if it is non NULL).
299 base::Time Now();
301 private:
302 int transaction_count_;
303 bool done_reading_called_;
304 bool stop_caching_called_;
305 net::RequestPriority last_create_transaction_priority_;
307 // By default clock_ is NULL but it can be set to a custom clock by test
308 // frameworks using SetClock.
309 base::Clock* clock_;
311 base::WeakPtr<MockNetworkTransaction> last_transaction_;
314 //-----------------------------------------------------------------------------
315 // helpers
317 // read the transaction completely
318 int ReadTransaction(net::HttpTransaction* trans, std::string* result);
320 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_