Upgrade ReadPixels to ES3 semantic in command buffer.
[chromium-blink-merge.git] / net / http / http_transaction_test_util.h
blob52b38e845decee9b81b9ba93d0b43645f09b09d7
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"
27 #include "net/socket/connection_attempts.h"
29 namespace net {
31 class HttpRequestHeaders;
32 class IOBuffer;
33 class X509Certificate;
34 struct HttpRequestInfo;
36 //-----------------------------------------------------------------------------
37 // mock transaction data
39 // these flags may be combined to form the test_mode field
40 enum {
41 TEST_MODE_NORMAL = 0,
42 TEST_MODE_SYNC_NET_START = 1 << 0,
43 TEST_MODE_SYNC_NET_READ = 1 << 1,
44 TEST_MODE_SYNC_CACHE_START = 1 << 2,
45 TEST_MODE_SYNC_CACHE_READ = 1 << 3,
46 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4,
47 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ |
48 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ |
49 TEST_MODE_SYNC_CACHE_WRITE),
50 TEST_MODE_SLOW_READ = 1 << 5
53 typedef void (*MockTransactionHandler)(const HttpRequestInfo* request,
54 std::string* response_status,
55 std::string* response_headers,
56 std::string* response_data);
58 struct MockTransaction {
59 const char* url;
60 const char* method;
61 // If |request_time| is unspecified, the current time will be used.
62 base::Time request_time;
63 const char* request_headers;
64 int load_flags;
65 const char* status;
66 const char* response_headers;
67 // If |response_time| is unspecified, the current time will be used.
68 base::Time response_time;
69 const char* data;
70 int test_mode;
71 MockTransactionHandler handler;
72 scoped_refptr<X509Certificate> cert;
73 CertStatus cert_status;
74 int ssl_connection_status;
75 // Value returned by MockNetworkTransaction::Start (potentially
76 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
77 Error return_code;
80 extern const MockTransaction kSimpleGET_Transaction;
81 extern const MockTransaction kSimplePOST_Transaction;
82 extern const MockTransaction kTypicalGET_Transaction;
83 extern const MockTransaction kETagGET_Transaction;
84 extern const MockTransaction kRangeGET_Transaction;
86 // returns the mock transaction for the given URL
87 const MockTransaction* FindMockTransaction(const GURL& url);
89 // Add/Remove a mock transaction that can be accessed via FindMockTransaction.
90 // There can be only one MockTransaction associated with a given URL.
91 void AddMockTransaction(const MockTransaction* trans);
92 void RemoveMockTransaction(const MockTransaction* trans);
94 struct ScopedMockTransaction : MockTransaction {
95 ScopedMockTransaction() {
96 AddMockTransaction(this);
98 explicit ScopedMockTransaction(const MockTransaction& t)
99 : MockTransaction(t) {
100 AddMockTransaction(this);
102 ~ScopedMockTransaction() {
103 RemoveMockTransaction(this);
107 //-----------------------------------------------------------------------------
108 // mock http request
110 class MockHttpRequest : public HttpRequestInfo {
111 public:
112 explicit MockHttpRequest(const MockTransaction& t);
115 //-----------------------------------------------------------------------------
116 // use this class to test completely consuming a transaction
118 class TestTransactionConsumer {
119 public:
120 TestTransactionConsumer(RequestPriority priority,
121 HttpTransactionFactory* factory);
122 virtual ~TestTransactionConsumer();
124 void Start(const HttpRequestInfo* request, const BoundNetLog& net_log);
126 bool is_done() const { return state_ == DONE; }
127 int error() const { return error_; }
129 const HttpResponseInfo* response_info() const {
130 return trans_->GetResponseInfo();
132 const std::string& content() const { return content_; }
134 private:
135 enum State {
136 IDLE,
137 STARTING,
138 READING,
139 DONE
142 void DidStart(int result);
143 void DidRead(int result);
144 void DidFinish(int result);
145 void Read();
147 void OnIOComplete(int result);
149 State state_;
150 scoped_ptr<HttpTransaction> trans_;
151 std::string content_;
152 scoped_refptr<IOBuffer> read_buf_;
153 int error_;
155 static int quit_counter_;
158 //-----------------------------------------------------------------------------
159 // mock network layer
161 class MockNetworkLayer;
163 // This transaction class inspects the available set of mock transactions to
164 // find data for the request URL. It supports IO operations that complete
165 // synchronously or asynchronously to help exercise different code paths in the
166 // HttpCache implementation.
167 class MockNetworkTransaction
168 : public HttpTransaction,
169 public base::SupportsWeakPtr<MockNetworkTransaction> {
170 typedef WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
172 public:
173 MockNetworkTransaction(RequestPriority priority, MockNetworkLayer* factory);
174 ~MockNetworkTransaction() override;
176 int Start(const HttpRequestInfo* request,
177 const CompletionCallback& callback,
178 const BoundNetLog& net_log) override;
180 int RestartIgnoringLastError(const CompletionCallback& callback) override;
182 int RestartWithCertificate(X509Certificate* client_cert,
183 const CompletionCallback& callback) override;
185 int RestartWithAuth(const AuthCredentials& credentials,
186 const CompletionCallback& callback) override;
188 bool IsReadyToRestartForAuth() override;
190 int Read(IOBuffer* buf,
191 int buf_len,
192 const CompletionCallback& callback) override;
194 void StopCaching() override;
196 bool GetFullRequestHeaders(HttpRequestHeaders* headers) const override;
198 int64 GetTotalReceivedBytes() const override;
200 void DoneReading() override;
202 const HttpResponseInfo* GetResponseInfo() const override;
204 LoadState GetLoadState() const override;
206 UploadProgress GetUploadProgress() const override;
208 void SetQuicServerInfo(QuicServerInfo* quic_server_info) override;
210 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
212 void SetPriority(RequestPriority priority) override;
214 void SetWebSocketHandshakeStreamCreateHelper(
215 CreateHelper* create_helper) override;
217 void SetBeforeNetworkStartCallback(
218 const BeforeNetworkStartCallback& callback) override;
220 void SetBeforeProxyHeadersSentCallback(
221 const BeforeProxyHeadersSentCallback& callback) override;
223 int ResumeNetworkStart() override;
225 void GetConnectionAttempts(ConnectionAttempts* out) const override;
227 CreateHelper* websocket_handshake_stream_create_helper() {
228 return websocket_handshake_stream_create_helper_;
230 RequestPriority priority() const { return priority_; }
231 const HttpRequestInfo* request() const { return request_; }
233 private:
234 int StartInternal(const HttpRequestInfo* request,
235 const CompletionCallback& callback,
236 const BoundNetLog& net_log);
237 void CallbackLater(const CompletionCallback& callback, int result);
238 void RunCallback(const CompletionCallback& callback, int result);
240 const HttpRequestInfo* request_;
241 HttpResponseInfo response_;
242 std::string data_;
243 int data_cursor_;
244 int test_mode_;
245 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 HttpTransactionFactory,
260 public base::SupportsWeakPtr<MockNetworkLayer> {
261 public:
262 MockNetworkLayer();
263 ~MockNetworkLayer() override;
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 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 // HttpTransactionFactory:
293 int CreateTransaction(RequestPriority priority,
294 scoped_ptr<HttpTransaction>* trans) override;
295 HttpCache* GetCache() override;
296 HttpNetworkSession* GetSession() override;
298 // The caller must guarantee that |clock| will outlive this object.
299 void SetClock(base::Clock* clock);
300 base::Clock* clock() const { return clock_; }
302 // The current time (will use clock_ if it is non NULL).
303 base::Time Now();
305 private:
306 int transaction_count_;
307 bool done_reading_called_;
308 bool stop_caching_called_;
309 RequestPriority last_create_transaction_priority_;
311 // By default clock_ is NULL but it can be set to a custom clock by test
312 // frameworks using SetClock.
313 base::Clock* clock_;
315 base::WeakPtr<MockNetworkTransaction> last_transaction_;
318 //-----------------------------------------------------------------------------
319 // helpers
321 // read the transaction completely
322 int ReadTransaction(HttpTransaction* trans, std::string* result);
324 } // namespace net
326 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_