OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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" | |
nyquist
2017/04/19 06:35:57
Nit: Empty line after this?
Yusuf
2017/04/26 00:51:37
Done.
| |
6 #include "base/bind.h" | |
7 #include "base/command_line.h" | |
8 #include "base/json/json_reader.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/values.h" | |
11 #include "content/public/test/test_browser_thread.h" | |
12 #include "net/base/net_errors.h" | |
13 #include "net/http/http_status_code.h" | |
14 #include "net/url_request/test_url_fetcher_factory.h" | |
15 #include "net/url_request/url_fetcher.h" | |
16 #include "net/url_request/url_request_status.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace digital_asset_links { | |
20 | |
21 class DigitalAssetLinksHandlerTest : public ::testing::Test { | |
nyquist
2017/04/19 06:35:57
Nit: Should this class be in an anonymous namespac
Yusuf
2017/04/26 00:51:37
Done.
| |
22 public: | |
23 DigitalAssetLinksHandlerTest() | |
24 : io_thread_(content::BrowserThread::IO, &message_loop_), | |
25 num_invocations_(0), | |
26 response_(nullptr) {} | |
27 | |
28 ~DigitalAssetLinksHandlerTest() override {} | |
nyquist
2017/04/19 06:35:57
Nit: = default?
Yusuf
2017/04/26 00:51:37
Done.
| |
29 | |
30 void OnRelationshipCheckComplete( | |
31 std::unique_ptr<base::DictionaryValue> response) { | |
32 ++num_invocations_; | |
33 response_ = std::move(response); | |
34 } | |
35 | |
36 protected: | |
37 void SetUp() override { num_invocations_ = 0; } | |
38 | |
39 void SendResponse(net::Error error, int response_code, bool linked) { | |
40 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
41 ASSERT_TRUE(fetcher); | |
42 fetcher->set_status(net::URLRequestStatus::FromError(error)); | |
43 fetcher->set_response_code(response_code); | |
44 if (error == net::OK && response_code == net::HTTP_OK && linked) { | |
45 fetcher->SetResponseString( | |
46 R"({ | |
47 "linked": true , | |
48 "maxAge": "40.188652381s" | |
49 })"); | |
50 } else if (error == net::OK && response_code == net::HTTP_OK) { | |
51 fetcher->SetResponseString( | |
52 R"({ | |
53 "linked": false , | |
54 "maxAge": "40.188652381s" | |
55 })"); | |
56 } else if (error == net::OK && response_code == net::HTTP_BAD_REQUEST) { | |
57 fetcher->SetResponseString( | |
58 R"({ | |
59 "code": 400 , | |
60 "message": "Invalid statement query received." | |
61 "status": "INVALID_ARGUMENT" | |
62 })"); | |
63 } else { | |
64 fetcher->SetResponseString(""); | |
65 } | |
66 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
67 } | |
68 | |
69 base::MessageLoop message_loop_; | |
nyquist
2017/04/19 06:35:57
Nit: Could some of these be private?
Yusuf
2017/04/26 00:51:37
Done.
| |
70 content::TestBrowserThread io_thread_; | |
71 net::TestURLFetcherFactory url_fetcher_factory_; | |
72 | |
73 int num_invocations_; | |
74 std::unique_ptr<base::DictionaryValue> response_; | |
75 }; | |
nyquist
2017/04/19 06:35:57
Nit:
private:
DISALLOW_COPY_AND_ASSIGN(DigitalA
Yusuf
2017/04/26 00:51:37
Done.
| |
76 | |
77 TEST_F(DigitalAssetLinksHandlerTest, ValidResponse) { | |
78 DigitalAssetLinksHandler handler(nullptr); | |
79 handler.CheckDigitalAssetLinkRelationship( | |
80 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
81 base::Unretained(this)), | |
82 "", "", "", ""); | |
83 SendResponse(net::OK, net::HTTP_OK, true); | |
84 | |
85 bool verified = false; | |
86 EXPECT_EQ(1, num_invocations_); | |
87 EXPECT_TRUE(response_); | |
88 response_->GetBoolean( | |
89 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); | |
90 EXPECT_TRUE(verified); | |
91 } | |
92 | |
93 TEST_F(DigitalAssetLinksHandlerTest, InvalidResponse) { | |
nyquist
2017/04/19 06:35:57
Nit: Is the response invalid, or is it just that t
Yusuf
2017/04/26 00:51:37
Renamed to Positive/NegativeResponse.
| |
94 DigitalAssetLinksHandler handler(nullptr); | |
95 handler.CheckDigitalAssetLinkRelationship( | |
96 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
97 base::Unretained(this)), | |
98 "", "", "", ""); | |
99 SendResponse(net::OK, net::HTTP_OK, false); | |
100 | |
101 bool verified = false; | |
102 EXPECT_EQ(1, num_invocations_); | |
103 EXPECT_TRUE(response_); | |
104 response_->GetBoolean( | |
105 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); | |
106 EXPECT_FALSE(verified); | |
107 } | |
108 | |
109 TEST_F(DigitalAssetLinksHandlerTest, BadRequest) { | |
110 DigitalAssetLinksHandler handler(nullptr); | |
111 handler.CheckDigitalAssetLinkRelationship( | |
112 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
113 base::Unretained(this)), | |
114 "", "", "", ""); | |
115 SendResponse(net::OK, net::HTTP_BAD_REQUEST, true); | |
116 | |
117 EXPECT_EQ(1, num_invocations_); | |
118 EXPECT_FALSE(response_); | |
119 } | |
120 | |
121 TEST_F(DigitalAssetLinksHandlerTest, NetworkError) { | |
122 DigitalAssetLinksHandler handler(nullptr); | |
123 handler.CheckDigitalAssetLinkRelationship( | |
124 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
125 base::Unretained(this)), | |
126 "", "", "", ""); | |
127 SendResponse(net::ERR_ABORTED, net::HTTP_OK, true); | |
128 | |
129 EXPECT_EQ(1, num_invocations_); | |
130 EXPECT_FALSE(response_); | |
131 } | |
132 } // namespace digital_asset_links | |
OLD | NEW |