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

Side by Side Diff: components/password_manager/core/browser/site_affiliation/asset_link_retriever_unittest.cc

Issue 2944533003: Implement AssetLinkRetriever for Asset Links in Chrome. (Closed)
Patch Set: comments Created 3 years, 6 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
« no previous file with comments | « components/password_manager/core/browser/site_affiliation/asset_link_retriever.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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/password_manager/core/browser/site_affiliation/asset_link_r etriever.h"
6
7 #include <memory>
8
9 #include "base/test/scoped_task_environment.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace password_manager {
15 namespace {
16
17 constexpr char kAssetLinkFile[] =
18 "https://example.com/.well-known/assetlinks.json";
19
20 // A test URL fecther which is very cautious about not following the redirects.
21 class AssetLinksTestFetcher : public net::FakeURLFetcher {
22 public:
23 using FakeURLFetcher::FakeURLFetcher;
24
25 ~AssetLinksTestFetcher() override { EXPECT_TRUE(stop_on_redirect_); }
26
27 // FakeURLFetcher:
28 void SetStopOnRedirect(bool stop_on_redirect) override {
29 FakeURLFetcher::SetStopOnRedirect(stop_on_redirect);
30 stop_on_redirect_ = stop_on_redirect;
31 }
32
33 private:
34 bool stop_on_redirect_ = false;
35
36 DISALLOW_COPY_AND_ASSIGN(AssetLinksTestFetcher);
37 };
38
39 std::unique_ptr<net::FakeURLFetcher> AssetLinksFetcherCreator(
40 const GURL& url,
41 net::URLFetcherDelegate* delegate,
42 const std::string& response_data,
43 net::HttpStatusCode response_code,
44 net::URLRequestStatus::Status status) {
45 return base::MakeUnique<AssetLinksTestFetcher>(url, delegate, response_data,
46 response_code, status);
47 }
48
49 class AssetLinkRetrieverTest : public testing::Test {
50 public:
51 AssetLinkRetrieverTest();
52
53 net::TestURLRequestContextGetter* request_context() const {
54 return request_context_.get();
55 }
56
57 net::FakeURLFetcherFactory& factory() { return factory_; }
58
59 void RunUntilIdle() { scoped_task_environment_.RunUntilIdle(); }
60
61 private:
62 base::test::ScopedTaskEnvironment scoped_task_environment_;
63 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
64 net::FakeURLFetcherFactory factory_;
65
66 DISALLOW_COPY_AND_ASSIGN(AssetLinkRetrieverTest);
67 };
68
69 AssetLinkRetrieverTest::AssetLinkRetrieverTest()
70 : request_context_(new net::TestURLRequestContextGetter(
71 base::ThreadTaskRunnerHandle::Get())),
72 factory_(nullptr, base::Bind(&AssetLinksFetcherCreator)) {}
73
74 // Load the asset links resource that isn't available.
75 TEST_F(AssetLinkRetrieverTest, LoadNonExistent) {
76 scoped_refptr<AssetLinkRetriever> asset_link_retriever =
77 base::MakeRefCounted<AssetLinkRetriever>(GURL(kAssetLinkFile));
78 EXPECT_EQ(AssetLinkRetriever::State::INACTIVE, asset_link_retriever->state());
79
80 factory().SetFakeResponse(GURL(kAssetLinkFile), std::string(),
81 net::HTTP_NOT_FOUND, net::URLRequestStatus::FAILED);
82 asset_link_retriever->Start(request_context());
83 EXPECT_EQ(AssetLinkRetriever::State::NETWORK_REQUEST,
84 asset_link_retriever->state());
85
86 RunUntilIdle();
87 EXPECT_EQ(AssetLinkRetriever::State::FINISHED, asset_link_retriever->state());
88 EXPECT_TRUE(asset_link_retriever->error());
89 }
90
91 // Load the asset links resource that replies with redirect. It should be
92 // treated as an error.
93 TEST_F(AssetLinkRetrieverTest, LoadRedirect) {
94 scoped_refptr<AssetLinkRetriever> asset_link_retriever =
95 base::MakeRefCounted<AssetLinkRetriever>(GURL(kAssetLinkFile));
96 EXPECT_EQ(AssetLinkRetriever::State::INACTIVE, asset_link_retriever->state());
97
98 factory().SetFakeResponse(GURL(kAssetLinkFile), std::string(),
99 net::HTTP_FOUND, net::URLRequestStatus::CANCELED);
100 asset_link_retriever->Start(request_context());
101
102 RunUntilIdle();
103 EXPECT_EQ(AssetLinkRetriever::State::FINISHED, asset_link_retriever->state());
104 EXPECT_TRUE(asset_link_retriever->error());
105 }
106
107 } // namespace
108 } // namespace password_manager
OLDNEW
« no previous file with comments | « components/password_manager/core/browser/site_affiliation/asset_link_retriever.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698