| 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 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 base::Time(), | 44 base::Time(), |
| 45 "", | 45 "", |
| 46 LOAD_NORMAL, | 46 LOAD_NORMAL, |
| 47 "HTTP/1.1 200 OK", | 47 "HTTP/1.1 200 OK", |
| 48 "Cache-Control: max-age=10000\n", | 48 "Cache-Control: max-age=10000\n", |
| 49 base::Time(), | 49 base::Time(), |
| 50 "<html><body>Google Blah Blah</body></html>", | 50 "<html><body>Google Blah Blah</body></html>", |
| 51 TEST_MODE_NORMAL, | 51 TEST_MODE_NORMAL, |
| 52 nullptr, | 52 nullptr, |
| 53 nullptr, | 53 nullptr, |
| 54 nullptr, |
| 54 0, | 55 0, |
| 55 0, | 56 0, |
| 56 OK}; | 57 OK}; |
| 57 | 58 |
| 58 const MockTransaction kSimplePOST_Transaction = { | 59 const MockTransaction kSimplePOST_Transaction = { |
| 59 "http://bugdatabase.com/edit", | 60 "http://bugdatabase.com/edit", |
| 60 "POST", | 61 "POST", |
| 61 base::Time(), | 62 base::Time(), |
| 62 "", | 63 "", |
| 63 LOAD_NORMAL, | 64 LOAD_NORMAL, |
| 64 "HTTP/1.1 200 OK", | 65 "HTTP/1.1 200 OK", |
| 65 "", | 66 "", |
| 66 base::Time(), | 67 base::Time(), |
| 67 "<html><body>Google Blah Blah</body></html>", | 68 "<html><body>Google Blah Blah</body></html>", |
| 68 TEST_MODE_NORMAL, | 69 TEST_MODE_NORMAL, |
| 69 nullptr, | 70 nullptr, |
| 70 nullptr, | 71 nullptr, |
| 72 nullptr, |
| 71 0, | 73 0, |
| 72 0, | 74 0, |
| 73 OK}; | 75 OK}; |
| 74 | 76 |
| 75 const MockTransaction kTypicalGET_Transaction = { | 77 const MockTransaction kTypicalGET_Transaction = { |
| 76 "http://www.example.com/~foo/bar.html", | 78 "http://www.example.com/~foo/bar.html", "GET", base::Time(), "", |
| 77 "GET", | 79 LOAD_NORMAL, "HTTP/1.1 200 OK", |
| 78 base::Time(), | |
| 79 "", | |
| 80 LOAD_NORMAL, | |
| 81 "HTTP/1.1 200 OK", | |
| 82 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | 80 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
| 83 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", | 81 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", |
| 84 base::Time(), | 82 base::Time(), "<html><body>Google Blah Blah</body></html>", |
| 85 "<html><body>Google Blah Blah</body></html>", | 83 TEST_MODE_NORMAL, nullptr, nullptr, nullptr, 0, 0, OK}; |
| 86 TEST_MODE_NORMAL, | |
| 87 nullptr, | |
| 88 nullptr, | |
| 89 0, | |
| 90 0, | |
| 91 OK}; | |
| 92 | 84 |
| 93 const MockTransaction kETagGET_Transaction = { | 85 const MockTransaction kETagGET_Transaction = { |
| 94 "http://www.google.com/foopy", | 86 "http://www.google.com/foopy", "GET", base::Time(), "", LOAD_NORMAL, |
| 95 "GET", | |
| 96 base::Time(), | |
| 97 "", | |
| 98 LOAD_NORMAL, | |
| 99 "HTTP/1.1 200 OK", | 87 "HTTP/1.1 200 OK", |
| 100 "Cache-Control: max-age=10000\n" | 88 "Cache-Control: max-age=10000\n" |
| 101 "Etag: \"foopy\"\n", | 89 "Etag: \"foopy\"\n", |
| 102 base::Time(), | 90 base::Time(), "<html><body>Google Blah Blah</body></html>", |
| 103 "<html><body>Google Blah Blah</body></html>", | 91 TEST_MODE_NORMAL, nullptr, nullptr, nullptr, 0, 0, OK}; |
| 104 TEST_MODE_NORMAL, | |
| 105 nullptr, | |
| 106 nullptr, | |
| 107 0, | |
| 108 0, | |
| 109 OK}; | |
| 110 | 92 |
| 111 const MockTransaction kRangeGET_Transaction = { | 93 const MockTransaction kRangeGET_Transaction = { |
| 112 "http://www.google.com/", | 94 "http://www.google.com/", |
| 113 "GET", | 95 "GET", |
| 114 base::Time(), | 96 base::Time(), |
| 115 "Range: 0-100\r\n", | 97 "Range: 0-100\r\n", |
| 116 LOAD_NORMAL, | 98 LOAD_NORMAL, |
| 117 "HTTP/1.1 200 OK", | 99 "HTTP/1.1 200 OK", |
| 118 "Cache-Control: max-age=10000\n", | 100 "Cache-Control: max-age=10000\n", |
| 119 base::Time(), | 101 base::Time(), |
| 120 "<html><body>Google Blah Blah</body></html>", | 102 "<html><body>Google Blah Blah</body></html>", |
| 121 TEST_MODE_NORMAL, | 103 TEST_MODE_NORMAL, |
| 122 nullptr, | 104 nullptr, |
| 123 nullptr, | 105 nullptr, |
| 106 nullptr, |
| 124 0, | 107 0, |
| 125 0, | 108 0, |
| 126 OK}; | 109 OK}; |
| 127 | 110 |
| 128 static const MockTransaction* const kBuiltinMockTransactions[] = { | 111 static const MockTransaction* const kBuiltinMockTransactions[] = { |
| 129 &kSimpleGET_Transaction, | 112 &kSimpleGET_Transaction, |
| 130 &kSimplePOST_Transaction, | 113 &kSimplePOST_Transaction, |
| 131 &kTypicalGET_Transaction, | 114 &kTypicalGET_Transaction, |
| 132 &kETagGET_Transaction, | 115 &kETagGET_Transaction, |
| 133 &kRangeGET_Transaction | 116 &kRangeGET_Transaction |
| 134 }; | 117 }; |
| 135 | 118 |
| 136 const MockTransaction* FindMockTransaction(const GURL& url) { | 119 const MockTransaction* FindMockTransaction(const GURL& url) { |
| 137 // look for overrides: | 120 // look for overrides: |
| 138 MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); | 121 MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); |
| 139 if (it != mock_transactions.end()) | 122 if (it != mock_transactions.end()) |
| 140 return it->second; | 123 return it->second; |
| 141 | 124 |
| 142 // look for builtins: | 125 // look for builtins: |
| 143 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { | 126 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { |
| 144 if (url == GURL(kBuiltinMockTransactions[i]->url)) | 127 if (url == GURL(kBuiltinMockTransactions[i]->url)) |
| 145 return kBuiltinMockTransactions[i]; | 128 return kBuiltinMockTransactions[i]; |
| 146 } | 129 } |
| 147 return NULL; | 130 return nullptr; |
| 148 } | 131 } |
| 149 | 132 |
| 150 void AddMockTransaction(const MockTransaction* trans) { | 133 void AddMockTransaction(const MockTransaction* trans) { |
| 151 mock_transactions[GURL(trans->url).spec()] = trans; | 134 mock_transactions[GURL(trans->url).spec()] = trans; |
| 152 } | 135 } |
| 153 | 136 |
| 154 void RemoveMockTransaction(const MockTransaction* trans) { | 137 void RemoveMockTransaction(const MockTransaction* trans) { |
| 155 mock_transactions.erase(GURL(trans->url).spec()); | 138 mock_transactions.erase(GURL(trans->url).spec()); |
| 156 } | 139 } |
| 157 | 140 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 case READING: | 215 case READING: |
| 233 DidRead(result); | 216 DidRead(result); |
| 234 break; | 217 break; |
| 235 default: | 218 default: |
| 236 NOTREACHED(); | 219 NOTREACHED(); |
| 237 } | 220 } |
| 238 } | 221 } |
| 239 | 222 |
| 240 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, | 223 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, |
| 241 MockNetworkLayer* factory) | 224 MockNetworkLayer* factory) |
| 242 : request_(NULL), | 225 : request_(nullptr), |
| 243 data_cursor_(0), | 226 data_cursor_(0), |
| 227 content_length_(0), |
| 244 priority_(priority), | 228 priority_(priority), |
| 245 websocket_handshake_stream_create_helper_(NULL), | 229 read_handler_(nullptr), |
| 230 websocket_handshake_stream_create_helper_(nullptr), |
| 246 transaction_factory_(factory->AsWeakPtr()), | 231 transaction_factory_(factory->AsWeakPtr()), |
| 247 received_bytes_(0), | 232 received_bytes_(0), |
| 248 sent_bytes_(0), | 233 sent_bytes_(0), |
| 249 socket_log_id_(NetLog::Source::kInvalidId), | 234 socket_log_id_(NetLog::Source::kInvalidId), |
| 250 done_reading_called_(false), | 235 done_reading_called_(false), |
| 251 weak_factory_(this) {} | 236 weak_factory_(this) {} |
| 252 | 237 |
| 253 MockNetworkTransaction::~MockNetworkTransaction() {} | 238 MockNetworkTransaction::~MockNetworkTransaction() {} |
| 254 | 239 |
| 255 int MockNetworkTransaction::Start(const HttpRequestInfo* request, | 240 int MockNetworkTransaction::Start(const HttpRequestInfo* request, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 | 285 |
| 301 if (!request_->extra_headers.HasHeader("X-Require-Mock-Auth")) | 286 if (!request_->extra_headers.HasHeader("X-Require-Mock-Auth")) |
| 302 return false; | 287 return false; |
| 303 | 288 |
| 304 // Allow the mock server to decide whether authentication is required or not. | 289 // Allow the mock server to decide whether authentication is required or not. |
| 305 std::string status_line = response_.headers->GetStatusLine(); | 290 std::string status_line = response_.headers->GetStatusLine(); |
| 306 return status_line.find(" 401 ") != std::string::npos || | 291 return status_line.find(" 401 ") != std::string::npos || |
| 307 status_line.find(" 407 ") != std::string::npos; | 292 status_line.find(" 407 ") != std::string::npos; |
| 308 } | 293 } |
| 309 | 294 |
| 310 int MockNetworkTransaction::Read(IOBuffer* buf, | 295 int MockNetworkTransaction::Read(net::IOBuffer* buf, |
| 311 int buf_len, | 296 int buf_len, |
| 312 const CompletionCallback& callback) { | 297 const CompletionCallback& callback) { |
| 313 CHECK(!done_reading_called_); | 298 CHECK(!done_reading_called_); |
| 314 int data_len = static_cast<int>(data_.size()); | 299 int num = 0; |
| 315 int num = std::min(buf_len, data_len - data_cursor_); | 300 if (read_handler_) { |
| 316 if (test_mode_ & TEST_MODE_SLOW_READ) | 301 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); |
| 317 num = std::min(num, 1); | |
| 318 if (num) { | |
| 319 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
| 320 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_t>(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 } |
| 321 } | 312 } |
| 322 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | 313 if (test_mode_ & TEST_MODE_SYNC_NET_READ) |
| 323 return num; | 314 return num; |
| 324 | 315 |
| 325 CallbackLater(callback, num); | 316 CallbackLater(callback, num); |
| 326 return ERR_IO_PENDING; | 317 return ERR_IO_PENDING; |
| 327 } | 318 } |
| 328 | 319 |
| 329 void MockNetworkTransaction::StopCaching() { | 320 void MockNetworkTransaction::StopCaching() { |
| 330 if (transaction_factory_.get()) | 321 if (transaction_factory_.get()) |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 // static | 402 // static |
| 412 const int64_t MockNetworkTransaction::kTotalSentBytes = 100; | 403 const int64_t MockNetworkTransaction::kTotalSentBytes = 100; |
| 413 | 404 |
| 414 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, | 405 int MockNetworkTransaction::StartInternal(const HttpRequestInfo* request, |
| 415 const CompletionCallback& callback, | 406 const CompletionCallback& callback, |
| 416 const BoundNetLog& net_log) { | 407 const BoundNetLog& net_log) { |
| 417 const MockTransaction* t = FindMockTransaction(request->url); | 408 const MockTransaction* t = FindMockTransaction(request->url); |
| 418 if (!t) | 409 if (!t) |
| 419 return ERR_FAILED; | 410 return ERR_FAILED; |
| 420 | 411 |
| 412 if (!before_network_start_callback_.is_null()) { |
| 413 bool defer = false; |
| 414 before_network_start_callback_.Run(&defer); |
| 415 if (defer) |
| 416 return net::ERR_IO_PENDING; |
| 417 } |
| 418 |
| 421 test_mode_ = t->test_mode; | 419 test_mode_ = t->test_mode; |
| 422 | 420 |
| 423 // Return immediately if we're returning an error. | 421 // Return immediately if we're returning an error. |
| 424 if (OK != t->return_code) { | 422 if (OK != t->return_code) { |
| 425 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 423 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
| 426 return t->return_code; | 424 return t->return_code; |
| 427 CallbackLater(callback, t->return_code); | 425 CallbackLater(callback, t->return_code); |
| 428 return ERR_IO_PENDING; | 426 return ERR_IO_PENDING; |
| 429 } | 427 } |
| 430 | 428 |
| 431 sent_bytes_ = kTotalSentBytes; | 429 sent_bytes_ = kTotalSentBytes; |
| 432 received_bytes_ = kTotalReceivedBytes; | 430 received_bytes_ = kTotalReceivedBytes; |
| 433 | 431 |
| 434 std::string resp_status = t->status; | 432 std::string resp_status = t->status; |
| 435 std::string resp_headers = t->response_headers; | 433 std::string resp_headers = t->response_headers; |
| 436 std::string resp_data = t->data; | 434 std::string resp_data = t->data; |
| 437 if (t->handler) | 435 if (t->handler) |
| 438 (t->handler)(request, &resp_status, &resp_headers, &resp_data); | 436 (t->handler)(request, &resp_status, &resp_headers, &resp_data); |
| 437 if (t->read_handler) |
| 438 read_handler_ = t->read_handler; |
| 439 | 439 |
| 440 std::string header_data = base::StringPrintf( | 440 std::string header_data = base::StringPrintf( |
| 441 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); | 441 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); |
| 442 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); | 442 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); |
| 443 | 443 |
| 444 response_.request_time = transaction_factory_->Now(); | 444 response_.request_time = transaction_factory_->Now(); |
| 445 if (!t->request_time.is_null()) | 445 if (!t->request_time.is_null()) |
| 446 response_.request_time = t->request_time; | 446 response_.request_time = t->request_time; |
| 447 | 447 |
| 448 response_.was_cached = false; | 448 response_.was_cached = false; |
| 449 response_.network_accessed = true; | 449 response_.network_accessed = true; |
| 450 | 450 |
| 451 response_.response_time = transaction_factory_->Now(); | 451 response_.response_time = transaction_factory_->Now(); |
| 452 if (!t->response_time.is_null()) | 452 if (!t->response_time.is_null()) |
| 453 response_.response_time = t->response_time; | 453 response_.response_time = t->response_time; |
| 454 | 454 |
| 455 response_.headers = new HttpResponseHeaders(header_data); | 455 response_.headers = new HttpResponseHeaders(header_data); |
| 456 response_.vary_data.Init(*request, *response_.headers.get()); | 456 response_.vary_data.Init(*request, *response_.headers.get()); |
| 457 response_.ssl_info.cert = t->cert; | 457 response_.ssl_info.cert = t->cert; |
| 458 response_.ssl_info.cert_status = t->cert_status; | 458 response_.ssl_info.cert_status = t->cert_status; |
| 459 response_.ssl_info.connection_status = t->ssl_connection_status; | 459 response_.ssl_info.connection_status = t->ssl_connection_status; |
| 460 data_ = resp_data; | 460 data_ = resp_data; |
| 461 content_length_ = response_.headers->GetContentLength(); |
| 461 | 462 |
| 462 if (net_log.net_log()) | 463 if (net_log.net_log()) |
| 463 socket_log_id_ = net_log.net_log()->NextID(); | 464 socket_log_id_ = net_log.net_log()->NextID(); |
| 464 | 465 |
| 465 if (request_->load_flags & LOAD_PREFETCH) | 466 if (request_->load_flags & LOAD_PREFETCH) |
| 466 response_.unused_since_prefetch = true; | 467 response_.unused_since_prefetch = true; |
| 467 | 468 |
| 468 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 469 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
| 469 return OK; | 470 return OK; |
| 470 | 471 |
| 471 CallbackLater(callback, OK); | 472 CallbackLater(callback, OK); |
| 472 return ERR_IO_PENDING; | 473 return ERR_IO_PENDING; |
| 473 } | 474 } |
| 474 | 475 |
| 475 void MockNetworkTransaction::SetBeforeNetworkStartCallback( | 476 void MockNetworkTransaction::SetBeforeNetworkStartCallback( |
| 476 const BeforeNetworkStartCallback& callback) { | 477 const BeforeNetworkStartCallback& callback) { |
| 478 before_network_start_callback_ = callback; |
| 477 } | 479 } |
| 478 | 480 |
| 479 void MockNetworkTransaction::SetBeforeHeadersSentCallback( | 481 void MockNetworkTransaction::SetBeforeHeadersSentCallback( |
| 480 const BeforeHeadersSentCallback& callback) {} | 482 const BeforeHeadersSentCallback& callback) {} |
| 481 | 483 |
| 482 int MockNetworkTransaction::ResumeNetworkStart() { | 484 int MockNetworkTransaction::ResumeNetworkStart() { |
| 483 // Should not get here. | 485 // Should not get here. |
| 484 return ERR_FAILED; | 486 return ERR_FAILED; |
| 485 } | 487 } |
| 486 | 488 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 513 | 515 |
| 514 void MockNetworkLayer::TransactionDoneReading() { | 516 void MockNetworkLayer::TransactionDoneReading() { |
| 515 CHECK(!done_reading_called_); | 517 CHECK(!done_reading_called_); |
| 516 done_reading_called_ = true; | 518 done_reading_called_ = true; |
| 517 } | 519 } |
| 518 | 520 |
| 519 void MockNetworkLayer::TransactionStopCaching() { | 521 void MockNetworkLayer::TransactionStopCaching() { |
| 520 stop_caching_called_ = true; | 522 stop_caching_called_ = true; |
| 521 } | 523 } |
| 522 | 524 |
| 525 void MockNetworkLayer::ResetTransactionCount() { |
| 526 transaction_count_ = 0; |
| 527 } |
| 528 |
| 523 int MockNetworkLayer::CreateTransaction( | 529 int MockNetworkLayer::CreateTransaction( |
| 524 RequestPriority priority, | 530 RequestPriority priority, |
| 525 std::unique_ptr<HttpTransaction>* trans) { | 531 std::unique_ptr<HttpTransaction>* trans) { |
| 526 transaction_count_++; | 532 transaction_count_++; |
| 527 last_create_transaction_priority_ = priority; | 533 last_create_transaction_priority_ = priority; |
| 528 std::unique_ptr<MockNetworkTransaction> mock_transaction( | 534 std::unique_ptr<MockNetworkTransaction> mock_transaction( |
| 529 new MockNetworkTransaction(priority, this)); | 535 new MockNetworkTransaction(priority, this)); |
| 530 last_transaction_ = mock_transaction->AsWeakPtr(); | 536 last_transaction_ = mock_transaction->AsWeakPtr(); |
| 531 *trans = std::move(mock_transaction); | 537 *trans = std::move(mock_transaction); |
| 532 return OK; | 538 return OK; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 content.append(buf->data(), rv); | 576 content.append(buf->data(), rv); |
| 571 else if (rv < 0) | 577 else if (rv < 0) |
| 572 return rv; | 578 return rv; |
| 573 } while (rv > 0); | 579 } while (rv > 0); |
| 574 | 580 |
| 575 result->swap(content); | 581 result->swap(content); |
| 576 return OK; | 582 return OK; |
| 577 } | 583 } |
| 578 | 584 |
| 579 } // namespace net | 585 } // namespace net |
| OLD | NEW |