OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_test_util.h" | 5 #include "net/http/http_transaction_test_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 "", | 42 "", |
43 LOAD_NORMAL, | 43 LOAD_NORMAL, |
44 "HTTP/1.1 200 OK", | 44 "HTTP/1.1 200 OK", |
45 "Cache-Control: max-age=10000\n", | 45 "Cache-Control: max-age=10000\n", |
46 base::Time(), | 46 base::Time(), |
47 "<html><body>Google Blah Blah</body></html>", | 47 "<html><body>Google Blah Blah</body></html>", |
48 TEST_MODE_NORMAL, | 48 TEST_MODE_NORMAL, |
49 nullptr, | 49 nullptr, |
50 nullptr, | 50 nullptr, |
51 0, | 51 0, |
52 0, | |
rvargas (doing something else)
2015/09/15 01:07:39
Why are you removing stuff (xx_satus) instead of a
asanka
2015/09/17 22:20:29
Wow. Done.
| |
53 OK}; | 52 OK}; |
54 | 53 |
55 const MockTransaction kSimplePOST_Transaction = { | 54 const MockTransaction kSimplePOST_Transaction = { |
56 "http://bugdatabase.com/edit", | 55 "http://bugdatabase.com/edit", |
57 "POST", | 56 "POST", |
58 base::Time(), | 57 base::Time(), |
59 "", | 58 "", |
60 LOAD_NORMAL, | 59 LOAD_NORMAL, |
61 "HTTP/1.1 200 OK", | 60 "HTTP/1.1 200 OK", |
62 "", | 61 "", |
63 base::Time(), | 62 base::Time(), |
64 "<html><body>Google Blah Blah</body></html>", | 63 "<html><body>Google Blah Blah</body></html>", |
65 TEST_MODE_NORMAL, | 64 TEST_MODE_NORMAL, |
66 nullptr, | 65 nullptr, |
67 nullptr, | 66 nullptr, |
68 0, | 67 0, |
69 0, | |
70 OK}; | 68 OK}; |
71 | 69 |
72 const MockTransaction kTypicalGET_Transaction = { | 70 const MockTransaction kTypicalGET_Transaction = { |
73 "http://www.example.com/~foo/bar.html", | 71 "http://www.example.com/~foo/bar.html", |
74 "GET", | 72 "GET", |
75 base::Time(), | 73 base::Time(), |
76 "", | 74 "", |
77 LOAD_NORMAL, | 75 LOAD_NORMAL, |
78 "HTTP/1.1 200 OK", | 76 "HTTP/1.1 200 OK", |
79 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | 77 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
80 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", | 78 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", |
81 base::Time(), | 79 base::Time(), |
82 "<html><body>Google Blah Blah</body></html>", | 80 "<html><body>Google Blah Blah</body></html>", |
83 TEST_MODE_NORMAL, | 81 TEST_MODE_NORMAL, |
84 nullptr, | 82 nullptr, |
85 nullptr, | 83 nullptr, |
86 0, | 84 0, |
87 0, | |
88 OK}; | 85 OK}; |
89 | 86 |
90 const MockTransaction kETagGET_Transaction = { | 87 const MockTransaction kETagGET_Transaction = { |
91 "http://www.google.com/foopy", | 88 "http://www.google.com/foopy", |
92 "GET", | 89 "GET", |
93 base::Time(), | 90 base::Time(), |
94 "", | 91 "", |
95 LOAD_NORMAL, | 92 LOAD_NORMAL, |
96 "HTTP/1.1 200 OK", | 93 "HTTP/1.1 200 OK", |
97 "Cache-Control: max-age=10000\n" | 94 "Cache-Control: max-age=10000\n" |
98 "Etag: \"foopy\"\n", | 95 "Etag: \"foopy\"\n", |
99 base::Time(), | 96 base::Time(), |
100 "<html><body>Google Blah Blah</body></html>", | 97 "<html><body>Google Blah Blah</body></html>", |
101 TEST_MODE_NORMAL, | 98 TEST_MODE_NORMAL, |
102 nullptr, | 99 nullptr, |
103 nullptr, | 100 nullptr, |
104 0, | 101 0, |
105 0, | |
106 OK}; | 102 OK}; |
107 | 103 |
108 const MockTransaction kRangeGET_Transaction = { | 104 const MockTransaction kRangeGET_Transaction = { |
109 "http://www.google.com/", | 105 "http://www.google.com/", |
110 "GET", | 106 "GET", |
111 base::Time(), | 107 base::Time(), |
112 "Range: 0-100\r\n", | 108 "Range: 0-100\r\n", |
113 LOAD_NORMAL, | 109 LOAD_NORMAL, |
114 "HTTP/1.1 200 OK", | 110 "HTTP/1.1 200 OK", |
115 "Cache-Control: max-age=10000\n", | 111 "Cache-Control: max-age=10000\n", |
116 base::Time(), | 112 base::Time(), |
117 "<html><body>Google Blah Blah</body></html>", | 113 "<html><body>Google Blah Blah</body></html>", |
118 TEST_MODE_NORMAL, | 114 TEST_MODE_NORMAL, |
119 nullptr, | 115 nullptr, |
120 nullptr, | 116 nullptr, |
121 0, | 117 0, |
122 0, | |
123 OK}; | 118 OK}; |
124 | 119 |
125 static const MockTransaction* const kBuiltinMockTransactions[] = { | 120 static const MockTransaction* const kBuiltinMockTransactions[] = { |
126 &kSimpleGET_Transaction, | 121 &kSimpleGET_Transaction, |
127 &kSimplePOST_Transaction, | 122 &kSimplePOST_Transaction, |
128 &kTypicalGET_Transaction, | 123 &kTypicalGET_Transaction, |
129 &kETagGET_Transaction, | 124 &kETagGET_Transaction, |
130 &kRangeGET_Transaction | 125 &kRangeGET_Transaction |
131 }; | 126 }; |
132 | 127 |
133 const MockTransaction* FindMockTransaction(const GURL& url) { | 128 const MockTransaction* FindMockTransaction(const GURL& url) { |
134 // look for overrides: | 129 // look for overrides: |
135 MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); | 130 MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); |
136 if (it != mock_transactions.end()) | 131 if (it != mock_transactions.end()) |
137 return it->second; | 132 return it->second; |
138 | 133 |
139 // look for builtins: | 134 // look for builtins: |
140 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { | 135 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { |
141 if (url == GURL(kBuiltinMockTransactions[i]->url)) | 136 if (url == GURL(kBuiltinMockTransactions[i]->url)) |
142 return kBuiltinMockTransactions[i]; | 137 return kBuiltinMockTransactions[i]; |
143 } | 138 } |
144 return NULL; | 139 return nullptr; |
145 } | 140 } |
146 | 141 |
147 void AddMockTransaction(const MockTransaction* trans) { | 142 void AddMockTransaction(const MockTransaction* trans) { |
148 mock_transactions[GURL(trans->url).spec()] = trans; | 143 mock_transactions[GURL(trans->url).spec()] = trans; |
149 } | 144 } |
150 | 145 |
151 void RemoveMockTransaction(const MockTransaction* trans) { | 146 void RemoveMockTransaction(const MockTransaction* trans) { |
152 mock_transactions.erase(GURL(trans->url).spec()); | 147 mock_transactions.erase(GURL(trans->url).spec()); |
153 } | 148 } |
154 | 149 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 case READING: | 224 case READING: |
230 DidRead(result); | 225 DidRead(result); |
231 break; | 226 break; |
232 default: | 227 default: |
233 NOTREACHED(); | 228 NOTREACHED(); |
234 } | 229 } |
235 } | 230 } |
236 | 231 |
237 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, | 232 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, |
238 MockNetworkLayer* factory) | 233 MockNetworkLayer* factory) |
239 : request_(NULL), | 234 : request_(nullptr), |
240 data_cursor_(0), | 235 data_cursor_(0), |
236 content_length_(0), | |
241 priority_(priority), | 237 priority_(priority), |
242 websocket_handshake_stream_create_helper_(NULL), | 238 read_handler_(nullptr), |
239 websocket_handshake_stream_create_helper_(nullptr), | |
243 transaction_factory_(factory->AsWeakPtr()), | 240 transaction_factory_(factory->AsWeakPtr()), |
244 received_bytes_(0), | 241 received_bytes_(0), |
245 socket_log_id_(NetLog::Source::kInvalidId), | 242 socket_log_id_(NetLog::Source::kInvalidId), |
246 weak_factory_(this) { | 243 weak_factory_(this) {} |
rvargas (doing something else)
2015/09/15 01:07:39
<rant>I hate cl format</rant>
asanka
2015/09/17 22:20:29
:)
| |
247 } | |
248 | 244 |
249 MockNetworkTransaction::~MockNetworkTransaction() {} | 245 MockNetworkTransaction::~MockNetworkTransaction() {} |
250 | 246 |
251 int MockNetworkTransaction::Start(const HttpRequestInfo* request, | 247 int MockNetworkTransaction::Start(const HttpRequestInfo* request, |
252 const CompletionCallback& callback, | 248 const CompletionCallback& callback, |
253 const BoundNetLog& net_log) { | 249 const BoundNetLog& net_log) { |
254 if (request_) | 250 if (request_) |
255 return ERR_FAILED; | 251 return ERR_FAILED; |
256 | 252 |
257 request_ = request; | 253 request_ = request; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 | 286 |
291 if (!request_->extra_headers.HasHeader("X-Require-Mock-Auth")) | 287 if (!request_->extra_headers.HasHeader("X-Require-Mock-Auth")) |
292 return false; | 288 return false; |
293 | 289 |
294 // Allow the mock server to decide whether authentication is required or not. | 290 // Allow the mock server to decide whether authentication is required or not. |
295 std::string status_line = response_.headers->GetStatusLine(); | 291 std::string status_line = response_.headers->GetStatusLine(); |
296 return status_line.find(" 401 ") != std::string::npos || | 292 return status_line.find(" 401 ") != std::string::npos || |
297 status_line.find(" 407 ") != std::string::npos; | 293 status_line.find(" 407 ") != std::string::npos; |
298 } | 294 } |
299 | 295 |
300 int MockNetworkTransaction::Read(IOBuffer* buf, | 296 int MockNetworkTransaction::Read(net::IOBuffer* buf, |
301 int buf_len, | 297 int buf_len, |
302 const CompletionCallback& callback) { | 298 const CompletionCallback& callback) { |
303 int data_len = static_cast<int>(data_.size()); | 299 int num = 0; |
304 int num = std::min(buf_len, data_len - data_cursor_); | 300 if (read_handler_) { |
305 if (test_mode_ & TEST_MODE_SLOW_READ) | 301 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); |
306 num = std::min(num, 1); | |
307 if (num) { | |
308 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
309 data_cursor_ += num; | 302 data_cursor_ += num; |
303 } else { | |
304 int data_len = static_cast<int>(data_.size()); | |
305 num = std::min(static_cast<int64>(buf_len), data_len - data_cursor_); | |
306 if (test_mode_ & TEST_MODE_SLOW_READ) | |
307 num = std::min(num, 1); | |
308 if (num) { | |
309 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
310 data_cursor_ += num; | |
311 } | |
310 } | 312 } |
311 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | 313 if (test_mode_ & TEST_MODE_SYNC_NET_READ) |
312 return num; | 314 return num; |
313 | 315 |
314 CallbackLater(callback, num); | 316 CallbackLater(callback, num); |
315 return ERR_IO_PENDING; | 317 return ERR_IO_PENDING; |
316 } | 318 } |
317 | 319 |
318 void MockNetworkTransaction::StopCaching() { | 320 void MockNetworkTransaction::StopCaching() { |
319 if (transaction_factory_.get()) | 321 if (transaction_factory_.get()) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
383 websocket_handshake_stream_create_helper_ = create_helper; | 385 websocket_handshake_stream_create_helper_ = create_helper; |
384 } | 386 } |
385 | 387 |
386 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, | 388 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, |
387 const CompletionCallback& callback, | 389 const CompletionCallback& callback, |
388 const BoundNetLog& net_log) { | 390 const BoundNetLog& net_log) { |
389 const MockTransaction* t = FindMockTransaction(request->url); | 391 const MockTransaction* t = FindMockTransaction(request->url); |
390 if (!t) | 392 if (!t) |
391 return ERR_FAILED; | 393 return ERR_FAILED; |
392 | 394 |
395 if (!before_network_start_callback_.is_null()) { | |
396 bool defer = false; | |
397 before_network_start_callback_.Run(&defer); | |
398 if (defer) | |
399 return net::ERR_IO_PENDING; | |
400 } | |
401 | |
393 test_mode_ = t->test_mode; | 402 test_mode_ = t->test_mode; |
394 | 403 |
395 // Return immediately if we're returning an error. | 404 // Return immediately if we're returning an error. |
396 if (OK != t->return_code) { | 405 if (OK != t->return_code) { |
397 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 406 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
398 return t->return_code; | 407 return t->return_code; |
399 CallbackLater(callback, t->return_code); | 408 CallbackLater(callback, t->return_code); |
400 return ERR_IO_PENDING; | 409 return ERR_IO_PENDING; |
401 } | 410 } |
402 | 411 |
403 std::string resp_status = t->status; | 412 std::string resp_status = t->status; |
404 std::string resp_headers = t->response_headers; | 413 std::string resp_headers = t->response_headers; |
405 std::string resp_data = t->data; | 414 std::string resp_data = t->data; |
406 received_bytes_ = resp_status.size() + resp_headers.size() + resp_data.size(); | 415 received_bytes_ = resp_status.size() + resp_headers.size() + resp_data.size(); |
407 if (t->handler) | 416 if (t->handler) |
408 (t->handler)(request, &resp_status, &resp_headers, &resp_data); | 417 (t->handler)(request, &resp_status, &resp_headers, &resp_data); |
418 if (t->read_handler) | |
419 read_handler_ = t->read_handler; | |
409 | 420 |
410 std::string header_data = base::StringPrintf( | 421 std::string header_data = base::StringPrintf( |
411 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); | 422 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); |
412 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); | 423 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); |
413 | 424 |
414 response_.request_time = transaction_factory_->Now(); | 425 response_.request_time = transaction_factory_->Now(); |
415 if (!t->request_time.is_null()) | 426 if (!t->request_time.is_null()) |
416 response_.request_time = t->request_time; | 427 response_.request_time = t->request_time; |
417 | 428 |
418 response_.was_cached = false; | 429 response_.was_cached = false; |
419 response_.network_accessed = true; | 430 response_.network_accessed = true; |
420 | 431 |
421 response_.response_time = transaction_factory_->Now(); | 432 response_.response_time = transaction_factory_->Now(); |
422 if (!t->response_time.is_null()) | 433 if (!t->response_time.is_null()) |
423 response_.response_time = t->response_time; | 434 response_.response_time = t->response_time; |
424 | 435 |
425 response_.headers = new HttpResponseHeaders(header_data); | 436 response_.headers = new HttpResponseHeaders(header_data); |
426 response_.vary_data.Init(*request, *response_.headers.get()); | 437 response_.vary_data.Init(*request, *response_.headers.get()); |
427 response_.ssl_info.cert = t->cert; | 438 response_.ssl_info.cert = t->cert; |
428 response_.ssl_info.cert_status = t->cert_status; | 439 response_.ssl_info.cert_status = t->cert_status; |
429 response_.ssl_info.connection_status = t->ssl_connection_status; | 440 response_.ssl_info.connection_status = t->ssl_connection_status; |
430 data_ = resp_data; | 441 data_ = resp_data; |
442 content_length_ = response_.headers->GetContentLength(); | |
431 | 443 |
432 if (net_log.net_log()) | 444 if (net_log.net_log()) |
433 socket_log_id_ = net_log.net_log()->NextID(); | 445 socket_log_id_ = net_log.net_log()->NextID(); |
434 | 446 |
435 if (request_->load_flags & LOAD_PREFETCH) | 447 if (request_->load_flags & LOAD_PREFETCH) |
436 response_.unused_since_prefetch = true; | 448 response_.unused_since_prefetch = true; |
437 | 449 |
438 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 450 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
439 return OK; | 451 return OK; |
440 | 452 |
441 CallbackLater(callback, OK); | 453 CallbackLater(callback, OK); |
442 return ERR_IO_PENDING; | 454 return ERR_IO_PENDING; |
443 } | 455 } |
444 | 456 |
445 void MockNetworkTransaction::SetBeforeNetworkStartCallback( | 457 void MockNetworkTransaction::SetBeforeNetworkStartCallback( |
446 const BeforeNetworkStartCallback& callback) { | 458 const BeforeNetworkStartCallback& callback) { |
459 before_network_start_callback_ = callback; | |
447 } | 460 } |
448 | 461 |
449 void MockNetworkTransaction::SetBeforeProxyHeadersSentCallback( | 462 void MockNetworkTransaction::SetBeforeProxyHeadersSentCallback( |
450 const BeforeProxyHeadersSentCallback& callback) { | 463 const BeforeProxyHeadersSentCallback& callback) { |
451 } | 464 } |
452 | 465 |
453 int MockNetworkTransaction::ResumeNetworkStart() { | 466 int MockNetworkTransaction::ResumeNetworkStart() { |
454 // Should not get here. | 467 // Should not get here. |
455 return ERR_FAILED; | 468 return ERR_FAILED; |
456 } | 469 } |
(...skipping 26 matching lines...) Expand all Loading... | |
483 MockNetworkLayer::~MockNetworkLayer() {} | 496 MockNetworkLayer::~MockNetworkLayer() {} |
484 | 497 |
485 void MockNetworkLayer::TransactionDoneReading() { | 498 void MockNetworkLayer::TransactionDoneReading() { |
486 done_reading_called_ = true; | 499 done_reading_called_ = true; |
487 } | 500 } |
488 | 501 |
489 void MockNetworkLayer::TransactionStopCaching() { | 502 void MockNetworkLayer::TransactionStopCaching() { |
490 stop_caching_called_ = true; | 503 stop_caching_called_ = true; |
491 } | 504 } |
492 | 505 |
506 void MockNetworkLayer::ResetTransactionCount() { | |
507 transaction_count_ = 0; | |
508 } | |
509 | |
493 int MockNetworkLayer::CreateTransaction(RequestPriority priority, | 510 int MockNetworkLayer::CreateTransaction(RequestPriority priority, |
494 scoped_ptr<HttpTransaction>* trans) { | 511 scoped_ptr<HttpTransaction>* trans) { |
495 transaction_count_++; | 512 transaction_count_++; |
496 last_create_transaction_priority_ = priority; | 513 last_create_transaction_priority_ = priority; |
497 scoped_ptr<MockNetworkTransaction> mock_transaction( | 514 scoped_ptr<MockNetworkTransaction> mock_transaction( |
498 new MockNetworkTransaction(priority, this)); | 515 new MockNetworkTransaction(priority, this)); |
499 last_transaction_ = mock_transaction->AsWeakPtr(); | 516 last_transaction_ = mock_transaction->AsWeakPtr(); |
500 *trans = mock_transaction.Pass(); | 517 *trans = mock_transaction.Pass(); |
501 return OK; | 518 return OK; |
502 } | 519 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
539 content.append(buf->data(), rv); | 556 content.append(buf->data(), rv); |
540 else if (rv < 0) | 557 else if (rv < 0) |
541 return rv; | 558 return rv; |
542 } while (rv > 0); | 559 } while (rv > 0); |
543 | 560 |
544 result->swap(content); | 561 result->swap(content); |
545 return OK; | 562 return OK; |
546 } | 563 } |
547 | 564 |
548 } // namespace net | 565 } // namespace net |
OLD | NEW |