172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/http/http_transaction_unittest.h" 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include <algorithm> 872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/message_loop.h" 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/string_util.h" 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/base/net_errors.h" 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/base/load_flags.h" 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/disk_cache/disk_cache.h" 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/http/http_cache.h" 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/http/http_request_info.h" 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/http/http_response_info.h" 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/http/http_transaction.h" 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "testing/gtest/include/gtest/gtest.h" 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 2072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsennamespace { 2172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsentypedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; 2272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenstatic MockTransactionMap mock_transactions; 2372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} // namespace 2472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//----------------------------------------------------------------------------- 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// mock transaction data 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst MockTransaction kSimpleGET_Transaction = { 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.google.com/", 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "GET", 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott net::LOAD_NORMAL, 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "HTTP/1.1 200 OK", 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "Cache-Control: max-age=10000\n", 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<html><body>Google Blah Blah</body></html>", 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TEST_MODE_NORMAL, 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott NULL, 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 0 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst MockTransaction kSimplePOST_Transaction = { 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://bugdatabase.com/edit", 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "POST", 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott net::LOAD_NORMAL, 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "HTTP/1.1 200 OK", 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<html><body>Google Blah Blah</body></html>", 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TEST_MODE_NORMAL, 54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott NULL, 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 0 56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst MockTransaction kTypicalGET_Transaction = { 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.example.com/~foo/bar.html", 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "GET", 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", 63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott net::LOAD_NORMAL, 64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "HTTP/1.1 200 OK", 65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" 66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", 67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<html><body>Google Blah Blah</body></html>", 69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TEST_MODE_NORMAL, 70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott NULL, 71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 0 72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst MockTransaction kETagGET_Transaction = { 75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.google.com/foopy", 76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "GET", 77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "", 79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott net::LOAD_NORMAL, 80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "HTTP/1.1 200 OK", 81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "Cache-Control: max-age=10000\n" 82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "Etag: foopy\n", 83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<html><body>Google Blah Blah</body></html>", 85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TEST_MODE_NORMAL, 86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott NULL, 87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 0 88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst MockTransaction kRangeGET_Transaction = { 91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "http://www.google.com/", 92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "GET", 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "Range: 0-100\r\n", 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott net::LOAD_NORMAL, 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "HTTP/1.1 200 OK", 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "Cache-Control: max-age=10000\n", 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::Time(), 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott "<html><body>Google Blah Blah</body></html>", 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TEST_MODE_NORMAL, 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott NULL, 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 0 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic const MockTransaction* const kBuiltinMockTransactions[] = { 106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott &kSimpleGET_Transaction, 107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott &kSimplePOST_Transaction, 108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott &kTypicalGET_Transaction, 109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott &kETagGET_Transaction, 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott &kRangeGET_Transaction 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst MockTransaction* FindMockTransaction(const GURL& url) { 114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // look for overrides: 115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); 116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (it != mock_transactions.end()) 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return it->second; 118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // look for builtins: 120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { 121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (url == GURL(kBuiltinMockTransactions[i]->url)) 122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return kBuiltinMockTransactions[i]; 123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return NULL; 125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 126c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 12772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid AddMockTransaction(const MockTransaction* trans) { 12872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen mock_transactions[GURL(trans->url).spec()] = trans; 12972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 13072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 13172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid RemoveMockTransaction(const MockTransaction* trans) { 13272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen mock_transactions.erase(GURL(trans->url).spec()); 13372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 13472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 13572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenMockHttpRequest::MockHttpRequest(const MockTransaction& t) { 13672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen url = GURL(t.url); 13772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen method = t.method; 13872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen extra_headers.AddHeadersFromString(t.request_headers); 13972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen load_flags = t.load_flags; 14072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//----------------------------------------------------------------------------- 143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// static 145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottint TestTransactionConsumer::quit_counter_ = 0; 146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 14772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenTestTransactionConsumer::TestTransactionConsumer( 14872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::HttpTransactionFactory* factory) 14972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen : state_(IDLE), 15072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen trans_(NULL), 15172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen error_(net::OK) { 15272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen // Disregard the error code. 15372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen factory->CreateTransaction(&trans_); 15472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen ++quit_counter_; 15572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 15672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 15772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenTestTransactionConsumer::~TestTransactionConsumer() { 15872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 15972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 16072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid TestTransactionConsumer::Start(const net::HttpRequestInfo* request, 16172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const net::BoundNetLog& net_log) { 16272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen state_ = STARTING; 16372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int result = trans_->Start(request, this, net_log); 16472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (result != net::ERR_IO_PENDING) 16572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DidStart(result); 16672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 16772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 16872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid TestTransactionConsumer::DidStart(int result) { 16972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (result != net::OK) { 17072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DidFinish(result); 17172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen } else { 17272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen Read(); 17372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen } 17472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 17572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 17672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid TestTransactionConsumer::DidRead(int result) { 17772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (result <= 0) { 17872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DidFinish(result); 17972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen } else { 18072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen content_.append(read_buf_->data(), result); 18172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen Read(); 18272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen } 18372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 18472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 18572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid TestTransactionConsumer::DidFinish(int result) { 18672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen state_ = DONE; 18772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen error_ = result; 18872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (--quit_counter_ == 0) 18972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen MessageLoop::current()->Quit(); 19072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 19172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 19272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid TestTransactionConsumer::Read() { 19372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen state_ = READING; 19472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen read_buf_ = new net::IOBuffer(1024); 19572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int result = trans_->Read(read_buf_, 1024, this); 19672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (result != net::ERR_IO_PENDING) 19772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DidRead(result); 19872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 19972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 20072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid TestTransactionConsumer::RunWithParams(const Tuple1<int>& params) { 20172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int result = params.a; 20272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen switch (state_) { 20372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen case STARTING: 20472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DidStart(result); 20572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen break; 20672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen case READING: 20772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen DidRead(result); 20872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen break; 20972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen default: 21072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen NOTREACHED(); 21172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen } 21272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 21372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 21472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 21572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenMockNetworkTransaction::MockNetworkTransaction() : 21672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), data_cursor_(0) { 21772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 21872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 21972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenMockNetworkTransaction::~MockNetworkTransaction() {} 22072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 22172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenint MockNetworkTransaction::Start(const net::HttpRequestInfo* request, 22272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::CompletionCallback* callback, 22372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const net::BoundNetLog& net_log) { 22472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const MockTransaction* t = FindMockTransaction(request->url); 22572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (!t) 22672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::ERR_FAILED; 22772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 22872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen std::string resp_status = t->status; 22972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen std::string resp_headers = t->response_headers; 23072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen std::string resp_data = t->data; 23172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (t->handler) 23272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen (t->handler)(request, &resp_status, &resp_headers, &resp_data); 23372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 23472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen std::string header_data = base::StringPrintf( 23572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen "%s\n%s\n", resp_status.c_str(), resp_headers.c_str()); 23672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen std::replace(header_data.begin(), header_data.end(), '\n', '\0'); 23772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 23872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen response_.request_time = base::Time::Now(); 23972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (!t->request_time.is_null()) 24072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen response_.request_time = t->request_time; 24172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 24272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen response_.was_cached = false; 24372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 24472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen response_.response_time = base::Time::Now(); 24572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (!t->response_time.is_null()) 24672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen response_.response_time = t->response_time; 24772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 24872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen response_.headers = new net::HttpResponseHeaders(header_data); 24972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen response_.ssl_info.cert_status = t->cert_status; 25072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen data_ = resp_data; 25172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen test_mode_ = t->test_mode; 25272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 25372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (test_mode_ & TEST_MODE_SYNC_NET_START) 25472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::OK; 25572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 25672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen CallbackLater(callback, net::OK); 25772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::ERR_IO_PENDING; 25872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 25972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 26072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenint MockNetworkTransaction::RestartIgnoringLastError( 26172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::CompletionCallback* callback) { 26272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::ERR_FAILED; 26372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 26472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 26572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenint MockNetworkTransaction::RestartWithCertificate( 26672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::X509Certificate* client_cert, 26772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::CompletionCallback* callback) { 26872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::ERR_FAILED; 26972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 27072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 27172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenint MockNetworkTransaction::RestartWithAuth(const string16& username, 27272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen const string16& password, 27372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::CompletionCallback* callback) { 27472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::ERR_FAILED; 27572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 27672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 27772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenbool MockNetworkTransaction::IsReadyToRestartForAuth() { 27872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return false; 27972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 28072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 28172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenint MockNetworkTransaction::Read(net::IOBuffer* buf, int buf_len, 28272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::CompletionCallback* callback) { 28372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int data_len = static_cast<int>(data_.size()); 28472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int num = std::min(buf_len, data_len - data_cursor_); 28572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (num) { 28672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen memcpy(buf->data(), data_.data() + data_cursor_, num); 28772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen data_cursor_ += num; 28872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen } 28972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (test_mode_ & TEST_MODE_SYNC_NET_READ) 29072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return num; 29172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 29272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen CallbackLater(callback, num); 29372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::ERR_IO_PENDING; 29472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 29572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 29672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid MockNetworkTransaction::StopCaching() {} 29772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 29872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenconst net::HttpResponseInfo* MockNetworkTransaction::GetResponseInfo() const { 29972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return &response_; 30072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 30172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 30272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsennet::LoadState MockNetworkTransaction::GetLoadState() const { 30372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen if (data_cursor_) 30472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::LOAD_STATE_READING_RESPONSE; 30572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::LOAD_STATE_IDLE; 30672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 30772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 30872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenuint64 MockNetworkTransaction::GetUploadProgress() const { 30972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return 0; 31072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 31172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 31272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid MockNetworkTransaction::CallbackLater(net::CompletionCallback* callback, 31372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int result) { 31472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen MessageLoop::current()->PostTask(FROM_HERE, task_factory_.NewRunnableMethod( 31572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen &MockNetworkTransaction::RunCallback, callback, result)); 31672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 31772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 31872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid MockNetworkTransaction::RunCallback(net::CompletionCallback* callback, 31972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen int result) { 32072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen callback->Run(result); 32172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 32272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 32372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenMockNetworkLayer::MockNetworkLayer() : transaction_count_(0) {} 32472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 32572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenMockNetworkLayer::~MockNetworkLayer() {} 32672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 32772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenint MockNetworkLayer::CreateTransaction( 32872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen scoped_ptr<net::HttpTransaction>* trans) { 32972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen transaction_count_++; 33072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen trans->reset(new MockNetworkTransaction()); 33172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return net::OK; 33272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 33372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 33472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsennet::HttpCache* MockNetworkLayer::GetCache() { 33572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return NULL; 33672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 33772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 33872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsennet::HttpNetworkSession* MockNetworkLayer::GetSession() { 33972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen return NULL; 34072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen} 34172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen 34272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid MockNetworkLayer::Suspend(bool suspend) {} 343c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 344c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//----------------------------------------------------------------------------- 345c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// helpers 346c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 347c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottint ReadTransaction(net::HttpTransaction* trans, std::string* result) { 348c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int rv; 349c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 350c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott TestCompletionCallback callback; 351c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 352c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string content; 353c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott do { 354513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); 355c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott rv = trans->Read(buf, 256, &callback); 356c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (rv == net::ERR_IO_PENDING) 357c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott rv = callback.WaitForResult(); 358c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (rv > 0) { 359c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott content.append(buf->data(), rv); 360c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } else if (rv < 0) { 361c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return rv; 362c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 363c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } while (rv > 0); 364c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 365c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott result->swap(content); 366c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return net::OK; 367c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 368