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/common/appcache_interfaces.h"
19 #include "content/common/resource_messages.h"
20 #include "content/common/service_worker/service_worker_types.h"
21 #include "content/public/child/request_peer.h"
22 #include "content/public/common/resource_response.h"
23 #include "net/base/net_errors.h"
24 #include "net/http/http_response_headers.h"
25 #include "testing/gtest/include/gtest/gtest.h"
29 static const char kTestPageUrl
[] = "http://www.google.com/";
30 static const char kTestPageHeaders
[] =
31 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
32 static const char kTestPageMimeType
[] = "text/html";
33 static const char kTestPageCharset
[] = "";
34 static const char kTestPageContents
[] =
35 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
36 static const char kTestRedirectHeaders
[] =
37 "HTTP/1.1 302 Found\nLocation:http://www.google.com/\n\n";
39 // Listens for request response data and stores it so that it can be compared
40 // to the reference data.
41 class TestRequestPeer
: public RequestPeer
{
43 TestRequestPeer(ResourceDispatcher
* dispatcher
)
44 : follow_redirects_(true),
45 defer_on_redirect_(false),
47 cancel_on_receive_response_(false),
48 received_response_(false),
49 total_encoded_data_length_(0),
50 total_downloaded_data_length_(0),
52 dispatcher_(dispatcher
),
56 void set_request_id(int request_id
) { request_id_
= request_id
; }
58 void OnUploadProgress(uint64 position
, uint64 size
) override
{}
60 bool OnReceivedRedirect(const net::RedirectInfo
& redirect_info
,
61 const ResourceResponseInfo
& info
) override
{
63 if (defer_on_redirect_
)
64 dispatcher_
->SetDefersLoading(request_id_
, true);
65 return follow_redirects_
;
68 void OnReceivedResponse(const ResourceResponseInfo
& info
) override
{
69 EXPECT_FALSE(received_response_
);
70 received_response_
= true;
71 if (cancel_on_receive_response_
)
72 dispatcher_
->Cancel(request_id_
);
75 void OnDownloadedData(int len
, int encoded_data_length
) override
{
76 total_downloaded_data_length_
+= len
;
77 total_encoded_data_length_
+= encoded_data_length
;
80 void OnReceivedData(scoped_ptr
<ReceivedData
> data
) override
{
81 EXPECT_TRUE(received_response_
);
82 EXPECT_FALSE(complete_
);
83 data_
.append(data
->payload(), data
->length());
84 total_encoded_data_length_
+= data
->encoded_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 ResourceDispatcher
* dispatcher_
;
150 DISALLOW_COPY_AND_ASSIGN(TestRequestPeer
);
153 // Sets up the message sender override for the unit test.
154 class ResourceDispatcherTest
: public testing::Test
, public IPC::Sender
{
156 ResourceDispatcherTest() : dispatcher_(this, message_loop_
.task_runner()) {}
158 ~ResourceDispatcherTest() override
{
159 STLDeleteContainerPairSecondPointers(shared_memory_map_
.begin(),
160 shared_memory_map_
.end());
163 // Emulates IPC send operations (IPC::Sender) by adding
164 // pending messages to the queue.
165 bool Send(IPC::Message
* msg
) override
{
166 message_queue_
.push_back(IPC::Message(*msg
));
171 size_t queued_messages() const { return message_queue_
.size(); }
173 // Returns the ID of the consumed request. Can't make assumptions about the
174 // ID, because numbering is based on a global.
175 int ConsumeRequestResource() {
176 if (message_queue_
.empty()) {
177 ADD_FAILURE() << "Missing resource request message";
181 ResourceHostMsg_RequestResource::Param params
;
182 if (ResourceHostMsg_RequestResource::ID
!= message_queue_
[0].type() ||
183 !ResourceHostMsg_RequestResource::Read(&message_queue_
[0], ¶ms
)) {
184 ADD_FAILURE() << "Expected ResourceHostMsg_RequestResource message";
187 ResourceHostMsg_Request request
= base::get
<2>(params
);
188 EXPECT_EQ(kTestPageUrl
, request
.url
.spec());
189 message_queue_
.erase(message_queue_
.begin());
190 return base::get
<1>(params
);
193 void ConsumeFollowRedirect(int expected_request_id
) {
194 ASSERT_FALSE(message_queue_
.empty());
195 base::Tuple
<int> args
;
196 ASSERT_EQ(ResourceHostMsg_FollowRedirect::ID
, message_queue_
[0].type());
197 ASSERT_TRUE(ResourceHostMsg_FollowRedirect::Read(
198 &message_queue_
[0], &args
));
199 EXPECT_EQ(expected_request_id
, base::get
<0>(args
));
200 message_queue_
.erase(message_queue_
.begin());
203 void ConsumeDataReceived_ACK(int expected_request_id
) {
204 ASSERT_FALSE(message_queue_
.empty());
205 base::Tuple
<int> args
;
206 ASSERT_EQ(ResourceHostMsg_DataReceived_ACK::ID
, message_queue_
[0].type());
207 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
208 &message_queue_
[0], &args
));
209 EXPECT_EQ(expected_request_id
, base::get
<0>(args
));
210 message_queue_
.erase(message_queue_
.begin());
213 void ConsumeDataDownloaded_ACK(int expected_request_id
) {
214 ASSERT_FALSE(message_queue_
.empty());
215 base::Tuple
<int> args
;
216 ASSERT_EQ(ResourceHostMsg_DataDownloaded_ACK::ID
, message_queue_
[0].type());
217 ASSERT_TRUE(ResourceHostMsg_DataDownloaded_ACK::Read(
218 &message_queue_
[0], &args
));
219 EXPECT_EQ(expected_request_id
, base::get
<0>(args
));
220 message_queue_
.erase(message_queue_
.begin());
223 void ConsumeReleaseDownloadedFile(int expected_request_id
) {
224 ASSERT_FALSE(message_queue_
.empty());
225 base::Tuple
<int> args
;
226 ASSERT_EQ(ResourceHostMsg_ReleaseDownloadedFile::ID
,
227 message_queue_
[0].type());
228 ASSERT_TRUE(ResourceHostMsg_ReleaseDownloadedFile::Read(
229 &message_queue_
[0], &args
));
230 EXPECT_EQ(expected_request_id
, base::get
<0>(args
));
231 message_queue_
.erase(message_queue_
.begin());
234 void ConsumeCancelRequest(int expected_request_id
) {
235 ASSERT_FALSE(message_queue_
.empty());
236 base::Tuple
<int> args
;
237 ASSERT_EQ(ResourceHostMsg_CancelRequest::ID
, message_queue_
[0].type());
238 ASSERT_TRUE(ResourceHostMsg_CancelRequest::Read(
239 &message_queue_
[0], &args
));
240 EXPECT_EQ(expected_request_id
, base::get
<0>(args
));
241 message_queue_
.erase(message_queue_
.begin());
244 void NotifyReceivedRedirect(int request_id
) {
245 ResourceResponseHead head
;
246 std::string
raw_headers(kTestRedirectHeaders
);
247 std::replace(raw_headers
.begin(), raw_headers
.end(), '\n', '\0');
248 head
.headers
= new net::HttpResponseHeaders(raw_headers
);
249 net::RedirectInfo redirect_info
;
250 redirect_info
.status_code
= 302;
251 redirect_info
.new_method
= "GET";
252 redirect_info
.new_url
= GURL(kTestPageUrl
);
253 redirect_info
.new_first_party_for_cookies
= GURL(kTestPageUrl
);
254 EXPECT_EQ(true, dispatcher_
.OnMessageReceived(
255 ResourceMsg_ReceivedRedirect(request_id
, redirect_info
, head
)));
258 void NotifyReceivedResponse(int request_id
) {
259 ResourceResponseHead head
;
260 std::string
raw_headers(kTestPageHeaders
);
261 std::replace(raw_headers
.begin(), raw_headers
.end(), '\n', '\0');
262 head
.headers
= new net::HttpResponseHeaders(raw_headers
);
263 head
.mime_type
= kTestPageMimeType
;
264 head
.charset
= kTestPageCharset
;
266 dispatcher_
.OnMessageReceived(
267 ResourceMsg_ReceivedResponse(request_id
, head
)));
270 void NotifySetDataBuffer(int request_id
, size_t buffer_size
) {
271 base::SharedMemory
* shared_memory
= new base::SharedMemory();
272 ASSERT_FALSE(shared_memory_map_
[request_id
]);
273 shared_memory_map_
[request_id
] = shared_memory
;
274 EXPECT_TRUE(shared_memory
->CreateAndMapAnonymous(buffer_size
));
276 base::SharedMemoryHandle duplicate_handle
;
277 EXPECT_TRUE(shared_memory
->ShareToProcess(base::GetCurrentProcessHandle(),
279 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
280 ResourceMsg_SetDataBuffer(request_id
, duplicate_handle
,
281 shared_memory
->requested_size(), 0)));
284 void NotifyDataReceived(int request_id
, std::string data
) {
285 ASSERT_LE(data
.length(), shared_memory_map_
[request_id
]->requested_size());
286 memcpy(shared_memory_map_
[request_id
]->memory(), data
.c_str(),
289 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
290 ResourceMsg_DataReceived(request_id
, 0, data
.length(), data
.length())));
293 void NotifyDataDownloaded(int request_id
, int decoded_length
,
294 int encoded_length
) {
295 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
296 ResourceMsg_DataDownloaded(request_id
, decoded_length
,
300 void NotifyRequestComplete(int request_id
, size_t total_size
) {
301 ResourceMsg_RequestCompleteData request_complete_data
;
302 request_complete_data
.error_code
= net::OK
;
303 request_complete_data
.was_ignored_by_handler
= false;
304 request_complete_data
.exists_in_cache
= false;
305 request_complete_data
.encoded_data_length
= total_size
;
306 EXPECT_TRUE(dispatcher_
.OnMessageReceived(
307 ResourceMsg_RequestComplete(request_id
, request_complete_data
)));
310 RequestInfo
* CreateRequestInfo(bool download_to_file
) {
311 RequestInfo
* request_info
= new RequestInfo();
312 request_info
->method
= "GET";
313 request_info
->url
= GURL(kTestPageUrl
);
314 request_info
->first_party_for_cookies
= GURL(kTestPageUrl
);
315 request_info
->referrer
= Referrer();
316 request_info
->headers
= std::string();
317 request_info
->load_flags
= 0;
318 request_info
->requestor_pid
= 0;
319 request_info
->request_type
= RESOURCE_TYPE_SUB_RESOURCE
;
320 request_info
->appcache_host_id
= kAppCacheNoHostId
;
321 request_info
->should_reset_appcache
= false;
322 request_info
->routing_id
= 0;
323 request_info
->download_to_file
= download_to_file
;
324 RequestExtraData extra_data
;
329 ResourceDispatcher
* dispatcher() { return &dispatcher_
; }
332 // Map of request IDs to shared memory.
333 std::map
<int, base::SharedMemory
*> shared_memory_map_
;
335 std::vector
<IPC::Message
> message_queue_
;
336 base::MessageLoop message_loop_
;
337 ResourceDispatcher dispatcher_
;
340 // Does a simple request and tests that the correct data is received. Simulates
342 TEST_F(ResourceDispatcherTest
, RoundTrip
) {
343 // Number of bytes received in the first read.
344 const size_t kFirstReceiveSize
= 2;
345 ASSERT_LT(kFirstReceiveSize
, strlen(kTestPageContents
));
347 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
348 TestRequestPeer
peer(dispatcher());
349 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
350 peer
.set_request_id(request_id
);
352 int id
= ConsumeRequestResource();
353 EXPECT_EQ(0u, queued_messages());
355 NotifyReceivedResponse(id
);
356 EXPECT_EQ(0u, queued_messages());
357 EXPECT_TRUE(peer
.received_response());
359 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
360 NotifyDataReceived(id
, std::string(kTestPageContents
, kFirstReceiveSize
));
361 ConsumeDataReceived_ACK(id
);
362 EXPECT_EQ(0u, queued_messages());
364 NotifyDataReceived(id
, kTestPageContents
+ kFirstReceiveSize
);
365 ConsumeDataReceived_ACK(id
);
366 EXPECT_EQ(0u, queued_messages());
368 NotifyRequestComplete(id
, strlen(kTestPageContents
));
369 EXPECT_EQ(kTestPageContents
, peer
.data());
370 EXPECT_TRUE(peer
.complete());
371 EXPECT_EQ(0u, queued_messages());
374 // Tests that the request IDs are straight when there are two interleaving
376 TEST_F(ResourceDispatcherTest
, MultipleRequests
) {
377 const char kTestPageContents2
[] = "Not kTestPageContents";
379 scoped_ptr
<RequestInfo
> request_info1(CreateRequestInfo(false));
380 TestRequestPeer
peer1(dispatcher());
381 int request_id1
= dispatcher()->StartAsync(
382 *request_info1
.get(), NULL
, &peer1
);
383 peer1
.set_request_id(request_id1
);
384 scoped_ptr
<RequestInfo
> request_info2(CreateRequestInfo(false));
385 TestRequestPeer
peer2(dispatcher());
386 int request_id2
= dispatcher()->StartAsync(
387 *request_info1
.get(), NULL
, &peer2
);
388 peer2
.set_request_id(request_id2
);
390 int id1
= ConsumeRequestResource();
391 int id2
= ConsumeRequestResource();
392 EXPECT_EQ(0u, queued_messages());
394 NotifyReceivedResponse(id1
);
395 EXPECT_TRUE(peer1
.received_response());
396 EXPECT_FALSE(peer2
.received_response());
397 NotifyReceivedResponse(id2
);
398 EXPECT_TRUE(peer2
.received_response());
399 EXPECT_EQ(0u, queued_messages());
401 NotifySetDataBuffer(id2
, strlen(kTestPageContents2
));
402 NotifyDataReceived(id2
, kTestPageContents2
);
403 ConsumeDataReceived_ACK(id2
);
404 NotifySetDataBuffer(id1
, strlen(kTestPageContents
));
405 NotifyDataReceived(id1
, kTestPageContents
);
406 ConsumeDataReceived_ACK(id1
);
407 EXPECT_EQ(0u, queued_messages());
409 NotifyRequestComplete(id1
, strlen(kTestPageContents
));
410 EXPECT_EQ(kTestPageContents
, peer1
.data());
411 EXPECT_TRUE(peer1
.complete());
412 EXPECT_FALSE(peer2
.complete());
414 NotifyRequestComplete(id2
, strlen(kTestPageContents2
));
415 EXPECT_EQ(kTestPageContents2
, peer2
.data());
416 EXPECT_TRUE(peer2
.complete());
418 EXPECT_EQ(0u, queued_messages());
421 // Tests that the cancel method prevents other messages from being received.
422 TEST_F(ResourceDispatcherTest
, Cancel
) {
423 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
424 TestRequestPeer
peer(dispatcher());
425 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
426 peer
.set_request_id(request_id
);
428 int id
= ConsumeRequestResource();
429 EXPECT_EQ(0u, queued_messages());
431 // Cancel the request.
432 dispatcher()->Cancel(request_id
);
433 ConsumeCancelRequest(id
);
435 // Any future messages related to the request should be ignored.
436 NotifyReceivedResponse(id
);
437 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
438 NotifyDataReceived(id
, kTestPageContents
);
439 NotifyRequestComplete(id
, strlen(kTestPageContents
));
441 EXPECT_EQ(0u, queued_messages());
442 EXPECT_EQ("", peer
.data());
443 EXPECT_FALSE(peer
.received_response());
444 EXPECT_FALSE(peer
.complete());
447 // Tests that calling cancel during a callback works as expected.
448 TEST_F(ResourceDispatcherTest
, CancelDuringCallback
) {
449 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
450 TestRequestPeer
peer(dispatcher());
451 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
452 peer
.set_request_id(request_id
);
453 peer
.set_cancel_on_receive_response(true);
455 int id
= ConsumeRequestResource();
456 EXPECT_EQ(0u, queued_messages());
458 NotifyReceivedResponse(id
);
459 EXPECT_TRUE(peer
.received_response());
460 // Request should have been cancelled.
461 ConsumeCancelRequest(id
);
463 // Any future messages related to the request should be ignored.
464 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
465 NotifyDataReceived(id
, kTestPageContents
);
466 NotifyRequestComplete(id
, strlen(kTestPageContents
));
468 EXPECT_EQ(0u, queued_messages());
469 EXPECT_EQ("", peer
.data());
470 EXPECT_FALSE(peer
.complete());
473 // Checks that redirects work as expected.
474 TEST_F(ResourceDispatcherTest
, Redirect
) {
475 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
476 TestRequestPeer
peer(dispatcher());
477 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
478 peer
.set_request_id(request_id
);
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
<RequestInfo
> request_info(CreateRequestInfo(false));
508 TestRequestPeer
peer(dispatcher());
509 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
510 peer
.set_request_id(request_id
);
511 peer
.set_follow_redirects(false);
513 int id
= ConsumeRequestResource();
514 EXPECT_EQ(0u, queued_messages());
516 // Redirect the request, which triggers a cancellation.
517 NotifyReceivedRedirect(id
);
518 ConsumeCancelRequest(id
);
519 EXPECT_EQ(1, peer
.seen_redirects());
520 EXPECT_EQ(0u, queued_messages());
522 // Any future messages related to the request should be ignored. In practice,
523 // only the NotifyRequestComplete should be received after this point.
524 NotifyReceivedRedirect(id
);
525 NotifyReceivedResponse(id
);
526 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
527 NotifyDataReceived(id
, kTestPageContents
);
528 NotifyRequestComplete(id
, strlen(kTestPageContents
));
530 EXPECT_EQ(0u, queued_messages());
531 EXPECT_EQ("", peer
.data());
532 EXPECT_FALSE(peer
.complete());
533 EXPECT_EQ(1, peer
.seen_redirects());
536 // Checks that deferring a request delays messages until it's resumed.
537 TEST_F(ResourceDispatcherTest
, Defer
) {
538 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
539 TestRequestPeer
peer(dispatcher());
540 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
541 peer
.set_request_id(request_id
);
543 int id
= ConsumeRequestResource();
544 EXPECT_EQ(0u, queued_messages());
546 dispatcher()->SetDefersLoading(request_id
, true);
547 NotifyReceivedResponse(id
);
548 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
549 NotifyDataReceived(id
, kTestPageContents
);
550 NotifyRequestComplete(id
, strlen(kTestPageContents
));
552 // None of the messages should have been processed yet, so no queued messages
553 // to the browser process, and no data received by the peer.
554 EXPECT_EQ(0u, queued_messages());
555 EXPECT_EQ("", peer
.data());
556 EXPECT_FALSE(peer
.complete());
557 EXPECT_EQ(0, peer
.seen_redirects());
559 // Resuming the request should asynchronously unleash the deferred messages.
560 dispatcher()->SetDefersLoading(request_id
, false);
561 base::RunLoop().RunUntilIdle();
563 ConsumeDataReceived_ACK(id
);
564 EXPECT_EQ(0u, queued_messages());
565 EXPECT_TRUE(peer
.received_response());
566 EXPECT_EQ(kTestPageContents
, peer
.data());
567 EXPECT_TRUE(peer
.complete());
570 // Checks that deferring a request during a redirect delays messages until it's
572 TEST_F(ResourceDispatcherTest
, DeferOnRedirect
) {
573 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
574 TestRequestPeer
peer(dispatcher());
575 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
576 peer
.set_request_id(request_id
);
577 peer
.set_defer_on_redirect(true);
579 int id
= ConsumeRequestResource();
580 EXPECT_EQ(0u, queued_messages());
582 // The request should be deferred during the redirect, including the message
583 // to follow the redirect.
584 NotifyReceivedRedirect(id
);
585 NotifyReceivedResponse(id
);
586 NotifySetDataBuffer(id
, strlen(kTestPageContents
));
587 NotifyDataReceived(id
, kTestPageContents
);
588 NotifyRequestComplete(id
, strlen(kTestPageContents
));
590 // None of the messages should have been processed yet, so no queued messages
591 // to the browser process, and no data received by the peer.
592 EXPECT_EQ(0u, queued_messages());
593 EXPECT_EQ("", peer
.data());
594 EXPECT_FALSE(peer
.complete());
595 EXPECT_EQ(1, peer
.seen_redirects());
597 // Resuming the request should asynchronously unleash the deferred messages.
598 dispatcher()->SetDefersLoading(request_id
, false);
599 base::RunLoop().RunUntilIdle();
601 ConsumeFollowRedirect(id
);
602 ConsumeDataReceived_ACK(id
);
604 EXPECT_EQ(0u, queued_messages());
605 EXPECT_TRUE(peer
.received_response());
606 EXPECT_EQ(kTestPageContents
, peer
.data());
607 EXPECT_TRUE(peer
.complete());
608 EXPECT_EQ(1, peer
.seen_redirects());
611 // Checks that a deferred request that's cancelled doesn't receive any messages.
612 TEST_F(ResourceDispatcherTest
, CancelDeferredRequest
) {
613 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
614 TestRequestPeer
peer(dispatcher());
615 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
616 peer
.set_request_id(request_id
);
618 int id
= ConsumeRequestResource();
619 EXPECT_EQ(0u, queued_messages());
621 dispatcher()->SetDefersLoading(request_id
, true);
622 NotifyReceivedRedirect(id
);
623 dispatcher()->Cancel(request_id
);
624 ConsumeCancelRequest(id
);
626 NotifyRequestComplete(id
, 0);
627 base::RunLoop().RunUntilIdle();
629 // None of the messages should have been processed.
630 EXPECT_EQ(0u, queued_messages());
631 EXPECT_EQ("", peer
.data());
632 EXPECT_FALSE(peer
.complete());
633 EXPECT_EQ(0, peer
.seen_redirects());
636 TEST_F(ResourceDispatcherTest
, DownloadToFile
) {
637 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(true));
638 TestRequestPeer
peer(dispatcher());
639 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
640 peer
.set_request_id(request_id
);
641 const int kDownloadedIncrement
= 100;
642 const int kEncodedIncrement
= 50;
644 int id
= ConsumeRequestResource();
645 EXPECT_EQ(0u, queued_messages());
647 NotifyReceivedResponse(id
);
648 EXPECT_EQ(0u, queued_messages());
649 EXPECT_TRUE(peer
.received_response());
651 int expected_total_downloaded_length
= 0;
652 int expected_total_encoded_length
= 0;
653 for (int i
= 0; i
< 10; ++i
) {
654 NotifyDataDownloaded(id
, kDownloadedIncrement
, kEncodedIncrement
);
655 ConsumeDataDownloaded_ACK(id
);
656 expected_total_downloaded_length
+= kDownloadedIncrement
;
657 expected_total_encoded_length
+= kEncodedIncrement
;
658 EXPECT_EQ(expected_total_downloaded_length
,
659 peer
.total_downloaded_data_length());
660 EXPECT_EQ(expected_total_encoded_length
, peer
.total_encoded_data_length());
663 NotifyRequestComplete(id
, strlen(kTestPageContents
));
664 EXPECT_EQ("", peer
.data());
665 EXPECT_TRUE(peer
.complete());
666 EXPECT_EQ(0u, queued_messages());
668 dispatcher()->RemovePendingRequest(request_id
);
669 ConsumeReleaseDownloadedFile(id
);
670 EXPECT_EQ(0u, queued_messages());
671 EXPECT_EQ(expected_total_downloaded_length
,
672 peer
.total_downloaded_data_length());
673 EXPECT_EQ(expected_total_encoded_length
, peer
.total_encoded_data_length());
676 // Make sure that when a download to file is cancelled, the file is destroyed.
677 TEST_F(ResourceDispatcherTest
, CancelDownloadToFile
) {
678 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(true));
679 TestRequestPeer
peer(dispatcher());
680 int request_id
= dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
681 peer
.set_request_id(request_id
);
683 int id
= ConsumeRequestResource();
684 EXPECT_EQ(0u, queued_messages());
686 NotifyReceivedResponse(id
);
687 EXPECT_EQ(0u, queued_messages());
688 EXPECT_TRUE(peer
.received_response());
690 // Cancelling the request deletes the file.
691 dispatcher()->Cancel(request_id
);
692 ConsumeCancelRequest(id
);
693 ConsumeReleaseDownloadedFile(id
);
696 TEST_F(ResourceDispatcherTest
, Cookies
) {
700 TEST_F(ResourceDispatcherTest
, SerializedPostData
) {
704 class TimeConversionTest
: public ResourceDispatcherTest
,
707 bool Send(IPC::Message
* msg
) override
{
712 void PerformTest(const ResourceResponseHead
& response_head
) {
713 scoped_ptr
<RequestInfo
> request_info(CreateRequestInfo(false));
714 TestRequestPeer
peer(dispatcher());
715 dispatcher()->StartAsync(*request_info
.get(), NULL
, &peer
);
717 dispatcher()->OnMessageReceived(
718 ResourceMsg_ReceivedResponse(0, response_head
));
721 // RequestPeer methods.
722 void OnUploadProgress(uint64 position
, uint64 size
) override
{}
724 bool OnReceivedRedirect(const net::RedirectInfo
& redirect_info
,
725 const ResourceResponseInfo
& info
) override
{
729 void OnReceivedResponse(const ResourceResponseInfo
& info
) override
{
730 response_info_
= info
;
733 void OnDownloadedData(int len
, int encoded_data_length
) override
{}
735 void OnReceivedData(scoped_ptr
<ReceivedData
> data
) override
{}
737 void OnCompletedRequest(int error_code
,
738 bool was_ignored_by_handler
,
739 bool stale_copy_in_cache
,
740 const std::string
& security_info
,
741 const base::TimeTicks
& completion_time
,
742 int64 total_transfer_size
) override
{}
744 const ResourceResponseInfo
& response_info() const { return response_info_
; }
747 ResourceResponseInfo response_info_
;
750 // TODO(simonjam): Enable this when 10829031 lands.
751 TEST_F(TimeConversionTest
, DISABLED_ProperlyInitialized
) {
752 ResourceResponseHead response_head
;
753 response_head
.request_start
= base::TimeTicks::FromInternalValue(5);
754 response_head
.response_start
= base::TimeTicks::FromInternalValue(15);
755 response_head
.load_timing
.request_start_time
= base::Time::Now();
756 response_head
.load_timing
.request_start
=
757 base::TimeTicks::FromInternalValue(10);
758 response_head
.load_timing
.connect_timing
.connect_start
=
759 base::TimeTicks::FromInternalValue(13);
761 PerformTest(response_head
);
763 EXPECT_LT(base::TimeTicks(), response_info().load_timing
.request_start
);
764 EXPECT_EQ(base::TimeTicks(),
765 response_info().load_timing
.connect_timing
.dns_start
);
766 EXPECT_LE(response_head
.load_timing
.request_start
,
767 response_info().load_timing
.connect_timing
.connect_start
);
770 TEST_F(TimeConversionTest
, PartiallyInitialized
) {
771 ResourceResponseHead response_head
;
772 response_head
.request_start
= base::TimeTicks::FromInternalValue(5);
773 response_head
.response_start
= base::TimeTicks::FromInternalValue(15);
775 PerformTest(response_head
);
777 EXPECT_EQ(base::TimeTicks(), response_info().load_timing
.request_start
);
778 EXPECT_EQ(base::TimeTicks(),
779 response_info().load_timing
.connect_timing
.dns_start
);
782 TEST_F(TimeConversionTest
, NotInitialized
) {
783 ResourceResponseHead response_head
;
785 PerformTest(response_head
);
787 EXPECT_EQ(base::TimeTicks(), response_info().load_timing
.request_start
);
788 EXPECT_EQ(base::TimeTicks(),
789 response_info().load_timing
.connect_timing
.dns_start
);
792 } // namespace content