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.
4
5#include "net/http/failing_http_transaction_factory.h"
6
7#include "base/bind.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"
13
14namespace net {
15
16class AuthCredentials;
17class BoundNetLog;
18class HttpRequestHeaders;
19class IOBuffer;
20class X509Certificate;
21
22struct HttpRequestInfo;
23
24namespace {
25
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.
29class FailingHttpTransaction : public HttpTransaction {
30 public:
31  explicit FailingHttpTransaction(Error error);
32  virtual ~FailingHttpTransaction();
33
34  // HttpTransaction
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;
67
68 private:
69  Error error_;
70};
71
72FailingHttpTransaction::FailingHttpTransaction(Error error) : error_(error) {
73  DCHECK_LT(error, OK);
74}
75
76FailingHttpTransaction::~FailingHttpTransaction() {}
77
78int 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;
84}
85
86int FailingHttpTransaction::RestartIgnoringLastError(
87    const CompletionCallback& callback)  {
88  return ERR_FAILED;
89}
90
91int FailingHttpTransaction::RestartWithCertificate(
92    X509Certificate* client_cert,
93    const CompletionCallback& callback)  {
94  return ERR_FAILED;
95}
96
97int FailingHttpTransaction::RestartWithAuth(
98    const AuthCredentials& credentials,
99    const CompletionCallback& callback)  {
100  return ERR_FAILED;
101}
102
103bool FailingHttpTransaction::IsReadyToRestartForAuth()  {
104  return false;
105}
106
107int FailingHttpTransaction::Read(IOBuffer* buf, int buf_len,
108                                 const CompletionCallback& callback)  {
109  NOTREACHED();
110  return ERR_FAILED;
111}
112
113void FailingHttpTransaction::StopCaching()  {}
114
115bool FailingHttpTransaction::GetFullRequestHeaders(
116    HttpRequestHeaders* headers) const  {
117  return false;
118}
119
120int64 FailingHttpTransaction::GetTotalReceivedBytes() const  {
121  return 0;
122}
123
124void FailingHttpTransaction::DoneReading()  {
125  NOTREACHED();
126}
127
128const HttpResponseInfo* FailingHttpTransaction::GetResponseInfo() const  {
129  return NULL;
130}
131
132LoadState FailingHttpTransaction::GetLoadState() const  {
133  return LOAD_STATE_IDLE;
134}
135
136UploadProgress FailingHttpTransaction::GetUploadProgress() const  {
137  return UploadProgress();
138}
139
140void FailingHttpTransaction::SetQuicServerInfo(
141    net::QuicServerInfo* quic_server_info) {}
142
143bool FailingHttpTransaction::GetLoadTimingInfo(
144    LoadTimingInfo* load_timing_info) const  {
145  return false;
146}
147
148void FailingHttpTransaction::SetPriority(RequestPriority priority)  {}
149
150void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper(
151    WebSocketHandshakeStreamBase::CreateHelper* create_helper)  {
152  NOTREACHED();
153}
154
155void FailingHttpTransaction::SetBeforeNetworkStartCallback(
156    const BeforeNetworkStartCallback& callback)  {
157}
158
159int FailingHttpTransaction::ResumeNetworkStart()  {
160  NOTREACHED();
161  return ERR_FAILED;
162}
163
164}  // namespace
165
166FailingHttpTransactionFactory::FailingHttpTransactionFactory(
167    HttpNetworkSession* session,
168    Error error) : session_(session), error_(error) {
169  DCHECK_LT(error, OK);
170}
171
172FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {}
173
174// HttpTransactionFactory:
175int FailingHttpTransactionFactory::CreateTransaction(
176    RequestPriority priority,
177    scoped_ptr<HttpTransaction>* trans) {
178  trans->reset(new FailingHttpTransaction(error_));
179  return OK;
180}
181
182HttpCache* FailingHttpTransactionFactory::GetCache() {
183  return NULL;
184}
185
186HttpNetworkSession* FailingHttpTransactionFactory::GetSession() {
187  return session_;
188}
189
190}  // namespace net
191
192