http_transaction_unittest.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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#ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ 6#define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ 7#pragma once 8 9#include "net/http/http_transaction.h" 10 11#include <string> 12 13#include "base/callback.h" 14#include "base/compiler_specific.h" 15#include "base/string16.h" 16#include "net/base/io_buffer.h" 17#include "net/base/load_flags.h" 18#include "net/base/net_errors.h" 19#include "net/base/test_completion_callback.h" 20#include "net/disk_cache/disk_cache.h" 21#include "net/http/http_cache.h" 22#include "net/http/http_request_info.h" 23#include "net/http/http_response_headers.h" 24#include "net/http/http_response_info.h" 25 26namespace net { 27class IOBuffer; 28} 29 30//----------------------------------------------------------------------------- 31// mock transaction data 32 33// these flags may be combined to form the test_mode field 34enum { 35 TEST_MODE_NORMAL = 0, 36 TEST_MODE_SYNC_NET_START = 1 << 0, 37 TEST_MODE_SYNC_NET_READ = 1 << 1, 38 TEST_MODE_SYNC_CACHE_START = 1 << 2, 39 TEST_MODE_SYNC_CACHE_READ = 1 << 3, 40 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4, 41 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ | 42 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ | 43 TEST_MODE_SYNC_CACHE_WRITE) 44}; 45 46typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request, 47 std::string* response_status, 48 std::string* response_headers, 49 std::string* response_data); 50 51struct MockTransaction { 52 const char* url; 53 const char* method; 54 // If |request_time| is unspecified, the current time will be used. 55 base::Time request_time; 56 const char* request_headers; 57 int load_flags; 58 const char* status; 59 const char* response_headers; 60 // If |response_time| is unspecified, the current time will be used. 61 base::Time response_time; 62 const char* data; 63 int test_mode; 64 MockTransactionHandler handler; 65 int cert_status; 66}; 67 68extern const MockTransaction kSimpleGET_Transaction; 69extern const MockTransaction kSimplePOST_Transaction; 70extern const MockTransaction kTypicalGET_Transaction; 71extern const MockTransaction kETagGET_Transaction; 72extern const MockTransaction kRangeGET_Transaction; 73 74// returns the mock transaction for the given URL 75const MockTransaction* FindMockTransaction(const GURL& url); 76 77// Add/Remove a mock transaction that can be accessed via FindMockTransaction. 78// There can be only one MockTransaction associated with a given URL. 79void AddMockTransaction(const MockTransaction* trans); 80void RemoveMockTransaction(const MockTransaction* trans); 81 82struct ScopedMockTransaction : MockTransaction { 83 ScopedMockTransaction() { 84 AddMockTransaction(this); 85 } 86 explicit ScopedMockTransaction(const MockTransaction& t) 87 : MockTransaction(t) { 88 AddMockTransaction(this); 89 } 90 ~ScopedMockTransaction() { 91 RemoveMockTransaction(this); 92 } 93}; 94 95//----------------------------------------------------------------------------- 96// mock http request 97 98class MockHttpRequest : public net::HttpRequestInfo { 99 public: 100 explicit MockHttpRequest(const MockTransaction& t); 101}; 102 103//----------------------------------------------------------------------------- 104// use this class to test completely consuming a transaction 105 106class TestTransactionConsumer : public CallbackRunner< Tuple1<int> > { 107 public: 108 explicit TestTransactionConsumer(net::HttpTransactionFactory* factory); 109 virtual ~TestTransactionConsumer(); 110 111 void Start(const net::HttpRequestInfo* request, 112 const net::BoundNetLog& net_log); 113 114 bool is_done() const { return state_ == DONE; } 115 int error() const { return error_; } 116 117 const net::HttpResponseInfo* response_info() const { 118 return trans_->GetResponseInfo(); 119 } 120 const std::string& content() const { return content_; } 121 122 private: 123 enum State { 124 IDLE, 125 STARTING, 126 READING, 127 DONE 128 }; 129 130 void DidStart(int result); 131 void DidRead(int result); 132 void DidFinish(int result); 133 void Read(); 134 135 // Callback implementation: 136 virtual void RunWithParams(const Tuple1<int>& params); 137 138 State state_; 139 scoped_ptr<net::HttpTransaction> trans_; 140 std::string content_; 141 scoped_refptr<net::IOBuffer> read_buf_; 142 int error_; 143 144 static int quit_counter_; 145}; 146 147//----------------------------------------------------------------------------- 148// mock network layer 149 150// This transaction class inspects the available set of mock transactions to 151// find data for the request URL. It supports IO operations that complete 152// synchronously or asynchronously to help exercise different code paths in the 153// HttpCache implementation. 154class MockNetworkTransaction : public net::HttpTransaction { 155 public: 156 MockNetworkTransaction(); 157 virtual ~MockNetworkTransaction(); 158 159 virtual int Start(const net::HttpRequestInfo* request, 160 net::CompletionCallback* callback, 161 const net::BoundNetLog& net_log); 162 163 virtual int RestartIgnoringLastError(net::CompletionCallback* callback); 164 165 virtual int RestartWithCertificate(net::X509Certificate* client_cert, 166 net::CompletionCallback* callback); 167 168 virtual int RestartWithAuth(const string16& username, 169 const string16& password, 170 net::CompletionCallback* callback); 171 172 virtual bool IsReadyToRestartForAuth(); 173 174 virtual int Read(net::IOBuffer* buf, int buf_len, 175 net::CompletionCallback* callback); 176 177 virtual void StopCaching(); 178 179 virtual const net::HttpResponseInfo* GetResponseInfo() const; 180 181 virtual net::LoadState GetLoadState() const; 182 183 virtual uint64 GetUploadProgress() const; 184 185 private: 186 void CallbackLater(net::CompletionCallback* callback, int result); 187 void RunCallback(net::CompletionCallback* callback, int result); 188 189 ScopedRunnableMethodFactory<MockNetworkTransaction> task_factory_; 190 net::HttpResponseInfo response_; 191 std::string data_; 192 int data_cursor_; 193 int test_mode_; 194}; 195 196class MockNetworkLayer : public net::HttpTransactionFactory { 197 public: 198 MockNetworkLayer(); 199 virtual ~MockNetworkLayer(); 200 201 int transaction_count() const { return transaction_count_; } 202 203 // net::HttpTransactionFactory: 204 virtual int CreateTransaction(scoped_ptr<net::HttpTransaction>* trans); 205 virtual net::HttpCache* GetCache(); 206 virtual net::HttpNetworkSession* GetSession(); 207 virtual void Suspend(bool suspend); 208 209 private: 210 int transaction_count_; 211}; 212 213//----------------------------------------------------------------------------- 214// helpers 215 216// read the transaction completely 217int ReadTransaction(net::HttpTransaction* trans, std::string* result); 218 219#endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ 220