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"
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "net/base/load_timing_info.h"
12 #include "net/base/upload_progress.h"
13 #include "net/http/http_response_info.h"
14 #include "net/socket/connection_attempts.h"
18 class AuthCredentials
;
20 class HttpRequestHeaders
;
22 class X509Certificate
;
26 // A simple class to interpose between the cache and network http layers.
27 // These transactions can be generated by the FailingHttpTransactionFactory
28 // to test interactions between cache and network.
29 class FailingHttpTransaction
: public HttpTransaction
{
31 explicit FailingHttpTransaction(Error error
);
32 ~FailingHttpTransaction() override
;
35 int Start(const HttpRequestInfo
* request_info
,
36 const CompletionCallback
& callback
,
37 const BoundNetLog
& net_log
) override
;
38 int RestartIgnoringLastError(const CompletionCallback
& callback
) override
;
39 int RestartWithCertificate(X509Certificate
* client_cert
,
40 const CompletionCallback
& callback
) override
;
41 int RestartWithAuth(const AuthCredentials
& credentials
,
42 const CompletionCallback
& callback
) override
;
43 bool IsReadyToRestartForAuth() override
;
44 int Read(IOBuffer
* buf
,
46 const CompletionCallback
& callback
) override
;
47 void StopCaching() override
;
48 bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const override
;
49 int64
GetTotalReceivedBytes() const override
;
50 void DoneReading() override
;
51 const HttpResponseInfo
* GetResponseInfo() const override
;
52 LoadState
GetLoadState() const override
;
53 UploadProgress
GetUploadProgress() const override
;
54 void SetQuicServerInfo(QuicServerInfo
* quic_server_info
) override
;
55 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
56 void SetPriority(RequestPriority priority
) override
;
57 void SetWebSocketHandshakeStreamCreateHelper(
58 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) override
;
59 void SetBeforeNetworkStartCallback(
60 const BeforeNetworkStartCallback
& callback
) override
;
61 void SetBeforeProxyHeadersSentCallback(
62 const BeforeProxyHeadersSentCallback
& callback
) override
;
63 int ResumeNetworkStart() override
;
64 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
68 HttpResponseInfo response_
;
71 FailingHttpTransaction::FailingHttpTransaction(Error error
) : error_(error
) {
75 FailingHttpTransaction::~FailingHttpTransaction() {}
77 int FailingHttpTransaction::Start(const HttpRequestInfo
* request_info
,
78 const CompletionCallback
& callback
,
79 const BoundNetLog
& net_log
) {
80 base::MessageLoop::current()->PostTask(
81 FROM_HERE
, base::Bind(callback
, error_
));
82 return ERR_IO_PENDING
;
85 int FailingHttpTransaction::RestartIgnoringLastError(
86 const CompletionCallback
& callback
) {
90 int FailingHttpTransaction::RestartWithCertificate(
91 X509Certificate
* client_cert
,
92 const CompletionCallback
& callback
) {
96 int FailingHttpTransaction::RestartWithAuth(
97 const AuthCredentials
& credentials
,
98 const CompletionCallback
& callback
) {
102 bool FailingHttpTransaction::IsReadyToRestartForAuth() {
106 int FailingHttpTransaction::Read(IOBuffer
* buf
, int buf_len
,
107 const CompletionCallback
& callback
) {
112 void FailingHttpTransaction::StopCaching() {}
114 bool FailingHttpTransaction::GetFullRequestHeaders(
115 HttpRequestHeaders
* headers
) const {
119 int64
FailingHttpTransaction::GetTotalReceivedBytes() const {
123 void FailingHttpTransaction::DoneReading() {
127 const HttpResponseInfo
* FailingHttpTransaction::GetResponseInfo() const {
131 LoadState
FailingHttpTransaction::GetLoadState() const {
132 return LOAD_STATE_IDLE
;
135 UploadProgress
FailingHttpTransaction::GetUploadProgress() const {
136 return UploadProgress();
139 void FailingHttpTransaction::SetQuicServerInfo(
140 QuicServerInfo
* quic_server_info
) {
143 bool FailingHttpTransaction::GetLoadTimingInfo(
144 LoadTimingInfo
* load_timing_info
) const {
148 void FailingHttpTransaction::SetPriority(RequestPriority priority
) {}
150 void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper(
151 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
155 void FailingHttpTransaction::SetBeforeNetworkStartCallback(
156 const BeforeNetworkStartCallback
& callback
) {
159 void FailingHttpTransaction::SetBeforeProxyHeadersSentCallback(
160 const BeforeProxyHeadersSentCallback
& callback
) {
163 int FailingHttpTransaction::ResumeNetworkStart() {
168 void FailingHttpTransaction::GetConnectionAttempts(
169 ConnectionAttempts
* out
) const {
175 FailingHttpTransactionFactory::FailingHttpTransactionFactory(
176 HttpNetworkSession
* session
,
177 Error error
) : session_(session
), error_(error
) {
178 DCHECK_LT(error
, OK
);
181 FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
183 // HttpTransactionFactory:
184 int FailingHttpTransactionFactory::CreateTransaction(
185 RequestPriority priority
,
186 scoped_ptr
<HttpTransaction
>* trans
) {
187 trans
->reset(new FailingHttpTransaction(error_
));
191 HttpCache
* FailingHttpTransactionFactory::GetCache() {
195 HttpNetworkSession
* FailingHttpTransactionFactory::GetSession() {