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