| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/http/http_transaction_test_util.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "base/time/clock.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "net/base/load_flags.h" | |
| 15 #include "net/base/load_timing_info.h" | |
| 16 #include "net/base/net_errors.h" | |
| 17 #include "net/disk_cache/disk_cache.h" | |
| 18 #include "net/http/http_cache.h" | |
| 19 #include "net/http/http_request_info.h" | |
| 20 #include "net/http/http_response_info.h" | |
| 21 #include "net/http/http_transaction.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 namespace { | |
| 25 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; | |
| 26 static MockTransactionMap mock_transactions; | |
| 27 } // namespace | |
| 28 | |
| 29 //----------------------------------------------------------------------------- | |
| 30 // mock transaction data | |
| 31 | |
| 32 const MockTransaction kSimpleGET_Transaction = { | |
| 33 "http://www.google.com/", | |
| 34 "GET", | |
| 35 base::Time(), | |
| 36 "", | |
| 37 net::LOAD_NORMAL, | |
| 38 "HTTP/1.1 200 OK", | |
| 39 "Cache-Control: max-age=10000\n", | |
| 40 base::Time(), | |
| 41 "<html><body>Google Blah Blah</body></html>", | |
| 42 TEST_MODE_NORMAL, | |
| 43 NULL, | |
| 44 0, | |
| 45 net::OK | |
| 46 }; | |
| 47 | |
| 48 const MockTransaction kSimplePOST_Transaction = { | |
| 49 "http://bugdatabase.com/edit", | |
| 50 "POST", | |
| 51 base::Time(), | |
| 52 "", | |
| 53 net::LOAD_NORMAL, | |
| 54 "HTTP/1.1 200 OK", | |
| 55 "", | |
| 56 base::Time(), | |
| 57 "<html><body>Google Blah Blah</body></html>", | |
| 58 TEST_MODE_NORMAL, | |
| 59 NULL, | |
| 60 0, | |
| 61 net::OK | |
| 62 }; | |
| 63 | |
| 64 const MockTransaction kTypicalGET_Transaction = { | |
| 65 "http://www.example.com/~foo/bar.html", | |
| 66 "GET", | |
| 67 base::Time(), | |
| 68 "", | |
| 69 net::LOAD_NORMAL, | |
| 70 "HTTP/1.1 200 OK", | |
| 71 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | |
| 72 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", | |
| 73 base::Time(), | |
| 74 "<html><body>Google Blah Blah</body></html>", | |
| 75 TEST_MODE_NORMAL, | |
| 76 NULL, | |
| 77 0, | |
| 78 net::OK | |
| 79 }; | |
| 80 | |
| 81 const MockTransaction kETagGET_Transaction = { | |
| 82 "http://www.google.com/foopy", | |
| 83 "GET", | |
| 84 base::Time(), | |
| 85 "", | |
| 86 net::LOAD_NORMAL, | |
| 87 "HTTP/1.1 200 OK", | |
| 88 "Cache-Control: max-age=10000\n" | |
| 89 "Etag: \"foopy\"\n", | |
| 90 base::Time(), | |
| 91 "<html><body>Google Blah Blah</body></html>", | |
| 92 TEST_MODE_NORMAL, | |
| 93 NULL, | |
| 94 0, | |
| 95 net::OK | |
| 96 }; | |
| 97 | |
| 98 const MockTransaction kRangeGET_Transaction = { | |
| 99 "http://www.google.com/", | |
| 100 "GET", | |
| 101 base::Time(), | |
| 102 "Range: 0-100\r\n", | |
| 103 net::LOAD_NORMAL, | |
| 104 "HTTP/1.1 200 OK", | |
| 105 "Cache-Control: max-age=10000\n", | |
| 106 base::Time(), | |
| 107 "<html><body>Google Blah Blah</body></html>", | |
| 108 TEST_MODE_NORMAL, | |
| 109 NULL, | |
| 110 0, | |
| 111 net::OK | |
| 112 }; | |
| 113 | |
| 114 static const MockTransaction* const kBuiltinMockTransactions[] = { | |
| 115 &kSimpleGET_Transaction, | |
| 116 &kSimplePOST_Transaction, | |
| 117 &kTypicalGET_Transaction, | |
| 118 &kETagGET_Transaction, | |
| 119 &kRangeGET_Transaction | |
| 120 }; | |
| 121 | |
| 122 const MockTransaction* FindMockTransaction(const GURL& url) { | |
| 123 // look for overrides: | |
| 124 MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); | |
| 125 if (it != mock_transactions.end()) | |
| 126 return it->second; | |
| 127 | |
| 128 // look for builtins: | |
| 129 for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { | |
| 130 if (url == GURL(kBuiltinMockTransactions[i]->url)) | |
| 131 return kBuiltinMockTransactions[i]; | |
| 132 } | |
| 133 return NULL; | |
| 134 } | |
| 135 | |
| 136 void AddMockTransaction(const MockTransaction* trans) { | |
| 137 mock_transactions[GURL(trans->url).spec()] = trans; | |
| 138 } | |
| 139 | |
| 140 void RemoveMockTransaction(const MockTransaction* trans) { | |
| 141 mock_transactions.erase(GURL(trans->url).spec()); | |
| 142 } | |
| 143 | |
| 144 MockHttpRequest::MockHttpRequest(const MockTransaction& t) { | |
| 145 url = GURL(t.url); | |
| 146 method = t.method; | |
| 147 extra_headers.AddHeadersFromString(t.request_headers); | |
| 148 load_flags = t.load_flags; | |
| 149 } | |
| 150 | |
| 151 //----------------------------------------------------------------------------- | |
| 152 | |
| 153 // static | |
| 154 int TestTransactionConsumer::quit_counter_ = 0; | |
| 155 | |
| 156 TestTransactionConsumer::TestTransactionConsumer( | |
| 157 net::RequestPriority priority, | |
| 158 net::HttpTransactionFactory* factory) | |
| 159 : state_(IDLE), error_(net::OK) { | |
| 160 // Disregard the error code. | |
| 161 factory->CreateTransaction(priority, &trans_); | |
| 162 ++quit_counter_; | |
| 163 } | |
| 164 | |
| 165 TestTransactionConsumer::~TestTransactionConsumer() { | |
| 166 } | |
| 167 | |
| 168 void TestTransactionConsumer::Start(const net::HttpRequestInfo* request, | |
| 169 const net::BoundNetLog& net_log) { | |
| 170 state_ = STARTING; | |
| 171 int result = trans_->Start( | |
| 172 request, base::Bind(&TestTransactionConsumer::OnIOComplete, | |
| 173 base::Unretained(this)), net_log); | |
| 174 if (result != net::ERR_IO_PENDING) | |
| 175 DidStart(result); | |
| 176 } | |
| 177 | |
| 178 void TestTransactionConsumer::DidStart(int result) { | |
| 179 if (result != net::OK) { | |
| 180 DidFinish(result); | |
| 181 } else { | |
| 182 Read(); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void TestTransactionConsumer::DidRead(int result) { | |
| 187 if (result <= 0) { | |
| 188 DidFinish(result); | |
| 189 } else { | |
| 190 content_.append(read_buf_->data(), result); | |
| 191 Read(); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 void TestTransactionConsumer::DidFinish(int result) { | |
| 196 state_ = DONE; | |
| 197 error_ = result; | |
| 198 if (--quit_counter_ == 0) | |
| 199 base::MessageLoop::current()->Quit(); | |
| 200 } | |
| 201 | |
| 202 void TestTransactionConsumer::Read() { | |
| 203 state_ = READING; | |
| 204 read_buf_ = new net::IOBuffer(1024); | |
| 205 int result = trans_->Read(read_buf_.get(), | |
| 206 1024, | |
| 207 base::Bind(&TestTransactionConsumer::OnIOComplete, | |
| 208 base::Unretained(this))); | |
| 209 if (result != net::ERR_IO_PENDING) | |
| 210 DidRead(result); | |
| 211 } | |
| 212 | |
| 213 void TestTransactionConsumer::OnIOComplete(int result) { | |
| 214 switch (state_) { | |
| 215 case STARTING: | |
| 216 DidStart(result); | |
| 217 break; | |
| 218 case READING: | |
| 219 DidRead(result); | |
| 220 break; | |
| 221 default: | |
| 222 NOTREACHED(); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 MockNetworkTransaction::MockNetworkTransaction( | |
| 227 net::RequestPriority priority, | |
| 228 MockNetworkLayer* factory) | |
| 229 : request_(NULL), | |
| 230 data_cursor_(0), | |
| 231 priority_(priority), | |
| 232 websocket_handshake_stream_create_helper_(NULL), | |
| 233 transaction_factory_(factory->AsWeakPtr()), | |
| 234 received_bytes_(0), | |
| 235 socket_log_id_(net::NetLog::Source::kInvalidId), | |
| 236 weak_factory_(this) { | |
| 237 } | |
| 238 | |
| 239 MockNetworkTransaction::~MockNetworkTransaction() {} | |
| 240 | |
| 241 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, | |
| 242 const net::CompletionCallback& callback, | |
| 243 const net::BoundNetLog& net_log) { | |
| 244 if (request_) | |
| 245 return net::ERR_FAILED; | |
| 246 | |
| 247 request_ = request; | |
| 248 return StartInternal(request, callback, net_log); | |
| 249 } | |
| 250 | |
| 251 int MockNetworkTransaction::RestartIgnoringLastError( | |
| 252 const net::CompletionCallback& callback) { | |
| 253 return net::ERR_FAILED; | |
| 254 } | |
| 255 | |
| 256 int MockNetworkTransaction::RestartWithCertificate( | |
| 257 net::X509Certificate* client_cert, | |
| 258 const net::CompletionCallback& callback) { | |
| 259 return net::ERR_FAILED; | |
| 260 } | |
| 261 | |
| 262 int MockNetworkTransaction::RestartWithAuth( | |
| 263 const net::AuthCredentials& credentials, | |
| 264 const net::CompletionCallback& callback) { | |
| 265 if (!IsReadyToRestartForAuth()) | |
| 266 return net::ERR_FAILED; | |
| 267 | |
| 268 net::HttpRequestInfo auth_request_info = *request_; | |
| 269 auth_request_info.extra_headers.AddHeaderFromString("Authorization: Bar"); | |
| 270 | |
| 271 // Let the MockTransactionHandler worry about this: the only way for this | |
| 272 // test to succeed is by using an explicit handler for the transaction so | |
| 273 // that server behavior can be simulated. | |
| 274 return StartInternal(&auth_request_info, callback, net::BoundNetLog()); | |
| 275 } | |
| 276 | |
| 277 bool MockNetworkTransaction::IsReadyToRestartForAuth() { | |
| 278 if (!request_) | |
| 279 return false; | |
| 280 | |
| 281 if (!request_->extra_headers.HasHeader("X-Require-Mock-Auth")) | |
| 282 return false; | |
| 283 | |
| 284 // Allow the mock server to decide whether authentication is required or not. | |
| 285 std::string status_line = response_.headers->GetStatusLine(); | |
| 286 return status_line.find(" 401 ") != std::string::npos || | |
| 287 status_line.find(" 407 ") != std::string::npos; | |
| 288 } | |
| 289 | |
| 290 int MockNetworkTransaction::Read(net::IOBuffer* buf, int buf_len, | |
| 291 const net::CompletionCallback& callback) { | |
| 292 int data_len = static_cast<int>(data_.size()); | |
| 293 int num = std::min(buf_len, data_len - data_cursor_); | |
| 294 if (test_mode_ & TEST_MODE_SLOW_READ) | |
| 295 num = std::min(num, 1); | |
| 296 if (num) { | |
| 297 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
| 298 data_cursor_ += num; | |
| 299 } | |
| 300 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | |
| 301 return num; | |
| 302 | |
| 303 CallbackLater(callback, num); | |
| 304 return net::ERR_IO_PENDING; | |
| 305 } | |
| 306 | |
| 307 void MockNetworkTransaction::StopCaching() { | |
| 308 if (transaction_factory_.get()) | |
| 309 transaction_factory_->TransactionStopCaching(); | |
| 310 } | |
| 311 | |
| 312 bool MockNetworkTransaction::GetFullRequestHeaders( | |
| 313 net::HttpRequestHeaders* headers) const { | |
| 314 return false; | |
| 315 } | |
| 316 | |
| 317 int64 MockNetworkTransaction::GetTotalReceivedBytes() const { | |
| 318 return received_bytes_; | |
| 319 } | |
| 320 | |
| 321 void MockNetworkTransaction::DoneReading() { | |
| 322 if (transaction_factory_.get()) | |
| 323 transaction_factory_->TransactionDoneReading(); | |
| 324 } | |
| 325 | |
| 326 const net::HttpResponseInfo* MockNetworkTransaction::GetResponseInfo() const { | |
| 327 return &response_; | |
| 328 } | |
| 329 | |
| 330 net::LoadState MockNetworkTransaction::GetLoadState() const { | |
| 331 if (data_cursor_) | |
| 332 return net::LOAD_STATE_READING_RESPONSE; | |
| 333 return net::LOAD_STATE_IDLE; | |
| 334 } | |
| 335 | |
| 336 net::UploadProgress MockNetworkTransaction::GetUploadProgress() const { | |
| 337 return net::UploadProgress(); | |
| 338 } | |
| 339 | |
| 340 void MockNetworkTransaction::SetQuicServerInfo( | |
| 341 net::QuicServerInfo* quic_server_info) {} | |
| 342 | |
| 343 bool MockNetworkTransaction::GetLoadTimingInfo( | |
| 344 net::LoadTimingInfo* load_timing_info) const { | |
| 345 if (socket_log_id_ != net::NetLog::Source::kInvalidId) { | |
| 346 // The minimal set of times for a request that gets a response, assuming it | |
| 347 // gets a new socket. | |
| 348 load_timing_info->socket_reused = false; | |
| 349 load_timing_info->socket_log_id = socket_log_id_; | |
| 350 load_timing_info->connect_timing.connect_start = base::TimeTicks::Now(); | |
| 351 load_timing_info->connect_timing.connect_end = base::TimeTicks::Now(); | |
| 352 load_timing_info->send_start = base::TimeTicks::Now(); | |
| 353 load_timing_info->send_end = base::TimeTicks::Now(); | |
| 354 } else { | |
| 355 // If there's no valid socket ID, just use the generic socket reused values. | |
| 356 // No tests currently depend on this, just should not match the values set | |
| 357 // by a cache hit. | |
| 358 load_timing_info->socket_reused = true; | |
| 359 load_timing_info->send_start = base::TimeTicks::Now(); | |
| 360 load_timing_info->send_end = base::TimeTicks::Now(); | |
| 361 } | |
| 362 return true; | |
| 363 } | |
| 364 | |
| 365 void MockNetworkTransaction::SetPriority(net::RequestPriority priority) { | |
| 366 priority_ = priority; | |
| 367 } | |
| 368 | |
| 369 void MockNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( | |
| 370 net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) { | |
| 371 websocket_handshake_stream_create_helper_ = create_helper; | |
| 372 } | |
| 373 | |
| 374 int MockNetworkTransaction::StartInternal( | |
| 375 const net::HttpRequestInfo* request, | |
| 376 const net::CompletionCallback& callback, | |
| 377 const net::BoundNetLog& net_log) { | |
| 378 const MockTransaction* t = FindMockTransaction(request->url); | |
| 379 if (!t) | |
| 380 return net::ERR_FAILED; | |
| 381 | |
| 382 test_mode_ = t->test_mode; | |
| 383 | |
| 384 // Return immediately if we're returning an error. | |
| 385 if (net::OK != t->return_code) { | |
| 386 if (test_mode_ & TEST_MODE_SYNC_NET_START) | |
| 387 return t->return_code; | |
| 388 CallbackLater(callback, t->return_code); | |
| 389 return net::ERR_IO_PENDING; | |
| 390 } | |
| 391 | |
| 392 std::string resp_status = t->status; | |
| 393 std::string resp_headers = t->response_headers; | |
| 394 std::string resp_data = t->data; | |
| 395 received_bytes_ = resp_status.size() + resp_headers.size() + resp_data.size(); | |
| 396 if (t->handler) | |
| 397 (t->handler)(request, &resp_status, &resp_headers, &resp_data); | |
| 398 | |
| 399 std::string header_data = base::StringPrintf( | |
| 400 "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); | |
| 401 std::replace(header_data.begin(), header_data.end(), '\n', '\0'); | |
| 402 | |
| 403 response_.request_time = transaction_factory_->Now(); | |
| 404 if (!t->request_time.is_null()) | |
| 405 response_.request_time = t->request_time; | |
| 406 | |
| 407 response_.was_cached = false; | |
| 408 response_.network_accessed = true; | |
| 409 | |
| 410 response_.response_time = transaction_factory_->Now(); | |
| 411 if (!t->response_time.is_null()) | |
| 412 response_.response_time = t->response_time; | |
| 413 | |
| 414 response_.headers = new net::HttpResponseHeaders(header_data); | |
| 415 response_.vary_data.Init(*request, *response_.headers.get()); | |
| 416 response_.ssl_info.cert_status = t->cert_status; | |
| 417 data_ = resp_data; | |
| 418 | |
| 419 if (net_log.net_log()) | |
| 420 socket_log_id_ = net_log.net_log()->NextID(); | |
| 421 | |
| 422 if (request_->load_flags & net::LOAD_PREFETCH) | |
| 423 response_.unused_since_prefetch = true; | |
| 424 | |
| 425 if (test_mode_ & TEST_MODE_SYNC_NET_START) | |
| 426 return net::OK; | |
| 427 | |
| 428 CallbackLater(callback, net::OK); | |
| 429 return net::ERR_IO_PENDING; | |
| 430 } | |
| 431 | |
| 432 void MockNetworkTransaction::SetBeforeNetworkStartCallback( | |
| 433 const BeforeNetworkStartCallback& callback) { | |
| 434 } | |
| 435 | |
| 436 void MockNetworkTransaction::SetBeforeProxyHeadersSentCallback( | |
| 437 const BeforeProxyHeadersSentCallback& callback) { | |
| 438 } | |
| 439 | |
| 440 int MockNetworkTransaction::ResumeNetworkStart() { | |
| 441 // Should not get here. | |
| 442 return net::ERR_FAILED; | |
| 443 } | |
| 444 | |
| 445 void MockNetworkTransaction::CallbackLater( | |
| 446 const net::CompletionCallback& callback, int result) { | |
| 447 base::MessageLoop::current()->PostTask( | |
| 448 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, | |
| 449 weak_factory_.GetWeakPtr(), callback, result)); | |
| 450 } | |
| 451 | |
| 452 void MockNetworkTransaction::RunCallback( | |
| 453 const net::CompletionCallback& callback, int result) { | |
| 454 callback.Run(result); | |
| 455 } | |
| 456 | |
| 457 MockNetworkLayer::MockNetworkLayer() | |
| 458 : transaction_count_(0), | |
| 459 done_reading_called_(false), | |
| 460 stop_caching_called_(false), | |
| 461 last_create_transaction_priority_(net::DEFAULT_PRIORITY), | |
| 462 clock_(nullptr) { | |
| 463 } | |
| 464 | |
| 465 MockNetworkLayer::~MockNetworkLayer() {} | |
| 466 | |
| 467 void MockNetworkLayer::TransactionDoneReading() { | |
| 468 done_reading_called_ = true; | |
| 469 } | |
| 470 | |
| 471 void MockNetworkLayer::TransactionStopCaching() { | |
| 472 stop_caching_called_ = true; | |
| 473 } | |
| 474 | |
| 475 int MockNetworkLayer::CreateTransaction( | |
| 476 net::RequestPriority priority, | |
| 477 scoped_ptr<net::HttpTransaction>* trans) { | |
| 478 transaction_count_++; | |
| 479 last_create_transaction_priority_ = priority; | |
| 480 scoped_ptr<MockNetworkTransaction> mock_transaction( | |
| 481 new MockNetworkTransaction(priority, this)); | |
| 482 last_transaction_ = mock_transaction->AsWeakPtr(); | |
| 483 *trans = mock_transaction.Pass(); | |
| 484 return net::OK; | |
| 485 } | |
| 486 | |
| 487 net::HttpCache* MockNetworkLayer::GetCache() { | |
| 488 return NULL; | |
| 489 } | |
| 490 | |
| 491 net::HttpNetworkSession* MockNetworkLayer::GetSession() { | |
| 492 return NULL; | |
| 493 } | |
| 494 | |
| 495 void MockNetworkLayer::SetClock(base::Clock* clock) { | |
| 496 DCHECK(!clock_); | |
| 497 clock_ = clock; | |
| 498 } | |
| 499 | |
| 500 base::Time MockNetworkLayer::Now() { | |
| 501 if (clock_) | |
| 502 return clock_->Now(); | |
| 503 return base::Time::Now(); | |
| 504 } | |
| 505 | |
| 506 //----------------------------------------------------------------------------- | |
| 507 // helpers | |
| 508 | |
| 509 int ReadTransaction(net::HttpTransaction* trans, std::string* result) { | |
| 510 int rv; | |
| 511 | |
| 512 net::TestCompletionCallback callback; | |
| 513 | |
| 514 std::string content; | |
| 515 do { | |
| 516 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
| 517 rv = trans->Read(buf.get(), 256, callback.callback()); | |
| 518 if (rv == net::ERR_IO_PENDING) | |
| 519 rv = callback.WaitForResult(); | |
| 520 | |
| 521 if (rv > 0) | |
| 522 content.append(buf->data(), rv); | |
| 523 else if (rv < 0) | |
| 524 return rv; | |
| 525 } while (rv > 0); | |
| 526 | |
| 527 result->swap(content); | |
| 528 return net::OK; | |
| 529 } | |
| OLD | NEW |