| OLD | NEW |
| 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 <string.h> | 7 #include <string.h> |
| 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "content/child/request_extra_data.h" | 16 #include "content/child/request_extra_data.h" |
| 16 #include "content/child/request_info.h" | 17 #include "content/child/request_info.h" |
| 17 #include "content/child/resource_dispatcher.h" | 18 #include "content/child/resource_dispatcher.h" |
| 18 #include "content/public/child/request_peer.h" | 19 #include "content/public/child/request_peer.h" |
| 19 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
| 20 #include "content/public/common/resource_response_info.h" | 21 #include "content/public/common/resource_response_info.h" |
| 21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 22 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 23 #include "net/http/http_util.h" | 24 #include "net/http/http_util.h" |
| 24 #include "net/url_request/redirect_info.h" | 25 #include "net/url_request/redirect_info.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/WebKit/public/platform/WebString.h" | 27 #include "third_party/WebKit/public/platform/WebString.h" |
| 27 #include "third_party/WebKit/public/platform/WebURLError.h" | 28 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 28 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | 29 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
| 29 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 30 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 30 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 31 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 31 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 32 | 33 |
| 33 namespace content { | 34 namespace content { |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 37 class FixedReceivedData final : public RequestPeer::ReceivedData { |
| 38 public: |
| 39 FixedReceivedData(const char* payload, int length, int encoded_length) |
| 40 : data_(&payload[0], &payload[length]), encoded_length_(encoded_length) {} |
| 41 ~FixedReceivedData() override {} |
| 42 |
| 43 const char* payload() const override { |
| 44 // TODO(yhirano): Use |data_.data()| when we can use c++11. |
| 45 return data_.empty() ? nullptr : &data_[0]; |
| 46 } |
| 47 int length() const override { return data_.size(); } |
| 48 int encoded_length() const override { return encoded_length_; } |
| 49 |
| 50 private: |
| 51 const std::vector<char> data_; |
| 52 const int encoded_length_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(FixedReceivedData); |
| 55 }; |
| 56 |
| 36 const char kTestURL[] = "http://foo"; | 57 const char kTestURL[] = "http://foo"; |
| 37 const char kTestData[] = "blah!"; | 58 const char kTestData[] = "blah!"; |
| 38 | 59 |
| 39 const char kFtpDirMimeType[] = "text/vnd.chromium.ftp-dir"; | 60 const char kFtpDirMimeType[] = "text/vnd.chromium.ftp-dir"; |
| 40 // Simple FTP directory listing. Tests are not concerned with correct parsing, | 61 // Simple FTP directory listing. Tests are not concerned with correct parsing, |
| 41 // but rather correct cleanup when deleted while parsing. Important details of | 62 // but rather correct cleanup when deleted while parsing. Important details of |
| 42 // this list are that it contains more than one entry that are not "." or "..". | 63 // this list are that it contains more than one entry that are not "." or "..". |
| 43 const char kFtpDirListing[] = | 64 const char kFtpDirListing[] = |
| 44 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 goat\n" | 65 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 goat\n" |
| 45 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 hat"; | 66 "drwxr-xr-x 3 ftp ftp 4096 May 15 18:11 hat"; |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 299 |
| 279 void DoReceiveResponse() { | 300 void DoReceiveResponse() { |
| 280 EXPECT_FALSE(client()->did_receive_response()); | 301 EXPECT_FALSE(client()->did_receive_response()); |
| 281 peer()->OnReceivedResponse(content::ResourceResponseInfo()); | 302 peer()->OnReceivedResponse(content::ResourceResponseInfo()); |
| 282 EXPECT_TRUE(client()->did_receive_response()); | 303 EXPECT_TRUE(client()->did_receive_response()); |
| 283 } | 304 } |
| 284 | 305 |
| 285 // Assumes it is called only once for a request. | 306 // Assumes it is called only once for a request. |
| 286 void DoReceiveData() { | 307 void DoReceiveData() { |
| 287 EXPECT_EQ("", client()->received_data()); | 308 EXPECT_EQ("", client()->received_data()); |
| 288 peer()->OnReceivedData(kTestData, strlen(kTestData), strlen(kTestData)); | 309 peer()->OnReceivedData(make_scoped_ptr(new FixedReceivedData( |
| 310 kTestData, strlen(kTestData), strlen(kTestData)))); |
| 289 EXPECT_EQ(kTestData, client()->received_data()); | 311 EXPECT_EQ(kTestData, client()->received_data()); |
| 290 } | 312 } |
| 291 | 313 |
| 292 void DoCompleteRequest() { | 314 void DoCompleteRequest() { |
| 293 EXPECT_FALSE(client()->did_finish()); | 315 EXPECT_FALSE(client()->did_finish()); |
| 294 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(), | 316 peer()->OnCompletedRequest(net::OK, false, false, "", base::TimeTicks(), |
| 295 strlen(kTestData)); | 317 strlen(kTestData)); |
| 296 EXPECT_TRUE(client()->did_finish()); | 318 EXPECT_TRUE(client()->did_finish()); |
| 297 // There should be no error. | 319 // There should be no error. |
| 298 EXPECT_EQ(net::OK, client()->error().reason); | 320 EXPECT_EQ(net::OK, client()->error().reason); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 310 | 332 |
| 311 void DoReceiveResponseFtp() { | 333 void DoReceiveResponseFtp() { |
| 312 EXPECT_FALSE(client()->did_receive_response()); | 334 EXPECT_FALSE(client()->did_receive_response()); |
| 313 content::ResourceResponseInfo response_info; | 335 content::ResourceResponseInfo response_info; |
| 314 response_info.mime_type = kFtpDirMimeType; | 336 response_info.mime_type = kFtpDirMimeType; |
| 315 peer()->OnReceivedResponse(response_info); | 337 peer()->OnReceivedResponse(response_info); |
| 316 EXPECT_TRUE(client()->did_receive_response()); | 338 EXPECT_TRUE(client()->did_receive_response()); |
| 317 } | 339 } |
| 318 | 340 |
| 319 void DoReceiveDataFtp() { | 341 void DoReceiveDataFtp() { |
| 320 peer()->OnReceivedData(kFtpDirListing, strlen(kFtpDirListing), | 342 peer()->OnReceivedData(make_scoped_ptr(new FixedReceivedData( |
| 321 strlen(kFtpDirListing)); | 343 kFtpDirListing, strlen(kFtpDirListing), strlen(kFtpDirListing)))); |
| 322 // The FTP delegate should modify the data the client sees. | 344 // The FTP delegate should modify the data the client sees. |
| 323 EXPECT_NE(kFtpDirListing, client()->received_data()); | 345 EXPECT_NE(kFtpDirListing, client()->received_data()); |
| 324 } | 346 } |
| 325 | 347 |
| 326 void DoReceiveResponseMultipart() { | 348 void DoReceiveResponseMultipart() { |
| 327 EXPECT_FALSE(client()->did_receive_response()); | 349 EXPECT_FALSE(client()->did_receive_response()); |
| 328 content::ResourceResponseInfo response_info; | 350 content::ResourceResponseInfo response_info; |
| 329 response_info.headers = new net::HttpResponseHeaders( | 351 response_info.headers = new net::HttpResponseHeaders( |
| 330 net::HttpUtil::AssembleRawHeaders(kMultipartResponseHeaders, | 352 net::HttpUtil::AssembleRawHeaders(kMultipartResponseHeaders, |
| 331 strlen(kMultipartResponseHeaders))); | 353 strlen(kMultipartResponseHeaders))); |
| 332 response_info.mime_type = kMultipartResponseMimeType; | 354 response_info.mime_type = kMultipartResponseMimeType; |
| 333 peer()->OnReceivedResponse(response_info); | 355 peer()->OnReceivedResponse(response_info); |
| 334 EXPECT_TRUE(client()->did_receive_response()); | 356 EXPECT_TRUE(client()->did_receive_response()); |
| 335 } | 357 } |
| 336 | 358 |
| 337 void DoReceiveDataMultipart() { | 359 void DoReceiveDataMultipart() { |
| 338 peer()->OnReceivedData(kMultipartResponse, strlen(kMultipartResponse), | 360 peer()->OnReceivedData(make_scoped_ptr( |
| 339 strlen(kMultipartResponse)); | 361 new FixedReceivedData(kMultipartResponse, strlen(kMultipartResponse), |
| 362 strlen(kMultipartResponse)))); |
| 340 // Multipart delegate should modify the data the client sees. | 363 // Multipart delegate should modify the data the client sees. |
| 341 EXPECT_NE(kMultipartResponse, client()->received_data()); | 364 EXPECT_NE(kMultipartResponse, client()->received_data()); |
| 342 } | 365 } |
| 343 | 366 |
| 344 TestWebURLLoaderClient* client() { return &client_; } | 367 TestWebURLLoaderClient* client() { return &client_; } |
| 345 TestResourceDispatcher* dispatcher() { return &dispatcher_; } | 368 TestResourceDispatcher* dispatcher() { return &dispatcher_; } |
| 346 RequestPeer* peer() { return dispatcher()->peer(); } | 369 RequestPeer* peer() { return dispatcher()->peer(); } |
| 347 base::MessageLoop* message_loop() { return &message_loop_; } | 370 base::MessageLoop* message_loop() { return &message_loop_; } |
| 348 | 371 |
| 349 private: | 372 private: |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 EXPECT_EQ(kMimeType, client()->response().mimeType().latin1()); | 702 EXPECT_EQ(kMimeType, client()->response().mimeType().latin1()); |
| 680 | 703 |
| 681 DoReceiveData(); | 704 DoReceiveData(); |
| 682 DoCompleteRequest(); | 705 DoCompleteRequest(); |
| 683 EXPECT_FALSE(dispatcher()->canceled()); | 706 EXPECT_FALSE(dispatcher()->canceled()); |
| 684 EXPECT_EQ(kTestData, client()->received_data()); | 707 EXPECT_EQ(kTestData, client()->received_data()); |
| 685 } | 708 } |
| 686 | 709 |
| 687 } // namespace | 710 } // namespace |
| 688 } // namespace content | 711 } // namespace content |
| OLD | NEW |