| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_ftp_job.h" | 5 #include "net/url_request/url_request_ftp_job.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 *resolver = std::move(owned_resolver); | 57 *resolver = std::move(owned_resolver); |
| 58 return OK; | 58 return OK; |
| 59 } | 59 } |
| 60 | 60 |
| 61 MockAsyncProxyResolver* resolver() { return resolver_; } | 61 MockAsyncProxyResolver* resolver() { return resolver_; } |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 MockAsyncProxyResolver* resolver_; | 64 MockAsyncProxyResolver* resolver_; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 class MockFtpTransactionFactory : public FtpTransactionFactory { |
| 68 public: |
| 69 std::unique_ptr<FtpTransaction> CreateTransaction() override { |
| 70 return std::unique_ptr<FtpTransaction>(); |
| 71 } |
| 72 |
| 73 void Suspend(bool suspend) override {} |
| 74 }; |
| 75 |
| 67 } // namespace | 76 } // namespace |
| 68 | 77 |
| 69 class FtpTestURLRequestContext : public TestURLRequestContext { | 78 class FtpTestURLRequestContext : public TestURLRequestContext { |
| 70 public: | 79 public: |
| 71 FtpTestURLRequestContext(ClientSocketFactory* socket_factory, | 80 FtpTestURLRequestContext(ClientSocketFactory* socket_factory, |
| 72 std::unique_ptr<ProxyService> proxy_service, | 81 std::unique_ptr<ProxyService> proxy_service, |
| 73 NetworkDelegate* network_delegate, | 82 NetworkDelegate* network_delegate) |
| 74 FtpTransactionFactory* ftp_transaction_factory) | 83 : TestURLRequestContext(true) { |
| 75 : TestURLRequestContext(true), | |
| 76 ftp_protocol_handler_(new FtpProtocolHandler(ftp_transaction_factory)) { | |
| 77 set_client_socket_factory(socket_factory); | 84 set_client_socket_factory(socket_factory); |
| 78 context_storage_.set_proxy_service(std::move(proxy_service)); | 85 context_storage_.set_proxy_service(std::move(proxy_service)); |
| 79 set_network_delegate(network_delegate); | 86 set_network_delegate(network_delegate); |
| 87 std::unique_ptr<FtpProtocolHandler> ftp_protocol_handler( |
| 88 FtpProtocolHandler::CreateForTesting( |
| 89 base::MakeUnique<MockFtpTransactionFactory>())); |
| 90 auth_cache_ = ftp_protocol_handler->ftp_auth_cache_.get(); |
| 80 std::unique_ptr<URLRequestJobFactoryImpl> job_factory = | 91 std::unique_ptr<URLRequestJobFactoryImpl> job_factory = |
| 81 base::WrapUnique(new URLRequestJobFactoryImpl); | 92 base::WrapUnique(new URLRequestJobFactoryImpl); |
| 82 job_factory->SetProtocolHandler("ftp", | 93 job_factory->SetProtocolHandler("ftp", std::move(ftp_protocol_handler)); |
| 83 base::WrapUnique(ftp_protocol_handler_)); | |
| 84 context_storage_.set_job_factory(std::move(job_factory)); | 94 context_storage_.set_job_factory(std::move(job_factory)); |
| 85 Init(); | 95 Init(); |
| 86 } | 96 } |
| 87 | 97 |
| 88 FtpAuthCache* GetFtpAuthCache() { | 98 FtpAuthCache* GetFtpAuthCache() { return auth_cache_; } |
| 89 return ftp_protocol_handler_->ftp_auth_cache_.get(); | |
| 90 } | |
| 91 | 99 |
| 92 void set_proxy_service(std::unique_ptr<ProxyService> proxy_service) { | 100 void set_proxy_service(std::unique_ptr<ProxyService> proxy_service) { |
| 93 context_storage_.set_proxy_service(std::move(proxy_service)); | 101 context_storage_.set_proxy_service(std::move(proxy_service)); |
| 94 } | 102 } |
| 95 | 103 |
| 96 private: | 104 private: |
| 97 FtpProtocolHandler* ftp_protocol_handler_; | 105 // Owned by the JobFactory's FtpProtocolHandler. |
| 106 FtpAuthCache* auth_cache_; |
| 98 }; | 107 }; |
| 99 | 108 |
| 100 namespace { | 109 namespace { |
| 101 | 110 |
| 102 class SimpleProxyConfigService : public ProxyConfigService { | 111 class SimpleProxyConfigService : public ProxyConfigService { |
| 103 public: | 112 public: |
| 104 SimpleProxyConfigService() { | 113 SimpleProxyConfigService() { |
| 105 // Any FTP requests that ever go through HTTP paths are proxied requests. | 114 // Any FTP requests that ever go through HTTP paths are proxied requests. |
| 106 config_.proxy_rules().ParseFromString("ftp=localhost"); | 115 config_.proxy_rules().ParseFromString("ftp=localhost"); |
| 107 } | 116 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 ~TestURLRequestFtpJob() override {} | 149 ~TestURLRequestFtpJob() override {} |
| 141 | 150 |
| 142 using URLRequestFtpJob::SetPriority; | 151 using URLRequestFtpJob::SetPriority; |
| 143 using URLRequestFtpJob::Start; | 152 using URLRequestFtpJob::Start; |
| 144 using URLRequestFtpJob::Kill; | 153 using URLRequestFtpJob::Kill; |
| 145 using URLRequestFtpJob::priority; | 154 using URLRequestFtpJob::priority; |
| 146 | 155 |
| 147 protected: | 156 protected: |
| 148 }; | 157 }; |
| 149 | 158 |
| 150 class MockFtpTransactionFactory : public FtpTransactionFactory { | |
| 151 public: | |
| 152 std::unique_ptr<FtpTransaction> CreateTransaction() override { | |
| 153 return std::unique_ptr<FtpTransaction>(); | |
| 154 } | |
| 155 | |
| 156 void Suspend(bool suspend) override {} | |
| 157 }; | |
| 158 | |
| 159 // Fixture for priority-related tests. Priority matters when there is | 159 // Fixture for priority-related tests. Priority matters when there is |
| 160 // an HTTP proxy. | 160 // an HTTP proxy. |
| 161 class URLRequestFtpJobPriorityTest : public testing::Test { | 161 class URLRequestFtpJobPriorityTest : public testing::Test { |
| 162 protected: | 162 protected: |
| 163 URLRequestFtpJobPriorityTest() | 163 URLRequestFtpJobPriorityTest() |
| 164 : proxy_service_(base::WrapUnique(new SimpleProxyConfigService), | 164 : proxy_service_(base::WrapUnique(new SimpleProxyConfigService), |
| 165 NULL, | 165 NULL, |
| 166 NULL), | 166 NULL), |
| 167 req_(context_.CreateRequest(GURL("ftp://ftp.example.com"), | 167 req_(context_.CreateRequest(GURL("ftp://ftp.example.com"), |
| 168 DEFAULT_PRIORITY, | 168 DEFAULT_PRIORITY, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } | 250 } |
| 251 | 251 |
| 252 class URLRequestFtpJobTest : public testing::Test { | 252 class URLRequestFtpJobTest : public testing::Test { |
| 253 public: | 253 public: |
| 254 URLRequestFtpJobTest() | 254 URLRequestFtpJobTest() |
| 255 : request_context_(&socket_factory_, | 255 : request_context_(&socket_factory_, |
| 256 base::MakeUnique<ProxyService>( | 256 base::MakeUnique<ProxyService>( |
| 257 base::WrapUnique(new SimpleProxyConfigService), | 257 base::WrapUnique(new SimpleProxyConfigService), |
| 258 nullptr, | 258 nullptr, |
| 259 nullptr), | 259 nullptr), |
| 260 &network_delegate_, | 260 &network_delegate_) {} |
| 261 &ftp_transaction_factory_) {} | |
| 262 | 261 |
| 263 ~URLRequestFtpJobTest() override { | 262 ~URLRequestFtpJobTest() override { |
| 264 // Clean up any remaining tasks that mess up unrelated tests. | 263 // Clean up any remaining tasks that mess up unrelated tests. |
| 265 base::RunLoop().RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
| 266 } | 265 } |
| 267 | 266 |
| 268 void AddSocket(MockRead* reads, size_t reads_size, | 267 void AddSocket(MockRead* reads, size_t reads_size, |
| 269 MockWrite* writes, size_t writes_size) { | 268 MockWrite* writes, size_t writes_size) { |
| 270 std::unique_ptr<SequencedSocketData> socket_data( | 269 std::unique_ptr<SequencedSocketData> socket_data( |
| 271 base::MakeUnique<SequencedSocketData>(reads, reads_size, writes, | 270 base::MakeUnique<SequencedSocketData>(reads, reads_size, writes, |
| 272 writes_size)); | 271 writes_size)); |
| 273 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 272 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 274 socket_factory_.AddSocketDataProvider(socket_data.get()); | 273 socket_factory_.AddSocketDataProvider(socket_data.get()); |
| 275 | 274 |
| 276 socket_data_.push_back(std::move(socket_data)); | 275 socket_data_.push_back(std::move(socket_data)); |
| 277 } | 276 } |
| 278 | 277 |
| 279 FtpTestURLRequestContext* request_context() { return &request_context_; } | 278 FtpTestURLRequestContext* request_context() { return &request_context_; } |
| 280 TestNetworkDelegate* network_delegate() { return &network_delegate_; } | 279 TestNetworkDelegate* network_delegate() { return &network_delegate_; } |
| 281 | 280 |
| 282 private: | 281 private: |
| 283 std::vector<std::unique_ptr<SequencedSocketData>> socket_data_; | 282 std::vector<std::unique_ptr<SequencedSocketData>> socket_data_; |
| 284 MockClientSocketFactory socket_factory_; | 283 MockClientSocketFactory socket_factory_; |
| 285 TestNetworkDelegate network_delegate_; | 284 TestNetworkDelegate network_delegate_; |
| 286 MockFtpTransactionFactory ftp_transaction_factory_; | |
| 287 | 285 |
| 288 FtpTestURLRequestContext request_context_; | 286 FtpTestURLRequestContext request_context_; |
| 289 }; | 287 }; |
| 290 | 288 |
| 291 TEST_F(URLRequestFtpJobTest, FtpProxyRequest) { | 289 TEST_F(URLRequestFtpJobTest, FtpProxyRequest) { |
| 292 MockWrite writes[] = { | 290 MockWrite writes[] = { |
| 293 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" | 291 MockWrite(ASYNC, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n" |
| 294 "Host: ftp.example.com\r\n" | 292 "Host: ftp.example.com\r\n" |
| 295 "Proxy-Connection: keep-alive\r\n\r\n"), | 293 "Proxy-Connection: keep-alive\r\n\r\n"), |
| 296 }; | 294 }; |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 801 EXPECT_TRUE(url_request2->status().is_success()); | 799 EXPECT_TRUE(url_request2->status().is_success()); |
| 802 EXPECT_EQ(2, network_delegate()->completed_requests()); | 800 EXPECT_EQ(2, network_delegate()->completed_requests()); |
| 803 EXPECT_EQ(0, network_delegate()->error_count()); | 801 EXPECT_EQ(0, network_delegate()->error_count()); |
| 804 EXPECT_FALSE(request_delegate2.auth_required_called()); | 802 EXPECT_FALSE(request_delegate2.auth_required_called()); |
| 805 EXPECT_EQ("test2.html", request_delegate2.data_received()); | 803 EXPECT_EQ("test2.html", request_delegate2.data_received()); |
| 806 } | 804 } |
| 807 | 805 |
| 808 } // namespace | 806 } // namespace |
| 809 | 807 |
| 810 } // namespace net | 808 } // namespace net |
| OLD | NEW |