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