blob: b250ca45455a278be2e530cd302f4a33dfa82ea3 [file] [log] [blame]
[email protected]80744782012-05-04 01:47:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
dchengf63a1252015-12-26 20:43:135#include "content/child/resource_dispatcher.h"
6
avi66a07722015-12-25 23:38:127#include <stddef.h>
8#include <stdint.h>
dchengc864f522016-04-08 23:55:279
10#include <memory>
initial.commit09911bf2008-07-26 23:55:2911#include <string>
dchengf63a1252015-12-26 20:43:1312#include <utility>
initial.commit09911bf2008-07-26 23:55:2913#include <vector>
14
[email protected]0a0e2c7b2014-06-16 19:10:1415#include "base/macros.h"
dchengc864f522016-04-08 23:55:2716#include "base/memory/ptr_util.h"
[email protected]0a0e2c7b2014-06-16 19:10:1417#include "base/memory/shared_memory.h"
[email protected]e55f5642013-07-18 00:22:5418#include "base/message_loop/message_loop.h"
[email protected]54724e22013-07-25 13:02:1519#include "base/process/process_handle.h"
[email protected]2a1a06d2014-06-04 12:24:3620#include "base/run_loop.h"
[email protected]0a0e2c7b2014-06-16 19:10:1421#include "base/stl_util.h"
[email protected]541b7b02013-06-07 00:59:3422#include "content/child/request_extra_data.h"
[email protected]abfd92e2014-03-24 02:34:4623#include "content/child/request_info.h"
[email protected]98d6d4562014-06-25 20:57:5524#include "content/common/appcache_interfaces.h"
[email protected]94dc971d2011-03-05 19:08:3225#include "content/common/resource_messages.h"
yhirano20c94ea9b2016-05-18 05:20:4526#include "content/common/resource_request.h"
27#include "content/common/resource_request_completion_status.h"
[email protected]fa07f6572014-03-06 13:10:1028#include "content/common/service_worker/service_worker_types.h"
kinuko5e4122a22016-02-09 02:38:2429#include "content/public/child/fixed_received_data.h"
[email protected]ddf55bb2014-04-03 08:24:4730#include "content/public/child/request_peer.h"
kinuko5e4122a22016-02-09 02:38:2431#include "content/public/child/resource_dispatcher_delegate.h"
[email protected]2336ffe2011-11-24 01:23:3432#include "content/public/common/resource_response.h"
[email protected]2756a8e2012-09-07 18:24:2933#include "net/base/net_errors.h"
[email protected]7a4de7a62010-08-17 18:38:2434#include "net/http/http_response_headers.h"
initial.commit09911bf2008-07-26 23:55:2935#include "testing/gtest/include/gtest/gtest.h"
initial.commit09911bf2008-07-26 23:55:2936
[email protected]be7b41e82012-07-04 09:46:5137namespace content {
38
[email protected]0a0e2c7b2014-06-16 19:10:1439static const char kTestPageUrl[] = "http://www.google.com/";
40static const char kTestPageHeaders[] =
initial.commit09911bf2008-07-26 23:55:2941 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
[email protected]0a0e2c7b2014-06-16 19:10:1442static const char kTestPageMimeType[] = "text/html";
43static const char kTestPageCharset[] = "";
44static const char kTestPageContents[] =
initial.commit09911bf2008-07-26 23:55:2945 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
[email protected]0a0e2c7b2014-06-16 19:10:1446static const char kTestRedirectHeaders[] =
47 "HTTP/1.1 302 Found\nLocation:http://www.google.com/\n\n";
initial.commit09911bf2008-07-26 23:55:2948
49// Listens for request response data and stores it so that it can be compared
50// to the reference data.
[email protected]0a0e2c7b2014-06-16 19:10:1451class TestRequestPeer : public RequestPeer {
initial.commit09911bf2008-07-26 23:55:2952 public:
kinukoccbf2c9572016-02-03 22:54:3753 struct Context;
54 TestRequestPeer(ResourceDispatcher* dispatcher, Context* context)
55 : dispatcher_(dispatcher), context_(context) {}
jam4a1511ef2015-02-19 20:29:1756
avi66a07722015-12-25 23:38:1257 void OnUploadProgress(uint64_t position, uint64_t size) override {}
[email protected]bb551622010-07-22 20:52:4958
dchenge933b3e2014-10-21 11:44:0959 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info,
60 const ResourceResponseInfo& info) override {
kinuko5e4122a22016-02-09 02:38:2461 EXPECT_FALSE(context_->cancelled);
kinukoccbf2c9572016-02-03 22:54:3762 ++context_->seen_redirects;
63 if (context_->defer_on_redirect)
64 dispatcher_->SetDefersLoading(context_->request_id, true);
65 return context_->follow_redirects;
initial.commit09911bf2008-07-26 23:55:2966 }
67
dchenge933b3e2014-10-21 11:44:0968 void OnReceivedResponse(const ResourceResponseInfo& info) override {
kinuko5e4122a22016-02-09 02:38:2469 EXPECT_FALSE(context_->cancelled);
kinukoccbf2c9572016-02-03 22:54:3770 EXPECT_FALSE(context_->received_response);
71 context_->received_response = true;
kinuko5e4122a22016-02-09 02:38:2472 if (context_->cancel_on_receive_response) {
kinukoccbf2c9572016-02-03 22:54:3773 dispatcher_->Cancel(context_->request_id);
kinuko5e4122a22016-02-09 02:38:2474 context_->cancelled = true;
75 }
initial.commit09911bf2008-07-26 23:55:2976 }
77
dchenge933b3e2014-10-21 11:44:0978 void OnDownloadedData(int len, int encoded_data_length) override {
kinuko5e4122a22016-02-09 02:38:2479 EXPECT_FALSE(context_->cancelled);
kinukoccbf2c9572016-02-03 22:54:3780 context_->total_downloaded_data_length += len;
81 context_->total_encoded_data_length += encoded_data_length;
[email protected]bb551622010-07-22 20:52:4982 }
83
dchengc864f522016-04-08 23:55:2784 void OnReceivedData(std::unique_ptr<ReceivedData> data) override {
kinuko5e4122a22016-02-09 02:38:2485 if (context_->cancelled)
86 return;
kinukoccbf2c9572016-02-03 22:54:3787 EXPECT_TRUE(context_->received_response);
88 EXPECT_FALSE(context_->complete);
89 context_->data.append(data->payload(), data->length());
90 context_->total_encoded_data_length += data->encoded_length();
initial.commit09911bf2008-07-26 23:55:2991 }
92
dchenge933b3e2014-10-21 11:44:0993 void OnCompletedRequest(int error_code,
94 bool was_ignored_by_handler,
95 bool stale_copy_in_cache,
96 const std::string& security_info,
97 const base::TimeTicks& completion_time,
avi66a07722015-12-25 23:38:1298 int64_t total_transfer_size) override {
kinuko5e4122a22016-02-09 02:38:2499 if (context_->cancelled)
100 return;
kinukoccbf2c9572016-02-03 22:54:37101 EXPECT_TRUE(context_->received_response);
102 EXPECT_FALSE(context_->complete);
103 context_->complete = true;
initial.commit09911bf2008-07-26 23:55:29104 }
105
kinukoccbf2c9572016-02-03 22:54:37106 struct Context {
107 // True if should follow redirects, false if should cancel them.
108 bool follow_redirects = true;
109 // True if the request should be deferred on redirects.
110 bool defer_on_redirect = false;
[email protected]0a0e2c7b2014-06-16 19:10:14111
kinukoccbf2c9572016-02-03 22:54:37112 // Number of total redirects seen.
113 int seen_redirects = 0;
[email protected]0a0e2c7b2014-06-16 19:10:14114
kinukoccbf2c9572016-02-03 22:54:37115 bool cancel_on_receive_response = false;
116 bool received_response = false;
[email protected]0a0e2c7b2014-06-16 19:10:14117
kinukoccbf2c9572016-02-03 22:54:37118 // Data received. If downloading to file, remains empty.
119 std::string data;
[email protected]0a0e2c7b2014-06-16 19:10:14120
kinukoccbf2c9572016-02-03 22:54:37121 // Total encoded data length, regardless of whether downloading to a file or
122 // not.
123 int total_encoded_data_length = 0;
124 // Total length when downloading to a file.
125 int total_downloaded_data_length = 0;
[email protected]0a0e2c7b2014-06-16 19:10:14126
kinukoccbf2c9572016-02-03 22:54:37127 bool complete = false;
kinuko5e4122a22016-02-09 02:38:24128 bool cancelled = false;
kinukoccbf2c9572016-02-03 22:54:37129 int request_id = -1;
130 };
initial.commit09911bf2008-07-26 23:55:29131
132 private:
jam4a1511ef2015-02-19 20:29:17133 ResourceDispatcher* dispatcher_;
kinukoccbf2c9572016-02-03 22:54:37134 Context* context_;
[email protected]0a0e2c7b2014-06-16 19:10:14135
136 DISALLOW_COPY_AND_ASSIGN(TestRequestPeer);
initial.commit09911bf2008-07-26 23:55:29137};
138
[email protected]0a0e2c7b2014-06-16 19:10:14139// Sets up the message sender override for the unit test.
[email protected]d84effeb2012-06-25 17:03:10140class ResourceDispatcherTest : public testing::Test, public IPC::Sender {
initial.commit09911bf2008-07-26 23:55:29141 public:
kinukoccbf2c9572016-02-03 22:54:37142 ResourceDispatcherTest()
143 : dispatcher_(new ResourceDispatcher(this, message_loop_.task_runner())) {
144 }
[email protected]0a0e2c7b2014-06-16 19:10:14145
dchengf5762152014-10-29 02:12:06146 ~ResourceDispatcherTest() override {
[email protected]0a0e2c7b2014-06-16 19:10:14147 STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(),
148 shared_memory_map_.end());
kinukoccbf2c9572016-02-03 22:54:37149 dispatcher_.reset();
150 base::RunLoop().RunUntilIdle();
[email protected]0a0e2c7b2014-06-16 19:10:14151 }
152
[email protected]d84effeb2012-06-25 17:03:10153 // Emulates IPC send operations (IPC::Sender) by adding
initial.commit09911bf2008-07-26 23:55:29154 // pending messages to the queue.
dchenge933b3e2014-10-21 11:44:09155 bool Send(IPC::Message* msg) override {
initial.commit09911bf2008-07-26 23:55:29156 message_queue_.push_back(IPC::Message(*msg));
157 delete msg;
158 return true;
159 }
160
[email protected]0a0e2c7b2014-06-16 19:10:14161 size_t queued_messages() const { return message_queue_.size(); }
initial.commit09911bf2008-07-26 23:55:29162
[email protected]0a0e2c7b2014-06-16 19:10:14163 // Returns the ID of the consumed request. Can't make assumptions about the
164 // ID, because numbering is based on a global.
165 int ConsumeRequestResource() {
166 if (message_queue_.empty()) {
167 ADD_FAILURE() << "Missing resource request message";
168 return -1;
initial.commit09911bf2008-07-26 23:55:29169 }
[email protected]0a0e2c7b2014-06-16 19:10:14170
171 ResourceHostMsg_RequestResource::Param params;
172 if (ResourceHostMsg_RequestResource::ID != message_queue_[0].type() ||
173 !ResourceHostMsg_RequestResource::Read(&message_queue_[0], &params)) {
174 ADD_FAILURE() << "Expected ResourceHostMsg_RequestResource message";
175 return -1;
176 }
yhirano20c94ea9b2016-05-18 05:20:45177 ResourceRequest request = base::get<2>(params);
[email protected]0a0e2c7b2014-06-16 19:10:14178 EXPECT_EQ(kTestPageUrl, request.url.spec());
179 message_queue_.erase(message_queue_.begin());
brettwd5ca2bc2015-05-29 22:15:47180 return base::get<1>(params);
initial.commit09911bf2008-07-26 23:55:29181 }
182
[email protected]0a0e2c7b2014-06-16 19:10:14183 void ConsumeFollowRedirect(int expected_request_id) {
184 ASSERT_FALSE(message_queue_.empty());
brettwd5ca2bc2015-05-29 22:15:47185 base::Tuple<int> args;
[email protected]0a0e2c7b2014-06-16 19:10:14186 ASSERT_EQ(ResourceHostMsg_FollowRedirect::ID, message_queue_[0].type());
187 ASSERT_TRUE(ResourceHostMsg_FollowRedirect::Read(
188 &message_queue_[0], &args));
brettwd5ca2bc2015-05-29 22:15:47189 EXPECT_EQ(expected_request_id, base::get<0>(args));
[email protected]0a0e2c7b2014-06-16 19:10:14190 message_queue_.erase(message_queue_.begin());
initial.commit09911bf2008-07-26 23:55:29191 }
[email protected]0a0e2c7b2014-06-16 19:10:14192
193 void ConsumeDataReceived_ACK(int expected_request_id) {
194 ASSERT_FALSE(message_queue_.empty());
brettwd5ca2bc2015-05-29 22:15:47195 base::Tuple<int> args;
[email protected]0a0e2c7b2014-06-16 19:10:14196 ASSERT_EQ(ResourceHostMsg_DataReceived_ACK::ID, message_queue_[0].type());
197 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
198 &message_queue_[0], &args));
brettwd5ca2bc2015-05-29 22:15:47199 EXPECT_EQ(expected_request_id, base::get<0>(args));
[email protected]0a0e2c7b2014-06-16 19:10:14200 message_queue_.erase(message_queue_.begin());
201 }
202
203 void ConsumeDataDownloaded_ACK(int expected_request_id) {
204 ASSERT_FALSE(message_queue_.empty());
brettwd5ca2bc2015-05-29 22:15:47205 base::Tuple<int> args;
[email protected]0a0e2c7b2014-06-16 19:10:14206 ASSERT_EQ(ResourceHostMsg_DataDownloaded_ACK::ID, message_queue_[0].type());
207 ASSERT_TRUE(ResourceHostMsg_DataDownloaded_ACK::Read(
208 &message_queue_[0], &args));
brettwd5ca2bc2015-05-29 22:15:47209 EXPECT_EQ(expected_request_id, base::get<0>(args));
[email protected]0a0e2c7b2014-06-16 19:10:14210 message_queue_.erase(message_queue_.begin());
211 }
212
213 void ConsumeReleaseDownloadedFile(int expected_request_id) {
214 ASSERT_FALSE(message_queue_.empty());
brettwd5ca2bc2015-05-29 22:15:47215 base::Tuple<int> args;
[email protected]0a0e2c7b2014-06-16 19:10:14216 ASSERT_EQ(ResourceHostMsg_ReleaseDownloadedFile::ID,
217 message_queue_[0].type());
218 ASSERT_TRUE(ResourceHostMsg_ReleaseDownloadedFile::Read(
219 &message_queue_[0], &args));
brettwd5ca2bc2015-05-29 22:15:47220 EXPECT_EQ(expected_request_id, base::get<0>(args));
[email protected]0a0e2c7b2014-06-16 19:10:14221 message_queue_.erase(message_queue_.begin());
222 }
223
224 void ConsumeCancelRequest(int expected_request_id) {
225 ASSERT_FALSE(message_queue_.empty());
brettwd5ca2bc2015-05-29 22:15:47226 base::Tuple<int> args;
[email protected]0a0e2c7b2014-06-16 19:10:14227 ASSERT_EQ(ResourceHostMsg_CancelRequest::ID, message_queue_[0].type());
228 ASSERT_TRUE(ResourceHostMsg_CancelRequest::Read(
229 &message_queue_[0], &args));
brettwd5ca2bc2015-05-29 22:15:47230 EXPECT_EQ(expected_request_id, base::get<0>(args));
[email protected]0a0e2c7b2014-06-16 19:10:14231 message_queue_.erase(message_queue_.begin());
232 }
233
234 void NotifyReceivedRedirect(int request_id) {
235 ResourceResponseHead head;
236 std::string raw_headers(kTestRedirectHeaders);
237 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
238 head.headers = new net::HttpResponseHeaders(raw_headers);
[email protected]cba24642014-08-15 20:49:59239 net::RedirectInfo redirect_info;
240 redirect_info.status_code = 302;
241 redirect_info.new_method = "GET";
242 redirect_info.new_url = GURL(kTestPageUrl);
243 redirect_info.new_first_party_for_cookies = GURL(kTestPageUrl);
kinukoccbf2c9572016-02-03 22:54:37244 EXPECT_EQ(true, dispatcher_->OnMessageReceived(ResourceMsg_ReceivedRedirect(
245 request_id, redirect_info, head)));
[email protected]0a0e2c7b2014-06-16 19:10:14246 }
247
248 void NotifyReceivedResponse(int request_id) {
249 ResourceResponseHead head;
250 std::string raw_headers(kTestPageHeaders);
251 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
252 head.headers = new net::HttpResponseHeaders(raw_headers);
253 head.mime_type = kTestPageMimeType;
254 head.charset = kTestPageCharset;
kinukoccbf2c9572016-02-03 22:54:37255 EXPECT_EQ(true, dispatcher_->OnMessageReceived(
256 ResourceMsg_ReceivedResponse(request_id, head)));
[email protected]0a0e2c7b2014-06-16 19:10:14257 }
258
259 void NotifySetDataBuffer(int request_id, size_t buffer_size) {
260 base::SharedMemory* shared_memory = new base::SharedMemory();
261 ASSERT_FALSE(shared_memory_map_[request_id]);
262 shared_memory_map_[request_id] = shared_memory;
263 EXPECT_TRUE(shared_memory->CreateAndMapAnonymous(buffer_size));
264
265 base::SharedMemoryHandle duplicate_handle;
rvargas079d1842014-10-17 22:32:16266 EXPECT_TRUE(shared_memory->ShareToProcess(base::GetCurrentProcessHandle(),
267 &duplicate_handle));
kinukoccbf2c9572016-02-03 22:54:37268 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_SetDataBuffer(
269 request_id, duplicate_handle, shared_memory->requested_size(), 0)));
[email protected]0a0e2c7b2014-06-16 19:10:14270 }
271
ki.stfu7cf10ca2015-09-27 08:37:01272 void NotifyDataReceived(int request_id, const std::string& data) {
[email protected]0a0e2c7b2014-06-16 19:10:14273 ASSERT_LE(data.length(), shared_memory_map_[request_id]->requested_size());
274 memcpy(shared_memory_map_[request_id]->memory(), data.c_str(),
275 data.length());
276
kinukoccbf2c9572016-02-03 22:54:37277 EXPECT_TRUE(dispatcher_->OnMessageReceived(
erikchen9064a252015-09-20 22:30:02278 ResourceMsg_DataReceived(request_id, 0, data.length(), data.length())));
[email protected]0a0e2c7b2014-06-16 19:10:14279 }
280
281 void NotifyDataDownloaded(int request_id, int decoded_length,
282 int encoded_length) {
kinukoccbf2c9572016-02-03 22:54:37283 EXPECT_TRUE(dispatcher_->OnMessageReceived(ResourceMsg_DataDownloaded(
284 request_id, decoded_length, encoded_length)));
[email protected]0a0e2c7b2014-06-16 19:10:14285 }
286
287 void NotifyRequestComplete(int request_id, size_t total_size) {
yhirano20c94ea9b2016-05-18 05:20:45288 ResourceRequestCompletionStatus request_complete_data;
[email protected]0a0e2c7b2014-06-16 19:10:14289 request_complete_data.error_code = net::OK;
290 request_complete_data.was_ignored_by_handler = false;
291 request_complete_data.exists_in_cache = false;
292 request_complete_data.encoded_data_length = total_size;
kinukoccbf2c9572016-02-03 22:54:37293 EXPECT_TRUE(dispatcher_->OnMessageReceived(
[email protected]0a0e2c7b2014-06-16 19:10:14294 ResourceMsg_RequestComplete(request_id, request_complete_data)));
initial.commit09911bf2008-07-26 23:55:29295 }
296
jam4a1511ef2015-02-19 20:29:17297 RequestInfo* CreateRequestInfo(bool download_to_file) {
298 RequestInfo* request_info = new RequestInfo();
299 request_info->method = "GET";
300 request_info->url = GURL(kTestPageUrl);
301 request_info->first_party_for_cookies = GURL(kTestPageUrl);
302 request_info->referrer = Referrer();
303 request_info->headers = std::string();
304 request_info->load_flags = 0;
305 request_info->requestor_pid = 0;
306 request_info->request_type = RESOURCE_TYPE_SUB_RESOURCE;
307 request_info->appcache_host_id = kAppCacheNoHostId;
308 request_info->should_reset_appcache = false;
309 request_info->routing_id = 0;
310 request_info->download_to_file = download_to_file;
311 RequestExtraData extra_data;
[email protected]0a0e2c7b2014-06-16 19:10:14312
jam4a1511ef2015-02-19 20:29:17313 return request_info;
[email protected]0a0e2c7b2014-06-16 19:10:14314 }
315
kinukoccbf2c9572016-02-03 22:54:37316 ResourceDispatcher* dispatcher() { return dispatcher_.get(); }
317
318 int StartAsync(const RequestInfo& request_info,
319 ResourceRequestBody* request_body,
320 TestRequestPeer::Context* peer_context) {
dchengc864f522016-04-08 23:55:27321 std::unique_ptr<TestRequestPeer> peer(
kinukoccbf2c9572016-02-03 22:54:37322 new TestRequestPeer(dispatcher(), peer_context));
323 int request_id =
324 dispatcher()->StartAsync(request_info, request_body, std::move(peer));
325 peer_context->request_id = request_id;
326 return request_id;
327 }
[email protected]0a0e2c7b2014-06-16 19:10:14328
329 private:
[email protected]0a0e2c7b2014-06-16 19:10:14330 // Map of request IDs to shared memory.
331 std::map<int, base::SharedMemory*> shared_memory_map_;
332
initial.commit09911bf2008-07-26 23:55:29333 std::vector<IPC::Message> message_queue_;
[email protected]0a0e2c7b2014-06-16 19:10:14334 base::MessageLoop message_loop_;
dchengc864f522016-04-08 23:55:27335 std::unique_ptr<ResourceDispatcher> dispatcher_;
initial.commit09911bf2008-07-26 23:55:29336};
337
[email protected]0a0e2c7b2014-06-16 19:10:14338// Does a simple request and tests that the correct data is received. Simulates
339// two reads.
initial.commit09911bf2008-07-26 23:55:29340TEST_F(ResourceDispatcherTest, RoundTrip) {
[email protected]0a0e2c7b2014-06-16 19:10:14341 // Number of bytes received in the first read.
342 const size_t kFirstReceiveSize = 2;
343 ASSERT_LT(kFirstReceiveSize, strlen(kTestPageContents));
344
dchengc864f522016-04-08 23:55:27345 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37346 TestRequestPeer::Context peer_context;
347 StartAsync(*request_info.get(), NULL, &peer_context);
initial.commit09911bf2008-07-26 23:55:29348
[email protected]0a0e2c7b2014-06-16 19:10:14349 int id = ConsumeRequestResource();
350 EXPECT_EQ(0u, queued_messages());
initial.commit09911bf2008-07-26 23:55:29351
[email protected]0a0e2c7b2014-06-16 19:10:14352 NotifyReceivedResponse(id);
353 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37354 EXPECT_TRUE(peer_context.received_response);
initial.commit09911bf2008-07-26 23:55:29355
[email protected]0a0e2c7b2014-06-16 19:10:14356 NotifySetDataBuffer(id, strlen(kTestPageContents));
357 NotifyDataReceived(id, std::string(kTestPageContents, kFirstReceiveSize));
358 ConsumeDataReceived_ACK(id);
359 EXPECT_EQ(0u, queued_messages());
360
361 NotifyDataReceived(id, kTestPageContents + kFirstReceiveSize);
362 ConsumeDataReceived_ACK(id);
363 EXPECT_EQ(0u, queued_messages());
364
365 NotifyRequestComplete(id, strlen(kTestPageContents));
kinukoccbf2c9572016-02-03 22:54:37366 EXPECT_EQ(kTestPageContents, peer_context.data);
367 EXPECT_TRUE(peer_context.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14368 EXPECT_EQ(0u, queued_messages());
initial.commit09911bf2008-07-26 23:55:29369}
370
[email protected]0a0e2c7b2014-06-16 19:10:14371// Tests that the request IDs are straight when there are two interleaving
372// requests.
initial.commit09911bf2008-07-26 23:55:29373TEST_F(ResourceDispatcherTest, MultipleRequests) {
[email protected]0a0e2c7b2014-06-16 19:10:14374 const char kTestPageContents2[] = "Not kTestPageContents";
375
dchengc864f522016-04-08 23:55:27376 std::unique_ptr<RequestInfo> request_info1(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37377 TestRequestPeer::Context peer_context1;
378 StartAsync(*request_info1.get(), NULL, &peer_context1);
379
dchengc864f522016-04-08 23:55:27380 std::unique_ptr<RequestInfo> request_info2(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37381 TestRequestPeer::Context peer_context2;
382 StartAsync(*request_info2.get(), NULL, &peer_context2);
[email protected]0a0e2c7b2014-06-16 19:10:14383
[email protected]0a0e2c7b2014-06-16 19:10:14384 int id1 = ConsumeRequestResource();
[email protected]0a0e2c7b2014-06-16 19:10:14385 int id2 = ConsumeRequestResource();
386 EXPECT_EQ(0u, queued_messages());
387
388 NotifyReceivedResponse(id1);
kinukoccbf2c9572016-02-03 22:54:37389 EXPECT_TRUE(peer_context1.received_response);
390 EXPECT_FALSE(peer_context2.received_response);
[email protected]0a0e2c7b2014-06-16 19:10:14391 NotifyReceivedResponse(id2);
kinukoccbf2c9572016-02-03 22:54:37392 EXPECT_TRUE(peer_context2.received_response);
[email protected]0a0e2c7b2014-06-16 19:10:14393 EXPECT_EQ(0u, queued_messages());
394
395 NotifySetDataBuffer(id2, strlen(kTestPageContents2));
396 NotifyDataReceived(id2, kTestPageContents2);
397 ConsumeDataReceived_ACK(id2);
398 NotifySetDataBuffer(id1, strlen(kTestPageContents));
399 NotifyDataReceived(id1, kTestPageContents);
400 ConsumeDataReceived_ACK(id1);
401 EXPECT_EQ(0u, queued_messages());
402
403 NotifyRequestComplete(id1, strlen(kTestPageContents));
kinukoccbf2c9572016-02-03 22:54:37404 EXPECT_EQ(kTestPageContents, peer_context1.data);
405 EXPECT_TRUE(peer_context1.complete);
406 EXPECT_FALSE(peer_context2.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14407
408 NotifyRequestComplete(id2, strlen(kTestPageContents2));
kinukoccbf2c9572016-02-03 22:54:37409 EXPECT_EQ(kTestPageContents2, peer_context2.data);
410 EXPECT_TRUE(peer_context2.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14411
412 EXPECT_EQ(0u, queued_messages());
initial.commit09911bf2008-07-26 23:55:29413}
414
[email protected]0a0e2c7b2014-06-16 19:10:14415// Tests that the cancel method prevents other messages from being received.
initial.commit09911bf2008-07-26 23:55:29416TEST_F(ResourceDispatcherTest, Cancel) {
dchengc864f522016-04-08 23:55:27417 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37418 TestRequestPeer::Context peer_context;
419 int request_id = StartAsync(*request_info.get(), NULL, &peer_context);
[email protected]0a0e2c7b2014-06-16 19:10:14420
[email protected]0a0e2c7b2014-06-16 19:10:14421 int id = ConsumeRequestResource();
422 EXPECT_EQ(0u, queued_messages());
423
424 // Cancel the request.
jam4a1511ef2015-02-19 20:29:17425 dispatcher()->Cancel(request_id);
[email protected]0a0e2c7b2014-06-16 19:10:14426 ConsumeCancelRequest(id);
427
428 // Any future messages related to the request should be ignored.
429 NotifyReceivedResponse(id);
430 NotifySetDataBuffer(id, strlen(kTestPageContents));
431 NotifyDataReceived(id, kTestPageContents);
432 NotifyRequestComplete(id, strlen(kTestPageContents));
433
434 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37435 EXPECT_EQ("", peer_context.data);
436 EXPECT_FALSE(peer_context.received_response);
437 EXPECT_FALSE(peer_context.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14438}
439
440// Tests that calling cancel during a callback works as expected.
441TEST_F(ResourceDispatcherTest, CancelDuringCallback) {
dchengc864f522016-04-08 23:55:27442 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37443 TestRequestPeer::Context peer_context;
444 StartAsync(*request_info.get(), NULL, &peer_context);
445 peer_context.cancel_on_receive_response = true;
[email protected]0a0e2c7b2014-06-16 19:10:14446
[email protected]0a0e2c7b2014-06-16 19:10:14447 int id = ConsumeRequestResource();
448 EXPECT_EQ(0u, queued_messages());
449
450 NotifyReceivedResponse(id);
kinukoccbf2c9572016-02-03 22:54:37451 EXPECT_TRUE(peer_context.received_response);
[email protected]0a0e2c7b2014-06-16 19:10:14452 // Request should have been cancelled.
453 ConsumeCancelRequest(id);
454
455 // Any future messages related to the request should be ignored.
456 NotifySetDataBuffer(id, strlen(kTestPageContents));
457 NotifyDataReceived(id, kTestPageContents);
458 NotifyRequestComplete(id, strlen(kTestPageContents));
459
460 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37461 EXPECT_EQ("", peer_context.data);
462 EXPECT_FALSE(peer_context.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14463}
464
kinuko5e4122a22016-02-09 02:38:24465class TestResourceDispatcherDelegate : public ResourceDispatcherDelegate {
466 public:
467 TestResourceDispatcherDelegate() {}
468 ~TestResourceDispatcherDelegate() override {}
469
dchengc864f522016-04-08 23:55:27470 std::unique_ptr<RequestPeer> OnRequestComplete(
471 std::unique_ptr<RequestPeer> current_peer,
kinuko5e4122a22016-02-09 02:38:24472 ResourceType resource_type,
473 int error_code) override {
474 return current_peer;
475 }
476
dchengc864f522016-04-08 23:55:27477 std::unique_ptr<RequestPeer> OnReceivedResponse(
478 std::unique_ptr<RequestPeer> current_peer,
kinuko5e4122a22016-02-09 02:38:24479 const std::string& mime_type,
480 const GURL& url) override {
dchengc864f522016-04-08 23:55:27481 return base::WrapUnique(new WrapperPeer(std::move(current_peer)));
kinuko5e4122a22016-02-09 02:38:24482 }
483
484 class WrapperPeer : public RequestPeer {
485 public:
dchengc864f522016-04-08 23:55:27486 explicit WrapperPeer(std::unique_ptr<RequestPeer> original_peer)
kinuko5e4122a22016-02-09 02:38:24487 : original_peer_(std::move(original_peer)) {}
488
489 void OnUploadProgress(uint64_t position, uint64_t size) override {}
490
491 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info,
492 const ResourceResponseInfo& info) override {
493 return false;
494 }
495
496 void OnReceivedResponse(const ResourceResponseInfo& info) override {
497 response_info_ = info;
498 }
499
500 void OnDownloadedData(int len, int encoded_data_length) override {}
501
dchengc864f522016-04-08 23:55:27502 void OnReceivedData(std::unique_ptr<ReceivedData> data) override {
kinuko5e4122a22016-02-09 02:38:24503 data_.append(data->payload(), data->length());
504 }
505
506 void OnCompletedRequest(int error_code,
507 bool was_ignored_by_handler,
508 bool stale_copy_in_cache,
509 const std::string& security_info,
510 const base::TimeTicks& completion_time,
511 int64_t total_transfer_size) override {
512 original_peer_->OnReceivedResponse(response_info_);
513 if (!data_.empty()) {
dchengc864f522016-04-08 23:55:27514 original_peer_->OnReceivedData(base::WrapUnique(
kinuko5e4122a22016-02-09 02:38:24515 new FixedReceivedData(data_.data(), data_.size(), -1)));
516 }
517 original_peer_->OnCompletedRequest(error_code, was_ignored_by_handler,
518 stale_copy_in_cache, security_info,
519 completion_time, total_transfer_size);
520 }
521
522 private:
dchengc864f522016-04-08 23:55:27523 std::unique_ptr<RequestPeer> original_peer_;
kinuko5e4122a22016-02-09 02:38:24524 ResourceResponseInfo response_info_;
525 std::string data_;
526
527 DISALLOW_COPY_AND_ASSIGN(WrapperPeer);
528 };
529
530 private:
531 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherDelegate);
532};
533
534TEST_F(ResourceDispatcherTest, DelegateTest) {
dchengc864f522016-04-08 23:55:27535 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinuko5e4122a22016-02-09 02:38:24536 TestRequestPeer::Context peer_context;
537 StartAsync(*request_info.get(), nullptr, &peer_context);
538
539 // Set the delegate that inserts a new peer in OnReceivedResponse.
540 TestResourceDispatcherDelegate delegate;
541 dispatcher()->set_delegate(&delegate);
542
543 // Run a simple round-trip.
544 const size_t kFirstReceiveSize = 2;
545 ASSERT_LT(kFirstReceiveSize, strlen(kTestPageContents));
546
547 int id = ConsumeRequestResource();
548 EXPECT_EQ(0u, queued_messages());
549
550 // The wrapper eats all messages until RequestComplete message is sent.
551 NotifyReceivedResponse(id);
552 NotifySetDataBuffer(id, strlen(kTestPageContents));
553 NotifyDataReceived(id, std::string(kTestPageContents, kFirstReceiveSize));
554 ConsumeDataReceived_ACK(id);
555 NotifyDataReceived(id, kTestPageContents + kFirstReceiveSize);
556 ConsumeDataReceived_ACK(id);
557
558 EXPECT_FALSE(peer_context.received_response);
559 EXPECT_EQ(0u, queued_messages());
560
561 // This lets the wrapper peer pass all the messages to the original
562 // peer at once.
563 NotifyRequestComplete(id, strlen(kTestPageContents));
564
565 EXPECT_TRUE(peer_context.received_response);
566 EXPECT_EQ(kTestPageContents, peer_context.data);
567 EXPECT_TRUE(peer_context.complete);
568 EXPECT_EQ(0u, queued_messages());
569}
570
571TEST_F(ResourceDispatcherTest, CancelDuringCallbackWithWrapperPeer) {
dchengc864f522016-04-08 23:55:27572 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinuko5e4122a22016-02-09 02:38:24573 TestRequestPeer::Context peer_context;
574 StartAsync(*request_info.get(), nullptr, &peer_context);
575 peer_context.cancel_on_receive_response = true;
576
577 // Set the delegate that inserts a new peer in OnReceivedResponse.
578 TestResourceDispatcherDelegate delegate;
579 dispatcher()->set_delegate(&delegate);
580
581 int id = ConsumeRequestResource();
582 EXPECT_EQ(0u, queued_messages());
583
584 // The wrapper eats all messages until RequestComplete message is sent.
585 NotifyReceivedResponse(id);
586 NotifySetDataBuffer(id, strlen(kTestPageContents));
587 NotifyDataReceived(id, kTestPageContents);
588 ConsumeDataReceived_ACK(id);
589
590 EXPECT_FALSE(peer_context.received_response);
591 EXPECT_EQ(0u, queued_messages());
592
593 // This lets the wrapper peer pass all the messages to the original
594 // peer at once, but the original peer cancels right after it receives
595 // the response. (This will remove pending request info from
596 // ResourceDispatcher while the wrapper peer is still running
597 // OnCompletedRequest, but it should not lead to crashes.)
598 NotifyRequestComplete(id, strlen(kTestPageContents));
599
600 EXPECT_TRUE(peer_context.received_response);
601 // Request should have been cancelled.
602 ConsumeCancelRequest(id);
603 EXPECT_TRUE(peer_context.cancelled);
604
605 // Any future messages related to the request should be ignored.
606 NotifyDataReceived(id, kTestPageContents);
607 NotifyRequestComplete(id, strlen(kTestPageContents));
608
609 EXPECT_EQ(0u, queued_messages());
610 EXPECT_EQ("", peer_context.data);
611 EXPECT_FALSE(peer_context.complete);
612}
613
[email protected]0a0e2c7b2014-06-16 19:10:14614// Checks that redirects work as expected.
615TEST_F(ResourceDispatcherTest, Redirect) {
dchengc864f522016-04-08 23:55:27616 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37617 TestRequestPeer::Context peer_context;
618 StartAsync(*request_info.get(), NULL, &peer_context);
[email protected]0a0e2c7b2014-06-16 19:10:14619
[email protected]0a0e2c7b2014-06-16 19:10:14620 int id = ConsumeRequestResource();
621
622 NotifyReceivedRedirect(id);
623 ConsumeFollowRedirect(id);
kinukoccbf2c9572016-02-03 22:54:37624 EXPECT_EQ(1, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14625
626 NotifyReceivedRedirect(id);
627 ConsumeFollowRedirect(id);
kinukoccbf2c9572016-02-03 22:54:37628 EXPECT_EQ(2, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14629
630 NotifyReceivedResponse(id);
kinukoccbf2c9572016-02-03 22:54:37631 EXPECT_TRUE(peer_context.received_response);
[email protected]0a0e2c7b2014-06-16 19:10:14632
633 NotifySetDataBuffer(id, strlen(kTestPageContents));
634 NotifyDataReceived(id, kTestPageContents);
635 ConsumeDataReceived_ACK(id);
636
637 NotifyRequestComplete(id, strlen(kTestPageContents));
kinukoccbf2c9572016-02-03 22:54:37638 EXPECT_EQ(kTestPageContents, peer_context.data);
639 EXPECT_TRUE(peer_context.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14640 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37641 EXPECT_EQ(2, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14642}
643
644// Tests that that cancelling during a redirect method prevents other messages
645// from being received.
646TEST_F(ResourceDispatcherTest, CancelDuringRedirect) {
dchengc864f522016-04-08 23:55:27647 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37648 TestRequestPeer::Context peer_context;
649 StartAsync(*request_info.get(), NULL, &peer_context);
650 peer_context.follow_redirects = false;
[email protected]0a0e2c7b2014-06-16 19:10:14651
[email protected]0a0e2c7b2014-06-16 19:10:14652 int id = ConsumeRequestResource();
653 EXPECT_EQ(0u, queued_messages());
654
655 // Redirect the request, which triggers a cancellation.
656 NotifyReceivedRedirect(id);
657 ConsumeCancelRequest(id);
kinukoccbf2c9572016-02-03 22:54:37658 EXPECT_EQ(1, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14659 EXPECT_EQ(0u, queued_messages());
660
661 // Any future messages related to the request should be ignored. In practice,
662 // only the NotifyRequestComplete should be received after this point.
663 NotifyReceivedRedirect(id);
664 NotifyReceivedResponse(id);
665 NotifySetDataBuffer(id, strlen(kTestPageContents));
666 NotifyDataReceived(id, kTestPageContents);
667 NotifyRequestComplete(id, strlen(kTestPageContents));
668
669 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37670 EXPECT_EQ("", peer_context.data);
671 EXPECT_FALSE(peer_context.complete);
672 EXPECT_EQ(1, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14673}
674
675// Checks that deferring a request delays messages until it's resumed.
676TEST_F(ResourceDispatcherTest, Defer) {
dchengc864f522016-04-08 23:55:27677 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37678 TestRequestPeer::Context peer_context;
679 int request_id = StartAsync(*request_info.get(), NULL, &peer_context);
[email protected]0a0e2c7b2014-06-16 19:10:14680
[email protected]0a0e2c7b2014-06-16 19:10:14681 int id = ConsumeRequestResource();
682 EXPECT_EQ(0u, queued_messages());
683
jam4a1511ef2015-02-19 20:29:17684 dispatcher()->SetDefersLoading(request_id, true);
[email protected]0a0e2c7b2014-06-16 19:10:14685 NotifyReceivedResponse(id);
686 NotifySetDataBuffer(id, strlen(kTestPageContents));
687 NotifyDataReceived(id, kTestPageContents);
688 NotifyRequestComplete(id, strlen(kTestPageContents));
689
690 // None of the messages should have been processed yet, so no queued messages
691 // to the browser process, and no data received by the peer.
692 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37693 EXPECT_EQ("", peer_context.data);
694 EXPECT_FALSE(peer_context.complete);
695 EXPECT_EQ(0, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14696
697 // Resuming the request should asynchronously unleash the deferred messages.
jam4a1511ef2015-02-19 20:29:17698 dispatcher()->SetDefersLoading(request_id, false);
[email protected]0a0e2c7b2014-06-16 19:10:14699 base::RunLoop().RunUntilIdle();
700
701 ConsumeDataReceived_ACK(id);
702 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37703 EXPECT_TRUE(peer_context.received_response);
704 EXPECT_EQ(kTestPageContents, peer_context.data);
705 EXPECT_TRUE(peer_context.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14706}
707
708// Checks that deferring a request during a redirect delays messages until it's
709// resumed.
710TEST_F(ResourceDispatcherTest, DeferOnRedirect) {
dchengc864f522016-04-08 23:55:27711 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37712 TestRequestPeer::Context peer_context;
713 int request_id = StartAsync(*request_info.get(), NULL, &peer_context);
714 peer_context.defer_on_redirect = true;
[email protected]0a0e2c7b2014-06-16 19:10:14715
[email protected]0a0e2c7b2014-06-16 19:10:14716 int id = ConsumeRequestResource();
717 EXPECT_EQ(0u, queued_messages());
718
719 // The request should be deferred during the redirect, including the message
720 // to follow the redirect.
721 NotifyReceivedRedirect(id);
722 NotifyReceivedResponse(id);
723 NotifySetDataBuffer(id, strlen(kTestPageContents));
724 NotifyDataReceived(id, kTestPageContents);
725 NotifyRequestComplete(id, strlen(kTestPageContents));
726
727 // None of the messages should have been processed yet, so no queued messages
728 // to the browser process, and no data received by the peer.
729 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37730 EXPECT_EQ("", peer_context.data);
731 EXPECT_FALSE(peer_context.complete);
732 EXPECT_EQ(1, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14733
734 // Resuming the request should asynchronously unleash the deferred messages.
jam4a1511ef2015-02-19 20:29:17735 dispatcher()->SetDefersLoading(request_id, false);
[email protected]0a0e2c7b2014-06-16 19:10:14736 base::RunLoop().RunUntilIdle();
737
738 ConsumeFollowRedirect(id);
739 ConsumeDataReceived_ACK(id);
740
741 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37742 EXPECT_TRUE(peer_context.received_response);
743 EXPECT_EQ(kTestPageContents, peer_context.data);
744 EXPECT_TRUE(peer_context.complete);
745 EXPECT_EQ(1, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14746}
747
748// Checks that a deferred request that's cancelled doesn't receive any messages.
749TEST_F(ResourceDispatcherTest, CancelDeferredRequest) {
dchengc864f522016-04-08 23:55:27750 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37751 TestRequestPeer::Context peer_context;
752 int request_id = StartAsync(*request_info.get(), NULL, &peer_context);
[email protected]0a0e2c7b2014-06-16 19:10:14753
[email protected]0a0e2c7b2014-06-16 19:10:14754 int id = ConsumeRequestResource();
755 EXPECT_EQ(0u, queued_messages());
756
jam4a1511ef2015-02-19 20:29:17757 dispatcher()->SetDefersLoading(request_id, true);
[email protected]0a0e2c7b2014-06-16 19:10:14758 NotifyReceivedRedirect(id);
jam4a1511ef2015-02-19 20:29:17759 dispatcher()->Cancel(request_id);
[email protected]0a0e2c7b2014-06-16 19:10:14760 ConsumeCancelRequest(id);
761
762 NotifyRequestComplete(id, 0);
763 base::RunLoop().RunUntilIdle();
764
765 // None of the messages should have been processed.
766 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37767 EXPECT_EQ("", peer_context.data);
768 EXPECT_FALSE(peer_context.complete);
769 EXPECT_EQ(0, peer_context.seen_redirects);
[email protected]0a0e2c7b2014-06-16 19:10:14770}
771
772TEST_F(ResourceDispatcherTest, DownloadToFile) {
dchengc864f522016-04-08 23:55:27773 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(true));
kinukoccbf2c9572016-02-03 22:54:37774 TestRequestPeer::Context peer_context;
775 int request_id = StartAsync(*request_info.get(), NULL, &peer_context);
[email protected]0a0e2c7b2014-06-16 19:10:14776 const int kDownloadedIncrement = 100;
777 const int kEncodedIncrement = 50;
778
[email protected]0a0e2c7b2014-06-16 19:10:14779 int id = ConsumeRequestResource();
780 EXPECT_EQ(0u, queued_messages());
781
782 NotifyReceivedResponse(id);
783 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37784 EXPECT_TRUE(peer_context.received_response);
[email protected]0a0e2c7b2014-06-16 19:10:14785
786 int expected_total_downloaded_length = 0;
787 int expected_total_encoded_length = 0;
788 for (int i = 0; i < 10; ++i) {
789 NotifyDataDownloaded(id, kDownloadedIncrement, kEncodedIncrement);
790 ConsumeDataDownloaded_ACK(id);
791 expected_total_downloaded_length += kDownloadedIncrement;
792 expected_total_encoded_length += kEncodedIncrement;
793 EXPECT_EQ(expected_total_downloaded_length,
kinukoccbf2c9572016-02-03 22:54:37794 peer_context.total_downloaded_data_length);
795 EXPECT_EQ(expected_total_encoded_length,
796 peer_context.total_encoded_data_length);
[email protected]0a0e2c7b2014-06-16 19:10:14797 }
798
799 NotifyRequestComplete(id, strlen(kTestPageContents));
kinukoccbf2c9572016-02-03 22:54:37800 EXPECT_EQ("", peer_context.data);
801 EXPECT_TRUE(peer_context.complete);
[email protected]0a0e2c7b2014-06-16 19:10:14802 EXPECT_EQ(0u, queued_messages());
803
jam4a1511ef2015-02-19 20:29:17804 dispatcher()->RemovePendingRequest(request_id);
[email protected]0a0e2c7b2014-06-16 19:10:14805 ConsumeReleaseDownloadedFile(id);
806 EXPECT_EQ(0u, queued_messages());
807 EXPECT_EQ(expected_total_downloaded_length,
kinukoccbf2c9572016-02-03 22:54:37808 peer_context.total_downloaded_data_length);
809 EXPECT_EQ(expected_total_encoded_length,
810 peer_context.total_encoded_data_length);
[email protected]0a0e2c7b2014-06-16 19:10:14811}
812
813// Make sure that when a download to file is cancelled, the file is destroyed.
814TEST_F(ResourceDispatcherTest, CancelDownloadToFile) {
dchengc864f522016-04-08 23:55:27815 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(true));
kinukoccbf2c9572016-02-03 22:54:37816 TestRequestPeer::Context peer_context;
817 int request_id = StartAsync(*request_info.get(), NULL, &peer_context);
[email protected]0a0e2c7b2014-06-16 19:10:14818
[email protected]0a0e2c7b2014-06-16 19:10:14819 int id = ConsumeRequestResource();
820 EXPECT_EQ(0u, queued_messages());
821
822 NotifyReceivedResponse(id);
823 EXPECT_EQ(0u, queued_messages());
kinukoccbf2c9572016-02-03 22:54:37824 EXPECT_TRUE(peer_context.received_response);
[email protected]0a0e2c7b2014-06-16 19:10:14825
826 // Cancelling the request deletes the file.
jam4a1511ef2015-02-19 20:29:17827 dispatcher()->Cancel(request_id);
[email protected]0a0e2c7b2014-06-16 19:10:14828 ConsumeCancelRequest(id);
829 ConsumeReleaseDownloadedFile(id);
initial.commit09911bf2008-07-26 23:55:29830}
831
832TEST_F(ResourceDispatcherTest, Cookies) {
833 // FIXME
834}
835
836TEST_F(ResourceDispatcherTest, SerializedPostData) {
837 // FIXME
838}
[email protected]2602087e2009-08-24 23:12:16839
kinuko85732232016-01-06 04:20:42840class TimeConversionTest : public ResourceDispatcherTest {
[email protected]04f6f982012-08-03 01:02:15841 public:
dchenge933b3e2014-10-21 11:44:09842 bool Send(IPC::Message* msg) override {
[email protected]04f6f982012-08-03 01:02:15843 delete msg;
844 return true;
845 }
846
847 void PerformTest(const ResourceResponseHead& response_head) {
dchengc864f522016-04-08 23:55:27848 std::unique_ptr<RequestInfo> request_info(CreateRequestInfo(false));
kinukoccbf2c9572016-02-03 22:54:37849 TestRequestPeer::Context peer_context;
850 StartAsync(*request_info.get(), NULL, &peer_context);
[email protected]04f6f982012-08-03 01:02:15851
[email protected]0a0e2c7b2014-06-16 19:10:14852 dispatcher()->OnMessageReceived(
[email protected]f4653192013-09-06 19:24:05853 ResourceMsg_ReceivedResponse(0, response_head));
[email protected]04f6f982012-08-03 01:02:15854 }
855
[email protected]04f6f982012-08-03 01:02:15856 const ResourceResponseInfo& response_info() const { return response_info_; }
857
858 private:
859 ResourceResponseInfo response_info_;
860};
861
862// TODO(simonjam): Enable this when 10829031 lands.
863TEST_F(TimeConversionTest, DISABLED_ProperlyInitialized) {
864 ResourceResponseHead response_head;
[email protected]04f6f982012-08-03 01:02:15865 response_head.request_start = base::TimeTicks::FromInternalValue(5);
866 response_head.response_start = base::TimeTicks::FromInternalValue(15);
[email protected]ec298802013-03-27 16:45:07867 response_head.load_timing.request_start_time = base::Time::Now();
868 response_head.load_timing.request_start =
869 base::TimeTicks::FromInternalValue(10);
870 response_head.load_timing.connect_timing.connect_start =
871 base::TimeTicks::FromInternalValue(13);
[email protected]04f6f982012-08-03 01:02:15872
873 PerformTest(response_head);
874
[email protected]ec298802013-03-27 16:45:07875 EXPECT_LT(base::TimeTicks(), response_info().load_timing.request_start);
876 EXPECT_EQ(base::TimeTicks(),
877 response_info().load_timing.connect_timing.dns_start);
878 EXPECT_LE(response_head.load_timing.request_start,
879 response_info().load_timing.connect_timing.connect_start);
[email protected]04f6f982012-08-03 01:02:15880}
881
882TEST_F(TimeConversionTest, PartiallyInitialized) {
883 ResourceResponseHead response_head;
[email protected]04f6f982012-08-03 01:02:15884 response_head.request_start = base::TimeTicks::FromInternalValue(5);
885 response_head.response_start = base::TimeTicks::FromInternalValue(15);
886
887 PerformTest(response_head);
888
[email protected]ec298802013-03-27 16:45:07889 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start);
890 EXPECT_EQ(base::TimeTicks(),
891 response_info().load_timing.connect_timing.dns_start);
[email protected]04f6f982012-08-03 01:02:15892}
893
894TEST_F(TimeConversionTest, NotInitialized) {
895 ResourceResponseHead response_head;
[email protected]04f6f982012-08-03 01:02:15896
897 PerformTest(response_head);
898
[email protected]ec298802013-03-27 16:45:07899 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start);
900 EXPECT_EQ(base::TimeTicks(),
901 response_info().load_timing.connect_timing.dns_start);
[email protected]04f6f982012-08-03 01:02:15902}
903
[email protected]be7b41e82012-07-04 09:46:51904} // namespace content