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
3 // found in the LICENSE file.
5 #include "net/url_request/url_request_ftp_job.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/run_loop.h"
11 #include "net/base/host_port_pair.h"
12 #include "net/base/request_priority.h"
13 #include "net/ftp/ftp_auth_cache.h"
14 #include "net/http/http_transaction_test_util.h"
15 #include "net/proxy/mock_proxy_resolver.h"
16 #include "net/proxy/proxy_config_service.h"
17 #include "net/proxy/proxy_config_service_fixed.h"
18 #include "net/socket/socket_test_util.h"
19 #include "net/url_request/ftp_protocol_handler.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_job_factory_impl.h"
23 #include "net/url_request/url_request_status.h"
24 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h"
28 using base::ASCIIToUTF16
;
33 class MockProxyResolverFactory
: public ProxyResolverFactory
{
35 MockProxyResolverFactory() : ProxyResolverFactory(false) {}
36 int CreateProxyResolver(
37 const scoped_refptr
<ProxyResolverScriptData
>& pac_script
,
38 scoped_ptr
<ProxyResolver
>* resolver
,
39 const CompletionCallback
& callback
,
40 scoped_ptr
<Request
>* request
) override
{
41 resolver
->reset(new MockAsyncProxyResolver());
48 class FtpTestURLRequestContext
: public TestURLRequestContext
{
50 FtpTestURLRequestContext(ClientSocketFactory
* socket_factory
,
51 ProxyService
* proxy_service
,
52 NetworkDelegate
* network_delegate
,
53 FtpTransactionFactory
* ftp_transaction_factory
)
54 : TestURLRequestContext(true),
55 ftp_protocol_handler_(new FtpProtocolHandler(ftp_transaction_factory
)) {
56 set_client_socket_factory(socket_factory
);
57 context_storage_
.set_proxy_service(proxy_service
);
58 set_network_delegate(network_delegate
);
59 URLRequestJobFactoryImpl
* job_factory
= new URLRequestJobFactoryImpl
;
60 job_factory
->SetProtocolHandler("ftp", ftp_protocol_handler_
);
61 context_storage_
.set_job_factory(job_factory
);
65 FtpAuthCache
* GetFtpAuthCache() {
66 return ftp_protocol_handler_
->ftp_auth_cache_
.get();
69 void set_proxy_service(ProxyService
* proxy_service
) {
70 context_storage_
.set_proxy_service(proxy_service
);
74 FtpProtocolHandler
* ftp_protocol_handler_
;
79 class SimpleProxyConfigService
: public ProxyConfigService
{
81 SimpleProxyConfigService() {
82 // Any FTP requests that ever go through HTTP paths are proxied requests.
83 config_
.proxy_rules().ParseFromString("ftp=localhost");
86 void AddObserver(Observer
* observer
) override
{ observer_
= observer
; }
88 void RemoveObserver(Observer
* observer
) override
{
89 if (observer_
== observer
) {
94 ConfigAvailability
GetLatestProxyConfig(ProxyConfig
* config
) override
{
99 void IncrementConfigId() {
100 config_
.set_id(config_
.id() + 1);
101 observer_
->OnProxyConfigChanged(config_
, ProxyConfigService::CONFIG_VALID
);
109 // Inherit from URLRequestFtpJob to expose the priority and some
110 // other hidden functions.
111 class TestURLRequestFtpJob
: public URLRequestFtpJob
{
113 TestURLRequestFtpJob(URLRequest
* request
,
114 FtpTransactionFactory
* ftp_factory
,
115 FtpAuthCache
* ftp_auth_cache
)
116 : URLRequestFtpJob(request
, NULL
, ftp_factory
, ftp_auth_cache
) {}
118 using URLRequestFtpJob::SetPriority
;
119 using URLRequestFtpJob::Start
;
120 using URLRequestFtpJob::Kill
;
121 using URLRequestFtpJob::priority
;
124 ~TestURLRequestFtpJob() override
{}
127 class MockFtpTransactionFactory
: public FtpTransactionFactory
{
129 FtpTransaction
* CreateTransaction() override
{ return NULL
; }
131 void Suspend(bool suspend
) override
{}
134 // Fixture for priority-related tests. Priority matters when there is
136 class URLRequestFtpJobPriorityTest
: public testing::Test
{
138 URLRequestFtpJobPriorityTest()
139 : proxy_service_(new SimpleProxyConfigService
, NULL
, NULL
),
140 req_(context_
.CreateRequest(GURL("ftp://ftp.example.com"),
143 context_
.set_proxy_service(&proxy_service_
);
144 context_
.set_http_transaction_factory(&network_layer_
);
147 ProxyService proxy_service_
;
148 MockNetworkLayer network_layer_
;
149 MockFtpTransactionFactory ftp_factory_
;
150 FtpAuthCache ftp_auth_cache_
;
151 TestURLRequestContext context_
;
152 TestDelegate delegate_
;
153 scoped_ptr
<URLRequest
> req_
;
156 // Make sure that SetPriority actually sets the URLRequestFtpJob's
157 // priority, both before and after start.
158 TEST_F(URLRequestFtpJobPriorityTest
, SetPriorityBasic
) {
159 scoped_refptr
<TestURLRequestFtpJob
> job(new TestURLRequestFtpJob(
160 req_
.get(), &ftp_factory_
, &ftp_auth_cache_
));
161 EXPECT_EQ(DEFAULT_PRIORITY
, job
->priority());
163 job
->SetPriority(LOWEST
);
164 EXPECT_EQ(LOWEST
, job
->priority());
166 job
->SetPriority(LOW
);
167 EXPECT_EQ(LOW
, job
->priority());
170 EXPECT_EQ(LOW
, job
->priority());
172 job
->SetPriority(MEDIUM
);
173 EXPECT_EQ(MEDIUM
, job
->priority());
176 // Make sure that URLRequestFtpJob passes on its priority to its
177 // transaction on start.
178 TEST_F(URLRequestFtpJobPriorityTest
, SetTransactionPriorityOnStart
) {
179 scoped_refptr
<TestURLRequestFtpJob
> job(new TestURLRequestFtpJob(
180 req_
.get(), &ftp_factory_
, &ftp_auth_cache_
));
181 job
->SetPriority(LOW
);
183 EXPECT_FALSE(network_layer_
.last_transaction());
187 ASSERT_TRUE(network_layer_
.last_transaction());
188 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
191 // Make sure that URLRequestFtpJob passes on its priority updates to
193 TEST_F(URLRequestFtpJobPriorityTest
, SetTransactionPriority
) {
194 scoped_refptr
<TestURLRequestFtpJob
> job(new TestURLRequestFtpJob(
195 req_
.get(), &ftp_factory_
, &ftp_auth_cache_
));
196 job
->SetPriority(LOW
);
198 ASSERT_TRUE(network_layer_
.last_transaction());
199 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
201 job
->SetPriority(HIGHEST
);
202 EXPECT_EQ(HIGHEST
, network_layer_
.last_transaction()->priority());
205 // Make sure that URLRequestFtpJob passes on its priority updates to
206 // newly-created transactions after the first one.
207 TEST_F(URLRequestFtpJobPriorityTest
, SetSubsequentTransactionPriority
) {
208 scoped_refptr
<TestURLRequestFtpJob
> job(new TestURLRequestFtpJob(
209 req_
.get(), &ftp_factory_
, &ftp_auth_cache_
));
212 job
->SetPriority(LOW
);
213 ASSERT_TRUE(network_layer_
.last_transaction());
214 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
217 network_layer_
.ClearLastTransaction();
219 // Creates a second transaction.
221 ASSERT_TRUE(network_layer_
.last_transaction());
222 EXPECT_EQ(LOW
, network_layer_
.last_transaction()->priority());
225 class URLRequestFtpJobTest
: public testing::Test
{
227 URLRequestFtpJobTest()
228 : request_context_(&socket_factory_
,
230 new SimpleProxyConfigService
, NULL
, NULL
),
232 &ftp_transaction_factory_
) {
235 ~URLRequestFtpJobTest() override
{
236 // Clean up any remaining tasks that mess up unrelated tests.
237 base::RunLoop run_loop
;
238 run_loop
.RunUntilIdle();
241 void AddSocket(MockRead
* reads
, size_t reads_size
,
242 MockWrite
* writes
, size_t writes_size
) {
243 DeterministicSocketData
* socket_data
= new DeterministicSocketData(
244 reads
, reads_size
, writes
, writes_size
);
245 socket_data
->set_connect_data(MockConnect(SYNCHRONOUS
, OK
));
246 socket_data
->StopAfter(reads_size
+ writes_size
- 1);
247 socket_factory_
.AddSocketDataProvider(socket_data
);
249 socket_data_
.push_back(socket_data
);
252 FtpTestURLRequestContext
* request_context() { return &request_context_
; }
253 TestNetworkDelegate
* network_delegate() { return &network_delegate_
; }
254 DeterministicSocketData
* socket_data(size_t index
) {
255 return socket_data_
[index
];
259 ScopedVector
<DeterministicSocketData
> socket_data_
;
260 DeterministicMockClientSocketFactory socket_factory_
;
261 TestNetworkDelegate network_delegate_
;
262 MockFtpTransactionFactory ftp_transaction_factory_
;
264 FtpTestURLRequestContext request_context_
;
267 TEST_F(URLRequestFtpJobTest
, FtpProxyRequest
) {
268 MockWrite writes
[] = {
269 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
270 "Host: ftp.example.com\r\n"
271 "Proxy-Connection: keep-alive\r\n\r\n"),
274 MockRead(ASYNC
, 1, "HTTP/1.1 200 OK\r\n"),
275 MockRead(ASYNC
, 2, "Content-Length: 9\r\n\r\n"),
276 MockRead(ASYNC
, 3, "test.html"),
279 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
281 TestDelegate request_delegate
;
282 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
283 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
284 url_request
->Start();
285 ASSERT_TRUE(url_request
->is_pending());
286 socket_data(0)->RunFor(4);
288 EXPECT_TRUE(url_request
->status().is_success());
289 EXPECT_TRUE(url_request
->proxy_server().Equals(
290 HostPortPair::FromString("localhost:80")));
291 EXPECT_EQ(1, network_delegate()->completed_requests());
292 EXPECT_EQ(0, network_delegate()->error_count());
293 EXPECT_FALSE(request_delegate
.auth_required_called());
294 EXPECT_EQ("test.html", request_delegate
.data_received());
297 // Regression test for http://crbug.com/237526 .
298 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestOrphanJob
) {
299 // Use a PAC URL so that URLRequestFtpJob's |pac_request_| field is non-NULL.
300 request_context()->set_proxy_service(new ProxyService(
301 new ProxyConfigServiceFixed(
302 ProxyConfig::CreateFromCustomPacURL(GURL("http://foo"))),
303 make_scoped_ptr(new MockProxyResolverFactory
), NULL
));
305 TestDelegate request_delegate
;
306 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
307 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
308 url_request
->Start();
310 // Now |url_request| will be deleted before its completion,
311 // resulting in it being orphaned. It should not crash.
314 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestNeedProxyAuthNoCredentials
) {
315 MockWrite writes
[] = {
316 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
317 "Host: ftp.example.com\r\n"
318 "Proxy-Connection: keep-alive\r\n\r\n"),
322 MockRead(ASYNC
, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"),
323 MockRead(ASYNC
, 2, "Proxy-Authenticate: Basic "
324 "realm=\"MyRealm1\"\r\n"),
325 MockRead(ASYNC
, 3, "Content-Length: 9\r\n\r\n"),
326 MockRead(ASYNC
, 4, "test.html"),
329 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
331 TestDelegate request_delegate
;
332 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
333 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
334 url_request
->Start();
335 ASSERT_TRUE(url_request
->is_pending());
336 socket_data(0)->RunFor(5);
338 EXPECT_TRUE(url_request
->status().is_success());
339 EXPECT_TRUE(url_request
->proxy_server().Equals(
340 HostPortPair::FromString("localhost:80")));
341 EXPECT_EQ(1, network_delegate()->completed_requests());
342 EXPECT_EQ(0, network_delegate()->error_count());
343 EXPECT_TRUE(request_delegate
.auth_required_called());
344 EXPECT_EQ("test.html", request_delegate
.data_received());
347 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestNeedProxyAuthWithCredentials
) {
348 MockWrite writes
[] = {
349 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
350 "Host: ftp.example.com\r\n"
351 "Proxy-Connection: keep-alive\r\n\r\n"),
352 MockWrite(ASYNC
, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
353 "Host: ftp.example.com\r\n"
354 "Proxy-Connection: keep-alive\r\n"
355 "Proxy-Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"),
359 MockRead(ASYNC
, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"),
360 MockRead(ASYNC
, 2, "Proxy-Authenticate: Basic "
361 "realm=\"MyRealm1\"\r\n"),
362 MockRead(ASYNC
, 3, "Content-Length: 9\r\n\r\n"),
363 MockRead(ASYNC
, 4, "test.html"),
366 MockRead(ASYNC
, 6, "HTTP/1.1 200 OK\r\n"),
367 MockRead(ASYNC
, 7, "Content-Length: 10\r\n\r\n"),
368 MockRead(ASYNC
, 8, "test2.html"),
371 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
373 TestDelegate request_delegate
;
374 request_delegate
.set_credentials(
375 AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass")));
376 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
377 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
378 url_request
->Start();
379 ASSERT_TRUE(url_request
->is_pending());
380 socket_data(0)->RunFor(9);
382 EXPECT_TRUE(url_request
->status().is_success());
383 EXPECT_EQ(1, network_delegate()->completed_requests());
384 EXPECT_EQ(0, network_delegate()->error_count());
385 EXPECT_TRUE(request_delegate
.auth_required_called());
386 EXPECT_EQ("test2.html", request_delegate
.data_received());
389 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestNeedServerAuthNoCredentials
) {
390 MockWrite writes
[] = {
391 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
392 "Host: ftp.example.com\r\n"
393 "Proxy-Connection: keep-alive\r\n\r\n"),
397 MockRead(ASYNC
, 1, "HTTP/1.1 401 Unauthorized\r\n"),
398 MockRead(ASYNC
, 2, "WWW-Authenticate: Basic "
399 "realm=\"MyRealm1\"\r\n"),
400 MockRead(ASYNC
, 3, "Content-Length: 9\r\n\r\n"),
401 MockRead(ASYNC
, 4, "test.html"),
404 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
406 TestDelegate request_delegate
;
407 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
408 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
409 url_request
->Start();
410 ASSERT_TRUE(url_request
->is_pending());
411 socket_data(0)->RunFor(5);
413 EXPECT_TRUE(url_request
->status().is_success());
414 EXPECT_EQ(1, network_delegate()->completed_requests());
415 EXPECT_EQ(0, network_delegate()->error_count());
416 EXPECT_TRUE(request_delegate
.auth_required_called());
417 EXPECT_EQ("test.html", request_delegate
.data_received());
420 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestNeedServerAuthWithCredentials
) {
421 MockWrite writes
[] = {
422 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
423 "Host: ftp.example.com\r\n"
424 "Proxy-Connection: keep-alive\r\n\r\n"),
425 MockWrite(ASYNC
, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
426 "Host: ftp.example.com\r\n"
427 "Proxy-Connection: keep-alive\r\n"
428 "Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"),
432 MockRead(ASYNC
, 1, "HTTP/1.1 401 Unauthorized\r\n"),
433 MockRead(ASYNC
, 2, "WWW-Authenticate: Basic "
434 "realm=\"MyRealm1\"\r\n"),
435 MockRead(ASYNC
, 3, "Content-Length: 9\r\n\r\n"),
436 MockRead(ASYNC
, 4, "test.html"),
439 MockRead(ASYNC
, 6, "HTTP/1.1 200 OK\r\n"),
440 MockRead(ASYNC
, 7, "Content-Length: 10\r\n\r\n"),
441 MockRead(ASYNC
, 8, "test2.html"),
444 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
446 TestDelegate request_delegate
;
447 request_delegate
.set_credentials(
448 AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass")));
449 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
450 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
451 url_request
->Start();
452 ASSERT_TRUE(url_request
->is_pending());
453 socket_data(0)->RunFor(9);
455 EXPECT_TRUE(url_request
->status().is_success());
456 EXPECT_EQ(1, network_delegate()->completed_requests());
457 EXPECT_EQ(0, network_delegate()->error_count());
458 EXPECT_TRUE(request_delegate
.auth_required_called());
459 EXPECT_EQ("test2.html", request_delegate
.data_received());
462 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestNeedProxyAndServerAuth
) {
463 MockWrite writes
[] = {
464 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
465 "Host: ftp.example.com\r\n"
466 "Proxy-Connection: keep-alive\r\n\r\n"),
467 MockWrite(ASYNC
, 5, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
468 "Host: ftp.example.com\r\n"
469 "Proxy-Connection: keep-alive\r\n"
470 "Proxy-Authorization: Basic "
471 "cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"),
472 MockWrite(ASYNC
, 10, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
473 "Host: ftp.example.com\r\n"
474 "Proxy-Connection: keep-alive\r\n"
475 "Proxy-Authorization: Basic "
476 "cHJveHl1c2VyOnByb3h5cGFzcw==\r\n"
477 "Authorization: Basic bXl1c2VyOm15cGFzcw==\r\n\r\n"),
481 MockRead(ASYNC
, 1, "HTTP/1.1 407 Proxy Authentication Required\r\n"),
482 MockRead(ASYNC
, 2, "Proxy-Authenticate: Basic "
483 "realm=\"MyRealm1\"\r\n"),
484 MockRead(ASYNC
, 3, "Content-Length: 9\r\n\r\n"),
485 MockRead(ASYNC
, 4, "test.html"),
488 MockRead(ASYNC
, 6, "HTTP/1.1 401 Unauthorized\r\n"),
489 MockRead(ASYNC
, 7, "WWW-Authenticate: Basic "
490 "realm=\"MyRealm1\"\r\n"),
491 MockRead(ASYNC
, 8, "Content-Length: 9\r\n\r\n"),
492 MockRead(ASYNC
, 9, "test.html"),
495 MockRead(ASYNC
, 11, "HTTP/1.1 200 OK\r\n"),
496 MockRead(ASYNC
, 12, "Content-Length: 10\r\n\r\n"),
497 MockRead(ASYNC
, 13, "test2.html"),
500 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
502 GURL
url("ftp://ftp.example.com");
504 // Make sure cached FTP credentials are not used for proxy authentication.
505 request_context()->GetFtpAuthCache()->Add(
507 AuthCredentials(ASCIIToUTF16("userdonotuse"),
508 ASCIIToUTF16("passworddonotuse")));
510 TestDelegate request_delegate
;
511 request_delegate
.set_credentials(
512 AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")));
513 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
514 url
, DEFAULT_PRIORITY
, &request_delegate
));
515 url_request
->Start();
516 ASSERT_TRUE(url_request
->is_pending());
517 socket_data(0)->RunFor(5);
519 request_delegate
.set_credentials(
520 AuthCredentials(ASCIIToUTF16("myuser"), ASCIIToUTF16("mypass")));
521 socket_data(0)->RunFor(9);
523 EXPECT_TRUE(url_request
->status().is_success());
524 EXPECT_EQ(1, network_delegate()->completed_requests());
525 EXPECT_EQ(0, network_delegate()->error_count());
526 EXPECT_TRUE(request_delegate
.auth_required_called());
527 EXPECT_EQ("test2.html", request_delegate
.data_received());
530 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestDoNotSaveCookies
) {
531 MockWrite writes
[] = {
532 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
533 "Host: ftp.example.com\r\n"
534 "Proxy-Connection: keep-alive\r\n\r\n"),
537 MockRead(ASYNC
, 1, "HTTP/1.1 200 OK\r\n"),
538 MockRead(ASYNC
, 2, "Content-Length: 9\r\n"),
539 MockRead(ASYNC
, 3, "Set-Cookie: name=value\r\n\r\n"),
540 MockRead(ASYNC
, 4, "test.html"),
543 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
545 TestDelegate request_delegate
;
546 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
547 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
548 url_request
->Start();
549 ASSERT_TRUE(url_request
->is_pending());
551 socket_data(0)->RunFor(5);
553 EXPECT_TRUE(url_request
->status().is_success());
554 EXPECT_EQ(1, network_delegate()->completed_requests());
555 EXPECT_EQ(0, network_delegate()->error_count());
557 // Make sure we do not accept cookies.
558 EXPECT_EQ(0, network_delegate()->set_cookie_count());
560 EXPECT_FALSE(request_delegate
.auth_required_called());
561 EXPECT_EQ("test.html", request_delegate
.data_received());
564 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestDoNotFollowRedirects
) {
565 MockWrite writes
[] = {
566 MockWrite(SYNCHRONOUS
, 0, "GET ftp://ftp.example.com/ HTTP/1.1\r\n"
567 "Host: ftp.example.com\r\n"
568 "Proxy-Connection: keep-alive\r\n\r\n"),
571 MockRead(SYNCHRONOUS
, 1, "HTTP/1.1 302 Found\r\n"),
572 MockRead(ASYNC
, 2, "Location: http://other.example.com/\r\n\r\n"),
575 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
577 TestDelegate request_delegate
;
578 scoped_ptr
<URLRequest
> url_request(request_context()->CreateRequest(
579 GURL("ftp://ftp.example.com/"), DEFAULT_PRIORITY
, &request_delegate
));
580 url_request
->Start();
581 EXPECT_TRUE(url_request
->is_pending());
583 base::MessageLoop::current()->RunUntilIdle();
585 EXPECT_TRUE(url_request
->is_pending());
586 EXPECT_EQ(0, request_delegate
.response_started_count());
587 EXPECT_EQ(0, network_delegate()->error_count());
588 ASSERT_TRUE(url_request
->status().is_success());
590 socket_data(0)->RunFor(1);
592 EXPECT_EQ(1, network_delegate()->completed_requests());
593 EXPECT_EQ(1, network_delegate()->error_count());
594 EXPECT_FALSE(url_request
->status().is_success());
595 EXPECT_EQ(ERR_UNSAFE_REDIRECT
, url_request
->status().error());
598 // We should re-use socket for requests using the same scheme, host, and port.
599 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestReuseSocket
) {
600 MockWrite writes
[] = {
601 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/first HTTP/1.1\r\n"
602 "Host: ftp.example.com\r\n"
603 "Proxy-Connection: keep-alive\r\n\r\n"),
604 MockWrite(ASYNC
, 4, "GET ftp://ftp.example.com/second HTTP/1.1\r\n"
605 "Host: ftp.example.com\r\n"
606 "Proxy-Connection: keep-alive\r\n\r\n"),
609 MockRead(ASYNC
, 1, "HTTP/1.1 200 OK\r\n"),
610 MockRead(ASYNC
, 2, "Content-Length: 10\r\n\r\n"),
611 MockRead(ASYNC
, 3, "test1.html"),
612 MockRead(ASYNC
, 5, "HTTP/1.1 200 OK\r\n"),
613 MockRead(ASYNC
, 6, "Content-Length: 10\r\n\r\n"),
614 MockRead(ASYNC
, 7, "test2.html"),
617 AddSocket(reads
, arraysize(reads
), writes
, arraysize(writes
));
619 TestDelegate request_delegate1
;
621 scoped_ptr
<URLRequest
> url_request1(
622 request_context()->CreateRequest(GURL("ftp://ftp.example.com/first"),
623 DEFAULT_PRIORITY
, &request_delegate1
));
624 url_request1
->Start();
625 ASSERT_TRUE(url_request1
->is_pending());
626 socket_data(0)->RunFor(4);
628 EXPECT_TRUE(url_request1
->status().is_success());
629 EXPECT_TRUE(url_request1
->proxy_server().Equals(
630 HostPortPair::FromString("localhost:80")));
631 EXPECT_EQ(1, network_delegate()->completed_requests());
632 EXPECT_EQ(0, network_delegate()->error_count());
633 EXPECT_FALSE(request_delegate1
.auth_required_called());
634 EXPECT_EQ("test1.html", request_delegate1
.data_received());
636 TestDelegate request_delegate2
;
637 scoped_ptr
<URLRequest
> url_request2(
638 request_context()->CreateRequest(GURL("ftp://ftp.example.com/second"),
639 DEFAULT_PRIORITY
, &request_delegate2
));
640 url_request2
->Start();
641 ASSERT_TRUE(url_request2
->is_pending());
642 socket_data(0)->RunFor(4);
644 EXPECT_TRUE(url_request2
->status().is_success());
645 EXPECT_EQ(2, network_delegate()->completed_requests());
646 EXPECT_EQ(0, network_delegate()->error_count());
647 EXPECT_FALSE(request_delegate2
.auth_required_called());
648 EXPECT_EQ("test2.html", request_delegate2
.data_received());
651 // We should not re-use socket when there are two requests to the same host,
652 // but one is FTP and the other is HTTP.
653 TEST_F(URLRequestFtpJobTest
, FtpProxyRequestDoNotReuseSocket
) {
654 MockWrite writes1
[] = {
655 MockWrite(ASYNC
, 0, "GET ftp://ftp.example.com/first HTTP/1.1\r\n"
656 "Host: ftp.example.com\r\n"
657 "Proxy-Connection: keep-alive\r\n\r\n"),
659 MockWrite writes2
[] = {
660 MockWrite(ASYNC
, 0, "GET /second HTTP/1.1\r\n"
661 "Host: ftp.example.com\r\n"
662 "Connection: keep-alive\r\n"
664 "Accept-Encoding: gzip, deflate\r\n"
665 "Accept-Language: en-us,fr\r\n\r\n"),
667 MockRead reads1
[] = {
668 MockRead(ASYNC
, 1, "HTTP/1.1 200 OK\r\n"),
669 MockRead(ASYNC
, 2, "Content-Length: 10\r\n\r\n"),
670 MockRead(ASYNC
, 3, "test1.html"),
672 MockRead reads2
[] = {
673 MockRead(ASYNC
, 1, "HTTP/1.1 200 OK\r\n"),
674 MockRead(ASYNC
, 2, "Content-Length: 10\r\n\r\n"),
675 MockRead(ASYNC
, 3, "test2.html"),
678 AddSocket(reads1
, arraysize(reads1
), writes1
, arraysize(writes1
));
679 AddSocket(reads2
, arraysize(reads2
), writes2
, arraysize(writes2
));
681 TestDelegate request_delegate1
;
682 scoped_ptr
<URLRequest
> url_request1(
683 request_context()->CreateRequest(GURL("ftp://ftp.example.com/first"),
684 DEFAULT_PRIORITY
, &request_delegate1
));
685 url_request1
->Start();
686 ASSERT_TRUE(url_request1
->is_pending());
687 socket_data(0)->RunFor(4);
689 EXPECT_TRUE(url_request1
->status().is_success());
690 EXPECT_EQ(1, network_delegate()->completed_requests());
691 EXPECT_EQ(0, network_delegate()->error_count());
692 EXPECT_FALSE(request_delegate1
.auth_required_called());
693 EXPECT_EQ("test1.html", request_delegate1
.data_received());
695 TestDelegate request_delegate2
;
696 scoped_ptr
<URLRequest
> url_request2(
697 request_context()->CreateRequest(GURL("http://ftp.example.com/second"),
698 DEFAULT_PRIORITY
, &request_delegate2
));
699 url_request2
->Start();
700 ASSERT_TRUE(url_request2
->is_pending());
701 socket_data(1)->RunFor(4);
703 EXPECT_TRUE(url_request2
->status().is_success());
704 EXPECT_EQ(2, network_delegate()->completed_requests());
705 EXPECT_EQ(0, network_delegate()->error_count());
706 EXPECT_FALSE(request_delegate2
.auth_required_called());
707 EXPECT_EQ("test2.html", request_delegate2
.data_received());