Hide Google logo and custom launcher page in app list for non-Google search engines.
[chromium-blink-merge.git] / content / child / web_url_loader_impl_unittest.cc
blobab4c645d35dc10e4a78df42cf8b62ac469f8c7b0
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
3 // found in the LICENSE file.
5 #include "content/child/web_url_loader_impl.h"
7 #include <string.h>
9 #include "base/command_line.h"
10 #include "base/macros.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/time/time.h"
15 #include "content/child/request_extra_data.h"
16 #include "content/child/request_info.h"
17 #include "content/child/resource_dispatcher.h"
18 #include "content/child/resource_loader_bridge.h"
19 #include "content/public/child/request_peer.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/common/resource_response_info.h"
22 #include "net/base/net_errors.h"
23 #include "net/http/http_response_headers.h"
24 #include "net/http/http_util.h"
25 #include "net/url_request/redirect_info.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/WebKit/public/platform/WebString.h"
28 #include "third_party/WebKit/public/platform/WebURLError.h"
29 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
30 #include "third_party/WebKit/public/platform/WebURLRequest.h"
31 #include "third_party/WebKit/public/platform/WebURLResponse.h"
32 #include "url/gurl.h"
34 namespace content {
35 namespace {
37 const char kTestURL[] = "http://foo";
38 const char kTestData[] = "blah!";
40 const char kFtpDirMimeType[] = "text/vnd.chromium.ftp-dir";
41 // Simple FTP directory listing. Tests are not concerned with correct parsing,
42 // but rather correct cleanup when deleted while parsing. Important details of
43 // this list are that it contains more than one entry that are not "." or "..".
44 const char kFtpDirListing[] =
45 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 goat\n"
46 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 hat";
48 const char kMultipartResponseMimeType[] = "multipart/x-mixed-replace";
49 const char kMultipartResponseHeaders[] =
50 "HTTP/1.0 200 Peachy\r\n"
51 "Content-Type: multipart/x-mixed-replace; boundary=boundary\r\n\r\n";
52 // Simple multipart response. Imporant details for the tests are that it
53 // contains multiple chunks, and that it doesn't end with a boundary, so will
54 // send data in OnResponseComplete. Also, it will resolve to kTestData.
55 const char kMultipartResponse[] =
56 "--boundary\n"
57 "Content-type: text/html\n\n"
58 "bl"
59 "--boundary\n"
60 "Content-type: text/html\n\n"
61 "ah!";
63 class TestBridge : public ResourceLoaderBridge,
64 public base::SupportsWeakPtr<TestBridge> {
65 public:
66 TestBridge(const RequestInfo& info) :
67 peer_(NULL),
68 canceled_(false),
69 url_(info.url) {
72 ~TestBridge() override {}
74 // ResourceLoaderBridge implementation:
75 void SetRequestBody(ResourceRequestBody* request_body) override {}
77 bool Start(RequestPeer* peer) override {
78 EXPECT_FALSE(peer_);
79 peer_ = peer;
80 return true;
83 void Cancel() override {
84 EXPECT_FALSE(canceled_);
85 canceled_ = true;
88 void SetDefersLoading(bool value) override {}
90 void DidChangePriority(net::RequestPriority new_priority,
91 int intra_priority_value) override {}
93 bool AttachThreadedDataReceiver(
94 blink::WebThreadedDataReceiver* threaded_data_receiver) override {
95 NOTREACHED();
96 return false;
99 void SyncLoad(SyncLoadResponse* response) override {}
101 RequestPeer* peer() { return peer_; }
103 bool canceled() { return canceled_; }
105 const GURL& url() { return url_; }
107 private:
108 RequestPeer* peer_;
109 bool canceled_;
110 GURL url_;
112 DISALLOW_COPY_AND_ASSIGN(TestBridge);
115 class TestResourceDispatcher : public ResourceDispatcher {
116 public:
117 TestResourceDispatcher() : ResourceDispatcher(nullptr, nullptr) {}
118 ~TestResourceDispatcher() override {}
120 // ResourceDispatcher implementation:
121 ResourceLoaderBridge* CreateBridge(const RequestInfo& request_info) override {
122 EXPECT_FALSE(bridge_.get());
123 TestBridge* bridge = new TestBridge(request_info);
124 bridge_ = bridge->AsWeakPtr();
125 return bridge;
128 TestBridge* bridge() { return bridge_.get(); }
130 private:
131 base::WeakPtr<TestBridge> bridge_;
133 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcher);
136 class TestWebURLLoaderClient : public blink::WebURLLoaderClient {
137 public:
138 TestWebURLLoaderClient(
139 ResourceDispatcher* dispatcher,
140 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
141 : loader_(new WebURLLoaderImpl(dispatcher, task_runner)),
142 expect_multipart_response_(false),
143 delete_on_receive_redirect_(false),
144 delete_on_receive_response_(false),
145 delete_on_receive_data_(false),
146 delete_on_finish_(false),
147 delete_on_fail_(false),
148 did_receive_redirect_(false),
149 did_receive_response_(false),
150 did_finish_(false) {}
152 virtual ~TestWebURLLoaderClient() {}
154 // blink::WebURLLoaderClient implementation:
155 virtual void willSendRequest(
156 blink::WebURLLoader* loader,
157 blink::WebURLRequest& newRequest,
158 const blink::WebURLResponse& redirectResponse) override {
159 EXPECT_TRUE(loader_);
160 EXPECT_EQ(loader_.get(), loader);
161 // No test currently simulates mutiple redirects.
162 EXPECT_FALSE(did_receive_redirect_);
163 did_receive_redirect_ = true;
165 if (delete_on_receive_redirect_)
166 loader_.reset();
169 virtual void didSendData(blink::WebURLLoader* loader,
170 unsigned long long bytesSent,
171 unsigned long long totalBytesToBeSent) override {
172 EXPECT_TRUE(loader_);
173 EXPECT_EQ(loader_.get(), loader);
176 virtual void didReceiveResponse(
177 blink::WebURLLoader* loader,
178 const blink::WebURLResponse& response) override {
179 EXPECT_TRUE(loader_);
180 EXPECT_EQ(loader_.get(), loader);
182 // Only multipart requests may receive multiple response headers.
183 EXPECT_TRUE(expect_multipart_response_ || !did_receive_response_);
185 did_receive_response_ = true;
186 response_ = response;
187 if (delete_on_receive_response_)
188 loader_.reset();
191 virtual void didDownloadData(blink::WebURLLoader* loader,
192 int dataLength,
193 int encodedDataLength) override {
194 EXPECT_TRUE(loader_);
195 EXPECT_EQ(loader_.get(), loader);
198 virtual void didReceiveData(blink::WebURLLoader* loader,
199 const char* data,
200 int dataLength,
201 int encodedDataLength) override {
202 EXPECT_TRUE(loader_);
203 EXPECT_EQ(loader_.get(), loader);
204 // The response should have started, but must not have finished, or failed.
205 EXPECT_TRUE(did_receive_response_);
206 EXPECT_FALSE(did_finish_);
207 EXPECT_EQ(net::OK, error_.reason);
208 EXPECT_EQ("", error_.domain.utf8());
210 received_data_.append(data, dataLength);
212 if (delete_on_receive_data_)
213 loader_.reset();
216 virtual void didReceiveCachedMetadata(blink::WebURLLoader* loader,
217 const char* data,
218 int dataLength) override {
219 EXPECT_EQ(loader_.get(), loader);
222 virtual void didFinishLoading(blink::WebURLLoader* loader,
223 double finishTime,
224 int64_t totalEncodedDataLength) override {
225 EXPECT_TRUE(loader_);
226 EXPECT_EQ(loader_.get(), loader);
227 EXPECT_TRUE(did_receive_response_);
228 EXPECT_FALSE(did_finish_);
229 did_finish_ = true;
231 if (delete_on_finish_)
232 loader_.reset();
235 virtual void didFail(blink::WebURLLoader* loader,
236 const blink::WebURLError& error) override {
237 EXPECT_TRUE(loader_);
238 EXPECT_EQ(loader_.get(), loader);
239 EXPECT_FALSE(did_finish_);
240 error_ = error;
242 if (delete_on_fail_)
243 loader_.reset();
246 WebURLLoaderImpl* loader() { return loader_.get(); }
247 void DeleteLoader() {
248 loader_.reset();
251 void set_expect_multipart_response() { expect_multipart_response_ = true; }
253 void set_delete_on_receive_redirect() { delete_on_receive_redirect_ = true; }
254 void set_delete_on_receive_response() { delete_on_receive_response_ = true; }
255 void set_delete_on_receive_data() { delete_on_receive_data_ = true; }
256 void set_delete_on_finish() { delete_on_finish_ = true; }
257 void set_delete_on_fail() { delete_on_fail_ = true; }
259 bool did_receive_redirect() const { return did_receive_redirect_; }
260 bool did_receive_response() const { return did_receive_response_; }
261 const std::string& received_data() const { return received_data_; }
262 bool did_finish() const { return did_finish_; }
263 const blink::WebURLError& error() const { return error_; }
264 const blink::WebURLResponse& response() const { return response_; }
266 private:
267 scoped_ptr<WebURLLoaderImpl> loader_;
269 bool expect_multipart_response_;
271 bool delete_on_receive_redirect_;
272 bool delete_on_receive_response_;
273 bool delete_on_receive_data_;
274 bool delete_on_finish_;
275 bool delete_on_fail_;
277 bool did_receive_redirect_;
278 bool did_receive_response_;
279 std::string received_data_;
280 bool did_finish_;
281 blink::WebURLError error_;
282 blink::WebURLResponse response_;
284 DISALLOW_COPY_AND_ASSIGN(TestWebURLLoaderClient);
287 class WebURLLoaderImplTest : public testing::Test {
288 public:
289 explicit WebURLLoaderImplTest()
290 : client_(&dispatcher_, message_loop_.task_runner()) {}
291 ~WebURLLoaderImplTest() override {}
293 void DoStartAsyncRequest() {
294 blink::WebURLRequest request;
295 request.initialize();
296 request.setURL(GURL(kTestURL));
297 client()->loader()->loadAsynchronously(request, client());
298 ASSERT_TRUE(bridge());
299 ASSERT_TRUE(peer());
302 void DoReceiveRedirect() {
303 EXPECT_FALSE(client()->did_receive_redirect());
304 net::RedirectInfo redirect_info;
305 redirect_info.status_code = 302;
306 redirect_info.new_method = "GET";
307 redirect_info.new_url = GURL(kTestURL);
308 redirect_info.new_first_party_for_cookies = GURL(kTestURL);
309 peer()->OnReceivedRedirect(redirect_info,
310 content::ResourceResponseInfo());
311 EXPECT_TRUE(client()->did_receive_redirect());
314 void DoReceiveResponse() {
315 EXPECT_FALSE(client()->did_receive_response());
316 peer()->OnReceivedResponse(content::ResourceResponseInfo());
317 EXPECT_TRUE(client()->did_receive_response());
320 // Assumes it is called only once for a request.
321 void DoReceiveData() {
322 EXPECT_EQ("", client()->received_data());
323 peer()->OnReceivedData(kTestData, strlen(kTestData), strlen(kTestData));
324 EXPECT_EQ(kTestData, client()->received_data());
327 void DoCompleteRequest() {
328 EXPECT_FALSE(client()->did_finish());
329 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(),
330 strlen(kTestData));
331 EXPECT_TRUE(client()->did_finish());
332 // There should be no error.
333 EXPECT_EQ(net::OK, client()->error().reason);
334 EXPECT_EQ("", client()->error().domain.utf8());
337 void DoFailRequest() {
338 EXPECT_FALSE(client()->did_finish());
339 peer()->OnCompletedRequest(net::ERR_FAILED, false, false, "",
340 base::TimeTicks(), strlen(kTestData));
341 EXPECT_FALSE(client()->did_finish());
342 EXPECT_EQ(net::ERR_FAILED, client()->error().reason);
343 EXPECT_EQ(net::kErrorDomain, client()->error().domain.utf8());
346 void DoReceiveResponseFtp() {
347 EXPECT_FALSE(client()->did_receive_response());
348 content::ResourceResponseInfo response_info;
349 response_info.mime_type = kFtpDirMimeType;
350 peer()->OnReceivedResponse(response_info);
351 EXPECT_TRUE(client()->did_receive_response());
354 void DoReceiveDataFtp() {
355 peer()->OnReceivedData(kFtpDirListing, strlen(kFtpDirListing),
356 strlen(kFtpDirListing));
357 // The FTP delegate should modify the data the client sees.
358 EXPECT_NE(kFtpDirListing, client()->received_data());
361 void DoReceiveResponseMultipart() {
362 EXPECT_FALSE(client()->did_receive_response());
363 content::ResourceResponseInfo response_info;
364 response_info.headers = new net::HttpResponseHeaders(
365 net::HttpUtil::AssembleRawHeaders(kMultipartResponseHeaders,
366 strlen(kMultipartResponseHeaders)));
367 response_info.mime_type = kMultipartResponseMimeType;
368 peer()->OnReceivedResponse(response_info);
369 EXPECT_TRUE(client()->did_receive_response());
372 void DoReceiveDataMultipart() {
373 peer()->OnReceivedData(kMultipartResponse, strlen(kMultipartResponse),
374 strlen(kMultipartResponse));
375 // Multipart delegate should modify the data the client sees.
376 EXPECT_NE(kMultipartResponse, client()->received_data());
379 TestWebURLLoaderClient* client() { return &client_; }
380 TestBridge* bridge() { return dispatcher_.bridge(); }
381 RequestPeer* peer() { return bridge()->peer(); }
382 base::MessageLoop* message_loop() { return &message_loop_; }
384 private:
385 base::MessageLoop message_loop_;
386 TestResourceDispatcher dispatcher_;
387 TestWebURLLoaderClient client_;
390 TEST_F(WebURLLoaderImplTest, Success) {
391 DoStartAsyncRequest();
392 DoReceiveResponse();
393 DoReceiveData();
394 DoCompleteRequest();
395 EXPECT_FALSE(bridge()->canceled());
396 EXPECT_EQ(kTestData, client()->received_data());
399 TEST_F(WebURLLoaderImplTest, Redirect) {
400 DoStartAsyncRequest();
401 DoReceiveRedirect();
402 DoReceiveResponse();
403 DoReceiveData();
404 DoCompleteRequest();
405 EXPECT_FALSE(bridge()->canceled());
406 EXPECT_EQ(kTestData, client()->received_data());
409 TEST_F(WebURLLoaderImplTest, Failure) {
410 DoStartAsyncRequest();
411 DoReceiveResponse();
412 DoReceiveData();
413 DoFailRequest();
414 EXPECT_FALSE(bridge()->canceled());
417 // The client may delete the WebURLLoader during any callback from the loader.
418 // These tests make sure that doesn't result in a crash.
419 TEST_F(WebURLLoaderImplTest, DeleteOnReceiveRedirect) {
420 client()->set_delete_on_receive_redirect();
421 DoStartAsyncRequest();
422 DoReceiveRedirect();
423 EXPECT_FALSE(bridge());
426 TEST_F(WebURLLoaderImplTest, DeleteOnReceiveResponse) {
427 client()->set_delete_on_receive_response();
428 DoStartAsyncRequest();
429 DoReceiveResponse();
430 EXPECT_FALSE(bridge());
433 TEST_F(WebURLLoaderImplTest, DeleteOnReceiveData) {
434 client()->set_delete_on_receive_data();
435 DoStartAsyncRequest();
436 DoReceiveResponse();
437 DoReceiveData();
438 EXPECT_FALSE(bridge());
441 TEST_F(WebURLLoaderImplTest, DeleteOnFinish) {
442 client()->set_delete_on_finish();
443 DoStartAsyncRequest();
444 DoReceiveResponse();
445 DoReceiveData();
446 DoCompleteRequest();
447 EXPECT_FALSE(bridge());
450 TEST_F(WebURLLoaderImplTest, DeleteOnFail) {
451 client()->set_delete_on_fail();
452 DoStartAsyncRequest();
453 DoReceiveResponse();
454 DoReceiveData();
455 DoFailRequest();
456 EXPECT_FALSE(bridge());
459 TEST_F(WebURLLoaderImplTest, DeleteBeforeResponseDataURL) {
460 blink::WebURLRequest request;
461 request.initialize();
462 request.setURL(GURL("data:text/html;charset=utf-8,blah!"));
463 client()->loader()->loadAsynchronously(request, client());
464 client()->DeleteLoader();
465 message_loop()->RunUntilIdle();
466 EXPECT_FALSE(client()->did_receive_response());
467 EXPECT_FALSE(bridge());
470 // Data URL tests.
472 TEST_F(WebURLLoaderImplTest, DataURL) {
473 blink::WebURLRequest request;
474 request.initialize();
475 request.setURL(GURL("data:text/html;charset=utf-8,blah!"));
476 client()->loader()->loadAsynchronously(request, client());
477 message_loop()->RunUntilIdle();
478 EXPECT_EQ("blah!", client()->received_data());
479 EXPECT_TRUE(client()->did_finish());
480 EXPECT_EQ(net::OK, client()->error().reason);
481 EXPECT_EQ("", client()->error().domain.utf8());
484 TEST_F(WebURLLoaderImplTest, DataURLDeleteOnReceiveResponse) {
485 blink::WebURLRequest request;
486 request.initialize();
487 request.setURL(GURL("data:text/html;charset=utf-8,blah!"));
488 client()->set_delete_on_receive_response();
489 client()->loader()->loadAsynchronously(request, client());
490 message_loop()->RunUntilIdle();
491 EXPECT_TRUE(client()->did_receive_response());
492 EXPECT_EQ("", client()->received_data());
493 EXPECT_FALSE(client()->did_finish());
494 EXPECT_FALSE(bridge());
497 TEST_F(WebURLLoaderImplTest, DataURLDeleteOnReceiveData) {
498 blink::WebURLRequest request;
499 request.initialize();
500 request.setURL(GURL("data:text/html;charset=utf-8,blah!"));
501 client()->set_delete_on_receive_data();
502 client()->loader()->loadAsynchronously(request, client());
503 message_loop()->RunUntilIdle();
504 EXPECT_TRUE(client()->did_receive_response());
505 EXPECT_EQ("blah!", client()->received_data());
506 EXPECT_FALSE(client()->did_finish());
507 EXPECT_FALSE(bridge());
510 TEST_F(WebURLLoaderImplTest, DataURLDeleteOnFinish) {
511 blink::WebURLRequest request;
512 request.initialize();
513 request.setURL(GURL("data:text/html;charset=utf-8,blah!"));
514 client()->set_delete_on_finish();
515 client()->loader()->loadAsynchronously(request, client());
516 message_loop()->RunUntilIdle();
517 EXPECT_TRUE(client()->did_receive_response());
518 EXPECT_EQ("blah!", client()->received_data());
519 EXPECT_TRUE(client()->did_finish());
520 EXPECT_FALSE(bridge());
523 TEST_F(WebURLLoaderImplTest, DataURLDefersLoading) {
524 blink::WebURLRequest request;
525 request.initialize();
526 request.setURL(GURL("data:text/html;charset=utf-8,blah!"));
527 client()->loader()->loadAsynchronously(request, client());
529 // setDefersLoading() might be called with either false or true in no
530 // specific order. The user of the API will not have sufficient information
531 // about the WebURLLoader's internal state, so the latter gracefully needs to
532 // handle calling setDefersLoading any number of times with any values from
533 // any point in time.
535 client()->loader()->setDefersLoading(false);
536 client()->loader()->setDefersLoading(true);
537 client()->loader()->setDefersLoading(true);
538 message_loop()->RunUntilIdle();
539 EXPECT_FALSE(client()->did_finish());
541 client()->loader()->setDefersLoading(false);
542 client()->loader()->setDefersLoading(true);
543 message_loop()->RunUntilIdle();
544 EXPECT_FALSE(client()->did_finish());
546 client()->loader()->setDefersLoading(false);
547 message_loop()->RunUntilIdle();
548 EXPECT_TRUE(client()->did_finish());
550 client()->loader()->setDefersLoading(true);
551 client()->loader()->setDefersLoading(false);
552 client()->loader()->setDefersLoading(false);
553 message_loop()->RunUntilIdle();
554 EXPECT_TRUE(client()->did_finish());
556 EXPECT_EQ("blah!", client()->received_data());
557 EXPECT_EQ(net::OK, client()->error().reason);
558 EXPECT_EQ("", client()->error().domain.utf8());
561 // FTP integration tests. These are focused more on safe deletion than correct
562 // parsing of FTP responses.
564 TEST_F(WebURLLoaderImplTest, Ftp) {
565 DoStartAsyncRequest();
566 DoReceiveResponseFtp();
567 DoReceiveDataFtp();
568 DoCompleteRequest();
569 EXPECT_FALSE(bridge()->canceled());
572 TEST_F(WebURLLoaderImplTest, FtpDeleteOnReceiveResponse) {
573 client()->set_delete_on_receive_response();
574 DoStartAsyncRequest();
575 DoReceiveResponseFtp();
577 // No data should have been received.
578 EXPECT_EQ("", client()->received_data());
579 EXPECT_FALSE(bridge());
582 TEST_F(WebURLLoaderImplTest, FtpDeleteOnReceiveFirstData) {
583 client()->set_delete_on_receive_data();
584 DoStartAsyncRequest();
585 // Some data is sent in ReceiveResponse for FTP requests, so the bridge should
586 // be deleted here.
587 DoReceiveResponseFtp();
589 EXPECT_NE("", client()->received_data());
590 EXPECT_FALSE(bridge());
593 TEST_F(WebURLLoaderImplTest, FtpDeleteOnReceiveMoreData) {
594 DoStartAsyncRequest();
595 DoReceiveResponseFtp();
596 DoReceiveDataFtp();
598 // Directory listings are only parsed once the request completes, so this will
599 // cancel in DoReceiveDataFtp, before the request finishes.
600 client()->set_delete_on_receive_data();
601 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(),
602 strlen(kTestData));
603 EXPECT_FALSE(client()->did_finish());
605 EXPECT_FALSE(bridge());
608 TEST_F(WebURLLoaderImplTest, FtpDeleteOnFinish) {
609 client()->set_delete_on_finish();
610 DoStartAsyncRequest();
611 DoReceiveResponseFtp();
612 DoReceiveDataFtp();
613 DoCompleteRequest();
614 EXPECT_FALSE(bridge());
617 TEST_F(WebURLLoaderImplTest, FtpDeleteOnFail) {
618 client()->set_delete_on_fail();
619 DoStartAsyncRequest();
620 DoReceiveResponseFtp();
621 DoReceiveDataFtp();
622 DoFailRequest();
623 EXPECT_FALSE(bridge());
626 // Multipart integration tests. These are focused more on safe deletion than
627 // correct parsing of Multipart responses.
629 TEST_F(WebURLLoaderImplTest, Multipart) {
630 client()->set_expect_multipart_response();
631 DoStartAsyncRequest();
632 DoReceiveResponseMultipart();
633 DoReceiveDataMultipart();
634 DoCompleteRequest();
635 EXPECT_EQ(kTestData, client()->received_data());
636 EXPECT_FALSE(bridge()->canceled());
639 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveFirstResponse) {
640 client()->set_expect_multipart_response();
641 client()->set_delete_on_receive_response();
642 DoStartAsyncRequest();
643 DoReceiveResponseMultipart();
644 EXPECT_EQ("", client()->received_data());
645 EXPECT_FALSE(bridge());
648 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveSecondResponse) {
649 client()->set_expect_multipart_response();
650 DoStartAsyncRequest();
651 DoReceiveResponseMultipart();
652 client()->set_delete_on_receive_response();
653 DoReceiveDataMultipart();
654 EXPECT_EQ("", client()->received_data());
655 EXPECT_FALSE(bridge());
658 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveFirstData) {
659 client()->set_expect_multipart_response();
660 client()->set_delete_on_receive_data();
661 DoStartAsyncRequest();
662 DoReceiveResponseMultipart();
663 DoReceiveDataMultipart();
664 EXPECT_EQ("bl", client()->received_data());
665 EXPECT_FALSE(bridge());
668 TEST_F(WebURLLoaderImplTest, MultipartDeleteOnReceiveMoreData) {
669 client()->set_expect_multipart_response();
670 DoStartAsyncRequest();
671 DoReceiveResponseMultipart();
672 DoReceiveDataMultipart();
673 // For multipart responses, the delegate may send some data when notified
674 // of a request completing.
675 client()->set_delete_on_receive_data();
676 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(),
677 strlen(kTestData));
678 EXPECT_FALSE(client()->did_finish());
679 EXPECT_EQ(kTestData, client()->received_data());
680 EXPECT_FALSE(bridge());
683 TEST_F(WebURLLoaderImplTest, MultipartDeleteFinish) {
684 client()->set_expect_multipart_response();
685 client()->set_delete_on_finish();
686 DoStartAsyncRequest();
687 DoReceiveResponseMultipart();
688 DoReceiveDataMultipart();
689 DoCompleteRequest();
690 EXPECT_EQ(kTestData, client()->received_data());
691 EXPECT_FALSE(bridge());
694 TEST_F(WebURLLoaderImplTest, MultipartDeleteFail) {
695 client()->set_expect_multipart_response();
696 client()->set_delete_on_fail();
697 DoStartAsyncRequest();
698 DoReceiveResponseMultipart();
699 DoReceiveDataMultipart();
700 DoFailRequest();
701 EXPECT_FALSE(bridge());
704 // PlzNavigate: checks that the stream override parameters provided on
705 // navigation commit are properly applied.
706 TEST_F(WebURLLoaderImplTest, BrowserSideNavigationCommit) {
707 // Initialize the request and the stream override.
708 const GURL kStreamURL = GURL("http://bar");
709 const std::string kMimeType = "text/html";
710 blink::WebURLRequest request;
711 request.initialize();
712 request.setURL(GURL(kTestURL));
713 request.setFrameType(blink::WebURLRequest::FrameTypeTopLevel);
714 request.setRequestContext(blink::WebURLRequest::RequestContextFrame);
715 scoped_ptr<StreamOverrideParameters> stream_override(
716 new StreamOverrideParameters());
717 stream_override->stream_url = kStreamURL;
718 stream_override->response.mime_type = kMimeType;
719 RequestExtraData* extra_data = new RequestExtraData();
720 extra_data->set_stream_override(stream_override.Pass());
721 request.setExtraData(extra_data);
722 base::CommandLine::ForCurrentProcess()->AppendSwitch(
723 switches::kEnableBrowserSideNavigation);
725 client()->loader()->loadAsynchronously(request, client());
727 // The stream url should have been requestead instead of the request url.
728 ASSERT_TRUE(bridge());
729 ASSERT_TRUE(peer());
730 EXPECT_EQ(kStreamURL, bridge()->url());
732 EXPECT_FALSE(client()->did_receive_response());
733 peer()->OnReceivedResponse(content::ResourceResponseInfo());
734 EXPECT_TRUE(client()->did_receive_response());
736 // The response info should have been overriden.
737 ASSERT_FALSE(client()->response().isNull());
738 EXPECT_EQ(kMimeType, client()->response().mimeType().latin1());
740 DoReceiveData();
741 DoCompleteRequest();
742 EXPECT_FALSE(bridge()->canceled());
743 EXPECT_EQ(kTestData, client()->received_data());
746 } // namespace
747 } // namespace content