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"
16 class AuthCredentials
;
18 class HttpRequestHeaders
;
20 class X509Certificate
;
22 struct HttpRequestInfo
;
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 virtual ~FailingHttpTransaction();
35 virtual int Start(const HttpRequestInfo
* request_info
,
36 const CompletionCallback
& callback
,
37 const BoundNetLog
& net_log
) OVERRIDE
;
38 virtual int RestartIgnoringLastError(
39 const CompletionCallback
& callback
) OVERRIDE
;
40 virtual int RestartWithCertificate(
41 X509Certificate
* client_cert
,
42 const CompletionCallback
& callback
) OVERRIDE
;
43 virtual int RestartWithAuth(
44 const AuthCredentials
& credentials
,
45 const CompletionCallback
& callback
) OVERRIDE
;
46 virtual bool IsReadyToRestartForAuth() OVERRIDE
;
47 virtual int Read(IOBuffer
* buf
, int buf_len
,
48 const CompletionCallback
& callback
) OVERRIDE
;
49 virtual void StopCaching() OVERRIDE
;
50 virtual bool GetFullRequestHeaders(
51 HttpRequestHeaders
* headers
) const OVERRIDE
;
52 virtual int64
GetTotalReceivedBytes() const OVERRIDE
;
53 virtual void DoneReading() OVERRIDE
;
54 virtual const HttpResponseInfo
* GetResponseInfo() const OVERRIDE
;
55 virtual LoadState
GetLoadState() const OVERRIDE
;
56 virtual UploadProgress
GetUploadProgress() const OVERRIDE
;
57 virtual void SetQuicServerInfo(
58 net::QuicServerInfo
* quic_server_info
) OVERRIDE
;
59 virtual bool GetLoadTimingInfo(
60 LoadTimingInfo
* load_timing_info
) const OVERRIDE
;
61 virtual void SetPriority(RequestPriority priority
) OVERRIDE
;
62 virtual void SetWebSocketHandshakeStreamCreateHelper(
63 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) OVERRIDE
;
64 virtual void SetBeforeNetworkStartCallback(
65 const BeforeNetworkStartCallback
& callback
) OVERRIDE
;
66 virtual void SetBeforeProxyHeadersSentCallback(
67 const BeforeProxyHeadersSentCallback
& callback
) OVERRIDE
;
68 virtual int ResumeNetworkStart() OVERRIDE
;
74 FailingHttpTransaction::FailingHttpTransaction(Error error
) : error_(error
) {
78 FailingHttpTransaction::~FailingHttpTransaction() {}
80 int FailingHttpTransaction::Start(const HttpRequestInfo
* request_info
,
81 const CompletionCallback
& callback
,
82 const BoundNetLog
& net_log
) {
83 base::MessageLoop::current()->PostTask(
84 FROM_HERE
, base::Bind(callback
, error_
));
85 return ERR_IO_PENDING
;
88 int FailingHttpTransaction::RestartIgnoringLastError(
89 const CompletionCallback
& callback
) {
93 int FailingHttpTransaction::RestartWithCertificate(
94 X509Certificate
* client_cert
,
95 const CompletionCallback
& callback
) {
99 int FailingHttpTransaction::RestartWithAuth(
100 const AuthCredentials
& credentials
,
101 const CompletionCallback
& callback
) {
105 bool FailingHttpTransaction::IsReadyToRestartForAuth() {
109 int FailingHttpTransaction::Read(IOBuffer
* buf
, int buf_len
,
110 const CompletionCallback
& callback
) {
115 void FailingHttpTransaction::StopCaching() {}
117 bool FailingHttpTransaction::GetFullRequestHeaders(
118 HttpRequestHeaders
* headers
) const {
122 int64
FailingHttpTransaction::GetTotalReceivedBytes() const {
126 void FailingHttpTransaction::DoneReading() {
130 const HttpResponseInfo
* FailingHttpTransaction::GetResponseInfo() const {
134 LoadState
FailingHttpTransaction::GetLoadState() const {
135 return LOAD_STATE_IDLE
;
138 UploadProgress
FailingHttpTransaction::GetUploadProgress() const {
139 return UploadProgress();
142 void FailingHttpTransaction::SetQuicServerInfo(
143 net::QuicServerInfo
* quic_server_info
) {}
145 bool FailingHttpTransaction::GetLoadTimingInfo(
146 LoadTimingInfo
* load_timing_info
) const {
150 void FailingHttpTransaction::SetPriority(RequestPriority priority
) {}
152 void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper(
153 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
157 void FailingHttpTransaction::SetBeforeNetworkStartCallback(
158 const BeforeNetworkStartCallback
& callback
) {
161 void FailingHttpTransaction::SetBeforeProxyHeadersSentCallback(
162 const BeforeProxyHeadersSentCallback
& callback
) {
165 int FailingHttpTransaction::ResumeNetworkStart() {
172 FailingHttpTransactionFactory::FailingHttpTransactionFactory(
173 HttpNetworkSession
* session
,
174 Error error
) : session_(session
), error_(error
) {
175 DCHECK_LT(error
, OK
);
178 FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
180 // HttpTransactionFactory:
181 int FailingHttpTransactionFactory::CreateTransaction(
182 RequestPriority priority
,
183 scoped_ptr
<HttpTransaction
>* trans
) {
184 trans
->reset(new FailingHttpTransaction(error_
));
188 HttpCache
* FailingHttpTransactionFactory::GetCache() {
192 HttpNetworkSession
* FailingHttpTransactionFactory::GetSession() {