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 int ResumeNetworkStart() OVERRIDE
;
72 FailingHttpTransaction::FailingHttpTransaction(Error error
) : error_(error
) {
76 FailingHttpTransaction::~FailingHttpTransaction() {}
78 int FailingHttpTransaction::Start(const HttpRequestInfo
* request_info
,
79 const CompletionCallback
& callback
,
80 const BoundNetLog
& net_log
) {
81 base::MessageLoop::current()->PostTask(
82 FROM_HERE
, base::Bind(callback
, error_
));
83 return ERR_IO_PENDING
;
86 int FailingHttpTransaction::RestartIgnoringLastError(
87 const CompletionCallback
& callback
) {
91 int FailingHttpTransaction::RestartWithCertificate(
92 X509Certificate
* client_cert
,
93 const CompletionCallback
& callback
) {
97 int FailingHttpTransaction::RestartWithAuth(
98 const AuthCredentials
& credentials
,
99 const CompletionCallback
& callback
) {
103 bool FailingHttpTransaction::IsReadyToRestartForAuth() {
107 int FailingHttpTransaction::Read(IOBuffer
* buf
, int buf_len
,
108 const CompletionCallback
& callback
) {
113 void FailingHttpTransaction::StopCaching() {}
115 bool FailingHttpTransaction::GetFullRequestHeaders(
116 HttpRequestHeaders
* headers
) const {
120 int64
FailingHttpTransaction::GetTotalReceivedBytes() const {
124 void FailingHttpTransaction::DoneReading() {
128 const HttpResponseInfo
* FailingHttpTransaction::GetResponseInfo() const {
132 LoadState
FailingHttpTransaction::GetLoadState() const {
133 return LOAD_STATE_IDLE
;
136 UploadProgress
FailingHttpTransaction::GetUploadProgress() const {
137 return UploadProgress();
140 void FailingHttpTransaction::SetQuicServerInfo(
141 net::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 int FailingHttpTransaction::ResumeNetworkStart() {
166 FailingHttpTransactionFactory::FailingHttpTransactionFactory(
167 HttpNetworkSession
* session
,
168 Error error
) : session_(session
), error_(error
) {
169 DCHECK_LT(error
, OK
);
172 FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
174 // HttpTransactionFactory:
175 int FailingHttpTransactionFactory::CreateTransaction(
176 RequestPriority priority
,
177 scoped_ptr
<HttpTransaction
>* trans
) {
178 trans
->reset(new FailingHttpTransaction(error_
));
182 HttpCache
* FailingHttpTransactionFactory::GetCache() {
186 HttpNetworkSession
* FailingHttpTransactionFactory::GetSession() {