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.
5 #include "net/http/failing_http_transaction_factory.h"
10 #include "base/compiler_specific.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "net/base/load_timing_info.h"
16 #include "net/base/upload_progress.h"
17 #include "net/http/http_response_info.h"
18 #include "net/socket/connection_attempts.h"
22 class AuthCredentials
;
24 class HttpRequestHeaders
;
26 class X509Certificate
;
30 // A simple class to interpose between the cache and network http layers.
31 // These transactions can be generated by the FailingHttpTransactionFactory
32 // to test interactions between cache and network.
33 class FailingHttpTransaction
: public HttpTransaction
{
35 explicit FailingHttpTransaction(Error error
);
36 ~FailingHttpTransaction() override
;
39 int Start(const HttpRequestInfo
* request_info
,
40 const CompletionCallback
& callback
,
41 const BoundNetLog
& net_log
) override
;
42 int RestartIgnoringLastError(const CompletionCallback
& callback
) override
;
43 int RestartWithCertificate(X509Certificate
* client_cert
,
44 const CompletionCallback
& callback
) override
;
45 int RestartWithAuth(const AuthCredentials
& credentials
,
46 const CompletionCallback
& callback
) override
;
47 bool IsReadyToRestartForAuth() override
;
48 int Read(IOBuffer
* buf
,
50 const CompletionCallback
& callback
) override
;
51 void StopCaching() override
;
52 bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const override
;
53 int64
GetTotalReceivedBytes() const override
;
54 int64_t GetTotalSentBytes() const override
;
55 void DoneReading() override
;
56 const HttpResponseInfo
* GetResponseInfo() const override
;
57 LoadState
GetLoadState() const override
;
58 UploadProgress
GetUploadProgress() const override
;
59 void SetQuicServerInfo(QuicServerInfo
* quic_server_info
) override
;
60 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
61 void SetPriority(RequestPriority priority
) override
;
62 void SetWebSocketHandshakeStreamCreateHelper(
63 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) override
;
64 void SetBeforeNetworkStartCallback(
65 const BeforeNetworkStartCallback
& callback
) override
;
66 void SetBeforeProxyHeadersSentCallback(
67 const BeforeProxyHeadersSentCallback
& callback
) override
;
68 int ResumeNetworkStart() override
;
69 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
73 HttpResponseInfo response_
;
76 FailingHttpTransaction::FailingHttpTransaction(Error error
) : error_(error
) {
80 FailingHttpTransaction::~FailingHttpTransaction() {}
82 int FailingHttpTransaction::Start(const HttpRequestInfo
* request_info
,
83 const CompletionCallback
& callback
,
84 const BoundNetLog
& net_log
) {
85 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
86 base::Bind(callback
, error_
));
87 return ERR_IO_PENDING
;
90 int FailingHttpTransaction::RestartIgnoringLastError(
91 const CompletionCallback
& callback
) {
95 int FailingHttpTransaction::RestartWithCertificate(
96 X509Certificate
* client_cert
,
97 const CompletionCallback
& callback
) {
101 int FailingHttpTransaction::RestartWithAuth(
102 const AuthCredentials
& credentials
,
103 const CompletionCallback
& callback
) {
107 bool FailingHttpTransaction::IsReadyToRestartForAuth() {
111 int FailingHttpTransaction::Read(IOBuffer
* buf
, int buf_len
,
112 const CompletionCallback
& callback
) {
117 void FailingHttpTransaction::StopCaching() {}
119 bool FailingHttpTransaction::GetFullRequestHeaders(
120 HttpRequestHeaders
* headers
) const {
124 int64
FailingHttpTransaction::GetTotalReceivedBytes() const {
128 int64_t FailingHttpTransaction::GetTotalSentBytes() const {
132 void FailingHttpTransaction::DoneReading() {
136 const HttpResponseInfo
* FailingHttpTransaction::GetResponseInfo() const {
140 LoadState
FailingHttpTransaction::GetLoadState() const {
141 return LOAD_STATE_IDLE
;
144 UploadProgress
FailingHttpTransaction::GetUploadProgress() const {
145 return UploadProgress();
148 void FailingHttpTransaction::SetQuicServerInfo(
149 QuicServerInfo
* quic_server_info
) {
152 bool FailingHttpTransaction::GetLoadTimingInfo(
153 LoadTimingInfo
* load_timing_info
) const {
157 void FailingHttpTransaction::SetPriority(RequestPriority priority
) {}
159 void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper(
160 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
164 void FailingHttpTransaction::SetBeforeNetworkStartCallback(
165 const BeforeNetworkStartCallback
& callback
) {
168 void FailingHttpTransaction::SetBeforeProxyHeadersSentCallback(
169 const BeforeProxyHeadersSentCallback
& callback
) {
172 int FailingHttpTransaction::ResumeNetworkStart() {
177 void FailingHttpTransaction::GetConnectionAttempts(
178 ConnectionAttempts
* out
) const {
184 FailingHttpTransactionFactory::FailingHttpTransactionFactory(
185 HttpNetworkSession
* session
,
186 Error error
) : session_(session
), error_(error
) {
187 DCHECK_LT(error
, OK
);
190 FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
192 // HttpTransactionFactory:
193 int FailingHttpTransactionFactory::CreateTransaction(
194 RequestPriority priority
,
195 scoped_ptr
<HttpTransaction
>* trans
) {
196 trans
->reset(new FailingHttpTransaction(error_
));
200 HttpCache
* FailingHttpTransactionFactory::GetCache() {
204 HttpNetworkSession
* FailingHttpTransactionFactory::GetSession() {