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

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

Issue 1710733002: Move multipart resource handling to core/fetch (2/2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@multipart-cleanup
Patch Set: Created 4 years, 9 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/content_child.gypi » ('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 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 const char kTestData[] = "blah!"; 46 const char kTestData[] = "blah!";
47 47
48 const char kFtpDirMimeType[] = "text/vnd.chromium.ftp-dir"; 48 const char kFtpDirMimeType[] = "text/vnd.chromium.ftp-dir";
49 // Simple FTP directory listing. Tests are not concerned with correct parsing, 49 // Simple FTP directory listing. Tests are not concerned with correct parsing,
50 // but rather correct cleanup when deleted while parsing. Important details of 50 // but rather correct cleanup when deleted while parsing. Important details of
51 // this list are that it contains more than one entry that are not "." or "..". 51 // this list are that it contains more than one entry that are not "." or "..".
52 const char kFtpDirListing[] = 52 const char kFtpDirListing[] =
53 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 goat\n" 53 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 goat\n"
54 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 hat"; 54 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 hat";
55 55
56 const char kMultipartResponseMimeType[] = "multipart/x-mixed-replace";
57 const char kMultipartResponseHeaders[] =
58 "HTTP/1.0 200 Peachy\r\n"
59 "Content-Type: multipart/x-mixed-replace; boundary=boundary\r\n\r\n";
60 // Simple multipart response. Imporant details for the tests are that it
61 // contains multiple chunks, and that it doesn't end with a boundary, so will
62 // send data in OnResponseComplete. Also, it will resolve to kTestData.
63 const char kMultipartResponse[] =
64 "--boundary\n"
65 "Content-type: text/html\n\n"
66 "bl"
67 "--boundary\n"
68 "Content-type: text/html\n\n"
69 "ah!";
70
71 class TestResourceDispatcher : public ResourceDispatcher { 56 class TestResourceDispatcher : public ResourceDispatcher {
72 public: 57 public:
73 TestResourceDispatcher() : 58 TestResourceDispatcher() :
74 ResourceDispatcher(nullptr, nullptr), 59 ResourceDispatcher(nullptr, nullptr),
75 canceled_(false) { 60 canceled_(false) {
76 } 61 }
77 62
78 ~TestResourceDispatcher() override {} 63 ~TestResourceDispatcher() override {}
79 64
80 // TestDispatcher implementation: 65 // TestDispatcher implementation:
(...skipping 29 matching lines...) Expand all
110 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcher); 95 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcher);
111 }; 96 };
112 97
113 class TestWebURLLoaderClient : public blink::WebURLLoaderClient { 98 class TestWebURLLoaderClient : public blink::WebURLLoaderClient {
114 public: 99 public:
115 TestWebURLLoaderClient(ResourceDispatcher* dispatcher, 100 TestWebURLLoaderClient(ResourceDispatcher* dispatcher,
116 scoped_refptr<scheduler::TaskQueue> task_runner) 101 scoped_refptr<scheduler::TaskQueue> task_runner)
117 : loader_(new WebURLLoaderImpl( 102 : loader_(new WebURLLoaderImpl(
118 dispatcher, 103 dispatcher,
119 make_scoped_ptr(new scheduler::WebTaskRunnerImpl(task_runner)))), 104 make_scoped_ptr(new scheduler::WebTaskRunnerImpl(task_runner)))),
120 expect_multipart_response_(false),
121 delete_on_receive_redirect_(false), 105 delete_on_receive_redirect_(false),
122 delete_on_receive_response_(false), 106 delete_on_receive_response_(false),
123 delete_on_receive_data_(false), 107 delete_on_receive_data_(false),
124 delete_on_finish_(false), 108 delete_on_finish_(false),
125 delete_on_fail_(false), 109 delete_on_fail_(false),
126 did_receive_redirect_(false), 110 did_receive_redirect_(false),
127 did_receive_response_(false), 111 did_receive_response_(false),
128 did_finish_(false) {} 112 did_finish_(false) {}
129 113
130 ~TestWebURLLoaderClient() override {} 114 ~TestWebURLLoaderClient() override {}
(...skipping 18 matching lines...) Expand all
149 unsigned long long totalBytesToBeSent) override { 133 unsigned long long totalBytesToBeSent) override {
150 EXPECT_TRUE(loader_); 134 EXPECT_TRUE(loader_);
151 EXPECT_EQ(loader_.get(), loader); 135 EXPECT_EQ(loader_.get(), loader);
152 } 136 }
153 137
154 void didReceiveResponse( 138 void didReceiveResponse(
155 blink::WebURLLoader* loader, 139 blink::WebURLLoader* loader,
156 const blink::WebURLResponse& response) override { 140 const blink::WebURLResponse& response) override {
157 EXPECT_TRUE(loader_); 141 EXPECT_TRUE(loader_);
158 EXPECT_EQ(loader_.get(), loader); 142 EXPECT_EQ(loader_.get(), loader);
159 143 EXPECT_FALSE(did_receive_response_);
160 // Only multipart requests may receive multiple response headers.
161 EXPECT_TRUE(expect_multipart_response_ || !did_receive_response_);
162 144
163 did_receive_response_ = true; 145 did_receive_response_ = true;
164 response_ = response; 146 response_ = response;
165 if (delete_on_receive_response_) 147 if (delete_on_receive_response_)
166 loader_.reset(); 148 loader_.reset();
167 } 149 }
168 150
169 void didDownloadData(blink::WebURLLoader* loader, 151 void didDownloadData(blink::WebURLLoader* loader,
170 int dataLength, 152 int dataLength,
171 int encodedDataLength) override { 153 int encodedDataLength) override {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 201
220 if (delete_on_fail_) 202 if (delete_on_fail_)
221 loader_.reset(); 203 loader_.reset();
222 } 204 }
223 205
224 WebURLLoaderImpl* loader() { return loader_.get(); } 206 WebURLLoaderImpl* loader() { return loader_.get(); }
225 void DeleteLoader() { 207 void DeleteLoader() {
226 loader_.reset(); 208 loader_.reset();
227 } 209 }
228 210
229 void set_expect_multipart_response() { expect_multipart_response_ = true; }
230
231 void set_delete_on_receive_redirect() { delete_on_receive_redirect_ = true; } 211 void set_delete_on_receive_redirect() { delete_on_receive_redirect_ = true; }
232 void set_delete_on_receive_response() { delete_on_receive_response_ = true; } 212 void set_delete_on_receive_response() { delete_on_receive_response_ = true; }
233 void set_delete_on_receive_data() { delete_on_receive_data_ = true; } 213 void set_delete_on_receive_data() { delete_on_receive_data_ = true; }
234 void set_delete_on_finish() { delete_on_finish_ = true; } 214 void set_delete_on_finish() { delete_on_finish_ = true; }
235 void set_delete_on_fail() { delete_on_fail_ = true; } 215 void set_delete_on_fail() { delete_on_fail_ = true; }
236 216
237 bool did_receive_redirect() const { return did_receive_redirect_; } 217 bool did_receive_redirect() const { return did_receive_redirect_; }
238 bool did_receive_response() const { return did_receive_response_; } 218 bool did_receive_response() const { return did_receive_response_; }
239 const std::string& received_data() const { return received_data_; } 219 const std::string& received_data() const { return received_data_; }
240 bool did_finish() const { return did_finish_; } 220 bool did_finish() const { return did_finish_; }
241 const blink::WebURLError& error() const { return error_; } 221 const blink::WebURLError& error() const { return error_; }
242 const blink::WebURLResponse& response() const { return response_; } 222 const blink::WebURLResponse& response() const { return response_; }
243 223
244 private: 224 private:
245 scoped_ptr<WebURLLoaderImpl> loader_; 225 scoped_ptr<WebURLLoaderImpl> loader_;
246 226
247 bool expect_multipart_response_;
248
249 bool delete_on_receive_redirect_; 227 bool delete_on_receive_redirect_;
250 bool delete_on_receive_response_; 228 bool delete_on_receive_response_;
251 bool delete_on_receive_data_; 229 bool delete_on_receive_data_;
252 bool delete_on_finish_; 230 bool delete_on_finish_;
253 bool delete_on_fail_; 231 bool delete_on_fail_;
254 232
255 bool did_receive_redirect_; 233 bool did_receive_redirect_;
256 bool did_receive_response_; 234 bool did_receive_response_;
257 std::string received_data_; 235 std::string received_data_;
258 bool did_finish_; 236 bool did_finish_;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 EXPECT_TRUE(client()->did_receive_response()); 315 EXPECT_TRUE(client()->did_receive_response());
338 } 316 }
339 317
340 void DoReceiveDataFtp() { 318 void DoReceiveDataFtp() {
341 peer()->OnReceivedData(make_scoped_ptr(new FixedReceivedData( 319 peer()->OnReceivedData(make_scoped_ptr(new FixedReceivedData(
342 kFtpDirListing, strlen(kFtpDirListing), strlen(kFtpDirListing)))); 320 kFtpDirListing, strlen(kFtpDirListing), strlen(kFtpDirListing))));
343 // The FTP delegate should modify the data the client sees. 321 // The FTP delegate should modify the data the client sees.
344 EXPECT_NE(kFtpDirListing, client()->received_data()); 322 EXPECT_NE(kFtpDirListing, client()->received_data());
345 } 323 }
346 324
347 void DoReceiveResponseMultipart() {
348 EXPECT_FALSE(client()->did_receive_response());
349 content::ResourceResponseInfo response_info;
350 response_info.headers = new net::HttpResponseHeaders(
351 net::HttpUtil::AssembleRawHeaders(kMultipartResponseHeaders,
352 strlen(kMultipartResponseHeaders)));
353 response_info.mime_type = kMultipartResponseMimeType;
354 peer()->OnReceivedResponse(response_info);
355 EXPECT_TRUE(client()->did_receive_response());
356 }
357
358 void DoReceiveDataMultipart() {
359 peer()->OnReceivedData(make_scoped_ptr(
360 new FixedReceivedData(kMultipartResponse, strlen(kMultipartResponse),
361 strlen(kMultipartResponse))));
362 // Multipart delegate should modify the data the client sees.
363 EXPECT_NE(kMultipartResponse, client()->received_data());
364 }
365
366 TestWebURLLoaderClient* client() { return client_.get(); } 325 TestWebURLLoaderClient* client() { return client_.get(); }
367 TestResourceDispatcher* dispatcher() { return &dispatcher_; } 326 TestResourceDispatcher* dispatcher() { return &dispatcher_; }
368 RequestPeer* peer() { return dispatcher()->peer(); } 327 RequestPeer* peer() { return dispatcher()->peer(); }
369 base::MessageLoop* message_loop() { return &message_loop_; } 328 base::MessageLoop* message_loop() { return &message_loop_; }
370 329
371 private: 330 private:
372 base::MessageLoop message_loop_; 331 base::MessageLoop message_loop_;
373 // WorkerScheduler is needed because WebURLLoaderImpl needs a 332 // WorkerScheduler is needed because WebURLLoaderImpl needs a
374 // scheduler::TaskQueue. 333 // scheduler::TaskQueue.
375 scoped_ptr<scheduler::WorkerScheduler> worker_scheduler_; 334 scoped_ptr<scheduler::WorkerScheduler> worker_scheduler_;
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 } 548 }
590 549
591 TEST_F(WebURLLoaderImplTest, FtpDeleteOnFail) { 550 TEST_F(WebURLLoaderImplTest, FtpDeleteOnFail) {
592 client()->set_delete_on_fail(); 551 client()->set_delete_on_fail();
593 DoStartAsyncRequest(); 552 DoStartAsyncRequest();
594 DoReceiveResponseFtp(); 553 DoReceiveResponseFtp();
595 DoReceiveDataFtp(); 554 DoReceiveDataFtp();
596 DoFailRequest(); 555 DoFailRequest();
597 } 556 }
598 557
599 // Multipart integration tests. These are focused more on safe deletion than
600 // correct parsing of Multipart responses.
601
602 TEST_F(WebURLLoaderImplTest, Multipart) {
603 client()->set_expect_multipart_response();
604 DoStartAsyncRequest();
605 DoReceiveResponseMultipart();
606 DoReceiveDataMultipart();
607 DoCompleteRequest();
608 EXPECT_EQ(kTestData, client()->received_data());
609 EXPECT_FALSE(dispatcher()->canceled());
610 }
611
612 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveFirstResponse) {
613 client()->set_expect_multipart_response();
614 client()->set_delete_on_receive_response();
615 DoStartAsyncRequest();
616 DoReceiveResponseMultipart();
617 EXPECT_EQ("", client()->received_data());
618 }
619
620 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveSecondResponse) {
621 client()->set_expect_multipart_response();
622 DoStartAsyncRequest();
623 DoReceiveResponseMultipart();
624 client()->set_delete_on_receive_response();
625 DoReceiveDataMultipart();
626 EXPECT_EQ("", client()->received_data());
627 }
628
629 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveFirstData) {
630 client()->set_expect_multipart_response();
631 client()->set_delete_on_receive_data();
632 DoStartAsyncRequest();
633 DoReceiveResponseMultipart();
634 DoReceiveDataMultipart();
635 EXPECT_EQ("bl", client()->received_data());
636 }
637
638 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveMoreData) {
639 client()->set_expect_multipart_response();
640 DoStartAsyncRequest();
641 DoReceiveResponseMultipart();
642 DoReceiveDataMultipart();
643 // For multipart responses, the delegate may send some data when notified
644 // of a request completing.
645 client()->set_delete_on_receive_data();
646 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(),
647 strlen(kTestData));
648 EXPECT_FALSE(client()->did_finish());
649 EXPECT_EQ(kTestData, client()->received_data());
650 }
651
652 TEST_F(WebURLLoaderImplTest, MultipartDeleteFinish) {
653 client()->set_expect_multipart_response();
654 client()->set_delete_on_finish();
655 DoStartAsyncRequest();
656 DoReceiveResponseMultipart();
657 DoReceiveDataMultipart();
658 DoCompleteRequest();
659 EXPECT_EQ(kTestData, client()->received_data());
660 }
661
662 TEST_F(WebURLLoaderImplTest, MultipartDeleteFail) {
663 client()->set_expect_multipart_response();
664 client()->set_delete_on_fail();
665 DoStartAsyncRequest();
666 DoReceiveResponseMultipart();
667 DoReceiveDataMultipart();
668 DoFailRequest();
669 }
670
671 // PlzNavigate: checks that the stream override parameters provided on 558 // PlzNavigate: checks that the stream override parameters provided on
672 // navigation commit are properly applied. 559 // navigation commit are properly applied.
673 TEST_F(WebURLLoaderImplTest, BrowserSideNavigationCommit) { 560 TEST_F(WebURLLoaderImplTest, BrowserSideNavigationCommit) {
674 // Initialize the request and the stream override. 561 // Initialize the request and the stream override.
675 const GURL kNavigationURL = GURL(kTestURL); 562 const GURL kNavigationURL = GURL(kTestURL);
676 const GURL kStreamURL = GURL("http://bar"); 563 const GURL kStreamURL = GURL("http://bar");
677 const std::string kMimeType = "text/html"; 564 const std::string kMimeType = "text/html";
678 blink::WebURLRequest request; 565 blink::WebURLRequest request;
679 request.initialize(); 566 request.initialize();
680 request.setURL(kNavigationURL); 567 request.setURL(kNavigationURL);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 info.socket_address = net::HostPortPair(test.ip, 443); 620 info.socket_address = net::HostPortPair(test.ip, 443);
734 blink::WebURLResponse response; 621 blink::WebURLResponse response;
735 response.initialize(); 622 response.initialize();
736 WebURLLoaderImpl::PopulateURLResponse(url, info, &response, true); 623 WebURLLoaderImpl::PopulateURLResponse(url, info, &response, true);
737 EXPECT_EQ(test.expected, response.remoteIPAddress().utf8()); 624 EXPECT_EQ(test.expected, response.remoteIPAddress().utf8());
738 }; 625 };
739 } 626 }
740 627
741 } // namespace 628 } // namespace
742 } // namespace content 629 } // namespace content
OLDNEW
« no previous file with comments | « content/child/web_url_loader_impl.cc ('k') | content/content_child.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698