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 ~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(net::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
;
69 FailingHttpTransaction::FailingHttpTransaction(Error error
) : error_(error
) {
73 FailingHttpTransaction::~FailingHttpTransaction() {}
75 int FailingHttpTransaction::Start(const HttpRequestInfo
* request_info
,
76 const CompletionCallback
& callback
,
77 const BoundNetLog
& net_log
) {
78 base::MessageLoop::current()->PostTask(
79 FROM_HERE
, base::Bind(callback
, error_
));
80 return ERR_IO_PENDING
;
83 int FailingHttpTransaction::RestartIgnoringLastError(
84 const CompletionCallback
& callback
) {
88 int FailingHttpTransaction::RestartWithCertificate(
89 X509Certificate
* client_cert
,
90 const CompletionCallback
& callback
) {
94 int FailingHttpTransaction::RestartWithAuth(
95 const AuthCredentials
& credentials
,
96 const CompletionCallback
& callback
) {
100 bool FailingHttpTransaction::IsReadyToRestartForAuth() {
104 int FailingHttpTransaction::Read(IOBuffer
* buf
, int buf_len
,
105 const CompletionCallback
& callback
) {
110 void FailingHttpTransaction::StopCaching() {}
112 bool FailingHttpTransaction::GetFullRequestHeaders(
113 HttpRequestHeaders
* headers
) const {
117 int64
FailingHttpTransaction::GetTotalReceivedBytes() const {
121 void FailingHttpTransaction::DoneReading() {
125 const HttpResponseInfo
* FailingHttpTransaction::GetResponseInfo() const {
129 LoadState
FailingHttpTransaction::GetLoadState() const {
130 return LOAD_STATE_IDLE
;
133 UploadProgress
FailingHttpTransaction::GetUploadProgress() const {
134 return UploadProgress();
137 void FailingHttpTransaction::SetQuicServerInfo(
138 net::QuicServerInfo
* quic_server_info
) {}
140 bool FailingHttpTransaction::GetLoadTimingInfo(
141 LoadTimingInfo
* load_timing_info
) const {
145 void FailingHttpTransaction::SetPriority(RequestPriority priority
) {}
147 void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper(
148 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
152 void FailingHttpTransaction::SetBeforeNetworkStartCallback(
153 const BeforeNetworkStartCallback
& callback
) {
156 void FailingHttpTransaction::SetBeforeProxyHeadersSentCallback(
157 const BeforeProxyHeadersSentCallback
& callback
) {
160 int FailingHttpTransaction::ResumeNetworkStart() {
167 FailingHttpTransactionFactory::FailingHttpTransactionFactory(
168 HttpNetworkSession
* session
,
169 Error error
) : session_(session
), error_(error
) {
170 DCHECK_LT(error
, OK
);
173 FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
175 // HttpTransactionFactory:
176 int FailingHttpTransactionFactory::CreateTransaction(
177 RequestPriority priority
,
178 scoped_ptr
<HttpTransaction
>* trans
) {
179 trans
->reset(new FailingHttpTransaction(error_
));
183 HttpCache
* FailingHttpTransactionFactory::GetCache() {
187 HttpNetworkSession
* FailingHttpTransactionFactory::GetSession() {