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

Side by Side Diff: components/image_fetcher/core/image_data_fetcher_unittest.cc

Issue 2781473003: Add |SetImageDownloadLimit| to ImageFetcher to limit downloaded bytes (Closed)
Patch Set: Add |SetImageDownloadLimit| to ImageDataFetcher Created 3 years, 8 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
OLDNEW
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/core/image_data_fetcher.h" 5 #include "components/image_fetcher/core/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"
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 // multiple URLFetchers being created. 194 // multiple URLFetchers being created.
195 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); 195 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
196 ASSERT_NE(nullptr, test_url_fetcher); 196 ASSERT_NE(nullptr, test_url_fetcher);
197 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 197 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
198 198
199 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); 199 test_url_fetcher = fetcher_factory_.GetFetcherByID(1);
200 ASSERT_NE(nullptr, test_url_fetcher); 200 ASSERT_NE(nullptr, test_url_fetcher);
201 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); 201 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher);
202 } 202 }
203 203
204 TEST_F(ImageDataFetcherTest, FetchImageData_CancelFetchIfImageExceedsMaxSize) {
205 // In order to know whether the fetcher was canceled, it must notify about its
206 // deletion.
207 fetcher_factory_.set_remove_fetcher_on_delete(true);
208
209 int64_t max_download_bytes = 1024 * 1024;
210 image_data_fetcher_.SetImageDownloadLimit(max_download_bytes);
211 image_data_fetcher_.FetchImageData(
212 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched,
213 base::Unretained(this)));
214
215 // Fetching an oversized image will behave like any other failed request.
216 // There will be exactly one call to OnImageDataFetched containing a response
217 // code that would be possible for a completed fetch.
Marc Treib 2017/03/27 13:16:43 s/possible/impossible/ ?
fhorschig 2017/03/27 14:33:43 Absolutely.
218 RequestMetadata expected_metadata;
219 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
220 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata))
221 .Times(1);
Marc Treib 2017/03/27 13:16:42 nit: ".Times(1)" is a noop
fhorschig 2017/03/27 14:33:43 Done.
222
223 // Get and configure the TestURLFetcher.
224 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0);
225 ASSERT_NE(nullptr, test_url_fetcher);
226
227 // Create a completely valid response to make sure that the answer isn't
228 // accidentally invalid but intentionally.
229 test_url_fetcher->set_status(
230 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
231 test_url_fetcher->SetResponseString(kURLResponseData);
Marc Treib 2017/03/27 13:16:42 nit: add a comment saying that this isn't actually
fhorschig 2017/03/27 14:33:43 Do you mean this particular line? Because nothing
232 test_url_fetcher->set_response_code(net::HTTP_OK);
233
234 std::string raw_header =
235 "HTTP/1.1 200 OK\n"
236 "Content-type: image/png\n\n";
237 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0');
238 scoped_refptr<net::HttpResponseHeaders> headers(
239 new net::HttpResponseHeaders(raw_header));
240 test_url_fetcher->set_response_headers(headers);
241
Marc Treib 2017/03/27 13:16:42 Maybe first call OnURLFetchDownloadProgress with a
fhorschig 2017/03/27 14:33:43 Good idea. I introduced some edge cases.
242 // Call the URLFetcher delegate to continue the test.
Marc Treib 2017/03/27 13:16:43 nit: IMO this comment isn't really helpful.
fhorschig 2017/03/27 14:33:43 Gone.
243 test_url_fetcher->delegate()->OnURLFetchDownloadProgress(
244 test_url_fetcher,
245 /*current=*/2 * max_download_bytes, // Bytes received up to the call.
246 /*total=*/-1, // not determined
247 /*current_network_bytes=*/24 * 1024); // not relevant
248 // The fetcher has to be deleted for the request to be canceled.
Marc Treib 2017/03/27 13:16:42 "The URL fetch should have been canceled" ?
fhorschig 2017/03/27 14:33:43 Done.
249 EXPECT_EQ(nullptr, fetcher_factory_.GetFetcherByID(0));
250 }
251
204 } // namespace image_fetcher 252 } // namespace image_fetcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698