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/location.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "net/base/load_timing_info.h"
14 #include "net/base/upload_progress.h"
15 #include "net/http/http_response_info.h"
16 #include "net/socket/connection_attempts.h"
20 class AuthCredentials
;
22 class HttpRequestHeaders
;
24 class X509Certificate
;
28 // A simple class to interpose between the cache and network http layers.
29 // These transactions can be generated by the FailingHttpTransactionFactory
30 // to test interactions between cache and network.
31 class FailingHttpTransaction
: public HttpTransaction
{
33 explicit FailingHttpTransaction(Error error
);
34 ~FailingHttpTransaction() override
;
37 int Start(const HttpRequestInfo
* request_info
,
38 const CompletionCallback
& callback
,
39 const BoundNetLog
& net_log
) override
;
40 int RestartIgnoringLastError(const CompletionCallback
& callback
) override
;
41 int RestartWithCertificate(X509Certificate
* client_cert
,
42 const CompletionCallback
& callback
) override
;
43 int RestartWithAuth(const AuthCredentials
& credentials
,
44 const CompletionCallback
& callback
) override
;
45 bool IsReadyToRestartForAuth() override
;
46 int Read(IOBuffer
* buf
,
48 const CompletionCallback
& callback
) override
;
49 void StopCaching() override
;
50 bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const override
;
51 int64
GetTotalReceivedBytes() const override
;
52 void DoneReading() override
;
53 const HttpResponseInfo
* GetResponseInfo() const override
;
54 LoadState
GetLoadState() const override
;
55 UploadProgress
GetUploadProgress() const override
;
56 void SetQuicServerInfo(QuicServerInfo
* quic_server_info
) override
;
57 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
58 void SetPriority(RequestPriority priority
) override
;
59 void SetWebSocketHandshakeStreamCreateHelper(
60 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) override
;
61 void SetBeforeNetworkStartCallback(
62 const BeforeNetworkStartCallback
& callback
) override
;
63 void SetBeforeProxyHeadersSentCallback(
64 const BeforeProxyHeadersSentCallback
& callback
) override
;
65 int ResumeNetworkStart() override
;
66 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
70 HttpResponseInfo response_
;
73 FailingHttpTransaction::FailingHttpTransaction(Error error
) : error_(error
) {
77 FailingHttpTransaction::~FailingHttpTransaction() {}
79 int FailingHttpTransaction::Start(const HttpRequestInfo
* request_info
,
80 const CompletionCallback
& callback
,
81 const BoundNetLog
& net_log
) {
82 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
83 base::Bind(callback
, error_
));
84 return ERR_IO_PENDING
;
87 int FailingHttpTransaction::RestartIgnoringLastError(
88 const CompletionCallback
& callback
) {
92 int FailingHttpTransaction::RestartWithCertificate(
93 X509Certificate
* client_cert
,
94 const CompletionCallback
& callback
) {
98 int FailingHttpTransaction::RestartWithAuth(
99 const AuthCredentials
& credentials
,
100 const CompletionCallback
& callback
) {
104 bool FailingHttpTransaction::IsReadyToRestartForAuth() {
108 int FailingHttpTransaction::Read(IOBuffer
* buf
, int buf_len
,
109 const CompletionCallback
& callback
) {
114 void FailingHttpTransaction::StopCaching() {}
116 bool FailingHttpTransaction::GetFullRequestHeaders(
117 HttpRequestHeaders
* headers
) const {
121 int64
FailingHttpTransaction::GetTotalReceivedBytes() const {
125 void FailingHttpTransaction::DoneReading() {
129 const HttpResponseInfo
* FailingHttpTransaction::GetResponseInfo() const {
133 LoadState
FailingHttpTransaction::GetLoadState() const {
134 return LOAD_STATE_IDLE
;
137 UploadProgress
FailingHttpTransaction::GetUploadProgress() const {
138 return UploadProgress();
141 void FailingHttpTransaction::SetQuicServerInfo(
142 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() {
170 void FailingHttpTransaction::GetConnectionAttempts(
171 ConnectionAttempts
* out
) const {
177 FailingHttpTransactionFactory::FailingHttpTransactionFactory(
178 HttpNetworkSession
* session
,
179 Error error
) : session_(session
), error_(error
) {
180 DCHECK_LT(error
, OK
);
183 FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
185 // HttpTransactionFactory:
186 int FailingHttpTransactionFactory::CreateTransaction(
187 RequestPriority priority
,
188 scoped_ptr
<HttpTransaction
>* trans
) {
189 trans
->reset(new FailingHttpTransaction(error_
));
193 HttpCache
* FailingHttpTransactionFactory::GetCache() {
197 HttpNetworkSession
* FailingHttpTransactionFactory::GetSession() {