Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / http / http_transaction_test_util.h
blobe63e433341656ce6a55444078ecbe592a742b895
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/request_priority.h"
20 #include "net/base/test_completion_callback.h"
21 #include "net/disk_cache/disk_cache.h"
22 #include "net/http/http_cache.h"
23 #include "net/http/http_request_info.h"
24 #include "net/http/http_response_headers.h"
25 #include "net/http/http_response_info.h"
26 #include "net/log/net_log.h"
28 namespace net {
30 class HttpRequestHeaders;
31 class IOBuffer;
32 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 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 CertStatus cert_status;
71 // Value returned by MockNetworkTransaction::Start (potentially
72 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
73 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 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(RequestPriority priority,
117 HttpTransactionFactory* factory);
118 virtual ~TestTransactionConsumer();
120 void Start(const HttpRequestInfo* request, const BoundNetLog& net_log);
122 bool is_done() const { return state_ == DONE; }
123 int error() const { return error_; }
125 const 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<HttpTransaction> trans_;
147 std::string content_;
148 scoped_refptr<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 HttpTransaction,
165 public base::SupportsWeakPtr<MockNetworkTransaction> {
166 typedef WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
168 public:
169 MockNetworkTransaction(RequestPriority priority, MockNetworkLayer* factory);
170 ~MockNetworkTransaction() override;
172 int Start(const HttpRequestInfo* request,
173 const CompletionCallback& callback,
174 const BoundNetLog& net_log) override;
176 int RestartIgnoringLastError(const CompletionCallback& callback) override;
178 int RestartWithCertificate(X509Certificate* client_cert,
179 const CompletionCallback& callback) override;
181 int RestartWithAuth(const AuthCredentials& credentials,
182 const CompletionCallback& callback) override;
184 bool IsReadyToRestartForAuth() override;
186 int Read(IOBuffer* buf,
187 int buf_len,
188 const CompletionCallback& callback) override;
190 void StopCaching() override;
192 bool GetFullRequestHeaders(HttpRequestHeaders* headers) const override;
194 int64 GetTotalReceivedBytes() const override;
196 void DoneReading() override;
198 const HttpResponseInfo* GetResponseInfo() const override;
200 LoadState GetLoadState() const override;
202 UploadProgress GetUploadProgress() const override;
204 void SetQuicServerInfo(QuicServerInfo* quic_server_info) override;
206 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
208 void SetPriority(RequestPriority priority) override;
210 void SetWebSocketHandshakeStreamCreateHelper(
211 CreateHelper* create_helper) override;
213 void SetBeforeNetworkStartCallback(
214 const BeforeNetworkStartCallback& callback) override;
216 void SetBeforeProxyHeadersSentCallback(
217 const BeforeProxyHeadersSentCallback& callback) override;
219 int ResumeNetworkStart() override;
221 CreateHelper* websocket_handshake_stream_create_helper() {
222 return websocket_handshake_stream_create_helper_;
224 RequestPriority priority() const { return priority_; }
225 const HttpRequestInfo* request() const { return request_; }
227 private:
228 int StartInternal(const HttpRequestInfo* request,
229 const CompletionCallback& callback,
230 const BoundNetLog& net_log);
231 void CallbackLater(const CompletionCallback& callback, int result);
232 void RunCallback(const CompletionCallback& callback, int result);
234 const HttpRequestInfo* request_;
235 HttpResponseInfo response_;
236 std::string data_;
237 int data_cursor_;
238 int test_mode_;
239 RequestPriority priority_;
240 CreateHelper* websocket_handshake_stream_create_helper_;
241 base::WeakPtr<MockNetworkLayer> transaction_factory_;
242 int64 received_bytes_;
244 // NetLog ID of the fake / non-existent underlying socket used by the
245 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
246 // be initialized.
247 unsigned int socket_log_id_;
249 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
253 class MockNetworkLayer : public HttpTransactionFactory,
254 public base::SupportsWeakPtr<MockNetworkLayer> {
255 public:
256 MockNetworkLayer();
257 ~MockNetworkLayer() override;
259 int transaction_count() const { return transaction_count_; }
260 bool done_reading_called() const { return done_reading_called_; }
261 bool stop_caching_called() const { return stop_caching_called_; }
262 void TransactionDoneReading();
263 void TransactionStopCaching();
265 // Returns the last priority passed to CreateTransaction, or
266 // DEFAULT_PRIORITY if it hasn't been called yet.
267 RequestPriority last_create_transaction_priority() const {
268 return last_create_transaction_priority_;
271 // Returns the last transaction created by
272 // CreateTransaction. Returns a NULL WeakPtr if one has not been
273 // created yet, or the last transaction has been destroyed, or
274 // ClearLastTransaction() has been called and a new transaction
275 // hasn't been created yet.
276 base::WeakPtr<MockNetworkTransaction> last_transaction() {
277 return last_transaction_;
280 // Makes last_transaction() return NULL until the next transaction
281 // is created.
282 void ClearLastTransaction() {
283 last_transaction_.reset();
286 // HttpTransactionFactory:
287 int CreateTransaction(RequestPriority priority,
288 scoped_ptr<HttpTransaction>* trans) override;
289 HttpCache* GetCache() override;
290 HttpNetworkSession* GetSession() override;
292 // The caller must guarantee that |clock| will outlive this object.
293 void SetClock(base::Clock* clock);
294 base::Clock* clock() const { return clock_; }
296 // The current time (will use clock_ if it is non NULL).
297 base::Time Now();
299 private:
300 int transaction_count_;
301 bool done_reading_called_;
302 bool stop_caching_called_;
303 RequestPriority last_create_transaction_priority_;
305 // By default clock_ is NULL but it can be set to a custom clock by test
306 // frameworks using SetClock.
307 base::Clock* clock_;
309 base::WeakPtr<MockNetworkTransaction> last_transaction_;
312 //-----------------------------------------------------------------------------
313 // helpers
315 // read the transaction completely
316 int ReadTransaction(HttpTransaction* trans, std::string* result);
318 } // namespace net
320 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_