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

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: Initialise encoded_body_length for sync XHR to data: URLs 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
« no previous file with comments | « content/child/web_url_loader_impl.cc ('k') | content/public/child/fixed_received_data.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/run_loop.h" 18 #include "base/run_loop.h"
19 #include "base/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
20 #include "base/time/default_tick_clock.h" 20 #include "base/time/default_tick_clock.h"
21 #include "base/time/time.h" 21 #include "base/time/time.h"
22 #include "components/scheduler/child/scheduler_tqm_delegate_impl.h" 22 #include "components/scheduler/child/scheduler_tqm_delegate_impl.h"
23 #include "components/scheduler/child/web_task_runner_impl.h" 23 #include "components/scheduler/child/web_task_runner_impl.h"
24 #include "components/scheduler/child/worker_scheduler.h" 24 #include "components/scheduler/child/worker_scheduler.h"
25 #include "content/child/request_extra_data.h" 25 #include "content/child/request_extra_data.h"
26 #include "content/child/request_info.h" 26 #include "content/child/request_info.h"
27 #include "content/child/resource_dispatcher.h" 27 #include "content/child/resource_dispatcher.h"
28 #include "content/child/sync_load_response.h"
28 #include "content/public/child/fixed_received_data.h" 29 #include "content/public/child/fixed_received_data.h"
29 #include "content/public/child/request_peer.h" 30 #include "content/public/child/request_peer.h"
30 #include "content/public/common/content_switches.h" 31 #include "content/public/common/content_switches.h"
31 #include "content/public/common/resource_response_info.h" 32 #include "content/public/common/resource_response_info.h"
32 #include "net/base/host_port_pair.h" 33 #include "net/base/host_port_pair.h"
33 #include "net/base/net_errors.h" 34 #include "net/base/net_errors.h"
34 #include "net/http/http_response_headers.h" 35 #include "net/http/http_response_headers.h"
35 #include "net/http/http_util.h" 36 #include "net/http/http_util.h"
36 #include "net/url_request/redirect_info.h" 37 #include "net/url_request/redirect_info.h"
37 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 23 matching lines...) Expand all
61 TestResourceDispatcher() : 62 TestResourceDispatcher() :
62 ResourceDispatcher(nullptr, nullptr), 63 ResourceDispatcher(nullptr, nullptr),
63 canceled_(false), 64 canceled_(false),
64 defers_loading_(false) { 65 defers_loading_(false) {
65 } 66 }
66 67
67 ~TestResourceDispatcher() override {} 68 ~TestResourceDispatcher() override {}
68 69
69 // TestDispatcher implementation: 70 // TestDispatcher implementation:
70 71
72 void StartSync(const RequestInfo& request_info,
73 ResourceRequestBodyImpl* request_body,
74 SyncLoadResponse* response) override {
75 *response = sync_load_response_;
76 }
77
71 int StartAsync(const RequestInfo& request_info, 78 int StartAsync(const RequestInfo& request_info,
72 ResourceRequestBodyImpl* request_body, 79 ResourceRequestBodyImpl* request_body,
73 std::unique_ptr<RequestPeer> peer) override { 80 std::unique_ptr<RequestPeer> peer) override {
74 EXPECT_FALSE(peer_); 81 EXPECT_FALSE(peer_);
75 peer_ = std::move(peer); 82 peer_ = std::move(peer);
76 url_ = request_info.url; 83 url_ = request_info.url;
77 stream_url_ = request_info.resource_body_stream_url; 84 stream_url_ = request_info.resource_body_stream_url;
78 return 1; 85 return 1;
79 } 86 }
80 87
81 void Cancel(int request_id) override { 88 void Cancel(int request_id) override {
82 EXPECT_FALSE(canceled_); 89 EXPECT_FALSE(canceled_);
83 canceled_ = true; 90 canceled_ = true;
84 } 91 }
85 92
86 RequestPeer* peer() { return peer_.get(); } 93 RequestPeer* peer() { return peer_.get(); }
87 94
88 bool canceled() { return canceled_; } 95 bool canceled() { return canceled_; }
89 96
90 const GURL& url() { return url_; } 97 const GURL& url() { return url_; }
91 const GURL& stream_url() { return stream_url_; } 98 const GURL& stream_url() { return stream_url_; }
92 99
93 void SetDefersLoading(int request_id, bool value) override { 100 void SetDefersLoading(int request_id, bool value) override {
94 defers_loading_ = value; 101 defers_loading_ = value;
95 } 102 }
96 bool defers_loading() const { return defers_loading_; } 103 bool defers_loading() const { return defers_loading_; }
97 104
105 void set_sync_load_response(const SyncLoadResponse& sync_load_response) {
106 sync_load_response_ = sync_load_response;
107 }
108
98 private: 109 private:
99 std::unique_ptr<RequestPeer> peer_; 110 std::unique_ptr<RequestPeer> peer_;
100 bool canceled_; 111 bool canceled_;
101 bool defers_loading_; 112 bool defers_loading_;
102 GURL url_; 113 GURL url_;
103 GURL stream_url_; 114 GURL stream_url_;
115 SyncLoadResponse sync_load_response_;
104 116
105 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcher); 117 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcher);
106 }; 118 };
107 119
108 class TestWebURLLoaderClient : public blink::WebURLLoaderClient { 120 class TestWebURLLoaderClient : public blink::WebURLLoaderClient {
109 public: 121 public:
110 TestWebURLLoaderClient(ResourceDispatcher* dispatcher, 122 TestWebURLLoaderClient(ResourceDispatcher* dispatcher,
111 scoped_refptr<scheduler::TaskQueue> task_runner) 123 scoped_refptr<scheduler::TaskQueue> task_runner)
112 : loader_(new WebURLLoaderImpl( 124 : loader_(new WebURLLoaderImpl(
113 dispatcher, 125 dispatcher,
114 base::WrapUnique(new scheduler::WebTaskRunnerImpl(task_runner)))), 126 base::WrapUnique(new scheduler::WebTaskRunnerImpl(task_runner)))),
115 delete_on_receive_redirect_(false), 127 delete_on_receive_redirect_(false),
116 delete_on_receive_response_(false), 128 delete_on_receive_response_(false),
117 delete_on_receive_data_(false), 129 delete_on_receive_data_(false),
118 delete_on_finish_(false), 130 delete_on_finish_(false),
119 delete_on_fail_(false), 131 delete_on_fail_(false),
120 did_receive_redirect_(false), 132 did_receive_redirect_(false),
121 did_receive_response_(false), 133 did_receive_response_(false),
122 check_redirect_request_priority_(false), 134 check_redirect_request_priority_(false),
123 did_finish_(false) {} 135 did_finish_(false) {}
124 136
125 ~TestWebURLLoaderClient() override {} 137 ~TestWebURLLoaderClient() override {}
126 138
127 // blink::WebURLLoaderClient implementation: 139 // blink::WebURLLoaderClient implementation:
128 void willFollowRedirect( 140 void willFollowRedirect(blink::WebURLLoader* loader,
129 blink::WebURLLoader* loader, 141 blink::WebURLRequest& newRequest,
130 blink::WebURLRequest& newRequest, 142 const blink::WebURLResponse& redirectResponse,
131 const blink::WebURLResponse& redirectResponse) override { 143 int64_t encodedDataLength) override {
132 EXPECT_TRUE(loader_); 144 EXPECT_TRUE(loader_);
133 EXPECT_EQ(loader_.get(), loader); 145 EXPECT_EQ(loader_.get(), loader);
134 146
135 if (check_redirect_request_priority_) 147 if (check_redirect_request_priority_)
136 EXPECT_EQ(redirect_request_priority, newRequest.getPriority()); 148 EXPECT_EQ(redirect_request_priority, newRequest.getPriority());
137 149
138 // No test currently simulates mutiple redirects. 150 // No test currently simulates mutiple redirects.
139 EXPECT_FALSE(did_receive_redirect_); 151 EXPECT_FALSE(did_receive_redirect_);
140 did_receive_redirect_ = true; 152 did_receive_redirect_ = true;
141 153
(...skipping 24 matching lines...) Expand all
166 void didDownloadData(blink::WebURLLoader* loader, 178 void didDownloadData(blink::WebURLLoader* loader,
167 int dataLength, 179 int dataLength,
168 int encodedDataLength) override { 180 int encodedDataLength) override {
169 EXPECT_TRUE(loader_); 181 EXPECT_TRUE(loader_);
170 EXPECT_EQ(loader_.get(), loader); 182 EXPECT_EQ(loader_.get(), loader);
171 } 183 }
172 184
173 void didReceiveData(blink::WebURLLoader* loader, 185 void didReceiveData(blink::WebURLLoader* loader,
174 const char* data, 186 const char* data,
175 int dataLength, 187 int dataLength,
176 int encodedDataLength) override { 188 int encodedDataLength,
189 int encodedBodyLength) override {
177 EXPECT_TRUE(loader_); 190 EXPECT_TRUE(loader_);
178 EXPECT_EQ(loader_.get(), loader); 191 EXPECT_EQ(loader_.get(), loader);
179 // The response should have started, but must not have finished, or failed. 192 // The response should have started, but must not have finished, or failed.
180 EXPECT_TRUE(did_receive_response_); 193 EXPECT_TRUE(did_receive_response_);
181 EXPECT_FALSE(did_finish_); 194 EXPECT_FALSE(did_finish_);
182 EXPECT_EQ(net::OK, error_.reason); 195 EXPECT_EQ(net::OK, error_.reason);
183 EXPECT_EQ("", error_.domain.utf8()); 196 EXPECT_EQ("", error_.domain.utf8());
184 197
185 received_data_.append(data, dataLength); 198 received_data_.append(data, dataLength);
186 199
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 318
306 void DoReceiveResponse() { 319 void DoReceiveResponse() {
307 EXPECT_FALSE(client()->did_receive_response()); 320 EXPECT_FALSE(client()->did_receive_response());
308 peer()->OnReceivedResponse(content::ResourceResponseInfo()); 321 peer()->OnReceivedResponse(content::ResourceResponseInfo());
309 EXPECT_TRUE(client()->did_receive_response()); 322 EXPECT_TRUE(client()->did_receive_response());
310 } 323 }
311 324
312 // Assumes it is called only once for a request. 325 // Assumes it is called only once for a request.
313 void DoReceiveData() { 326 void DoReceiveData() {
314 EXPECT_EQ("", client()->received_data()); 327 EXPECT_EQ("", client()->received_data());
315 peer()->OnReceivedData(base::WrapUnique(new FixedReceivedData( 328 auto size = strlen(kTestData);
316 kTestData, strlen(kTestData), strlen(kTestData)))); 329 peer()->OnReceivedData(
330 base::WrapUnique(new FixedReceivedData(kTestData, size, size, size)));
317 EXPECT_EQ(kTestData, client()->received_data()); 331 EXPECT_EQ(kTestData, client()->received_data());
318 } 332 }
319 333
320 void DoCompleteRequest() { 334 void DoCompleteRequest() {
321 EXPECT_FALSE(client()->did_finish()); 335 EXPECT_FALSE(client()->did_finish());
322 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(), 336 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(),
323 strlen(kTestData)); 337 strlen(kTestData));
324 EXPECT_TRUE(client()->did_finish()); 338 EXPECT_TRUE(client()->did_finish());
325 // There should be no error. 339 // There should be no error.
326 EXPECT_EQ(net::OK, client()->error().reason); 340 EXPECT_EQ(net::OK, client()->error().reason);
(...skipping 11 matching lines...) Expand all
338 352
339 void DoReceiveResponseFtp() { 353 void DoReceiveResponseFtp() {
340 EXPECT_FALSE(client()->did_receive_response()); 354 EXPECT_FALSE(client()->did_receive_response());
341 content::ResourceResponseInfo response_info; 355 content::ResourceResponseInfo response_info;
342 response_info.mime_type = kFtpDirMimeType; 356 response_info.mime_type = kFtpDirMimeType;
343 peer()->OnReceivedResponse(response_info); 357 peer()->OnReceivedResponse(response_info);
344 EXPECT_TRUE(client()->did_receive_response()); 358 EXPECT_TRUE(client()->did_receive_response());
345 } 359 }
346 360
347 void DoReceiveDataFtp() { 361 void DoReceiveDataFtp() {
348 peer()->OnReceivedData(base::WrapUnique(new FixedReceivedData( 362 auto size = strlen(kFtpDirListing);
349 kFtpDirListing, strlen(kFtpDirListing), strlen(kFtpDirListing)))); 363 peer()->OnReceivedData(base::WrapUnique(
364 new FixedReceivedData(kFtpDirListing, size, size, size)));
350 // The FTP delegate should modify the data the client sees. 365 // The FTP delegate should modify the data the client sees.
351 EXPECT_NE(kFtpDirListing, client()->received_data()); 366 EXPECT_NE(kFtpDirListing, client()->received_data());
352 } 367 }
353 368
354 TestWebURLLoaderClient* client() { return client_.get(); } 369 TestWebURLLoaderClient* client() { return client_.get(); }
355 TestResourceDispatcher* dispatcher() { return &dispatcher_; } 370 TestResourceDispatcher* dispatcher() { return &dispatcher_; }
356 RequestPeer* peer() { return dispatcher()->peer(); } 371 RequestPeer* peer() { return dispatcher()->peer(); }
357 base::MessageLoop* message_loop() { return &message_loop_; } 372 base::MessageLoop* message_loop() { return &message_loop_; }
358 373
359 private: 374 private:
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 for (const auto& test : cases) { 674 for (const auto& test : cases) {
660 SCOPED_TRACE(test.ip); 675 SCOPED_TRACE(test.ip);
661 content::ResourceResponseInfo info; 676 content::ResourceResponseInfo info;
662 info.socket_address = net::HostPortPair(test.ip, 443); 677 info.socket_address = net::HostPortPair(test.ip, 443);
663 blink::WebURLResponse response; 678 blink::WebURLResponse response;
664 WebURLLoaderImpl::PopulateURLResponse(url, info, &response, true); 679 WebURLLoaderImpl::PopulateURLResponse(url, info, &response, true);
665 EXPECT_EQ(test.expected, response.remoteIPAddress().utf8()); 680 EXPECT_EQ(test.expected, response.remoteIPAddress().utf8());
666 }; 681 };
667 } 682 }
668 683
684 // Verifies that the lengths used by the PerformanceResourceTiming API are
685 // correctly assigned for sync XHR.
686 TEST_F(WebURLLoaderImplTest, SyncLengths) {
687 static const char kBodyData[] = "Today is Thursday";
688 const int kEncodedBodyLength = 30;
689 const GURL url(kTestURL);
690 blink::WebURLRequest request(url);
691
692 // Prepare a mock response
693 SyncLoadResponse sync_load_response;
694 sync_load_response.error_code = net::OK;
695 sync_load_response.url = url;
696 sync_load_response.data = kBodyData;
697 ASSERT_EQ(17u, sync_load_response.data.size());
698 sync_load_response.encoded_body_length = kEncodedBodyLength;
699 dispatcher()->set_sync_load_response(sync_load_response);
700
701 blink::WebURLResponse response;
702 blink::WebURLError error;
703 blink::WebData data;
704 client()->loader()->loadSynchronously(request, response, error, data);
705
706 EXPECT_EQ(kEncodedBodyLength, response.encodedBodyLength());
707 int expected_decoded_body_length = strlen(kBodyData);
708 EXPECT_EQ(expected_decoded_body_length, response.decodedBodyLength());
709 }
710
669 } // namespace 711 } // namespace
670 } // namespace content 712 } // namespace content
OLDNEW
« no previous file with comments | « content/child/web_url_loader_impl.cc ('k') | content/public/child/fixed_received_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698