Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: content/child/web_url_loader_impl_unittest.cc

Issue 2105713002: Render process changes for ResourceTiming sizes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@resource_timing_sizes_browser_process
Patch Set: Switch "redirect" and "original" SecurityOrigin Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/web_url_loader_impl.h" 5 #include "content/child/web_url_loader_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/single_thread_task_runner.h" 18 #include "base/single_thread_task_runner.h"
19 #include "base/time/default_tick_clock.h" 19 #include "base/time/default_tick_clock.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "components/scheduler/child/scheduler_tqm_delegate_impl.h" 21 #include "components/scheduler/child/scheduler_tqm_delegate_impl.h"
22 #include "components/scheduler/child/web_task_runner_impl.h" 22 #include "components/scheduler/child/web_task_runner_impl.h"
23 #include "components/scheduler/child/worker_scheduler.h" 23 #include "components/scheduler/child/worker_scheduler.h"
24 #include "content/child/request_extra_data.h" 24 #include "content/child/request_extra_data.h"
25 #include "content/child/request_info.h" 25 #include "content/child/request_info.h"
26 #include "content/child/resource_dispatcher.h" 26 #include "content/child/resource_dispatcher.h"
27 #include "content/child/sync_load_response.h"
27 #include "content/public/child/fixed_received_data.h" 28 #include "content/public/child/fixed_received_data.h"
28 #include "content/public/child/request_peer.h" 29 #include "content/public/child/request_peer.h"
29 #include "content/public/common/content_switches.h" 30 #include "content/public/common/content_switches.h"
30 #include "content/public/common/resource_response_info.h" 31 #include "content/public/common/resource_response_info.h"
31 #include "net/base/host_port_pair.h" 32 #include "net/base/host_port_pair.h"
32 #include "net/base/net_errors.h" 33 #include "net/base/net_errors.h"
33 #include "net/http/http_response_headers.h" 34 #include "net/http/http_response_headers.h"
34 #include "net/http/http_util.h" 35 #include "net/http/http_util.h"
35 #include "net/url_request/redirect_info.h" 36 #include "net/url_request/redirect_info.h"
36 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 23 matching lines...) Expand all
60 TestResourceDispatcher() : 61 TestResourceDispatcher() :
61 ResourceDispatcher(nullptr, nullptr), 62 ResourceDispatcher(nullptr, nullptr),
62 canceled_(false), 63 canceled_(false),
63 defers_loading_(false) { 64 defers_loading_(false) {
64 } 65 }
65 66
66 ~TestResourceDispatcher() override {} 67 ~TestResourceDispatcher() override {}
67 68
68 // TestDispatcher implementation: 69 // TestDispatcher implementation:
69 70
71 void StartSync(const RequestInfo& request_info,
72 ResourceRequestBodyImpl* request_body,
73 SyncLoadResponse* response) override {
74 *response = sync_load_response_;
75 }
76
70 int StartAsync(const RequestInfo& request_info, 77 int StartAsync(const RequestInfo& request_info,
71 ResourceRequestBodyImpl* request_body, 78 ResourceRequestBodyImpl* request_body,
72 std::unique_ptr<RequestPeer> peer) override { 79 std::unique_ptr<RequestPeer> peer) override {
73 EXPECT_FALSE(peer_); 80 EXPECT_FALSE(peer_);
74 peer_ = std::move(peer); 81 peer_ = std::move(peer);
75 url_ = request_info.url; 82 url_ = request_info.url;
76 stream_url_ = request_info.resource_body_stream_url; 83 stream_url_ = request_info.resource_body_stream_url;
77 return 1; 84 return 1;
78 } 85 }
79 86
80 void Cancel(int request_id) override { 87 void Cancel(int request_id) override {
81 EXPECT_FALSE(canceled_); 88 EXPECT_FALSE(canceled_);
82 canceled_ = true; 89 canceled_ = true;
83 } 90 }
84 91
85 RequestPeer* peer() { return peer_.get(); } 92 RequestPeer* peer() { return peer_.get(); }
86 93
87 bool canceled() { return canceled_; } 94 bool canceled() { return canceled_; }
88 95
89 const GURL& url() { return url_; } 96 const GURL& url() { return url_; }
90 const GURL& stream_url() { return stream_url_; } 97 const GURL& stream_url() { return stream_url_; }
91 98
92 void SetDefersLoading(int request_id, bool value) override { 99 void SetDefersLoading(int request_id, bool value) override {
93 defers_loading_ = value; 100 defers_loading_ = value;
94 } 101 }
95 bool defers_loading() const { return defers_loading_; } 102 bool defers_loading() const { return defers_loading_; }
96 103
104 void set_sync_load_response(const SyncLoadResponse& sync_load_response) {
105 sync_load_response_ = sync_load_response;
106 }
107
97 private: 108 private:
98 std::unique_ptr<RequestPeer> peer_; 109 std::unique_ptr<RequestPeer> peer_;
99 bool canceled_; 110 bool canceled_;
100 bool defers_loading_; 111 bool defers_loading_;
101 GURL url_; 112 GURL url_;
102 GURL stream_url_; 113 GURL stream_url_;
114 SyncLoadResponse sync_load_response_;
103 115
104 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcher); 116 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcher);
105 }; 117 };
106 118
107 class TestWebURLLoaderClient : public blink::WebURLLoaderClient { 119 class TestWebURLLoaderClient : public blink::WebURLLoaderClient {
108 public: 120 public:
109 TestWebURLLoaderClient(ResourceDispatcher* dispatcher, 121 TestWebURLLoaderClient(ResourceDispatcher* dispatcher,
110 scoped_refptr<scheduler::TaskQueue> task_runner) 122 scoped_refptr<scheduler::TaskQueue> task_runner)
111 : loader_(new WebURLLoaderImpl( 123 : loader_(new WebURLLoaderImpl(
112 dispatcher, 124 dispatcher,
113 base::WrapUnique(new scheduler::WebTaskRunnerImpl(task_runner)))), 125 base::WrapUnique(new scheduler::WebTaskRunnerImpl(task_runner)))),
114 delete_on_receive_redirect_(false), 126 delete_on_receive_redirect_(false),
115 delete_on_receive_response_(false), 127 delete_on_receive_response_(false),
116 delete_on_receive_data_(false), 128 delete_on_receive_data_(false),
117 delete_on_finish_(false), 129 delete_on_finish_(false),
118 delete_on_fail_(false), 130 delete_on_fail_(false),
119 did_receive_redirect_(false), 131 did_receive_redirect_(false),
120 did_receive_response_(false), 132 did_receive_response_(false),
121 check_redirect_request_priority_(false), 133 check_redirect_request_priority_(false),
122 did_finish_(false) {} 134 did_finish_(false) {}
123 135
124 ~TestWebURLLoaderClient() override {} 136 ~TestWebURLLoaderClient() override {}
125 137
126 // blink::WebURLLoaderClient implementation: 138 // blink::WebURLLoaderClient implementation:
127 void willFollowRedirect( 139 void willFollowRedirect(blink::WebURLLoader* loader,
128 blink::WebURLLoader* loader, 140 blink::WebURLRequest& newRequest,
129 blink::WebURLRequest& newRequest, 141 const blink::WebURLResponse& redirectResponse,
130 const blink::WebURLResponse& redirectResponse) override { 142 int64_t encodedDataLength) override {
131 EXPECT_TRUE(loader_); 143 EXPECT_TRUE(loader_);
132 EXPECT_EQ(loader_.get(), loader); 144 EXPECT_EQ(loader_.get(), loader);
133 145
134 if (check_redirect_request_priority_) 146 if (check_redirect_request_priority_)
135 EXPECT_EQ(redirect_request_priority, newRequest.getPriority()); 147 EXPECT_EQ(redirect_request_priority, newRequest.getPriority());
136 148
137 // No test currently simulates mutiple redirects. 149 // No test currently simulates mutiple redirects.
138 EXPECT_FALSE(did_receive_redirect_); 150 EXPECT_FALSE(did_receive_redirect_);
139 did_receive_redirect_ = true; 151 did_receive_redirect_ = true;
140 152
(...skipping 24 matching lines...) Expand all
165 void didDownloadData(blink::WebURLLoader* loader, 177 void didDownloadData(blink::WebURLLoader* loader,
166 int dataLength, 178 int dataLength,
167 int encodedDataLength) override { 179 int encodedDataLength) override {
168 EXPECT_TRUE(loader_); 180 EXPECT_TRUE(loader_);
169 EXPECT_EQ(loader_.get(), loader); 181 EXPECT_EQ(loader_.get(), loader);
170 } 182 }
171 183
172 void didReceiveData(blink::WebURLLoader* loader, 184 void didReceiveData(blink::WebURLLoader* loader,
173 const char* data, 185 const char* data,
174 int dataLength, 186 int dataLength,
175 int encodedDataLength) override { 187 int encodedDataLength,
188 int encodedBodyLength) override {
176 EXPECT_TRUE(loader_); 189 EXPECT_TRUE(loader_);
177 EXPECT_EQ(loader_.get(), loader); 190 EXPECT_EQ(loader_.get(), loader);
178 // The response should have started, but must not have finished, or failed. 191 // The response should have started, but must not have finished, or failed.
179 EXPECT_TRUE(did_receive_response_); 192 EXPECT_TRUE(did_receive_response_);
180 EXPECT_FALSE(did_finish_); 193 EXPECT_FALSE(did_finish_);
181 EXPECT_EQ(net::OK, error_.reason); 194 EXPECT_EQ(net::OK, error_.reason);
182 EXPECT_EQ("", error_.domain.utf8()); 195 EXPECT_EQ("", error_.domain.utf8());
183 196
184 received_data_.append(data, dataLength); 197 received_data_.append(data, dataLength);
185 198
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 319
307 void DoReceiveResponse() { 320 void DoReceiveResponse() {
308 EXPECT_FALSE(client()->did_receive_response()); 321 EXPECT_FALSE(client()->did_receive_response());
309 peer()->OnReceivedResponse(content::ResourceResponseInfo()); 322 peer()->OnReceivedResponse(content::ResourceResponseInfo());
310 EXPECT_TRUE(client()->did_receive_response()); 323 EXPECT_TRUE(client()->did_receive_response());
311 } 324 }
312 325
313 // Assumes it is called only once for a request. 326 // Assumes it is called only once for a request.
314 void DoReceiveData() { 327 void DoReceiveData() {
315 EXPECT_EQ("", client()->received_data()); 328 EXPECT_EQ("", client()->received_data());
316 peer()->OnReceivedData(base::WrapUnique(new FixedReceivedData( 329 auto size = strlen(kTestData);
317 kTestData, strlen(kTestData), strlen(kTestData)))); 330 peer()->OnReceivedData(
331 base::WrapUnique(new FixedReceivedData(kTestData, size, size, size)));
318 EXPECT_EQ(kTestData, client()->received_data()); 332 EXPECT_EQ(kTestData, client()->received_data());
319 } 333 }
320 334
321 void DoCompleteRequest() { 335 void DoCompleteRequest() {
322 EXPECT_FALSE(client()->did_finish()); 336 EXPECT_FALSE(client()->did_finish());
323 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(), 337 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(),
324 strlen(kTestData)); 338 strlen(kTestData));
325 EXPECT_TRUE(client()->did_finish()); 339 EXPECT_TRUE(client()->did_finish());
326 // There should be no error. 340 // There should be no error.
327 EXPECT_EQ(net::OK, client()->error().reason); 341 EXPECT_EQ(net::OK, client()->error().reason);
(...skipping 11 matching lines...) Expand all
339 353
340 void DoReceiveResponseFtp() { 354 void DoReceiveResponseFtp() {
341 EXPECT_FALSE(client()->did_receive_response()); 355 EXPECT_FALSE(client()->did_receive_response());
342 content::ResourceResponseInfo response_info; 356 content::ResourceResponseInfo response_info;
343 response_info.mime_type = kFtpDirMimeType; 357 response_info.mime_type = kFtpDirMimeType;
344 peer()->OnReceivedResponse(response_info); 358 peer()->OnReceivedResponse(response_info);
345 EXPECT_TRUE(client()->did_receive_response()); 359 EXPECT_TRUE(client()->did_receive_response());
346 } 360 }
347 361
348 void DoReceiveDataFtp() { 362 void DoReceiveDataFtp() {
349 peer()->OnReceivedData(base::WrapUnique(new FixedReceivedData( 363 auto size = strlen(kFtpDirListing);
350 kFtpDirListing, strlen(kFtpDirListing), strlen(kFtpDirListing)))); 364 peer()->OnReceivedData(base::WrapUnique(
365 new FixedReceivedData(kFtpDirListing, size, size, size)));
351 // The FTP delegate should modify the data the client sees. 366 // The FTP delegate should modify the data the client sees.
352 EXPECT_NE(kFtpDirListing, client()->received_data()); 367 EXPECT_NE(kFtpDirListing, client()->received_data());
353 } 368 }
354 369
355 TestWebURLLoaderClient* client() { return client_.get(); } 370 TestWebURLLoaderClient* client() { return client_.get(); }
356 TestResourceDispatcher* dispatcher() { return &dispatcher_; } 371 TestResourceDispatcher* dispatcher() { return &dispatcher_; }
357 RequestPeer* peer() { return dispatcher()->peer(); } 372 RequestPeer* peer() { return dispatcher()->peer(); }
358 base::MessageLoop* message_loop() { return &message_loop_; } 373 base::MessageLoop* message_loop() { return &message_loop_; }
359 374
360 private: 375 private:
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 SCOPED_TRACE(test.ip); 683 SCOPED_TRACE(test.ip);
669 content::ResourceResponseInfo info; 684 content::ResourceResponseInfo info;
670 info.socket_address = net::HostPortPair(test.ip, 443); 685 info.socket_address = net::HostPortPair(test.ip, 443);
671 blink::WebURLResponse response; 686 blink::WebURLResponse response;
672 response.initialize(); 687 response.initialize();
673 WebURLLoaderImpl::PopulateURLResponse(url, info, &response, true); 688 WebURLLoaderImpl::PopulateURLResponse(url, info, &response, true);
674 EXPECT_EQ(test.expected, response.remoteIPAddress().utf8()); 689 EXPECT_EQ(test.expected, response.remoteIPAddress().utf8());
675 }; 690 };
676 } 691 }
677 692
693 // Verifies that the lengths used by the PerformanceResourceTiming API are
694 // correctly assigned for sync XHR.
695 TEST_F(WebURLLoaderImplTest, SyncLengths) {
696 const GURL url(kTestURL);
697 blink::WebURLRequest request(url);
698
699 // Prepare a mock response
700 SyncLoadResponse sync_load_response;
701 sync_load_response.error_code = net::OK;
702 sync_load_response.url = url;
703 sync_load_response.data = "Today is Thursday";
704 ASSERT_EQ(17u, sync_load_response.data.size());
705 sync_load_response.encoded_body_length = 30;
706 dispatcher()->set_sync_load_response(sync_load_response);
707
708 blink::WebURLResponse response;
709 response.initialize();
710 blink::WebURLError error;
711 blink::WebData data;
712 client()->loader()->loadSynchronously(request, response, error, data);
713
714 EXPECT_EQ(30, response.encodedBodyLength());
715 EXPECT_EQ(17, response.decodedBodyLength());
kinuko 2016/07/07 05:11:40 nit: Prefer getting this number (17) explicitly fr
Adam Rice 2016/07/07 07:40:01 Done.
716 }
717
678 } // namespace 718 } // namespace
679 } // namespace content 719 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698