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

Side by Side Diff: chrome/browser/android/digital_asset_links/digital_asset_links_handler_unittest.cc

Issue 2767333006: Add Digital Asset Links verification for postMessage API (Closed)
Patch Set: destructor Created 3 years, 7 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
(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 "chrome/browser/android/digital_asset_links/digital_asset_links_handler .h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/json/json_reader.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/values.h"
13 #include "components/safe_json/testing_json_parser.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_status_code.h"
17 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_request_status.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace digital_asset_links {
23 namespace {
24
25 class DigitalAssetLinksHandlerTest : public ::testing::Test {
26 public:
27 DigitalAssetLinksHandlerTest()
28 : num_invocations_(0),
29 response_(nullptr),
30 io_thread_(content::BrowserThread::IO, &message_loop_) {}
31
32 void OnRelationshipCheckComplete(
33 std::unique_ptr<base::DictionaryValue> response) {
34 ++num_invocations_;
35 response_ = std::move(response);
36 }
37
38 protected:
39 void SetUp() override { num_invocations_ = 0; }
40
41 void SendResponse(net::Error error, int response_code, bool linked) {
42 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
43 ASSERT_TRUE(fetcher);
44 fetcher->set_status(net::URLRequestStatus::FromError(error));
45 fetcher->set_response_code(response_code);
46 if (error == net::OK && response_code == net::HTTP_OK && linked) {
47 fetcher->SetResponseString(
48 R"({
49 "linked": true ,
50 "maxAge": "40.188652381s"
51 })");
52 } else if (error == net::OK && response_code == net::HTTP_OK) {
53 fetcher->SetResponseString(
54 R"({
55 "linked": false ,
56 "maxAge": "40.188652381s"
57 })");
58 } else if (error == net::OK && response_code == net::HTTP_BAD_REQUEST) {
59 fetcher->SetResponseString(
60 R"({
61 "code": 400 ,
62 "message": "Invalid statement query received."
63 "status": "INVALID_ARGUMENT"
64 })");
65 } else {
66 fetcher->SetResponseString("");
67 }
68 fetcher->delegate()->OnURLFetchComplete(fetcher);
69 base::RunLoop().RunUntilIdle();
70 }
71
72 int num_invocations_;
73 std::unique_ptr<base::DictionaryValue> response_;
74
75 private:
76 base::MessageLoop message_loop_;
77 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_;
78 content::TestBrowserThread io_thread_;
79 net::TestURLFetcherFactory url_fetcher_factory_;
80
81 DISALLOW_COPY_AND_ASSIGN(DigitalAssetLinksHandlerTest);
82 };
83 } // namespace
84
85 TEST_F(DigitalAssetLinksHandlerTest, PositiveResponse) {
86 DigitalAssetLinksHandler handler(nullptr);
87 handler.CheckDigitalAssetLinkRelationship(
88 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete,
89 base::Unretained(this)),
90 "", "", "", "");
91 SendResponse(net::OK, net::HTTP_OK, true);
92
93 bool verified = false;
94 EXPECT_EQ(1, num_invocations_);
95 EXPECT_TRUE(response_);
96 response_->GetBoolean(
97 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified);
98 EXPECT_TRUE(verified);
99 }
100
101 TEST_F(DigitalAssetLinksHandlerTest, NegativeResponse) {
102 DigitalAssetLinksHandler handler(nullptr);
103 handler.CheckDigitalAssetLinkRelationship(
104 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete,
105 base::Unretained(this)),
106 "", "", "", "");
107 SendResponse(net::OK, net::HTTP_OK, false);
108
109 bool verified = false;
110 EXPECT_EQ(1, num_invocations_);
111 EXPECT_TRUE(response_);
112 response_->GetBoolean(
113 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified);
114 EXPECT_FALSE(verified);
115 }
116
117 TEST_F(DigitalAssetLinksHandlerTest, BadRequest) {
118 DigitalAssetLinksHandler handler(nullptr);
119 handler.CheckDigitalAssetLinkRelationship(
120 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete,
121 base::Unretained(this)),
122 "", "", "", "");
123 SendResponse(net::OK, net::HTTP_BAD_REQUEST, true);
124
125 EXPECT_EQ(1, num_invocations_);
126 EXPECT_FALSE(response_);
127 }
128
129 TEST_F(DigitalAssetLinksHandlerTest, NetworkError) {
130 DigitalAssetLinksHandler handler(nullptr);
131 handler.CheckDigitalAssetLinkRelationship(
132 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete,
133 base::Unretained(this)),
134 "", "", "", "");
135 SendResponse(net::ERR_ABORTED, net::HTTP_OK, true);
136
137 EXPECT_EQ(1, num_invocations_);
138 EXPECT_FALSE(response_);
139 }
140 } // namespace digital_asset_links
OLDNEW
« no previous file with comments | « chrome/browser/android/digital_asset_links/digital_asset_links_handler.cc ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698