15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_transaction.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/weak_ptr.h" 157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string16.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/io_buffer.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/load_flags.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h" 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "net/base/net_log.h" 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/request_priority.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/test_completion_callback.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/disk_cache/disk_cache.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_cache.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_request_info.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_response_headers.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_response_info.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass HttpRequestHeaders; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IOBuffer; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//----------------------------------------------------------------------------- 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// mock transaction data 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// these flags may be combined to form the test_mode field 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum { 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_NORMAL = 0, 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_NET_START = 1 << 0, 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_NET_READ = 1 << 1, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_CACHE_START = 1 << 2, 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_CACHE_READ = 1 << 3, 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_CACHE_WRITE = 1 << 4, 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ | 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ | 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TEST_MODE_SYNC_CACHE_WRITE) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request, 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* response_status, 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* response_headers, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* response_data); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct MockTransaction { 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* url; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* method; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If |request_time| is unspecified, the current time will be used. 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time request_time; 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* request_headers; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int load_flags; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* status; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* response_headers; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If |response_time| is unspecified, the current time will be used. 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time response_time; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* data; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int test_mode; 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MockTransactionHandler handler; 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::CertStatus cert_status; 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Value returned by MockNetworkTransaction::Start (potentially 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.) 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::Error return_code; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const MockTransaction kSimpleGET_Transaction; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const MockTransaction kSimplePOST_Transaction; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const MockTransaction kTypicalGET_Transaction; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const MockTransaction kETagGET_Transaction; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const MockTransaction kRangeGET_Transaction; 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returns the mock transaction for the given URL 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const MockTransaction* FindMockTransaction(const GURL& url); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Add/Remove a mock transaction that can be accessed via FindMockTransaction. 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There can be only one MockTransaction associated with a given URL. 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AddMockTransaction(const MockTransaction* trans); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RemoveMockTransaction(const MockTransaction* trans); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct ScopedMockTransaction : MockTransaction { 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ScopedMockTransaction() { 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AddMockTransaction(this); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit ScopedMockTransaction(const MockTransaction& t) 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : MockTransaction(t) { 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AddMockTransaction(this); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~ScopedMockTransaction() { 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RemoveMockTransaction(this); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//----------------------------------------------------------------------------- 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// mock http request 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockHttpRequest : public net::HttpRequestInfo { 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit MockHttpRequest(const MockTransaction& t); 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//----------------------------------------------------------------------------- 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use this class to test completely consuming a transaction 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestTransactionConsumer { 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TestTransactionConsumer(net::RequestPriority priority, 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::HttpTransactionFactory* factory); 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~TestTransactionConsumer(); 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Start(const net::HttpRequestInfo* request, 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::BoundNetLog& net_log); 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_done() const { return state_ == DONE; } 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int error() const { return error_; } 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::HttpResponseInfo* response_info() const { 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return trans_->GetResponseInfo(); 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& content() const { return content_; } 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum State { 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IDLE, 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STARTING, 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) READING, 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DONE 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DidStart(int result); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DidRead(int result); 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DidFinish(int result); 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Read(); 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void OnIOComplete(int result); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) State state_; 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<net::HttpTransaction> trans_; 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string content_; 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<net::IOBuffer> read_buf_; 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int error_; 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static int quit_counter_; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//----------------------------------------------------------------------------- 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// mock network layer 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockNetworkLayer; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This transaction class inspects the available set of mock transactions to 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// find data for the request URL. It supports IO operations that complete 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// synchronously or asynchronously to help exercise different code paths in the 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HttpCache implementation. 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class MockNetworkTransaction 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : public net::HttpTransaction, 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public base::SupportsWeakPtr<MockNetworkTransaction> { 165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper; 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MockNetworkTransaction(net::RequestPriority priority, 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MockNetworkLayer* factory); 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~MockNetworkTransaction(); 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int Start(const net::HttpRequestInfo* request, 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::CompletionCallback& callback, 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::BoundNetLog& net_log) OVERRIDE; 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int RestartIgnoringLastError( 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::CompletionCallback& callback) OVERRIDE; 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int RestartWithCertificate( 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::X509Certificate* client_cert, 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::CompletionCallback& callback) OVERRIDE; 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int RestartWithAuth( 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::AuthCredentials& credentials, 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::CompletionCallback& callback) OVERRIDE; 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool IsReadyToRestartForAuth() OVERRIDE; 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int Read(net::IOBuffer* buf, int buf_len, 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const net::CompletionCallback& callback) OVERRIDE; 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void StopCaching() OVERRIDE; 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 193eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch virtual bool GetFullRequestHeaders( 194eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch net::HttpRequestHeaders* headers) const OVERRIDE; 195eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void DoneReading() OVERRIDE; 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE; 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual net::LoadState GetLoadState() const OVERRIDE; 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual net::UploadProgress GetUploadProgress() const OVERRIDE; 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual bool GetLoadTimingInfo( 2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::LoadTimingInfo* load_timing_info) const OVERRIDE; 2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void SetPriority(net::RequestPriority priority) OVERRIDE; 2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 209f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) virtual void SetWebSocketHandshakeStreamCreateHelper( 210f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) CreateHelper* create_helper) OVERRIDE; 211f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 212f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) CreateHelper* websocket_handshake_stream_create_helper() { 213f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return websocket_handshake_stream_create_helper_; 214f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::RequestPriority priority() const { return priority_; } 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void CallbackLater(const net::CompletionCallback& callback, int result); 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void RunCallback(const net::CompletionCallback& callback, int result); 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::WeakPtrFactory<MockNetworkTransaction> weak_factory_; 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::HttpResponseInfo response_; 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string data_; 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int data_cursor_; 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int test_mode_; 2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::RequestPriority priority_; 227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) CreateHelper* websocket_handshake_stream_create_helper_; 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::WeakPtr<MockNetworkLayer> transaction_factory_; 22990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 23090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // NetLog ID of the fake / non-existent underlying socket used by the 23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // connection. Requires Start() be passed a BoundNetLog with a real NetLog to 23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // be initialized. 23390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) unsigned int socket_log_id_; 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockNetworkLayer : public net::HttpTransactionFactory, 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public base::SupportsWeakPtr<MockNetworkLayer> { 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) MockNetworkLayer(); 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~MockNetworkLayer(); 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int transaction_count() const { return transaction_count_; } 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool done_reading_called() const { return done_reading_called_; } 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void TransactionDoneReading(); 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns the last priority passed to CreateTransaction, or 2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // DEFAULT_PRIORITY if it hasn't been called yet. 2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::RequestPriority last_create_transaction_priority() const { 2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return last_create_transaction_priority_; 2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns the last transaction created by 2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // CreateTransaction. Returns a NULL WeakPtr if one has not been 2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // created yet, or the last transaction has been destroyed, or 2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // ClearLastTransaction() has been called and a new transaction 2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // hasn't been created yet. 2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::WeakPtr<MockNetworkTransaction> last_transaction() { 2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return last_transaction_; 2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Makes last_transaction() return NULL until the next transaction 2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // is created. 2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void ClearLastTransaction() { 2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) last_transaction_.reset(); 2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // net::HttpTransactionFactory: 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int CreateTransaction( 2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::RequestPriority priority, 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<net::HttpTransaction>* trans, 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) net::HttpTransactionDelegate* delegate) OVERRIDE; 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual net::HttpCache* GetCache() OVERRIDE; 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual net::HttpNetworkSession* GetSession() OVERRIDE; 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int transaction_count_; 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool done_reading_called_; 2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::RequestPriority last_create_transaction_priority_; 2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::WeakPtr<MockNetworkTransaction> last_transaction_; 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//----------------------------------------------------------------------------- 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// helpers 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// read the transaction completely 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ReadTransaction(net::HttpTransaction* trans, std::string* result); 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ 289