Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / http / failing_http_transaction_factory.cc
blob9603b7aa49007cfbb2f7a9ef1c193ccff4b409a1
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"
7 #include <stdint.h>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "net/base/load_timing_info.h"
16 #include "net/base/upload_progress.h"
17 #include "net/http/http_response_info.h"
18 #include "net/socket/connection_attempts.h"
20 namespace net {
22 class AuthCredentials;
23 class BoundNetLog;
24 class HttpRequestHeaders;
25 class IOBuffer;
26 class X509Certificate;
28 namespace {
30 // A simple class to interpose between the cache and network http layers.
31 // These transactions can be generated by the FailingHttpTransactionFactory
32 // to test interactions between cache and network.
33 class FailingHttpTransaction : public HttpTransaction {
34 public:
35 explicit FailingHttpTransaction(Error error);
36 ~FailingHttpTransaction() override;
38 // HttpTransaction
39 int Start(const HttpRequestInfo* request_info,
40 const CompletionCallback& callback,
41 const BoundNetLog& net_log) override;
42 int RestartIgnoringLastError(const CompletionCallback& callback) override;
43 int RestartWithCertificate(X509Certificate* client_cert,
44 const CompletionCallback& callback) override;
45 int RestartWithAuth(const AuthCredentials& credentials,
46 const CompletionCallback& callback) override;
47 bool IsReadyToRestartForAuth() override;
48 int Read(IOBuffer* buf,
49 int buf_len,
50 const CompletionCallback& callback) override;
51 void StopCaching() override;
52 bool GetFullRequestHeaders(HttpRequestHeaders* headers) const override;
53 int64 GetTotalReceivedBytes() const override;
54 int64_t GetTotalSentBytes() const override;
55 void DoneReading() override;
56 const HttpResponseInfo* GetResponseInfo() const override;
57 LoadState GetLoadState() const override;
58 UploadProgress GetUploadProgress() const override;
59 void SetQuicServerInfo(QuicServerInfo* quic_server_info) override;
60 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
61 void SetPriority(RequestPriority priority) override;
62 void SetWebSocketHandshakeStreamCreateHelper(
63 WebSocketHandshakeStreamBase::CreateHelper* create_helper) override;
64 void SetBeforeNetworkStartCallback(
65 const BeforeNetworkStartCallback& callback) override;
66 void SetBeforeProxyHeadersSentCallback(
67 const BeforeProxyHeadersSentCallback& callback) override;
68 int ResumeNetworkStart() override;
69 void GetConnectionAttempts(ConnectionAttempts* out) const override;
71 private:
72 Error error_;
73 HttpResponseInfo response_;
76 FailingHttpTransaction::FailingHttpTransaction(Error error) : error_(error) {
77 DCHECK_LT(error, OK);
80 FailingHttpTransaction::~FailingHttpTransaction() {}
82 int FailingHttpTransaction::Start(const HttpRequestInfo* request_info,
83 const CompletionCallback& callback,
84 const BoundNetLog& net_log) {
85 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
86 base::Bind(callback, error_));
87 return ERR_IO_PENDING;
90 int FailingHttpTransaction::RestartIgnoringLastError(
91 const CompletionCallback& callback) {
92 return ERR_FAILED;
95 int FailingHttpTransaction::RestartWithCertificate(
96 X509Certificate* client_cert,
97 const CompletionCallback& callback) {
98 return ERR_FAILED;
101 int FailingHttpTransaction::RestartWithAuth(
102 const AuthCredentials& credentials,
103 const CompletionCallback& callback) {
104 return ERR_FAILED;
107 bool FailingHttpTransaction::IsReadyToRestartForAuth() {
108 return false;
111 int FailingHttpTransaction::Read(IOBuffer* buf, int buf_len,
112 const CompletionCallback& callback) {
113 NOTREACHED();
114 return ERR_FAILED;
117 void FailingHttpTransaction::StopCaching() {}
119 bool FailingHttpTransaction::GetFullRequestHeaders(
120 HttpRequestHeaders* headers) const {
121 return false;
124 int64 FailingHttpTransaction::GetTotalReceivedBytes() const {
125 return 0;
128 int64_t FailingHttpTransaction::GetTotalSentBytes() const {
129 return 0;
132 void FailingHttpTransaction::DoneReading() {
133 NOTREACHED();
136 const HttpResponseInfo* FailingHttpTransaction::GetResponseInfo() const {
137 return &response_;
140 LoadState FailingHttpTransaction::GetLoadState() const {
141 return LOAD_STATE_IDLE;
144 UploadProgress FailingHttpTransaction::GetUploadProgress() const {
145 return UploadProgress();
148 void FailingHttpTransaction::SetQuicServerInfo(
149 QuicServerInfo* quic_server_info) {
152 bool FailingHttpTransaction::GetLoadTimingInfo(
153 LoadTimingInfo* load_timing_info) const {
154 return false;
157 void FailingHttpTransaction::SetPriority(RequestPriority priority) {}
159 void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper(
160 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
161 NOTREACHED();
164 void FailingHttpTransaction::SetBeforeNetworkStartCallback(
165 const BeforeNetworkStartCallback& callback) {
168 void FailingHttpTransaction::SetBeforeProxyHeadersSentCallback(
169 const BeforeProxyHeadersSentCallback& callback) {
172 int FailingHttpTransaction::ResumeNetworkStart() {
173 NOTREACHED();
174 return ERR_FAILED;
177 void FailingHttpTransaction::GetConnectionAttempts(
178 ConnectionAttempts* out) const {
179 NOTIMPLEMENTED();
182 } // namespace
184 FailingHttpTransactionFactory::FailingHttpTransactionFactory(
185 HttpNetworkSession* session,
186 Error error) : session_(session), error_(error) {
187 DCHECK_LT(error, OK);
190 FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
192 // HttpTransactionFactory:
193 int FailingHttpTransactionFactory::CreateTransaction(
194 RequestPriority priority,
195 scoped_ptr<HttpTransaction>* trans) {
196 trans->reset(new FailingHttpTransaction(error_));
197 return OK;
200 HttpCache* FailingHttpTransactionFactory::GetCache() {
201 return NULL;
204 HttpNetworkSession* FailingHttpTransactionFactory::GetSession() {
205 return session_;
208 } // namespace net