| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/image_fetcher/image_data_fetcher.h" | 5 #include "components/image_fetcher/image_data_fetcher.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_status_code.h" | |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" |
| 16 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 const char kImageURL[] = "http://www.example.com/image"; | 22 const char kImageURL[] = "http://www.example.com/image"; |
| 24 const char kURLResponseData[] = "EncodedImageData"; | 23 const char kURLResponseData[] = "EncodedImageData"; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 }; | 57 }; |
| 59 | 58 |
| 60 TEST_F(ImageDataFetcherTest, FetchImageData) { | 59 TEST_F(ImageDataFetcherTest, FetchImageData) { |
| 61 image_data_fetcher_.FetchImageData( | 60 image_data_fetcher_.FetchImageData( |
| 62 GURL(kImageURL), | 61 GURL(kImageURL), |
| 63 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 62 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, |
| 64 base::Unretained(this))); | 63 base::Unretained(this))); |
| 65 | 64 |
| 66 RequestMetadata expected_metadata; | 65 RequestMetadata expected_metadata; |
| 67 expected_metadata.mime_type = std::string("image/png"); | 66 expected_metadata.mime_type = std::string("image/png"); |
| 68 expected_metadata.response_code = net::HTTP_OK; | |
| 69 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), | 67 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), |
| 70 expected_metadata)); | 68 expected_metadata)); |
| 71 | 69 |
| 72 // Get and configure the TestURLFetcher. | 70 // Get and configure the TestURLFetcher. |
| 73 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 71 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 74 ASSERT_NE(nullptr, test_url_fetcher); | 72 ASSERT_NE(nullptr, test_url_fetcher); |
| 75 test_url_fetcher->set_status( | 73 test_url_fetcher->set_status( |
| 76 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 74 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 77 test_url_fetcher->SetResponseString(kURLResponseData); | 75 test_url_fetcher->SetResponseString(kURLResponseData); |
| 78 test_url_fetcher->set_response_code(net::HTTP_OK); | |
| 79 | 76 |
| 80 std::string raw_header = | 77 std::string raw_header = |
| 81 "HTTP/1.1 200 OK\n" | 78 "HTTP/1.1 200 OK\n" |
| 82 "Content-type: image/png\n\n"; | 79 "Content-type: image/png\n\n"; |
| 83 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); | 80 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); |
| 84 scoped_refptr<net::HttpResponseHeaders> headers( | 81 scoped_refptr<net::HttpResponseHeaders> headers( |
| 85 new net::HttpResponseHeaders(raw_header)); | 82 new net::HttpResponseHeaders(raw_header)); |
| 86 | 83 |
| 87 test_url_fetcher->set_response_headers(headers); | 84 test_url_fetcher->set_response_headers(headers); |
| 88 | 85 |
| 89 // Call the URLFetcher delegate to continue the test. | 86 // Call the URLFetcher delegate to continue the test. |
| 90 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 87 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 91 } | 88 } |
| 92 | 89 |
| 93 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { | 90 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { |
| 94 image_data_fetcher_.FetchImageData( | 91 image_data_fetcher_.FetchImageData( |
| 95 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 92 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, |
| 96 base::Unretained(this))); | 93 base::Unretained(this))); |
| 97 | 94 |
| 98 RequestMetadata expected_metadata; | 95 RequestMetadata expected_metadata; |
| 99 expected_metadata.mime_type = std::string("image/png"); | 96 expected_metadata.mime_type = std::string("image/png"); |
| 100 expected_metadata.response_code = net::HTTP_NOT_FOUND; | |
| 101 // For 404, expect an empty result even though correct image data is sent. | 97 // For 404, expect an empty result even though correct image data is sent. |
| 102 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); | 98 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); |
| 103 | 99 |
| 104 // Get and configure the TestURLFetcher. | 100 // Get and configure the TestURLFetcher. |
| 105 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 101 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 106 ASSERT_NE(nullptr, test_url_fetcher); | 102 ASSERT_NE(nullptr, test_url_fetcher); |
| 107 test_url_fetcher->set_status( | 103 test_url_fetcher->set_status( |
| 108 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 104 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 109 test_url_fetcher->SetResponseString(kURLResponseData); | 105 test_url_fetcher->SetResponseString(kURLResponseData); |
| 110 | 106 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 121 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 117 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 122 } | 118 } |
| 123 | 119 |
| 124 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { | 120 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { |
| 125 image_data_fetcher_.FetchImageData( | 121 image_data_fetcher_.FetchImageData( |
| 126 GURL(kImageURL), | 122 GURL(kImageURL), |
| 127 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, | 123 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, |
| 128 base::Unretained(this))); | 124 base::Unretained(this))); |
| 129 | 125 |
| 130 RequestMetadata expected_metadata; | 126 RequestMetadata expected_metadata; |
| 131 expected_metadata.response_code = net::URLFetcher::RESPONSE_CODE_INVALID; | |
| 132 EXPECT_CALL( | 127 EXPECT_CALL( |
| 133 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); | 128 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); |
| 134 | 129 |
| 135 // Get and configure the TestURLFetcher. | 130 // Get and configure the TestURLFetcher. |
| 136 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 131 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 137 ASSERT_NE(nullptr, test_url_fetcher); | 132 ASSERT_NE(nullptr, test_url_fetcher); |
| 138 test_url_fetcher->set_status( | 133 test_url_fetcher->set_status( |
| 139 net::URLRequestStatus(net::URLRequestStatus::FAILED, | 134 net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 140 net::ERR_INVALID_URL)); | 135 net::ERR_INVALID_URL)); |
| 141 | 136 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 158 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 153 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 159 ASSERT_NE(nullptr, test_url_fetcher); | 154 ASSERT_NE(nullptr, test_url_fetcher); |
| 160 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 155 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 161 | 156 |
| 162 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); | 157 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); |
| 163 ASSERT_NE(nullptr, test_url_fetcher); | 158 ASSERT_NE(nullptr, test_url_fetcher); |
| 164 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 159 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 165 } | 160 } |
| 166 | 161 |
| 167 } // namespace image_fetcher | 162 } // namespace image_fetcher |
| OLD | NEW |