[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 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" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame^] | 9 | #include "base/location.h" |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 10 | #include "base/logging.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame^] | 11 | #include "base/single_thread_task_runner.h" |
| 12 | #include "base/thread_task_runner_handle.h" |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 13 | #include "net/base/load_timing_info.h" |
| 14 | #include "net/base/upload_progress.h" |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 15 | #include "net/http/http_response_info.h" |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 16 | #include "net/socket/connection_attempts.h" |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | class AuthCredentials; |
| 21 | class BoundNetLog; |
| 22 | class HttpRequestHeaders; |
| 23 | class IOBuffer; |
| 24 | class X509Certificate; |
| 25 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 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 { |
| 32 | public: |
| 33 | explicit FailingHttpTransaction(Error error); |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 34 | ~FailingHttpTransaction() override; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 35 | |
| 36 | // HttpTransaction |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 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, |
| 47 | int buf_len, |
| 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; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 56 | void SetQuicServerInfo(QuicServerInfo* quic_server_info) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 57 | bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override; |
| 58 | void SetPriority(RequestPriority priority) override; |
| 59 | void SetWebSocketHandshakeStreamCreateHelper( |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 60 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 61 | void SetBeforeNetworkStartCallback( |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 62 | const BeforeNetworkStartCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 63 | void SetBeforeProxyHeadersSentCallback( |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 64 | const BeforeProxyHeadersSentCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 65 | int ResumeNetworkStart() override; |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 66 | void GetConnectionAttempts(ConnectionAttempts* out) const override; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | Error error_; |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 70 | HttpResponseInfo response_; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | FailingHttpTransaction::FailingHttpTransaction(Error error) : error_(error) { |
| 74 | DCHECK_LT(error, OK); |
| 75 | } |
| 76 | |
| 77 | FailingHttpTransaction::~FailingHttpTransaction() {} |
| 78 | |
| 79 | int FailingHttpTransaction::Start(const HttpRequestInfo* request_info, |
| 80 | const CompletionCallback& callback, |
| 81 | const BoundNetLog& net_log) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame^] | 82 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 83 | base::Bind(callback, error_)); |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 84 | return ERR_IO_PENDING; |
| 85 | } |
| 86 | |
| 87 | int FailingHttpTransaction::RestartIgnoringLastError( |
| 88 | const CompletionCallback& callback) { |
| 89 | return ERR_FAILED; |
| 90 | } |
| 91 | |
| 92 | int FailingHttpTransaction::RestartWithCertificate( |
| 93 | X509Certificate* client_cert, |
| 94 | const CompletionCallback& callback) { |
| 95 | return ERR_FAILED; |
| 96 | } |
| 97 | |
| 98 | int FailingHttpTransaction::RestartWithAuth( |
| 99 | const AuthCredentials& credentials, |
| 100 | const CompletionCallback& callback) { |
| 101 | return ERR_FAILED; |
| 102 | } |
| 103 | |
| 104 | bool FailingHttpTransaction::IsReadyToRestartForAuth() { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | int FailingHttpTransaction::Read(IOBuffer* buf, int buf_len, |
| 109 | const CompletionCallback& callback) { |
| 110 | NOTREACHED(); |
| 111 | return ERR_FAILED; |
| 112 | } |
| 113 | |
| 114 | void FailingHttpTransaction::StopCaching() {} |
| 115 | |
| 116 | bool FailingHttpTransaction::GetFullRequestHeaders( |
| 117 | HttpRequestHeaders* headers) const { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | int64 FailingHttpTransaction::GetTotalReceivedBytes() const { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | void FailingHttpTransaction::DoneReading() { |
| 126 | NOTREACHED(); |
| 127 | } |
| 128 | |
| 129 | const HttpResponseInfo* FailingHttpTransaction::GetResponseInfo() const { |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 130 | return &response_; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | LoadState FailingHttpTransaction::GetLoadState() const { |
| 134 | return LOAD_STATE_IDLE; |
| 135 | } |
| 136 | |
| 137 | UploadProgress FailingHttpTransaction::GetUploadProgress() const { |
| 138 | return UploadProgress(); |
| 139 | } |
| 140 | |
| 141 | void FailingHttpTransaction::SetQuicServerInfo( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 142 | QuicServerInfo* quic_server_info) { |
| 143 | } |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 144 | |
| 145 | bool FailingHttpTransaction::GetLoadTimingInfo( |
| 146 | LoadTimingInfo* load_timing_info) const { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | void FailingHttpTransaction::SetPriority(RequestPriority priority) {} |
| 151 | |
| 152 | void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper( |
| 153 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) { |
| 154 | NOTREACHED(); |
| 155 | } |
| 156 | |
| 157 | void FailingHttpTransaction::SetBeforeNetworkStartCallback( |
| 158 | const BeforeNetworkStartCallback& callback) { |
| 159 | } |
| 160 | |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 161 | void FailingHttpTransaction::SetBeforeProxyHeadersSentCallback( |
| 162 | const BeforeProxyHeadersSentCallback& callback) { |
| 163 | } |
| 164 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 165 | int FailingHttpTransaction::ResumeNetworkStart() { |
| 166 | NOTREACHED(); |
| 167 | return ERR_FAILED; |
| 168 | } |
| 169 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 170 | void FailingHttpTransaction::GetConnectionAttempts( |
| 171 | ConnectionAttempts* out) const { |
| 172 | NOTIMPLEMENTED(); |
| 173 | } |
| 174 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 175 | } // namespace |
| 176 | |
| 177 | FailingHttpTransactionFactory::FailingHttpTransactionFactory( |
| 178 | HttpNetworkSession* session, |
| 179 | Error error) : session_(session), error_(error) { |
| 180 | DCHECK_LT(error, OK); |
| 181 | } |
| 182 | |
| 183 | FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {} |
| 184 | |
| 185 | // HttpTransactionFactory: |
| 186 | int FailingHttpTransactionFactory::CreateTransaction( |
| 187 | RequestPriority priority, |
| 188 | scoped_ptr<HttpTransaction>* trans) { |
| 189 | trans->reset(new FailingHttpTransaction(error_)); |
| 190 | return OK; |
| 191 | } |
| 192 | |
| 193 | HttpCache* FailingHttpTransactionFactory::GetCache() { |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | HttpNetworkSession* FailingHttpTransactionFactory::GetSession() { |
| 198 | return session_; |
| 199 | } |
| 200 | |
| 201 | } // namespace net |
| 202 | |