| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/image_fetcher/image_data_fetcher.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "net/base/load_flags.h" | |
| 14 #include "net/http/http_response_headers.h" | |
| 15 #include "net/http/http_status_code.h" | |
| 16 #include "net/url_request/test_url_fetcher_factory.h" | |
| 17 #include "net/url_request/url_request_status.h" | |
| 18 #include "net/url_request/url_request_test_util.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const char kImageURL[] = "http://www.example.com/image"; | |
| 25 const char kURLResponseData[] = "EncodedImageData"; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 namespace image_fetcher { | |
| 30 | |
| 31 class ImageDataFetcherTest : public testing::Test { | |
| 32 public: | |
| 33 ImageDataFetcherTest() | |
| 34 : test_request_context_getter_( | |
| 35 new net::TestURLRequestContextGetter(message_loop_.task_runner())), | |
| 36 image_data_fetcher_(test_request_context_getter_.get()) {} | |
| 37 ~ImageDataFetcherTest() override {} | |
| 38 | |
| 39 MOCK_METHOD2(OnImageDataFetched, | |
| 40 void(const std::string&, const RequestMetadata&)); | |
| 41 | |
| 42 MOCK_METHOD2(OnImageDataFetchedFailedRequest, | |
| 43 void(const std::string&, const RequestMetadata&)); | |
| 44 | |
| 45 MOCK_METHOD2(OnImageDataFetchedMultipleRequests, | |
| 46 void(const std::string&, const RequestMetadata&)); | |
| 47 | |
| 48 protected: | |
| 49 base::MessageLoop message_loop_; | |
| 50 | |
| 51 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_; | |
| 52 | |
| 53 ImageDataFetcher image_data_fetcher_; | |
| 54 | |
| 55 net::TestURLFetcherFactory fetcher_factory_; | |
| 56 | |
| 57 private: | |
| 58 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest); | |
| 59 }; | |
| 60 | |
| 61 TEST_F(ImageDataFetcherTest, FetchImageData) { | |
| 62 image_data_fetcher_.FetchImageData( | |
| 63 GURL(kImageURL), | |
| 64 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | |
| 65 base::Unretained(this))); | |
| 66 | |
| 67 RequestMetadata expected_metadata; | |
| 68 expected_metadata.mime_type = std::string("image/png"); | |
| 69 expected_metadata.http_response_code = net::HTTP_OK; | |
| 70 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), | |
| 71 expected_metadata)); | |
| 72 | |
| 73 // Get and configure the TestURLFetcher. | |
| 74 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 75 ASSERT_NE(nullptr, test_url_fetcher); | |
| 76 EXPECT_TRUE(test_url_fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SEND_COOKIES); | |
| 77 EXPECT_TRUE(test_url_fetcher->GetLoadFlags() & net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 78 EXPECT_TRUE(test_url_fetcher->GetLoadFlags() & | |
| 79 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
| 80 test_url_fetcher->set_status( | |
| 81 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | |
| 82 test_url_fetcher->SetResponseString(kURLResponseData); | |
| 83 test_url_fetcher->set_response_code(net::HTTP_OK); | |
| 84 | |
| 85 std::string raw_header = | |
| 86 "HTTP/1.1 200 OK\n" | |
| 87 "Content-type: image/png\n\n"; | |
| 88 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); | |
| 89 scoped_refptr<net::HttpResponseHeaders> headers( | |
| 90 new net::HttpResponseHeaders(raw_header)); | |
| 91 | |
| 92 test_url_fetcher->set_response_headers(headers); | |
| 93 | |
| 94 // Call the URLFetcher delegate to continue the test. | |
| 95 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 96 } | |
| 97 | |
| 98 TEST_F(ImageDataFetcherTest, FetchImageData_FromCache) { | |
| 99 image_data_fetcher_.FetchImageData( | |
| 100 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | |
| 101 base::Unretained(this))); | |
| 102 | |
| 103 RequestMetadata expected_metadata; | |
| 104 expected_metadata.mime_type = std::string("image/png"); | |
| 105 expected_metadata.http_response_code = net::HTTP_OK; | |
| 106 expected_metadata.from_http_cache = true; | |
| 107 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), | |
| 108 expected_metadata)); | |
| 109 | |
| 110 // Get and configure the TestURLFetcher. | |
| 111 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 112 ASSERT_NE(nullptr, test_url_fetcher); | |
| 113 test_url_fetcher->set_status( | |
| 114 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | |
| 115 test_url_fetcher->SetResponseString(kURLResponseData); | |
| 116 test_url_fetcher->set_response_code(net::HTTP_OK); | |
| 117 test_url_fetcher->set_was_cached(true); | |
| 118 | |
| 119 std::string raw_header = | |
| 120 "HTTP/1.1 200 OK\n" | |
| 121 "Content-type: image/png\n\n"; | |
| 122 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); | |
| 123 scoped_refptr<net::HttpResponseHeaders> headers( | |
| 124 new net::HttpResponseHeaders(raw_header)); | |
| 125 | |
| 126 test_url_fetcher->set_response_headers(headers); | |
| 127 | |
| 128 // Call the URLFetcher delegate to continue the test. | |
| 129 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 130 } | |
| 131 | |
| 132 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { | |
| 133 image_data_fetcher_.FetchImageData( | |
| 134 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | |
| 135 base::Unretained(this))); | |
| 136 | |
| 137 RequestMetadata expected_metadata; | |
| 138 expected_metadata.mime_type = std::string("image/png"); | |
| 139 expected_metadata.http_response_code = net::HTTP_NOT_FOUND; | |
| 140 // For 404, expect an empty result even though correct image data is sent. | |
| 141 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); | |
| 142 | |
| 143 // Get and configure the TestURLFetcher. | |
| 144 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 145 ASSERT_NE(nullptr, test_url_fetcher); | |
| 146 test_url_fetcher->set_status( | |
| 147 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | |
| 148 test_url_fetcher->SetResponseString(kURLResponseData); | |
| 149 | |
| 150 std::string raw_header = | |
| 151 "HTTP/1.1 404 Not Found\n" | |
| 152 "Content-type: image/png\n\n"; | |
| 153 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); | |
| 154 scoped_refptr<net::HttpResponseHeaders> headers( | |
| 155 new net::HttpResponseHeaders(raw_header)); | |
| 156 | |
| 157 test_url_fetcher->set_response_headers(headers); | |
| 158 | |
| 159 // Call the URLFetcher delegate to continue the test. | |
| 160 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 161 } | |
| 162 | |
| 163 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { | |
| 164 image_data_fetcher_.FetchImageData( | |
| 165 GURL(kImageURL), | |
| 166 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, | |
| 167 base::Unretained(this))); | |
| 168 | |
| 169 RequestMetadata expected_metadata; | |
| 170 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID; | |
| 171 EXPECT_CALL( | |
| 172 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); | |
| 173 | |
| 174 // Get and configure the TestURLFetcher. | |
| 175 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 176 ASSERT_NE(nullptr, test_url_fetcher); | |
| 177 test_url_fetcher->set_status( | |
| 178 net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
| 179 net::ERR_INVALID_URL)); | |
| 180 | |
| 181 // Call the URLFetcher delegate to continue the test. | |
| 182 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 183 } | |
| 184 | |
| 185 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) { | |
| 186 ImageDataFetcher::ImageDataFetcherCallback callback = | |
| 187 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, | |
| 188 base::Unretained(this)); | |
| 189 EXPECT_CALL(*this, OnImageDataFetchedMultipleRequests(testing::_, testing::_)) | |
| 190 .Times(2); | |
| 191 | |
| 192 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); | |
| 193 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); | |
| 194 | |
| 195 // Multiple calls to FetchImageData for the same URL will result in | |
| 196 // multiple URLFetchers being created. | |
| 197 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 198 ASSERT_NE(nullptr, test_url_fetcher); | |
| 199 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 200 | |
| 201 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); | |
| 202 ASSERT_NE(nullptr, test_url_fetcher); | |
| 203 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 204 } | |
| 205 | |
| 206 } // namespace image_fetcher | |
| OLD | NEW |