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.
8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/process/process_handle.h"
13 #include "base/run_loop.h"
14 #include "base/stl_util.h"
15 #include "content/child/request_extra_data.h"
16 #include "content/child/request_info.h"
17 #include "content/child/resource_dispatcher.h"
18 #include "content/child/resource_loader_bridge.h"
19 #include "content/common/appcache_interfaces.h"
20 #include "content/common/resource_messages.h"
21 #include "content/common/service_worker/service_worker_types.h"
22 #include "content/public/child/request_peer.h"
23 #include "content/public/common/resource_response.h"
24 #include "net/base/net_errors.h"
25 #include "net/http/http_response_headers.h"
26 #include "testing/gtest/include/gtest/gtest.h"
30 static const char kTestPageUrl
[] = "http://www.google.com/";
31 static const char kTestPageHeaders
[] =
32 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
33 static const char kTestPageMimeType
[] = "text/html";
34 static const char kTestPageCharset
[] = "";
35 static const char kTestPageContents
[] =
36 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
37 static const char kTestRedirectHeaders
[] =
38 "HTTP/1.1 302 Found\nLocation:http://www.google.com/\n\n";
40 // Listens for request response data and stores it so that it can be compared
41 // to the reference data.
42 class TestRequestPeer
: public RequestPeer
{
44 TestRequestPeer(ResourceLoaderBridge
* bridge
)
45 : follow_redirects_(true),
46 defer_on_redirect_(false),
48 cancel_on_receive_response_(false),
49 received_response_(false),
50 total_encoded_data_length_(0),
51 total_downloaded_data_length_(0),
56 void OnUploadProgress(uint64 position
, uint64 size
) override
{}
58 bool OnReceivedRedirect(const net::RedirectInfo
& redirect_info
,
59 const ResourceResponseInfo
& info
) override
{
61 if (defer_on_redirect_
)
62 bridge_
->SetDefersLoading(true);
63 return follow_redirects_
;
66 void OnReceivedResponse(const ResourceResponseInfo
& info
) override
{
67 EXPECT_FALSE(received_response_
);
68 received_response_
= true;
69 if (cancel_on_receive_response_
)
73 void OnDownloadedData(int len
, int encoded_data_length
) override
{
74 total_downloaded_data_length_
+= len
;
75 total_encoded_data_length_
+= encoded_data_length
;
78 void OnReceivedData(const char* data
,
80 int encoded_data_length
) override
{
81 EXPECT_TRUE(received_response_
);
82 EXPECT_FALSE(complete_
);
83 data_
.append(data
, data_length
);
84 total_encoded_data_length_
+= encoded_data_length
;
87 void OnCompletedRequest(int error_code
,
88 bool was_ignored_by_handler
,
89 bool stale_copy_in_cache
,
90 const std::string
& security_info
,
91 const base::TimeTicks
& completion_time
,
92 int64 total_transfer_size
) override
{
93 EXPECT_TRUE(received_response_
);
94 EXPECT_FALSE(complete_
);
98 void set_follow_redirects(bool follow_redirects
) {
99 follow_redirects_
= follow_redirects
;
102 void set_defer_on_redirect(bool defer_on_redirect
) {
103 defer_on_redirect_
= defer_on_redirect
;
106 void set_cancel_on_receive_response(bool cancel_on_receive_response
) {
107 cancel_on_receive_response_
= cancel_on_receive_response
;
110 int seen_redirects() const { return seen_redirects_
; }
112 bool received_response() const { return received_response_
; }
114 const std::string
& data() const {
117 int total_encoded_data_length() const {
118 return total_encoded_data_length_
;
120 int total_downloaded_data_length() const {
121 return total_downloaded_data_length_
;
124 bool complete() const { return complete_
; }
127 // True if should follow redirects, false if should cancel them.
128 bool follow_redirects_
;
129 // True if the request should be deferred on redirects.
130 bool defer_on_redirect_
;
131 // Number of total redirects seen.
134 bool cancel_on_receive_response_
;
135 bool received_response_
;
137 // Data received. If downloading to file, remains empty.
139 // Total encoded data length, regardless of whether downloading to a file or
141 int total_encoded_data_length_
;
142 // Total length when downloading to a file.
143 int total_downloaded_data_length_
;
147 ResourceLoaderBridge
* bridge_
;
149 DISALLOW_COPY_AND_ASSIGN(TestRequestPeer
);
152 // Sets up the message sender override for the unit test.
153 class ResourceDispatcherTest
: public testing::Test
, public IPC::Sender
{
155 ResourceDispatcherTest() : dispatcher_(this, message_loop_
.task_runner()) {}
157 ~ResourceDispatcherTest() override
{
158 STLDeleteContainerPairSecondPointers(shared_memory_map_
.begin(),
159 shared_memory_map_
.end());
162 // Emulates IPC send operations (IPC::Sender) by adding
163 // pending messages to the queue.
164 bool Send(IPC::Message
* msg
) override
{
165 message_queue_
.push_back(IPC::Message(*msg
));
170 size_t queued_messages() const { return message_queue_
.size(); }
172 // Returns the ID of the consumed request. Can't make assumptions about the
173 // ID, because numbering is based on a global.
174 int ConsumeRequestResource() {
175 if (message_queue_
.empty()) {
176 ADD_FAILURE() << "Missing resource request message";
180 ResourceHostMsg_RequestResource::Param params
;
181 if (ResourceHostMsg_RequestResource::ID
!= message_queue_
[0].type() ||
182 !ResourceHostMsg_RequestResource::Read(&message_queue_
[0], ¶ms
)) {
183 ADD_FAILURE() << "Expected ResourceHostMsg_RequestResource message";
186 ResourceHostMsg_Request request
= get
<2>(params
);
187 EXPECT_EQ(kTestPageUrl
, request
.url
.spec());
188 message_queue_
.erase(message_queue_
.begin());
189 return get
<1>(params
);
192 void ConsumeFollowRedirect(int expected_request_id
) {
193 ASSERT_FALSE(message_queue_
.empty());
195 ASSERT_EQ(ResourceHostMsg_FollowRedirect::ID
, message_queue_
[0].type());
196 ASSERT_TRUE(ResourceHostMsg_FollowRedirect::Read(
197 &message_queue_
[0], &args
));
198 EXPECT_EQ(expected_request_id
, get
<0>(args
));
199 message_queue_
.erase(message_queue_
.begin());
202 void ConsumeDataReceived_ACK(int expected_request_id
) {
203 ASSERT_FALSE(message_queue_
.empty());
205 ASSERT_EQ(ResourceHostMsg_DataReceived_ACK::ID
, message_queue_
[0].type());
206 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
207 &message_queue_
[0], &args
));
208 EXPECT_EQ(expected_request_id
, get
<0>(args
));
209 message_queue_
.erase(message_queue_
.begin());
212 void ConsumeDataDownloaded_ACK(int expected_request_id
) {
213 ASSERT_FALSE(message_queue_
.empty());
215 ASSERT_EQ(ResourceHostMsg_DataDownloaded_ACK::ID
, message_queue_
[0].type());
216 ASSERT_TRUE(ResourceHostMsg_DataDownloaded_ACK::Read(
217 &message_queue_
[0], &args
));
218 EXPECT_EQ(expected_request_id
, get
<0>(args
));
219 message_queue_
.erase(message_queue_
.begin());
222 void ConsumeReleaseDownloadedFile(int expected_request_id
) {
223 ASSERT_FALSE(message_queue_
.empty());
225 ASSERT_EQ(ResourceHostMsg_ReleaseDownloadedFile::ID
,
226 message_queue_
[0].type());
227 ASSERT_TRUE(ResourceHostMsg_ReleaseDownloadedFile::Read(
228 &message_queue_
[0], &args
));
229 EXPECT_EQ(expected_request_id
, get
<0>(args
));
230 message_queue_
.erase(message_queue_
.begin());
233 void ConsumeCancelRequest(int expected_request_id
) {
234 ASSERT_FALSE(message_queue_
.empty());
236 ASSERT_EQ(ResourceHostMsg_CancelRequest::ID
, message_queue_
[0].type());
237 ASSERT_TRUE(ResourceHostMsg_CancelRequest::Read(
238 &message_queue_
[0], &args
));
239 EXPECT_EQ(expected_request_id
, get
<0>(args
));
240 message_queue_
.erase(message_queue_
.begin());
243 void NotifyReceivedRedirect(int request_id
) {
244 ResourceResponseHead head
;
245 std::string
raw_headers(kTestRedirectHeaders
);
246 std::replace(raw_headers
.begin(), raw_headers
.end(), '\n', '\0');
247 head
.headers
= new net::HttpResponseHeaders(raw_headers
);
248 net::RedirectInfo redirect_info
;
249 redirect_info
.status_code
= 302;
250 redirect_info
.new_method
= "GET";
251 redirect_info
.new_url
= GURL(kTestPageUrl
);
252 redirect_info
.new_first_party_for_cookies
= GURL(kTestPageUrl
);
253 EXPECT_EQ(true, dispatcher_
.OnMessageReceived(
254 ResourceMsg_ReceivedRedirect(request_id
, redirect_info
, head
)));
257 void NotifyReceivedResponse(int request_id
) {
258 ResourceResponseHead head
;
259 std::string
raw_headers(kTestPageHeaders
);
260 std::replace(raw_headers
.begin(), raw_headers
.end(), '\n', '\0');
261 head
.headers
= new net::HttpResponseHeaders(raw_headers
);
262 head
.mime_type
= kTestPageMimeType
;
263 head
.charset
= kTestPageCharset
;
265 dispatcher_
.OnMessageReceived(
266 ResourceMsg_ReceivedResponse(request_id
, head
)));
269 void NotifySetDataBuffer(int request_id
, size_t buffer_size
) {
270 base::SharedMemory
* shared_memory
= new base::SharedMemory();
271 ASSERT_FALSE(shared_memory_map_
[request_id
]);
272 shared_memory_map_
[request_id
] = shared_memory
;
273 EXPECT_TRUE(shared_memory
->CreateAndMapAnonymous(buffer_size
));
275 base::SharedMemoryHandle duplicate_handle
;
276 EXPECT_TRUE(shared_memory
->ShareToProcess(base::GetCurrentProcessHandle(),
278 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
279 ResourceMsg_SetDataBuffer(request_id
, duplicate_handle
,
280 shared_memory
->requested_size(), 0)));
283 void NotifyDataReceived(int request_id
, std::string data
) {
284 ASSERT_LE(data
.length(), shared_memory_map_
[request_id
]->requested_size());
285 memcpy(shared_memory_map_
[request_id
]->memory(), data
.c_str(),
288 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
289 ResourceMsg_DataReceived(request_id
, 0, data
.length(), data
.length())));
292 void NotifyDataDownloaded(int request_id
, int decoded_length
,
293 int encoded_length
) {
294 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
295 ResourceMsg_DataDownloaded(request_id
, decoded_length
,
299 void NotifyRequestComplete(int request_id
, size_t total_size
) {
300 ResourceMsg_RequestCompleteData request_complete_data
;
301 request_complete_data
.error_code
= net::OK
;
302 request_complete_data
.was_ignored_by_handler
= false;
303 request_complete_data
.exists_in_cache
= false;
304 request_complete_data
.encoded_data_length
= total_size
;
305 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
306 ResourceMsg_RequestComplete(request_id
, request_complete_data
)));
309 ResourceLoaderBridge
* CreateBridge() {
310 return CreateBridgeInternal(false);
313 ResourceLoaderBridge
* CreateBridgeForDownloadToFile() {
314 return CreateBridgeInternal(true);
317 ResourceDispatcher
* dispatcher() { return &dispatcher_
; }
320 ResourceLoaderBridge
* CreateBridgeInternal(bool download_to_file
) {
321 RequestInfo request_info
;
322 request_info
.method
= "GET";
323 request_info
.url
= GURL(kTestPageUrl
);
324 request_info
.first_party_for_cookies
= GURL(kTestPageUrl
);
325 request_info
.referrer
= Referrer();
326 request_info
.headers
= std::string();
327 request_info
.load_flags
= 0;
328 request_info
.requestor_pid
= 0;
329 request_info
.request_type
= RESOURCE_TYPE_SUB_RESOURCE
;
330 request_info
.appcache_host_id
= kAppCacheNoHostId
;
331 request_info
.should_reset_appcache
= false;
332 request_info
.routing_id
= 0;
333 request_info
.download_to_file
= download_to_file
;
334 RequestExtraData extra_data
;
335 request_info
.extra_data
= &extra_data
;
337 return dispatcher_
.CreateBridge(request_info
);
340 // Map of request IDs to shared memory.
341 std::map
<int, base::SharedMemory
*> shared_memory_map_
;
343 std::vector
<IPC::Message
> message_queue_
;
344 base::MessageLoop message_loop_
;
345 ResourceDispatcher dispatcher_
;
348 // Does a simple request and tests that the correct data is received. Simulates
350 TEST_F(ResourceDispatcherTest
, RoundTrip
) {
351 // Number of bytes received in the first read.
352 const size_t kFirstReceiveSize
= 2;
353 ASSERT_LT(kFirstReceiveSize
, strlen(kTestPageContents
));
355 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
356 TestRequestPeer
peer(bridge
.get());
358 EXPECT_TRUE(bridge
->Start(&peer
));
359 int id
= ConsumeRequestResource();
360 EXPECT_EQ(0u, queued_messages());
362 NotifyReceivedResponse(id
);
363 EXPECT_EQ(0u, queued_messages());
364 EXPECT_TRUE(peer
.received_response());
366 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
367 NotifyDataReceived(id
, std::string(kTestPageContents
, kFirstReceiveSize
));
368 ConsumeDataReceived_ACK(id
);
369 EXPECT_EQ(0u, queued_messages());
371 NotifyDataReceived(id
, kTestPageContents
+ kFirstReceiveSize
);
372 ConsumeDataReceived_ACK(id
);
373 EXPECT_EQ(0u, queued_messages());
375 NotifyRequestComplete(id
, strlen(kTestPageContents
));
376 EXPECT_EQ(kTestPageContents
, peer
.data());
377 EXPECT_TRUE(peer
.complete());
378 EXPECT_EQ(0u, queued_messages());
381 // Tests that the request IDs are straight when there are two interleaving
383 TEST_F(ResourceDispatcherTest
, MultipleRequests
) {
384 const char kTestPageContents2
[] = "Not kTestPageContents";
386 scoped_ptr
<ResourceLoaderBridge
> bridge1(CreateBridge());
387 TestRequestPeer
peer1(bridge1
.get());
388 scoped_ptr
<ResourceLoaderBridge
> bridge2(CreateBridge());
389 TestRequestPeer
peer2(bridge2
.get());
391 EXPECT_TRUE(bridge1
->Start(&peer1
));
392 int id1
= ConsumeRequestResource();
393 EXPECT_TRUE(bridge2
->Start(&peer2
));
394 int id2
= ConsumeRequestResource();
395 EXPECT_EQ(0u, queued_messages());
397 NotifyReceivedResponse(id1
);
398 EXPECT_TRUE(peer1
.received_response());
399 EXPECT_FALSE(peer2
.received_response());
400 NotifyReceivedResponse(id2
);
401 EXPECT_TRUE(peer2
.received_response());
402 EXPECT_EQ(0u, queued_messages());
404 NotifySetDataBuffer(id2
, strlen(kTestPageContents2
));
405 NotifyDataReceived(id2
, kTestPageContents2
);
406 ConsumeDataReceived_ACK(id2
);
407 NotifySetDataBuffer(id1
, strlen(kTestPageContents
));
408 NotifyDataReceived(id1
, kTestPageContents
);
409 ConsumeDataReceived_ACK(id1
);
410 EXPECT_EQ(0u, queued_messages());
412 NotifyRequestComplete(id1
, strlen(kTestPageContents
));
413 EXPECT_EQ(kTestPageContents
, peer1
.data());
414 EXPECT_TRUE(peer1
.complete());
415 EXPECT_FALSE(peer2
.complete());
417 NotifyRequestComplete(id2
, strlen(kTestPageContents2
));
418 EXPECT_EQ(kTestPageContents2
, peer2
.data());
419 EXPECT_TRUE(peer2
.complete());
421 EXPECT_EQ(0u, queued_messages());
424 // Tests that the cancel method prevents other messages from being received.
425 TEST_F(ResourceDispatcherTest
, Cancel
) {
426 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
427 TestRequestPeer
peer(bridge
.get());
429 EXPECT_TRUE(bridge
->Start(&peer
));
430 int id
= ConsumeRequestResource();
431 EXPECT_EQ(0u, queued_messages());
433 // Cancel the request.
435 ConsumeCancelRequest(id
);
437 // Any future messages related to the request should be ignored.
438 NotifyReceivedResponse(id
);
439 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
440 NotifyDataReceived(id
, kTestPageContents
);
441 NotifyRequestComplete(id
, strlen(kTestPageContents
));
443 EXPECT_EQ(0u, queued_messages());
444 EXPECT_EQ("", peer
.data());
445 EXPECT_FALSE(peer
.received_response());
446 EXPECT_FALSE(peer
.complete());
449 // Tests that calling cancel during a callback works as expected.
450 TEST_F(ResourceDispatcherTest
, CancelDuringCallback
) {
451 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
452 TestRequestPeer
peer(bridge
.get());
453 peer
.set_cancel_on_receive_response(true);
455 EXPECT_TRUE(bridge
->Start(&peer
));
456 int id
= ConsumeRequestResource();
457 EXPECT_EQ(0u, queued_messages());
459 NotifyReceivedResponse(id
);
460 EXPECT_TRUE(peer
.received_response());
461 // Request should have been cancelled.
462 ConsumeCancelRequest(id
);
464 // Any future messages related to the request should be ignored.
465 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
466 NotifyDataReceived(id
, kTestPageContents
);
467 NotifyRequestComplete(id
, strlen(kTestPageContents
));
469 EXPECT_EQ(0u, queued_messages());
470 EXPECT_EQ("", peer
.data());
471 EXPECT_FALSE(peer
.complete());
474 // Checks that redirects work as expected.
475 TEST_F(ResourceDispatcherTest
, Redirect
) {
476 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
477 TestRequestPeer
peer(bridge
.get());
479 EXPECT_TRUE(bridge
->Start(&peer
));
480 int id
= ConsumeRequestResource();
482 NotifyReceivedRedirect(id
);
483 ConsumeFollowRedirect(id
);
484 EXPECT_EQ(1, peer
.seen_redirects());
486 NotifyReceivedRedirect(id
);
487 ConsumeFollowRedirect(id
);
488 EXPECT_EQ(2, peer
.seen_redirects());
490 NotifyReceivedResponse(id
);
491 EXPECT_TRUE(peer
.received_response());
493 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
494 NotifyDataReceived(id
, kTestPageContents
);
495 ConsumeDataReceived_ACK(id
);
497 NotifyRequestComplete(id
, strlen(kTestPageContents
));
498 EXPECT_EQ(kTestPageContents
, peer
.data());
499 EXPECT_TRUE(peer
.complete());
500 EXPECT_EQ(0u, queued_messages());
501 EXPECT_EQ(2, peer
.seen_redirects());
504 // Tests that that cancelling during a redirect method prevents other messages
505 // from being received.
506 TEST_F(ResourceDispatcherTest
, CancelDuringRedirect
) {
507 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
508 TestRequestPeer
peer(bridge
.get());
509 peer
.set_follow_redirects(false);
511 EXPECT_TRUE(bridge
->Start(&peer
));
512 int id
= ConsumeRequestResource();
513 EXPECT_EQ(0u, queued_messages());
515 // Redirect the request, which triggers a cancellation.
516 NotifyReceivedRedirect(id
);
517 ConsumeCancelRequest(id
);
518 EXPECT_EQ(1, peer
.seen_redirects());
519 EXPECT_EQ(0u, queued_messages());
521 // Any future messages related to the request should be ignored. In practice,
522 // only the NotifyRequestComplete should be received after this point.
523 NotifyReceivedRedirect(id
);
524 NotifyReceivedResponse(id
);
525 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
526 NotifyDataReceived(id
, kTestPageContents
);
527 NotifyRequestComplete(id
, strlen(kTestPageContents
));
529 EXPECT_EQ(0u, queued_messages());
530 EXPECT_EQ("", peer
.data());
531 EXPECT_FALSE(peer
.complete());
532 EXPECT_EQ(1, peer
.seen_redirects());
535 // Checks that deferring a request delays messages until it's resumed.
536 TEST_F(ResourceDispatcherTest
, Defer
) {
537 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
538 TestRequestPeer
peer(bridge
.get());
540 EXPECT_TRUE(bridge
->Start(&peer
));
541 int id
= ConsumeRequestResource();
542 EXPECT_EQ(0u, queued_messages());
544 bridge
->SetDefersLoading(true);
545 NotifyReceivedResponse(id
);
546 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
547 NotifyDataReceived(id
, kTestPageContents
);
548 NotifyRequestComplete(id
, strlen(kTestPageContents
));
550 // None of the messages should have been processed yet, so no queued messages
551 // to the browser process, and no data received by the peer.
552 EXPECT_EQ(0u, queued_messages());
553 EXPECT_EQ("", peer
.data());
554 EXPECT_FALSE(peer
.complete());
555 EXPECT_EQ(0, peer
.seen_redirects());
557 // Resuming the request should asynchronously unleash the deferred messages.
558 bridge
->SetDefersLoading(false);
559 base::RunLoop().RunUntilIdle();
561 ConsumeDataReceived_ACK(id
);
562 EXPECT_EQ(0u, queued_messages());
563 EXPECT_TRUE(peer
.received_response());
564 EXPECT_EQ(kTestPageContents
, peer
.data());
565 EXPECT_TRUE(peer
.complete());
568 // Checks that deferring a request during a redirect delays messages until it's
570 TEST_F(ResourceDispatcherTest
, DeferOnRedirect
) {
571 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
572 TestRequestPeer
peer(bridge
.get());
573 peer
.set_defer_on_redirect(true);
575 EXPECT_TRUE(bridge
->Start(&peer
));
576 int id
= ConsumeRequestResource();
577 EXPECT_EQ(0u, queued_messages());
579 // The request should be deferred during the redirect, including the message
580 // to follow the redirect.
581 NotifyReceivedRedirect(id
);
582 NotifyReceivedResponse(id
);
583 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
584 NotifyDataReceived(id
, kTestPageContents
);
585 NotifyRequestComplete(id
, strlen(kTestPageContents
));
587 // None of the messages should have been processed yet, so no queued messages
588 // to the browser process, and no data received by the peer.
589 EXPECT_EQ(0u, queued_messages());
590 EXPECT_EQ("", peer
.data());
591 EXPECT_FALSE(peer
.complete());
592 EXPECT_EQ(1, peer
.seen_redirects());
594 // Resuming the request should asynchronously unleash the deferred messages.
595 bridge
->SetDefersLoading(false);
596 base::RunLoop().RunUntilIdle();
598 ConsumeFollowRedirect(id
);
599 ConsumeDataReceived_ACK(id
);
601 EXPECT_EQ(0u, queued_messages());
602 EXPECT_TRUE(peer
.received_response());
603 EXPECT_EQ(kTestPageContents
, peer
.data());
604 EXPECT_TRUE(peer
.complete());
605 EXPECT_EQ(1, peer
.seen_redirects());
608 // Checks that a deferred request that's cancelled doesn't receive any messages.
609 TEST_F(ResourceDispatcherTest
, CancelDeferredRequest
) {
610 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
611 TestRequestPeer
peer(bridge
.get());
613 EXPECT_TRUE(bridge
->Start(&peer
));
614 int id
= ConsumeRequestResource();
615 EXPECT_EQ(0u, queued_messages());
617 bridge
->SetDefersLoading(true);
618 NotifyReceivedRedirect(id
);
620 ConsumeCancelRequest(id
);
622 NotifyRequestComplete(id
, 0);
623 base::RunLoop().RunUntilIdle();
625 // None of the messages should have been processed.
626 EXPECT_EQ(0u, queued_messages());
627 EXPECT_EQ("", peer
.data());
628 EXPECT_FALSE(peer
.complete());
629 EXPECT_EQ(0, peer
.seen_redirects());
632 TEST_F(ResourceDispatcherTest
, DownloadToFile
) {
633 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridgeForDownloadToFile());
634 TestRequestPeer
peer(bridge
.get());
635 const int kDownloadedIncrement
= 100;
636 const int kEncodedIncrement
= 50;
638 EXPECT_TRUE(bridge
->Start(&peer
));
639 int id
= ConsumeRequestResource();
640 EXPECT_EQ(0u, queued_messages());
642 NotifyReceivedResponse(id
);
643 EXPECT_EQ(0u, queued_messages());
644 EXPECT_TRUE(peer
.received_response());
646 int expected_total_downloaded_length
= 0;
647 int expected_total_encoded_length
= 0;
648 for (int i
= 0; i
< 10; ++i
) {
649 NotifyDataDownloaded(id
, kDownloadedIncrement
, kEncodedIncrement
);
650 ConsumeDataDownloaded_ACK(id
);
651 expected_total_downloaded_length
+= kDownloadedIncrement
;
652 expected_total_encoded_length
+= kEncodedIncrement
;
653 EXPECT_EQ(expected_total_downloaded_length
,
654 peer
.total_downloaded_data_length());
655 EXPECT_EQ(expected_total_encoded_length
, peer
.total_encoded_data_length());
658 NotifyRequestComplete(id
, strlen(kTestPageContents
));
659 EXPECT_EQ("", peer
.data());
660 EXPECT_TRUE(peer
.complete());
661 EXPECT_EQ(0u, queued_messages());
664 ConsumeReleaseDownloadedFile(id
);
665 EXPECT_EQ(0u, queued_messages());
666 EXPECT_EQ(expected_total_downloaded_length
,
667 peer
.total_downloaded_data_length());
668 EXPECT_EQ(expected_total_encoded_length
, peer
.total_encoded_data_length());
671 // Make sure that when a download to file is cancelled, the file is destroyed.
672 TEST_F(ResourceDispatcherTest
, CancelDownloadToFile
) {
673 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridgeForDownloadToFile());
674 TestRequestPeer
peer(bridge
.get());
676 EXPECT_TRUE(bridge
->Start(&peer
));
677 int id
= ConsumeRequestResource();
678 EXPECT_EQ(0u, queued_messages());
680 NotifyReceivedResponse(id
);
681 EXPECT_EQ(0u, queued_messages());
682 EXPECT_TRUE(peer
.received_response());
684 // Cancelling the request deletes the file.
686 ConsumeCancelRequest(id
);
687 ConsumeReleaseDownloadedFile(id
);
689 // Deleting the bridge shouldn't send another message to delete the file.
691 EXPECT_EQ(0u, queued_messages());
694 TEST_F(ResourceDispatcherTest
, Cookies
) {
698 TEST_F(ResourceDispatcherTest
, SerializedPostData
) {
702 class TimeConversionTest
: public ResourceDispatcherTest
,
705 bool Send(IPC::Message
* msg
) override
{
710 void PerformTest(const ResourceResponseHead
& response_head
) {
711 scoped_ptr
<ResourceLoaderBridge
> bridge(CreateBridge());
714 dispatcher()->OnMessageReceived(
715 ResourceMsg_ReceivedResponse(0, response_head
));
718 // RequestPeer methods.
719 void OnUploadProgress(uint64 position
, uint64 size
) override
{}
721 bool OnReceivedRedirect(const net::RedirectInfo
& redirect_info
,
722 const ResourceResponseInfo
& info
) override
{
726 void OnReceivedResponse(const ResourceResponseInfo
& info
) override
{
727 response_info_
= info
;
730 void OnDownloadedData(int len
, int encoded_data_length
) override
{}
732 void OnReceivedData(const char* data
,
734 int encoded_data_length
) override
{}
736 void OnCompletedRequest(int error_code
,
737 bool was_ignored_by_handler
,
738 bool stale_copy_in_cache
,
739 const std::string
& security_info
,
740 const base::TimeTicks
& completion_time
,
741 int64 total_transfer_size
) override
{}
743 const ResourceResponseInfo
& response_info() const { return response_info_
; }
746 ResourceResponseInfo response_info_
;
749 // TODO(simonjam): Enable this when 10829031 lands.
750 TEST_F(TimeConversionTest
, DISABLED_ProperlyInitialized
) {
751 ResourceResponseHead response_head
;
752 response_head
.request_start
= base::TimeTicks::FromInternalValue(5);
753 response_head
.response_start
= base::TimeTicks::FromInternalValue(15);
754 response_head
.load_timing
.request_start_time
= base::Time::Now();
755 response_head
.load_timing
.request_start
=
756 base::TimeTicks::FromInternalValue(10);
757 response_head
.load_timing
.connect_timing
.connect_start
=
758 base::TimeTicks::FromInternalValue(13);
760 PerformTest(response_head
);
762 EXPECT_LT(base::TimeTicks(), response_info().load_timing
.request_start
);
763 EXPECT_EQ(base::TimeTicks(),
764 response_info().load_timing
.connect_timing
.dns_start
);
765 EXPECT_LE(response_head
.load_timing
.request_start
,
766 response_info().load_timing
.connect_timing
.connect_start
);
769 TEST_F(TimeConversionTest
, PartiallyInitialized
) {
770 ResourceResponseHead response_head
;
771 response_head
.request_start
= base::TimeTicks::FromInternalValue(5);
772 response_head
.response_start
= base::TimeTicks::FromInternalValue(15);
774 PerformTest(response_head
);
776 EXPECT_EQ(base::TimeTicks(), response_info().load_timing
.request_start
);
777 EXPECT_EQ(base::TimeTicks(),
778 response_info().load_timing
.connect_timing
.dns_start
);
781 TEST_F(TimeConversionTest
, NotInitialized
) {
782 ResourceResponseHead response_head
;
784 PerformTest(response_head
);
786 EXPECT_EQ(base::TimeTicks(), response_info().load_timing
.request_start
);
787 EXPECT_EQ(base::TimeTicks(),
788 response_info().load_timing
.connect_timing
.dns_start
);
791 } // namespace content