Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / child / resource_dispatcher_unittest.cc
blobe449c3346a18d2576a683db54bfb9fbf5a491ccf
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 <string>
6 #include <vector>
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"
27 namespace content {
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 {
42 public:
43 TestRequestPeer(ResourceDispatcher* dispatcher)
44 : follow_redirects_(true),
45 defer_on_redirect_(false),
46 seen_redirects_(0),
47 cancel_on_receive_response_(false),
48 received_response_(false),
49 total_encoded_data_length_(0),
50 total_downloaded_data_length_(0),
51 complete_(false),
52 dispatcher_(dispatcher),
53 request_id_(0) {
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 {
62 ++seen_redirects_;
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_);
95 complete_ = true;
98 void OnReceivedCompletedResponse(const ResourceResponseInfo& info,
99 scoped_ptr<ReceivedData> data,
100 int error_code,
101 bool was_ignored_by_handler,
102 bool stale_copy_in_cache,
103 const std::string& security_info,
104 const base::TimeTicks& completion_time,
105 int64 total_transfer_size) override {
106 bool cancel_on_receive_response = cancel_on_receive_response_;
107 OnReceivedResponse(info);
108 if (cancel_on_receive_response)
109 return;
110 if (data)
111 OnReceivedData(data.Pass());
112 OnCompletedRequest(error_code, was_ignored_by_handler, stale_copy_in_cache,
113 security_info, completion_time, total_transfer_size);
116 void set_follow_redirects(bool follow_redirects) {
117 follow_redirects_ = follow_redirects;
120 void set_defer_on_redirect(bool defer_on_redirect) {
121 defer_on_redirect_ = defer_on_redirect;
124 void set_cancel_on_receive_response(bool cancel_on_receive_response) {
125 cancel_on_receive_response_ = cancel_on_receive_response;
128 int seen_redirects() const { return seen_redirects_; }
130 bool received_response() const { return received_response_; }
132 const std::string& data() const {
133 return data_;
135 int total_encoded_data_length() const {
136 return total_encoded_data_length_;
138 int total_downloaded_data_length() const {
139 return total_downloaded_data_length_;
142 bool complete() const { return complete_; }
144 private:
145 // True if should follow redirects, false if should cancel them.
146 bool follow_redirects_;
147 // True if the request should be deferred on redirects.
148 bool defer_on_redirect_;
149 // Number of total redirects seen.
150 int seen_redirects_;
152 bool cancel_on_receive_response_;
153 bool received_response_;
155 // Data received. If downloading to file, remains empty.
156 std::string data_;
157 // Total encoded data length, regardless of whether downloading to a file or
158 // not.
159 int total_encoded_data_length_;
160 // Total length when downloading to a file.
161 int total_downloaded_data_length_;
163 bool complete_;
165 ResourceDispatcher* dispatcher_;
166 int request_id_;
168 DISALLOW_COPY_AND_ASSIGN(TestRequestPeer);
171 // Sets up the message sender override for the unit test.
172 class ResourceDispatcherTest : public testing::Test, public IPC::Sender {
173 public:
174 ResourceDispatcherTest() : dispatcher_(this, message_loop_.task_runner()) {}
176 ~ResourceDispatcherTest() override {
177 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(),
178 shared_memory_map_.end());
181 // Emulates IPC send operations (IPC::Sender) by adding
182 // pending messages to the queue.
183 bool Send(IPC::Message* msg) override {
184 message_queue_.push_back(IPC::Message(*msg));
185 delete msg;
186 return true;
189 size_t queued_messages() const { return message_queue_.size(); }
191 // Returns the ID of the consumed request. Can't make assumptions about the
192 // ID, because numbering is based on a global.
193 int ConsumeRequestResource() {
194 if (message_queue_.empty()) {
195 ADD_FAILURE() << "Missing resource request message";
196 return -1;
199 ResourceHostMsg_RequestResource::Param params;
200 if (ResourceHostMsg_RequestResource::ID != message_queue_[0].type() ||
201 !ResourceHostMsg_RequestResource::Read(&message_queue_[0], &params)) {
202 ADD_FAILURE() << "Expected ResourceHostMsg_RequestResource message";
203 return -1;
205 ResourceHostMsg_Request request = base::get<2>(params);
206 EXPECT_EQ(kTestPageUrl, request.url.spec());
207 message_queue_.erase(message_queue_.begin());
208 return base::get<1>(params);
211 void ConsumeFollowRedirect(int expected_request_id) {
212 ASSERT_FALSE(message_queue_.empty());
213 base::Tuple<int> args;
214 ASSERT_EQ(ResourceHostMsg_FollowRedirect::ID, message_queue_[0].type());
215 ASSERT_TRUE(ResourceHostMsg_FollowRedirect::Read(
216 &message_queue_[0], &args));
217 EXPECT_EQ(expected_request_id, base::get<0>(args));
218 message_queue_.erase(message_queue_.begin());
221 void ConsumeDataReceived_ACK(int expected_request_id) {
222 ASSERT_FALSE(message_queue_.empty());
223 base::Tuple<int> args;
224 ASSERT_EQ(ResourceHostMsg_DataReceived_ACK::ID, message_queue_[0].type());
225 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
226 &message_queue_[0], &args));
227 EXPECT_EQ(expected_request_id, base::get<0>(args));
228 message_queue_.erase(message_queue_.begin());
231 void ConsumeDataDownloaded_ACK(int expected_request_id) {
232 ASSERT_FALSE(message_queue_.empty());
233 base::Tuple<int> args;
234 ASSERT_EQ(ResourceHostMsg_DataDownloaded_ACK::ID, message_queue_[0].type());
235 ASSERT_TRUE(ResourceHostMsg_DataDownloaded_ACK::Read(
236 &message_queue_[0], &args));
237 EXPECT_EQ(expected_request_id, base::get<0>(args));
238 message_queue_.erase(message_queue_.begin());
241 void ConsumeReleaseDownloadedFile(int expected_request_id) {
242 ASSERT_FALSE(message_queue_.empty());
243 base::Tuple<int> args;
244 ASSERT_EQ(ResourceHostMsg_ReleaseDownloadedFile::ID,
245 message_queue_[0].type());
246 ASSERT_TRUE(ResourceHostMsg_ReleaseDownloadedFile::Read(
247 &message_queue_[0], &args));
248 EXPECT_EQ(expected_request_id, base::get<0>(args));
249 message_queue_.erase(message_queue_.begin());
252 void ConsumeCancelRequest(int expected_request_id) {
253 ASSERT_FALSE(message_queue_.empty());
254 base::Tuple<int> args;
255 ASSERT_EQ(ResourceHostMsg_CancelRequest::ID, message_queue_[0].type());
256 ASSERT_TRUE(ResourceHostMsg_CancelRequest::Read(
257 &message_queue_[0], &args));
258 EXPECT_EQ(expected_request_id, base::get<0>(args));
259 message_queue_.erase(message_queue_.begin());
262 void NotifyReceivedRedirect(int request_id) {
263 ResourceResponseHead head;
264 std::string raw_headers(kTestRedirectHeaders);
265 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
266 head.headers = new net::HttpResponseHeaders(raw_headers);
267 net::RedirectInfo redirect_info;
268 redirect_info.status_code = 302;
269 redirect_info.new_method = "GET";
270 redirect_info.new_url = GURL(kTestPageUrl);
271 redirect_info.new_first_party_for_cookies = GURL(kTestPageUrl);
272 EXPECT_EQ(true, dispatcher_.OnMessageReceived(
273 ResourceMsg_ReceivedRedirect(request_id, redirect_info, head)));
276 void NotifyReceivedResponse(int request_id) {
277 ResourceResponseHead head;
278 std::string raw_headers(kTestPageHeaders);
279 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
280 head.headers = new net::HttpResponseHeaders(raw_headers);
281 head.mime_type = kTestPageMimeType;
282 head.charset = kTestPageCharset;
283 EXPECT_EQ(true,
284 dispatcher_.OnMessageReceived(
285 ResourceMsg_ReceivedResponse(request_id, head)));
288 void NotifySetDataBuffer(int request_id, size_t buffer_size) {
289 base::SharedMemory* shared_memory = new base::SharedMemory();
290 ASSERT_FALSE(shared_memory_map_[request_id]);
291 shared_memory_map_[request_id] = shared_memory;
292 EXPECT_TRUE(shared_memory->CreateAndMapAnonymous(buffer_size));
294 base::SharedMemoryHandle duplicate_handle;
295 EXPECT_TRUE(shared_memory->ShareToProcess(base::GetCurrentProcessHandle(),
296 &duplicate_handle));
297 EXPECT_TRUE(dispatcher_.OnMessageReceived(
298 ResourceMsg_SetDataBuffer(request_id, duplicate_handle,
299 shared_memory->requested_size(), 0)));
302 void NotifyDataReceived(int request_id, std::string data) {
303 ASSERT_LE(data.length(), shared_memory_map_[request_id]->requested_size());
304 memcpy(shared_memory_map_[request_id]->memory(), data.c_str(),
305 data.length());
307 EXPECT_TRUE(dispatcher_.OnMessageReceived(
308 ResourceMsg_DataReceived(request_id, 0, data.length(), data.length())));
311 void NotifyDataDownloaded(int request_id, int decoded_length,
312 int encoded_length) {
313 EXPECT_TRUE(dispatcher_.OnMessageReceived(
314 ResourceMsg_DataDownloaded(request_id, decoded_length,
315 encoded_length)));
318 void NotifyRequestComplete(int request_id, size_t total_size) {
319 ResourceMsg_RequestCompleteData request_complete_data;
320 request_complete_data.error_code = net::OK;
321 request_complete_data.was_ignored_by_handler = false;
322 request_complete_data.exists_in_cache = false;
323 request_complete_data.encoded_data_length = total_size;
324 EXPECT_TRUE(dispatcher_.OnMessageReceived(
325 ResourceMsg_RequestComplete(request_id, request_complete_data)));
328 RequestInfo* CreateRequestInfo(bool download_to_file) {
329 RequestInfo* request_info = new RequestInfo();
330 request_info->method = "GET";
331 request_info->url = GURL(kTestPageUrl);
332 request_info->first_party_for_cookies = GURL(kTestPageUrl);
333 request_info->referrer = Referrer();
334 request_info->headers = std::string();
335 request_info->load_flags = 0;
336 request_info->requestor_pid = 0;
337 request_info->request_type = RESOURCE_TYPE_SUB_RESOURCE;
338 request_info->appcache_host_id = kAppCacheNoHostId;
339 request_info->should_reset_appcache = false;
340 request_info->routing_id = 0;
341 request_info->download_to_file = download_to_file;
342 RequestExtraData extra_data;
344 return request_info;
347 ResourceDispatcher* dispatcher() { return &dispatcher_; }
349 private:
350 // Map of request IDs to shared memory.
351 std::map<int, base::SharedMemory*> shared_memory_map_;
353 std::vector<IPC::Message> message_queue_;
354 base::MessageLoop message_loop_;
355 ResourceDispatcher dispatcher_;
358 // Does a simple request and tests that the correct data is received. Simulates
359 // two reads.
360 TEST_F(ResourceDispatcherTest, RoundTrip) {
361 // Number of bytes received in the first read.
362 const size_t kFirstReceiveSize = 2;
363 ASSERT_LT(kFirstReceiveSize, strlen(kTestPageContents));
365 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
366 TestRequestPeer peer(dispatcher());
367 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
368 peer.set_request_id(request_id);
370 int id = ConsumeRequestResource();
371 EXPECT_EQ(0u, queued_messages());
373 NotifyReceivedResponse(id);
374 EXPECT_EQ(0u, queued_messages());
375 EXPECT_TRUE(peer.received_response());
377 NotifySetDataBuffer(id, strlen(kTestPageContents));
378 NotifyDataReceived(id, std::string(kTestPageContents, kFirstReceiveSize));
379 ConsumeDataReceived_ACK(id);
380 EXPECT_EQ(0u, queued_messages());
382 NotifyDataReceived(id, kTestPageContents + kFirstReceiveSize);
383 ConsumeDataReceived_ACK(id);
384 EXPECT_EQ(0u, queued_messages());
386 NotifyRequestComplete(id, strlen(kTestPageContents));
387 EXPECT_EQ(kTestPageContents, peer.data());
388 EXPECT_TRUE(peer.complete());
389 EXPECT_EQ(0u, queued_messages());
392 // Tests that the request IDs are straight when there are two interleaving
393 // requests.
394 TEST_F(ResourceDispatcherTest, MultipleRequests) {
395 const char kTestPageContents2[] = "Not kTestPageContents";
397 scoped_ptr<RequestInfo> request_info1(CreateRequestInfo(false));
398 TestRequestPeer peer1(dispatcher());
399 int request_id1 = dispatcher()->StartAsync(
400 *request_info1.get(), NULL, &peer1);
401 peer1.set_request_id(request_id1);
402 scoped_ptr<RequestInfo> request_info2(CreateRequestInfo(false));
403 TestRequestPeer peer2(dispatcher());
404 int request_id2 = dispatcher()->StartAsync(
405 *request_info1.get(), NULL, &peer2);
406 peer2.set_request_id(request_id2);
408 int id1 = ConsumeRequestResource();
409 int id2 = ConsumeRequestResource();
410 EXPECT_EQ(0u, queued_messages());
412 NotifyReceivedResponse(id1);
413 EXPECT_TRUE(peer1.received_response());
414 EXPECT_FALSE(peer2.received_response());
415 NotifyReceivedResponse(id2);
416 EXPECT_TRUE(peer2.received_response());
417 EXPECT_EQ(0u, queued_messages());
419 NotifySetDataBuffer(id2, strlen(kTestPageContents2));
420 NotifyDataReceived(id2, kTestPageContents2);
421 ConsumeDataReceived_ACK(id2);
422 NotifySetDataBuffer(id1, strlen(kTestPageContents));
423 NotifyDataReceived(id1, kTestPageContents);
424 ConsumeDataReceived_ACK(id1);
425 EXPECT_EQ(0u, queued_messages());
427 NotifyRequestComplete(id1, strlen(kTestPageContents));
428 EXPECT_EQ(kTestPageContents, peer1.data());
429 EXPECT_TRUE(peer1.complete());
430 EXPECT_FALSE(peer2.complete());
432 NotifyRequestComplete(id2, strlen(kTestPageContents2));
433 EXPECT_EQ(kTestPageContents2, peer2.data());
434 EXPECT_TRUE(peer2.complete());
436 EXPECT_EQ(0u, queued_messages());
439 // Tests that the cancel method prevents other messages from being received.
440 TEST_F(ResourceDispatcherTest, Cancel) {
441 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
442 TestRequestPeer peer(dispatcher());
443 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
444 peer.set_request_id(request_id);
446 int id = ConsumeRequestResource();
447 EXPECT_EQ(0u, queued_messages());
449 // Cancel the request.
450 dispatcher()->Cancel(request_id);
451 ConsumeCancelRequest(id);
453 // Any future messages related to the request should be ignored.
454 NotifyReceivedResponse(id);
455 NotifySetDataBuffer(id, strlen(kTestPageContents));
456 NotifyDataReceived(id, kTestPageContents);
457 NotifyRequestComplete(id, strlen(kTestPageContents));
459 EXPECT_EQ(0u, queued_messages());
460 EXPECT_EQ("", peer.data());
461 EXPECT_FALSE(peer.received_response());
462 EXPECT_FALSE(peer.complete());
465 // Tests that calling cancel during a callback works as expected.
466 TEST_F(ResourceDispatcherTest, CancelDuringCallback) {
467 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
468 TestRequestPeer peer(dispatcher());
469 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
470 peer.set_request_id(request_id);
471 peer.set_cancel_on_receive_response(true);
473 int id = ConsumeRequestResource();
474 EXPECT_EQ(0u, queued_messages());
476 NotifyReceivedResponse(id);
477 EXPECT_TRUE(peer.received_response());
478 // Request should have been cancelled.
479 ConsumeCancelRequest(id);
481 // Any future messages related to the request should be ignored.
482 NotifySetDataBuffer(id, strlen(kTestPageContents));
483 NotifyDataReceived(id, kTestPageContents);
484 NotifyRequestComplete(id, strlen(kTestPageContents));
486 EXPECT_EQ(0u, queued_messages());
487 EXPECT_EQ("", peer.data());
488 EXPECT_FALSE(peer.complete());
491 // Checks that redirects work as expected.
492 TEST_F(ResourceDispatcherTest, Redirect) {
493 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
494 TestRequestPeer peer(dispatcher());
495 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
496 peer.set_request_id(request_id);
498 int id = ConsumeRequestResource();
500 NotifyReceivedRedirect(id);
501 ConsumeFollowRedirect(id);
502 EXPECT_EQ(1, peer.seen_redirects());
504 NotifyReceivedRedirect(id);
505 ConsumeFollowRedirect(id);
506 EXPECT_EQ(2, peer.seen_redirects());
508 NotifyReceivedResponse(id);
509 EXPECT_TRUE(peer.received_response());
511 NotifySetDataBuffer(id, strlen(kTestPageContents));
512 NotifyDataReceived(id, kTestPageContents);
513 ConsumeDataReceived_ACK(id);
515 NotifyRequestComplete(id, strlen(kTestPageContents));
516 EXPECT_EQ(kTestPageContents, peer.data());
517 EXPECT_TRUE(peer.complete());
518 EXPECT_EQ(0u, queued_messages());
519 EXPECT_EQ(2, peer.seen_redirects());
522 // Tests that that cancelling during a redirect method prevents other messages
523 // from being received.
524 TEST_F(ResourceDispatcherTest, CancelDuringRedirect) {
525 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
526 TestRequestPeer peer(dispatcher());
527 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
528 peer.set_request_id(request_id);
529 peer.set_follow_redirects(false);
531 int id = ConsumeRequestResource();
532 EXPECT_EQ(0u, queued_messages());
534 // Redirect the request, which triggers a cancellation.
535 NotifyReceivedRedirect(id);
536 ConsumeCancelRequest(id);
537 EXPECT_EQ(1, peer.seen_redirects());
538 EXPECT_EQ(0u, queued_messages());
540 // Any future messages related to the request should be ignored. In practice,
541 // only the NotifyRequestComplete should be received after this point.
542 NotifyReceivedRedirect(id);
543 NotifyReceivedResponse(id);
544 NotifySetDataBuffer(id, strlen(kTestPageContents));
545 NotifyDataReceived(id, kTestPageContents);
546 NotifyRequestComplete(id, strlen(kTestPageContents));
548 EXPECT_EQ(0u, queued_messages());
549 EXPECT_EQ("", peer.data());
550 EXPECT_FALSE(peer.complete());
551 EXPECT_EQ(1, peer.seen_redirects());
554 // Checks that deferring a request delays messages until it's resumed.
555 TEST_F(ResourceDispatcherTest, Defer) {
556 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
557 TestRequestPeer peer(dispatcher());
558 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
559 peer.set_request_id(request_id);
561 int id = ConsumeRequestResource();
562 EXPECT_EQ(0u, queued_messages());
564 dispatcher()->SetDefersLoading(request_id, true);
565 NotifyReceivedResponse(id);
566 NotifySetDataBuffer(id, strlen(kTestPageContents));
567 NotifyDataReceived(id, kTestPageContents);
568 NotifyRequestComplete(id, strlen(kTestPageContents));
570 // None of the messages should have been processed yet, so no queued messages
571 // to the browser process, and no data received by the peer.
572 EXPECT_EQ(0u, queued_messages());
573 EXPECT_EQ("", peer.data());
574 EXPECT_FALSE(peer.complete());
575 EXPECT_EQ(0, peer.seen_redirects());
577 // Resuming the request should asynchronously unleash the deferred messages.
578 dispatcher()->SetDefersLoading(request_id, false);
579 base::RunLoop().RunUntilIdle();
581 ConsumeDataReceived_ACK(id);
582 EXPECT_EQ(0u, queued_messages());
583 EXPECT_TRUE(peer.received_response());
584 EXPECT_EQ(kTestPageContents, peer.data());
585 EXPECT_TRUE(peer.complete());
588 // Checks that deferring a request during a redirect delays messages until it's
589 // resumed.
590 TEST_F(ResourceDispatcherTest, DeferOnRedirect) {
591 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
592 TestRequestPeer peer(dispatcher());
593 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
594 peer.set_request_id(request_id);
595 peer.set_defer_on_redirect(true);
597 int id = ConsumeRequestResource();
598 EXPECT_EQ(0u, queued_messages());
600 // The request should be deferred during the redirect, including the message
601 // to follow the redirect.
602 NotifyReceivedRedirect(id);
603 NotifyReceivedResponse(id);
604 NotifySetDataBuffer(id, strlen(kTestPageContents));
605 NotifyDataReceived(id, kTestPageContents);
606 NotifyRequestComplete(id, strlen(kTestPageContents));
608 // None of the messages should have been processed yet, so no queued messages
609 // to the browser process, and no data received by the peer.
610 EXPECT_EQ(0u, queued_messages());
611 EXPECT_EQ("", peer.data());
612 EXPECT_FALSE(peer.complete());
613 EXPECT_EQ(1, peer.seen_redirects());
615 // Resuming the request should asynchronously unleash the deferred messages.
616 dispatcher()->SetDefersLoading(request_id, false);
617 base::RunLoop().RunUntilIdle();
619 ConsumeFollowRedirect(id);
620 ConsumeDataReceived_ACK(id);
622 EXPECT_EQ(0u, queued_messages());
623 EXPECT_TRUE(peer.received_response());
624 EXPECT_EQ(kTestPageContents, peer.data());
625 EXPECT_TRUE(peer.complete());
626 EXPECT_EQ(1, peer.seen_redirects());
629 // Checks that a deferred request that's cancelled doesn't receive any messages.
630 TEST_F(ResourceDispatcherTest, CancelDeferredRequest) {
631 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
632 TestRequestPeer peer(dispatcher());
633 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
634 peer.set_request_id(request_id);
636 int id = ConsumeRequestResource();
637 EXPECT_EQ(0u, queued_messages());
639 dispatcher()->SetDefersLoading(request_id, true);
640 NotifyReceivedRedirect(id);
641 dispatcher()->Cancel(request_id);
642 ConsumeCancelRequest(id);
644 NotifyRequestComplete(id, 0);
645 base::RunLoop().RunUntilIdle();
647 // None of the messages should have been processed.
648 EXPECT_EQ(0u, queued_messages());
649 EXPECT_EQ("", peer.data());
650 EXPECT_FALSE(peer.complete());
651 EXPECT_EQ(0, peer.seen_redirects());
654 TEST_F(ResourceDispatcherTest, DownloadToFile) {
655 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(true));
656 TestRequestPeer peer(dispatcher());
657 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
658 peer.set_request_id(request_id);
659 const int kDownloadedIncrement = 100;
660 const int kEncodedIncrement = 50;
662 int id = ConsumeRequestResource();
663 EXPECT_EQ(0u, queued_messages());
665 NotifyReceivedResponse(id);
666 EXPECT_EQ(0u, queued_messages());
667 EXPECT_TRUE(peer.received_response());
669 int expected_total_downloaded_length = 0;
670 int expected_total_encoded_length = 0;
671 for (int i = 0; i < 10; ++i) {
672 NotifyDataDownloaded(id, kDownloadedIncrement, kEncodedIncrement);
673 ConsumeDataDownloaded_ACK(id);
674 expected_total_downloaded_length += kDownloadedIncrement;
675 expected_total_encoded_length += kEncodedIncrement;
676 EXPECT_EQ(expected_total_downloaded_length,
677 peer.total_downloaded_data_length());
678 EXPECT_EQ(expected_total_encoded_length, peer.total_encoded_data_length());
681 NotifyRequestComplete(id, strlen(kTestPageContents));
682 EXPECT_EQ("", peer.data());
683 EXPECT_TRUE(peer.complete());
684 EXPECT_EQ(0u, queued_messages());
686 dispatcher()->RemovePendingRequest(request_id);
687 ConsumeReleaseDownloadedFile(id);
688 EXPECT_EQ(0u, queued_messages());
689 EXPECT_EQ(expected_total_downloaded_length,
690 peer.total_downloaded_data_length());
691 EXPECT_EQ(expected_total_encoded_length, peer.total_encoded_data_length());
694 // Make sure that when a download to file is cancelled, the file is destroyed.
695 TEST_F(ResourceDispatcherTest, CancelDownloadToFile) {
696 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(true));
697 TestRequestPeer peer(dispatcher());
698 int request_id = dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
699 peer.set_request_id(request_id);
701 int id = ConsumeRequestResource();
702 EXPECT_EQ(0u, queued_messages());
704 NotifyReceivedResponse(id);
705 EXPECT_EQ(0u, queued_messages());
706 EXPECT_TRUE(peer.received_response());
708 // Cancelling the request deletes the file.
709 dispatcher()->Cancel(request_id);
710 ConsumeCancelRequest(id);
711 ConsumeReleaseDownloadedFile(id);
714 TEST_F(ResourceDispatcherTest, Cookies) {
715 // FIXME
718 TEST_F(ResourceDispatcherTest, SerializedPostData) {
719 // FIXME
722 class TimeConversionTest : public ResourceDispatcherTest,
723 public RequestPeer {
724 public:
725 bool Send(IPC::Message* msg) override {
726 delete msg;
727 return true;
730 void PerformTest(const ResourceResponseHead& response_head) {
731 scoped_ptr<RequestInfo> request_info(CreateRequestInfo(false));
732 TestRequestPeer peer(dispatcher());
733 dispatcher()->StartAsync(*request_info.get(), NULL, &peer);
735 dispatcher()->OnMessageReceived(
736 ResourceMsg_ReceivedResponse(0, response_head));
739 // RequestPeer methods.
740 void OnUploadProgress(uint64 position, uint64 size) override {}
742 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info,
743 const ResourceResponseInfo& info) override {
744 return true;
747 void OnReceivedResponse(const ResourceResponseInfo& info) override {
748 response_info_ = info;
751 void OnDownloadedData(int len, int encoded_data_length) override {}
753 void OnReceivedData(scoped_ptr<ReceivedData> data) override {}
755 void OnCompletedRequest(int error_code,
756 bool was_ignored_by_handler,
757 bool stale_copy_in_cache,
758 const std::string& security_info,
759 const base::TimeTicks& completion_time,
760 int64 total_transfer_size) override {}
762 void OnReceivedCompletedResponse(const ResourceResponseInfo& info,
763 scoped_ptr<ReceivedData> data,
764 int error_code,
765 bool was_ignored_by_handler,
766 bool stale_copy_in_cache,
767 const std::string& security_info,
768 const base::TimeTicks& completion_time,
769 int64 total_transfer_size) override {}
771 const ResourceResponseInfo& response_info() const { return response_info_; }
773 private:
774 ResourceResponseInfo response_info_;
777 // TODO(simonjam): Enable this when 10829031 lands.
778 TEST_F(TimeConversionTest, DISABLED_ProperlyInitialized) {
779 ResourceResponseHead response_head;
780 response_head.request_start = base::TimeTicks::FromInternalValue(5);
781 response_head.response_start = base::TimeTicks::FromInternalValue(15);
782 response_head.load_timing.request_start_time = base::Time::Now();
783 response_head.load_timing.request_start =
784 base::TimeTicks::FromInternalValue(10);
785 response_head.load_timing.connect_timing.connect_start =
786 base::TimeTicks::FromInternalValue(13);
788 PerformTest(response_head);
790 EXPECT_LT(base::TimeTicks(), response_info().load_timing.request_start);
791 EXPECT_EQ(base::TimeTicks(),
792 response_info().load_timing.connect_timing.dns_start);
793 EXPECT_LE(response_head.load_timing.request_start,
794 response_info().load_timing.connect_timing.connect_start);
797 TEST_F(TimeConversionTest, PartiallyInitialized) {
798 ResourceResponseHead response_head;
799 response_head.request_start = base::TimeTicks::FromInternalValue(5);
800 response_head.response_start = base::TimeTicks::FromInternalValue(15);
802 PerformTest(response_head);
804 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start);
805 EXPECT_EQ(base::TimeTicks(),
806 response_info().load_timing.connect_timing.dns_start);
809 TEST_F(TimeConversionTest, NotInitialized) {
810 ResourceResponseHead response_head;
812 PerformTest(response_head);
814 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start);
815 EXPECT_EQ(base::TimeTicks(),
816 response_info().load_timing.connect_timing.dns_start);
819 } // namespace content