OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_transaction_unittest.h" | 5 #include "net/http/http_transaction_unittest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace { | 23 namespace { |
24 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; | 24 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; |
25 static MockTransactionMap mock_transactions; | 25 static MockTransactionMap mock_transactions; |
26 } // namespace | 26 } // namespace |
27 | 27 |
28 //----------------------------------------------------------------------------- | 28 //----------------------------------------------------------------------------- |
29 // mock transaction data | 29 // mock transaction data |
30 | 30 |
31 const MockTransaction kSimpleGET_Transaction = { | 31 const MockTransaction kSimpleGET_Transaction = { |
32 "http://www.google.com/", | 32 "http://www.google.com/", "GET", |
33 "GET", | 33 base::Time(), "", |
34 base::Time(), | 34 net::LOAD_NORMAL, "HTTP/1.1 200 OK", |
35 "", | 35 "Cache-Control: max-age=10000\n", base::Time(), |
36 net::LOAD_NORMAL, | 36 "<html><body>Google Blah Blah</body></html>", TEST_MODE_NORMAL, |
37 "HTTP/1.1 200 OK", | 37 NULL, 0, |
38 "Cache-Control: max-age=10000\n", | 38 net::OK}; |
39 base::Time(), | |
40 "<html><body>Google Blah Blah</body></html>", | |
41 TEST_MODE_NORMAL, | |
42 NULL, | |
43 0, | |
44 net::OK | |
45 }; | |
46 | 39 |
47 const MockTransaction kSimplePOST_Transaction = { | 40 const MockTransaction kSimplePOST_Transaction = { |
48 "http://bugdatabase.com/edit", | 41 "http://bugdatabase.com/edit", "POST", |
49 "POST", | 42 base::Time(), "", |
50 base::Time(), | 43 net::LOAD_NORMAL, "HTTP/1.1 200 OK", |
51 "", | 44 "", base::Time(), |
52 net::LOAD_NORMAL, | 45 "<html><body>Google Blah Blah</body></html>", TEST_MODE_NORMAL, |
53 "HTTP/1.1 200 OK", | 46 NULL, 0, |
54 "", | 47 net::OK}; |
55 base::Time(), | |
56 "<html><body>Google Blah Blah</body></html>", | |
57 TEST_MODE_NORMAL, | |
58 NULL, | |
59 0, | |
60 net::OK | |
61 }; | |
62 | 48 |
63 const MockTransaction kTypicalGET_Transaction = { | 49 const MockTransaction kTypicalGET_Transaction = { |
64 "http://www.example.com/~foo/bar.html", | 50 "http://www.example.com/~foo/bar.html", "GET", base::Time(), "", |
65 "GET", | 51 net::LOAD_NORMAL, "HTTP/1.1 200 OK", |
66 base::Time(), | 52 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
67 "", | 53 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", |
68 net::LOAD_NORMAL, | 54 base::Time(), "<html><body>Google Blah Blah</body></html>", |
69 "HTTP/1.1 200 OK", | 55 TEST_MODE_NORMAL, NULL, 0, net::OK}; |
70 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | |
71 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", | |
72 base::Time(), | |
73 "<html><body>Google Blah Blah</body></html>", | |
74 TEST_MODE_NORMAL, | |
75 NULL, | |
76 0, | |
77 net::OK | |
78 }; | |
79 | 56 |
80 const MockTransaction kETagGET_Transaction = { | 57 const MockTransaction kETagGET_Transaction = { |
81 "http://www.google.com/foopy", | 58 "http://www.google.com/foopy", "GET", base::Time(), "", net::LOAD_NORMAL, |
82 "GET", | 59 "HTTP/1.1 200 OK", |
83 base::Time(), | 60 "Cache-Control: max-age=10000\n" |
84 "", | 61 "Etag: \"foopy\"\n", |
85 net::LOAD_NORMAL, | 62 base::Time(), "<html><body>Google Blah Blah</body></html>", |
86 "HTTP/1.1 200 OK", | 63 TEST_MODE_NORMAL, NULL, 0, net::OK}; |
87 "Cache-Control: max-age=10000\n" | |
88 "Etag: \"foopy\"\n", | |
89 base::Time(), | |
90 "<html><body>Google Blah Blah</body></html>", | |
91 TEST_MODE_NORMAL, | |
92 NULL, | |
93 0, | |
94 net::OK | |
95 }; | |
96 | 64 |
97 const MockTransaction kRangeGET_Transaction = { | 65 const MockTransaction kRangeGET_Transaction = { |
98 "http://www.google.com/", | 66 "http://www.google.com/", "GET", |
99 "GET", | 67 base::Time(), "Range: 0-100\r\n", |
100 base::Time(), | 68 net::LOAD_NORMAL, "HTTP/1.1 200 OK", |
101 "Range: 0-100\r\n", | 69 "Cache-Control: max-age=10000\n", base::Time(), |
102 net::LOAD_NORMAL, | 70 "<html><body>Google Blah Blah</body></html>", TEST_MODE_NORMAL, |
103 "HTTP/1.1 200 OK", | 71 NULL, 0, |
104 "Cache-Control: max-age=10000\n", | 72 net::OK}; |
105 base::Time(), | |
106 "<html><body>Google Blah Blah</body></html>", | |
107 TEST_MODE_NORMAL, | |
108 NULL, | |
109 0, | |
110 net::OK | |
111 }; | |
112 | 73 |
113 static const MockTransaction* const kBuiltinMockTransactions[] = { | 74 static const MockTransaction* const kBuiltinMockTransactions[] = { |
114 &kSimpleGET_Transaction, | 75 &kSimpleGET_Transaction, &kSimplePOST_Transaction, &kTypicalGET_Transaction, |
115 &kSimplePOST_Transaction, | 76 &kETagGET_Transaction, &kRangeGET_Transaction}; |
116 &kTypicalGET_Transaction, | |
117 &kETagGET_Transaction, | |
118 &kRangeGET_Transaction | |
119 }; | |
120 | 77 |
121 const MockTransaction* FindMockTransaction(const GURL& url) { | 78 const MockTransaction* FindMockTransaction(const GURL& url) { |
122 // look for overrides: | 79 // look for overrides: |
123 MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); | 80 MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); |
124 if (it != mock_transactions.end()) | 81 if (it != mock_transactions.end()) |
125 return it->second; | 82 return it->second; |
126 | 83 |
127 // look for builtins: | 84 // look for builtins: |
128 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { | 85 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { |
129 if (url == GURL(kBuiltinMockTransactions[i]->url)) | 86 if (url == GURL(kBuiltinMockTransactions[i]->url)) |
(...skipping 30 matching lines...) Expand all Loading... |
160 factory->CreateTransaction(priority, &trans_); | 117 factory->CreateTransaction(priority, &trans_); |
161 ++quit_counter_; | 118 ++quit_counter_; |
162 } | 119 } |
163 | 120 |
164 TestTransactionConsumer::~TestTransactionConsumer() { | 121 TestTransactionConsumer::~TestTransactionConsumer() { |
165 } | 122 } |
166 | 123 |
167 void TestTransactionConsumer::Start(const net::HttpRequestInfo* request, | 124 void TestTransactionConsumer::Start(const net::HttpRequestInfo* request, |
168 const net::BoundNetLog& net_log) { | 125 const net::BoundNetLog& net_log) { |
169 state_ = STARTING; | 126 state_ = STARTING; |
170 int result = trans_->Start( | 127 int result = trans_->Start(request, |
171 request, base::Bind(&TestTransactionConsumer::OnIOComplete, | 128 base::Bind(&TestTransactionConsumer::OnIOComplete, |
172 base::Unretained(this)), net_log); | 129 base::Unretained(this)), |
| 130 net_log); |
173 if (result != net::ERR_IO_PENDING) | 131 if (result != net::ERR_IO_PENDING) |
174 DidStart(result); | 132 DidStart(result); |
175 } | 133 } |
176 | 134 |
177 void TestTransactionConsumer::DidStart(int result) { | 135 void TestTransactionConsumer::DidStart(int result) { |
178 if (result != net::OK) { | 136 if (result != net::OK) { |
179 DidFinish(result); | 137 DidFinish(result); |
180 } else { | 138 } else { |
181 Read(); | 139 Read(); |
182 } | 140 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 DidStart(result); | 173 DidStart(result); |
216 break; | 174 break; |
217 case READING: | 175 case READING: |
218 DidRead(result); | 176 DidRead(result); |
219 break; | 177 break; |
220 default: | 178 default: |
221 NOTREACHED(); | 179 NOTREACHED(); |
222 } | 180 } |
223 } | 181 } |
224 | 182 |
225 MockNetworkTransaction::MockNetworkTransaction( | 183 MockNetworkTransaction::MockNetworkTransaction(net::RequestPriority priority, |
226 net::RequestPriority priority, | 184 MockNetworkLayer* factory) |
227 MockNetworkLayer* factory) | |
228 : weak_factory_(this), | 185 : weak_factory_(this), |
229 request_(NULL), | 186 request_(NULL), |
230 data_cursor_(0), | 187 data_cursor_(0), |
231 priority_(priority), | 188 priority_(priority), |
232 websocket_handshake_stream_create_helper_(NULL), | 189 websocket_handshake_stream_create_helper_(NULL), |
233 transaction_factory_(factory->AsWeakPtr()), | 190 transaction_factory_(factory->AsWeakPtr()), |
234 received_bytes_(0), | 191 received_bytes_(0), |
235 socket_log_id_(net::NetLog::Source::kInvalidId) { | 192 socket_log_id_(net::NetLog::Source::kInvalidId) { |
236 } | 193 } |
237 | 194 |
238 MockNetworkTransaction::~MockNetworkTransaction() {} | 195 MockNetworkTransaction::~MockNetworkTransaction() { |
| 196 } |
239 | 197 |
240 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, | 198 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, |
241 const net::CompletionCallback& callback, | 199 const net::CompletionCallback& callback, |
242 const net::BoundNetLog& net_log) { | 200 const net::BoundNetLog& net_log) { |
243 if (request_) | 201 if (request_) |
244 return net::ERR_FAILED; | 202 return net::ERR_FAILED; |
245 | 203 |
246 request_ = request; | 204 request_ = request; |
247 return StartInternal(request, callback, net_log); | 205 return StartInternal(request, callback, net_log); |
248 } | 206 } |
(...skipping 25 matching lines...) Expand all Loading... |
274 } | 232 } |
275 | 233 |
276 bool MockNetworkTransaction::IsReadyToRestartForAuth() { | 234 bool MockNetworkTransaction::IsReadyToRestartForAuth() { |
277 if (!request_) | 235 if (!request_) |
278 return false; | 236 return false; |
279 | 237 |
280 // Only mock auth when the test asks for it. | 238 // Only mock auth when the test asks for it. |
281 return request_->extra_headers.HasHeader("X-Require-Mock-Auth"); | 239 return request_->extra_headers.HasHeader("X-Require-Mock-Auth"); |
282 } | 240 } |
283 | 241 |
284 int MockNetworkTransaction::Read(net::IOBuffer* buf, int buf_len, | 242 int MockNetworkTransaction::Read(net::IOBuffer* buf, |
| 243 int buf_len, |
285 const net::CompletionCallback& callback) { | 244 const net::CompletionCallback& callback) { |
286 int data_len = static_cast<int>(data_.size()); | 245 int data_len = static_cast<int>(data_.size()); |
287 int num = std::min(buf_len, data_len - data_cursor_); | 246 int num = std::min(buf_len, data_len - data_cursor_); |
288 if (test_mode_ & TEST_MODE_SLOW_READ) | 247 if (test_mode_ & TEST_MODE_SLOW_READ) |
289 num = std::min(num, 1); | 248 num = std::min(num, 1); |
290 if (num) { | 249 if (num) { |
291 memcpy(buf->data(), data_.data() + data_cursor_, num); | 250 memcpy(buf->data(), data_.data() + data_cursor_, num); |
292 data_cursor_ += num; | 251 data_cursor_ += num; |
293 } | 252 } |
294 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | 253 if (test_mode_ & TEST_MODE_SYNC_NET_READ) |
(...skipping 30 matching lines...) Expand all Loading... |
325 if (data_cursor_) | 284 if (data_cursor_) |
326 return net::LOAD_STATE_READING_RESPONSE; | 285 return net::LOAD_STATE_READING_RESPONSE; |
327 return net::LOAD_STATE_IDLE; | 286 return net::LOAD_STATE_IDLE; |
328 } | 287 } |
329 | 288 |
330 net::UploadProgress MockNetworkTransaction::GetUploadProgress() const { | 289 net::UploadProgress MockNetworkTransaction::GetUploadProgress() const { |
331 return net::UploadProgress(); | 290 return net::UploadProgress(); |
332 } | 291 } |
333 | 292 |
334 void MockNetworkTransaction::SetQuicServerInfo( | 293 void MockNetworkTransaction::SetQuicServerInfo( |
335 net::QuicServerInfo* quic_server_info) {} | 294 net::QuicServerInfo* quic_server_info) { |
| 295 } |
336 | 296 |
337 bool MockNetworkTransaction::GetLoadTimingInfo( | 297 bool MockNetworkTransaction::GetLoadTimingInfo( |
338 net::LoadTimingInfo* load_timing_info) const { | 298 net::LoadTimingInfo* load_timing_info) const { |
339 if (socket_log_id_ != net::NetLog::Source::kInvalidId) { | 299 if (socket_log_id_ != net::NetLog::Source::kInvalidId) { |
340 // The minimal set of times for a request that gets a response, assuming it | 300 // The minimal set of times for a request that gets a response, assuming it |
341 // gets a new socket. | 301 // gets a new socket. |
342 load_timing_info->socket_reused = false; | 302 load_timing_info->socket_reused = false; |
343 load_timing_info->socket_log_id = socket_log_id_; | 303 load_timing_info->socket_log_id = socket_log_id_; |
344 load_timing_info->connect_timing.connect_start = base::TimeTicks::Now(); | 304 load_timing_info->connect_timing.connect_start = base::TimeTicks::Now(); |
345 load_timing_info->connect_timing.connect_end = base::TimeTicks::Now(); | 305 load_timing_info->connect_timing.connect_end = base::TimeTicks::Now(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 return net::ERR_IO_PENDING; | 343 return net::ERR_IO_PENDING; |
384 } | 344 } |
385 | 345 |
386 std::string resp_status = t->status; | 346 std::string resp_status = t->status; |
387 std::string resp_headers = t->response_headers; | 347 std::string resp_headers = t->response_headers; |
388 std::string resp_data = t->data; | 348 std::string resp_data = t->data; |
389 received_bytes_ = resp_status.size() + resp_headers.size() + resp_data.size(); | 349 received_bytes_ = resp_status.size() + resp_headers.size() + resp_data.size(); |
390 if (t->handler) | 350 if (t->handler) |
391 (t->handler)(request, &resp_status, &resp_headers, &resp_data); | 351 (t->handler)(request, &resp_status, &resp_headers, &resp_data); |
392 | 352 |
393 std::string header_data = base::StringPrintf( | 353 std::string header_data = |
394 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); | 354 base::StringPrintf("%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); |
395 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); | 355 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); |
396 | 356 |
397 response_.request_time = base::Time::Now(); | 357 response_.request_time = base::Time::Now(); |
398 if (!t->request_time.is_null()) | 358 if (!t->request_time.is_null()) |
399 response_.request_time = t->request_time; | 359 response_.request_time = t->request_time; |
400 | 360 |
401 response_.was_cached = false; | 361 response_.was_cached = false; |
402 response_.network_accessed = true; | 362 response_.network_accessed = true; |
403 | 363 |
404 response_.response_time = base::Time::Now(); | 364 response_.response_time = base::Time::Now(); |
(...skipping 18 matching lines...) Expand all Loading... |
423 void MockNetworkTransaction::SetBeforeNetworkStartCallback( | 383 void MockNetworkTransaction::SetBeforeNetworkStartCallback( |
424 const BeforeNetworkStartCallback& callback) { | 384 const BeforeNetworkStartCallback& callback) { |
425 } | 385 } |
426 | 386 |
427 int MockNetworkTransaction::ResumeNetworkStart() { | 387 int MockNetworkTransaction::ResumeNetworkStart() { |
428 // Should not get here. | 388 // Should not get here. |
429 return net::ERR_FAILED; | 389 return net::ERR_FAILED; |
430 } | 390 } |
431 | 391 |
432 void MockNetworkTransaction::CallbackLater( | 392 void MockNetworkTransaction::CallbackLater( |
433 const net::CompletionCallback& callback, int result) { | 393 const net::CompletionCallback& callback, |
| 394 int result) { |
434 base::MessageLoop::current()->PostTask( | 395 base::MessageLoop::current()->PostTask( |
435 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, | 396 FROM_HERE, |
436 weak_factory_.GetWeakPtr(), callback, result)); | 397 base::Bind(&MockNetworkTransaction::RunCallback, |
| 398 weak_factory_.GetWeakPtr(), |
| 399 callback, |
| 400 result)); |
437 } | 401 } |
438 | 402 |
439 void MockNetworkTransaction::RunCallback( | 403 void MockNetworkTransaction::RunCallback( |
440 const net::CompletionCallback& callback, int result) { | 404 const net::CompletionCallback& callback, |
| 405 int result) { |
441 callback.Run(result); | 406 callback.Run(result); |
442 } | 407 } |
443 | 408 |
444 MockNetworkLayer::MockNetworkLayer() | 409 MockNetworkLayer::MockNetworkLayer() |
445 : transaction_count_(0), | 410 : transaction_count_(0), |
446 done_reading_called_(false), | 411 done_reading_called_(false), |
447 stop_caching_called_(false), | 412 stop_caching_called_(false), |
448 last_create_transaction_priority_(net::DEFAULT_PRIORITY) {} | 413 last_create_transaction_priority_(net::DEFAULT_PRIORITY) { |
| 414 } |
449 | 415 |
450 MockNetworkLayer::~MockNetworkLayer() {} | 416 MockNetworkLayer::~MockNetworkLayer() { |
| 417 } |
451 | 418 |
452 void MockNetworkLayer::TransactionDoneReading() { | 419 void MockNetworkLayer::TransactionDoneReading() { |
453 done_reading_called_ = true; | 420 done_reading_called_ = true; |
454 } | 421 } |
455 | 422 |
456 void MockNetworkLayer::TransactionStopCaching() { | 423 void MockNetworkLayer::TransactionStopCaching() { |
457 stop_caching_called_ = true; | 424 stop_caching_called_ = true; |
458 } | 425 } |
459 | 426 |
460 int MockNetworkLayer::CreateTransaction( | 427 int MockNetworkLayer::CreateTransaction( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 | 461 |
495 if (rv > 0) | 462 if (rv > 0) |
496 content.append(buf->data(), rv); | 463 content.append(buf->data(), rv); |
497 else if (rv < 0) | 464 else if (rv < 0) |
498 return rv; | 465 return rv; |
499 } while (rv > 0); | 466 } while (rv > 0); |
500 | 467 |
501 result->swap(content); | 468 result->swap(content); |
502 return net::OK; | 469 return net::OK; |
503 } | 470 } |
OLD | NEW |