Chromium Code Reviews| 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" | |
| 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 "chrome/browser/profiles/profile_manager.h" | |
| 12 #include "content/public/test/test_browser_thread.h" | |
| 13 #include "net/base/net_errors.h" | |
| 14 #include "net/http/http_status_code.h" | |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | |
| 16 #include "net/url_request/url_fetcher.h" | |
| 17 #include "net/url_request/url_request_status.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace digital_asset_links { | |
| 21 | |
| 22 class DigitalAssetLinksHandlerTest : public ::testing::Test { | |
| 23 public: | |
| 24 DigitalAssetLinksHandlerTest() | |
| 25 : io_thread_(content::BrowserThread::IO, &message_loop_), | |
| 26 num_invocations_(0), | |
| 27 response_(nullptr) {} | |
| 28 | |
| 29 ~DigitalAssetLinksHandlerTest() override {} | |
| 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 SendResponse(net::Error error, int response_code, bool linked) { | |
| 39 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
| 40 CHECK(fetcher); | |
|
Benoit L
2017/04/18 16:14:31
nit: ASSERT_TRUE()?
Yusuf
2017/04/18 19:54:14
Done.
| |
| 41 fetcher->set_status(net::URLRequestStatus::FromError(error)); | |
| 42 fetcher->set_response_code(response_code); | |
| 43 if (error == net::OK && response_code == net::HTTP_OK && linked) { | |
| 44 fetcher->SetResponseString( | |
| 45 "{\n" | |
|
Benoit L
2017/04/18 16:14:31
nit: What about using the new hotness, raw string
Yusuf
2017/04/18 19:54:14
Done.
And thanks for letting me learn about them!
| |
| 46 " \"linked\": true ,\n" | |
| 47 " \"maxAge\": \"40.188652381s\"" | |
| 48 "}"); | |
| 49 } else if (error == net::OK && response_code == net::HTTP_OK) { | |
| 50 fetcher->SetResponseString( | |
| 51 "{\n" | |
| 52 " \"linked\": false ,\n" | |
| 53 " \"maxAge\": \"40.188652381s\"" | |
| 54 "}"); | |
| 55 } else if (error == net::OK && response_code == net::HTTP_BAD_REQUEST) { | |
| 56 fetcher->SetResponseString( | |
| 57 "{\n" | |
| 58 " \"code\": 400 ,\n" | |
| 59 " \"message\": \"Invalid statement query received.\"" | |
| 60 " \"status\": \"INVALID_ARGUMENT\"" | |
| 61 "}"); | |
| 62 } else { | |
| 63 fetcher->SetResponseString(nullptr); | |
| 64 } | |
| 65 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 66 } | |
| 67 | |
| 68 base::MessageLoop message_loop_; | |
| 69 content::TestBrowserThread io_thread_; | |
| 70 net::TestURLFetcherFactory url_fetcher_factory_; | |
| 71 | |
| 72 int num_invocations_; | |
|
Benoit L
2017/04/18 16:14:31
Should this be reset in SetUp()?
Yusuf
2017/04/18 19:54:14
Done.
| |
| 73 std::unique_ptr<base::DictionaryValue> response_; | |
| 74 }; | |
| 75 | |
| 76 TEST_F(DigitalAssetLinksHandlerTest, ValidResponse) { | |
| 77 DigitalAssetLinksHandler handler(nullptr); | |
| 78 handler.CheckDigitalAssetLinkRelationship( | |
| 79 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
| 80 base::Unretained(this)), | |
| 81 "", "", "", ""); | |
| 82 SendResponse(net::OK, net::HTTP_OK, true); | |
| 83 | |
| 84 bool verified = false; | |
| 85 EXPECT_EQ(1, num_invocations_); | |
| 86 EXPECT_FALSE(!response_); | |
| 87 response_->GetBoolean( | |
| 88 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); | |
| 89 EXPECT_TRUE(verified); | |
| 90 } | |
| 91 | |
| 92 TEST_F(DigitalAssetLinksHandlerTest, InvalidResponse) { | |
| 93 DigitalAssetLinksHandler handler(nullptr); | |
| 94 handler.CheckDigitalAssetLinkRelationship( | |
| 95 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
| 96 base::Unretained(this)), | |
| 97 "", "", "", ""); | |
| 98 SendResponse(net::OK, net::HTTP_OK, false); | |
| 99 | |
| 100 bool verified = false; | |
| 101 EXPECT_EQ(1, num_invocations_); | |
| 102 EXPECT_FALSE(!response_); | |
|
Benoit L
2017/04/18 16:14:31
nit: Why not EXPECT_TRUE?
Yusuf
2017/04/18 19:54:14
Done.
| |
| 103 response_->GetBoolean( | |
| 104 digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); | |
| 105 EXPECT_FALSE(verified); | |
| 106 } | |
| 107 | |
| 108 TEST_F(DigitalAssetLinksHandlerTest, BadRequest) { | |
| 109 DigitalAssetLinksHandler handler(nullptr); | |
| 110 handler.CheckDigitalAssetLinkRelationship( | |
| 111 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
| 112 base::Unretained(this)), | |
| 113 "", "", "", ""); | |
| 114 SendResponse(net::OK, net::HTTP_BAD_REQUEST, true); | |
| 115 | |
| 116 EXPECT_EQ(1, num_invocations_); | |
| 117 EXPECT_TRUE(!response_); | |
| 118 } | |
| 119 | |
| 120 TEST_F(DigitalAssetLinksHandlerTest, NetworkError) { | |
| 121 DigitalAssetLinksHandler handler(nullptr); | |
| 122 handler.CheckDigitalAssetLinkRelationship( | |
| 123 base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, | |
| 124 base::Unretained(this)), | |
| 125 "", "", "", ""); | |
| 126 SendResponse(net::ERR_ABORTED, net::HTTP_OK, true); | |
| 127 | |
| 128 EXPECT_EQ(1, num_invocations_); | |
| 129 EXPECT_TRUE(!response_); | |
| 130 } | |
| 131 } // namespace digital_asset_links | |
| OLD | NEW |