OLD | NEW |
---|---|
(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 { | |
23 | |
24 class DigitalAssetLinksHandlerTest : public ::testing::Test { | |
25 public: | |
26 DigitalAssetLinksHandlerTest() | |
27 : num_invocations_(0), | |
28 response_(nullptr), | |
29 io_thread_(content::BrowserThread::IO, &message_loop_) {} | |
30 | |
31 void OnRelationshipCheckComplete( | |
32 std::unique_ptr<base::DictionaryValue> response) { | |
33 ++num_invocations_; | |
34 response_ = std::move(response); | |
35 } | |
36 | |
37 protected: | |
38 void SetUp() override { num_invocations_ = 0; } | |
39 | |
40 void SendResponse(net::Error error, int response_code, bool linked) { | |
41 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
42 ASSERT_TRUE(fetcher); | |
43 fetcher->set_status(net::URLRequestStatus::FromError(error)); | |
44 fetcher->set_response_code(response_code); | |
45 if (error == net::OK && response_code == net::HTTP_OK && linked) { | |
46 fetcher->SetResponseString( | |
47 R"({ | |
48 "linked": true , | |
49 "maxAge": "40.188652381s" | |
50 })"); | |
51 } else if (error == net::OK && response_code == net::HTTP_OK) { | |
52 fetcher->SetResponseString( | |
53 R"({ | |
54 "linked": false , | |
55 "maxAge": "40.188652381s" | |
56 })"); | |
57 } else if (error == net::OK && response_code == net::HTTP_BAD_REQUEST) { | |
58 fetcher->SetResponseString( | |
59 R"({ | |
60 "code": 400 , | |
61 "message": "Invalid statement query received." | |
62 "status": "INVALID_ARGUMENT" | |
63 })"); | |
64 } else { | |
65 fetcher->SetResponseString(""); | |
66 } | |
67 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
68 base::RunLoop().RunUntilIdle(); | |
69 } | |
70 | |
71 int num_invocations_; | |
72 std::unique_ptr<base::DictionaryValue> response_; | |
73 | |
74 private: | |
75 base::MessageLoop message_loop_; | |
76 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; | |
77 content::TestBrowserThread io_thread_; | |
78 net::TestURLFetcherFactory url_fetcher_factory_; | |
79 DISALLOW_COPY_AND_ASSIGN(DigitalAssetLinksHandlerTest); | |
nyquist
2017/04/27 04:38:25
Nit: I think we usually have an empty line before
Yusuf
2017/04/27 17:38:56
Done.
| |
80 }; | |
81 | |
82 TEST_F(DigitalAssetLinksHandlerTest, PositiveResponse) { | |
nyquist
2017/04/27 04:38:25
Nit: I think I'd end the anonymous namespace right
Yusuf
2017/04/27 17:38:56
Done.
| |
83 digital_asset_links::DigitalAssetLinksHandler handler(nullptr); | |
84 handler.CheckDigitalAssetLinkRelationship( | |
85 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
86 base::Unretained(this)), | |
87 "", "", "", ""); | |
88 SendResponse(net::OK, net::HTTP_OK, true); | |
89 | |
90 bool verified = false; | |
91 EXPECT_EQ(1, num_invocations_); | |
92 EXPECT_TRUE(response_); | |
93 response_->GetBoolean( | |
94 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); | |
95 EXPECT_TRUE(verified); | |
96 } | |
97 | |
98 TEST_F(DigitalAssetLinksHandlerTest, NegativeResponse) { | |
99 digital_asset_links::DigitalAssetLinksHandler handler(nullptr); | |
100 handler.CheckDigitalAssetLinkRelationship( | |
101 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
102 base::Unretained(this)), | |
103 "", "", "", ""); | |
104 SendResponse(net::OK, net::HTTP_OK, false); | |
105 | |
106 bool verified = false; | |
107 EXPECT_EQ(1, num_invocations_); | |
108 EXPECT_TRUE(response_); | |
109 response_->GetBoolean( | |
110 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); | |
111 EXPECT_FALSE(verified); | |
112 } | |
113 | |
114 TEST_F(DigitalAssetLinksHandlerTest, BadRequest) { | |
115 digital_asset_links::DigitalAssetLinksHandler handler(nullptr); | |
116 handler.CheckDigitalAssetLinkRelationship( | |
117 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
118 base::Unretained(this)), | |
119 "", "", "", ""); | |
120 SendResponse(net::OK, net::HTTP_BAD_REQUEST, true); | |
121 | |
122 EXPECT_EQ(1, num_invocations_); | |
123 EXPECT_FALSE(response_); | |
124 } | |
125 | |
126 TEST_F(DigitalAssetLinksHandlerTest, NetworkError) { | |
127 digital_asset_links::DigitalAssetLinksHandler handler(nullptr); | |
128 handler.CheckDigitalAssetLinkRelationship( | |
129 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
130 base::Unretained(this)), | |
131 "", "", "", ""); | |
132 SendResponse(net::ERR_ABORTED, net::HTTP_OK, true); | |
133 | |
134 EXPECT_EQ(1, num_invocations_); | |
135 EXPECT_FALSE(response_); | |
136 } | |
137 } // namespace | |
OLD | NEW |