Add a function to create a bookmark app from a WebApplicationInfo.
[chromium-blink-merge.git] / net / http / http_transaction_unittest.h
blob5116f6585b23d864e16cb8a5a2a2c67634f5fc31
1 // Copyright (c) 2012 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)
49 typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request,
50 std::string* response_status,
51 std::string* response_headers,
52 std::string* response_data);
54 struct MockTransaction {
55 const char* url;
56 const char* method;
57 // If |request_time| is unspecified, the current time will be used.
58 base::Time request_time;
59 const char* request_headers;
60 int load_flags;
61 const char* status;
62 const char* response_headers;
63 // If |response_time| is unspecified, the current time will be used.
64 base::Time response_time;
65 const char* data;
66 int test_mode;
67 MockTransactionHandler handler;
68 net::CertStatus cert_status;
69 // Value returned by MockNetworkTransaction::Start (potentially
70 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
71 net::Error return_code;
74 extern const MockTransaction kSimpleGET_Transaction;
75 extern const MockTransaction kSimplePOST_Transaction;
76 extern const MockTransaction kTypicalGET_Transaction;
77 extern const MockTransaction kETagGET_Transaction;
78 extern const MockTransaction kRangeGET_Transaction;
80 // returns the mock transaction for the given URL
81 const MockTransaction* FindMockTransaction(const GURL& url);
83 // Add/Remove a mock transaction that can be accessed via FindMockTransaction.
84 // There can be only one MockTransaction associated with a given URL.
85 void AddMockTransaction(const MockTransaction* trans);
86 void RemoveMockTransaction(const MockTransaction* trans);
88 struct ScopedMockTransaction : MockTransaction {
89 ScopedMockTransaction() {
90 AddMockTransaction(this);
92 explicit ScopedMockTransaction(const MockTransaction& t)
93 : MockTransaction(t) {
94 AddMockTransaction(this);
96 ~ScopedMockTransaction() {
97 RemoveMockTransaction(this);
101 //-----------------------------------------------------------------------------
102 // mock http request
104 class MockHttpRequest : public net::HttpRequestInfo {
105 public:
106 explicit MockHttpRequest(const MockTransaction& t);
109 //-----------------------------------------------------------------------------
110 // use this class to test completely consuming a transaction
112 class TestTransactionConsumer {
113 public:
114 TestTransactionConsumer(net::RequestPriority priority,
115 net::HttpTransactionFactory* factory);
116 virtual ~TestTransactionConsumer();
118 void Start(const net::HttpRequestInfo* request,
119 const net::BoundNetLog& net_log);
121 bool is_done() const { return state_ == DONE; }
122 int error() const { return error_; }
124 const net::HttpResponseInfo* response_info() const {
125 return trans_->GetResponseInfo();
127 const std::string& content() const { return content_; }
129 private:
130 enum State {
131 IDLE,
132 STARTING,
133 READING,
134 DONE
137 void DidStart(int result);
138 void DidRead(int result);
139 void DidFinish(int result);
140 void Read();
142 void OnIOComplete(int result);
144 State state_;
145 scoped_ptr<net::HttpTransaction> trans_;
146 std::string content_;
147 scoped_refptr<net::IOBuffer> read_buf_;
148 int error_;
150 static int quit_counter_;
153 //-----------------------------------------------------------------------------
154 // mock network layer
156 class MockNetworkLayer;
158 // This transaction class inspects the available set of mock transactions to
159 // find data for the request URL. It supports IO operations that complete
160 // synchronously or asynchronously to help exercise different code paths in the
161 // HttpCache implementation.
162 class MockNetworkTransaction
163 : public net::HttpTransaction,
164 public base::SupportsWeakPtr<MockNetworkTransaction> {
165 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
166 public:
167 MockNetworkTransaction(net::RequestPriority priority,
168 MockNetworkLayer* factory);
169 virtual ~MockNetworkTransaction();
171 virtual int Start(const net::HttpRequestInfo* request,
172 const net::CompletionCallback& callback,
173 const net::BoundNetLog& net_log) OVERRIDE;
175 virtual int RestartIgnoringLastError(
176 const net::CompletionCallback& callback) OVERRIDE;
178 virtual int RestartWithCertificate(
179 net::X509Certificate* client_cert,
180 const net::CompletionCallback& callback) OVERRIDE;
182 virtual int RestartWithAuth(
183 const net::AuthCredentials& credentials,
184 const net::CompletionCallback& callback) OVERRIDE;
186 virtual bool IsReadyToRestartForAuth() OVERRIDE;
188 virtual int Read(net::IOBuffer* buf, int buf_len,
189 const net::CompletionCallback& callback) OVERRIDE;
191 virtual void StopCaching() OVERRIDE;
193 virtual bool GetFullRequestHeaders(
194 net::HttpRequestHeaders* headers) const OVERRIDE;
196 virtual int64 GetTotalReceivedBytes() const OVERRIDE;
198 virtual void DoneReading() OVERRIDE;
200 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE;
202 virtual net::LoadState GetLoadState() const OVERRIDE;
204 virtual net::UploadProgress GetUploadProgress() const OVERRIDE;
206 virtual void SetQuicServerInfo(
207 net::QuicServerInfo* quic_server_info) OVERRIDE;
209 virtual bool GetLoadTimingInfo(
210 net::LoadTimingInfo* load_timing_info) const OVERRIDE;
212 virtual void SetPriority(net::RequestPriority priority) OVERRIDE;
214 virtual void SetWebSocketHandshakeStreamCreateHelper(
215 CreateHelper* create_helper) OVERRIDE;
217 virtual void SetBeforeNetworkStartCallback(
218 const BeforeNetworkStartCallback& callback) OVERRIDE;
220 virtual int ResumeNetworkStart() OVERRIDE;
222 CreateHelper* websocket_handshake_stream_create_helper() {
223 return websocket_handshake_stream_create_helper_;
225 net::RequestPriority priority() const { return priority_; }
227 private:
228 int StartInternal(const net::HttpRequestInfo* request,
229 const net::CompletionCallback& callback,
230 const net::BoundNetLog& net_log);
231 void CallbackLater(const net::CompletionCallback& callback, int result);
232 void RunCallback(const net::CompletionCallback& callback, int result);
234 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
235 const net::HttpRequestInfo* request_;
236 net::HttpResponseInfo response_;
237 std::string data_;
238 int data_cursor_;
239 int test_mode_;
240 net::RequestPriority priority_;
241 CreateHelper* websocket_handshake_stream_create_helper_;
242 base::WeakPtr<MockNetworkLayer> transaction_factory_;
243 int64 received_bytes_;
245 // NetLog ID of the fake / non-existent underlying socket used by the
246 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
247 // be initialized.
248 unsigned int socket_log_id_;
251 class MockNetworkLayer : public net::HttpTransactionFactory,
252 public base::SupportsWeakPtr<MockNetworkLayer> {
253 public:
254 MockNetworkLayer();
255 virtual ~MockNetworkLayer();
257 int transaction_count() const { return transaction_count_; }
258 bool done_reading_called() const { return done_reading_called_; }
259 bool stop_caching_called() const { return stop_caching_called_; }
260 void TransactionDoneReading();
261 void TransactionStopCaching();
263 // Returns the last priority passed to CreateTransaction, or
264 // DEFAULT_PRIORITY if it hasn't been called yet.
265 net::RequestPriority last_create_transaction_priority() const {
266 return last_create_transaction_priority_;
269 // Returns the last transaction created by
270 // CreateTransaction. Returns a NULL WeakPtr if one has not been
271 // created yet, or the last transaction has been destroyed, or
272 // ClearLastTransaction() has been called and a new transaction
273 // hasn't been created yet.
274 base::WeakPtr<MockNetworkTransaction> last_transaction() {
275 return last_transaction_;
278 // Makes last_transaction() return NULL until the next transaction
279 // is created.
280 void ClearLastTransaction() {
281 last_transaction_.reset();
284 // net::HttpTransactionFactory:
285 virtual int CreateTransaction(
286 net::RequestPriority priority,
287 scoped_ptr<net::HttpTransaction>* trans) OVERRIDE;
288 virtual net::HttpCache* GetCache() OVERRIDE;
289 virtual net::HttpNetworkSession* GetSession() OVERRIDE;
291 private:
292 int transaction_count_;
293 bool done_reading_called_;
294 bool stop_caching_called_;
295 net::RequestPriority last_create_transaction_priority_;
296 base::WeakPtr<MockNetworkTransaction> last_transaction_;
299 //-----------------------------------------------------------------------------
300 // helpers
302 // read the transaction completely
303 int ReadTransaction(net::HttpTransaction* trans, std::string* result);
305 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_