Chromium Code Reviews| Index: chrome/browser/android/digital_asset_links/digital_asset_links_handler_unittest.cc |
| diff --git a/chrome/browser/android/digital_asset_links/digital_asset_links_handler_unittest.cc b/chrome/browser/android/digital_asset_links/digital_asset_links_handler_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84889502633ef7816913021eff8d5c4ad479a900 |
| --- /dev/null |
| +++ b/chrome/browser/android/digital_asset_links/digital_asset_links_handler_unittest.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/digital_asset_links/digital_asset_links_handler.h" |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/test_url_fetcher_factory.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_request_status.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace digital_asset_links { |
| + |
| +class DigitalAssetLinksHandlerTest : public ::testing::Test { |
| + public: |
| + DigitalAssetLinksHandlerTest() |
| + : io_thread_(content::BrowserThread::IO, &message_loop_), |
| + num_invocations_(0), |
| + response_(nullptr) {} |
| + |
| + ~DigitalAssetLinksHandlerTest() override {} |
| + |
| + void OnRelationshipCheckComplete( |
| + std::unique_ptr<base::DictionaryValue> response) { |
| + ++num_invocations_; |
| + response_ = std::move(response); |
| + } |
| + |
| + protected: |
| + void SendResponse(net::Error error, int response_code, bool linked) { |
| + net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| + CHECK(fetcher); |
|
Benoit L
2017/04/18 16:14:31
nit: ASSERT_TRUE()?
Yusuf
2017/04/18 19:54:14
Done.
|
| + fetcher->set_status(net::URLRequestStatus::FromError(error)); |
| + fetcher->set_response_code(response_code); |
| + if (error == net::OK && response_code == net::HTTP_OK && linked) { |
| + fetcher->SetResponseString( |
| + "{\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!
|
| + " \"linked\": true ,\n" |
| + " \"maxAge\": \"40.188652381s\"" |
| + "}"); |
| + } else if (error == net::OK && response_code == net::HTTP_OK) { |
| + fetcher->SetResponseString( |
| + "{\n" |
| + " \"linked\": false ,\n" |
| + " \"maxAge\": \"40.188652381s\"" |
| + "}"); |
| + } else if (error == net::OK && response_code == net::HTTP_BAD_REQUEST) { |
| + fetcher->SetResponseString( |
| + "{\n" |
| + " \"code\": 400 ,\n" |
| + " \"message\": \"Invalid statement query received.\"" |
| + " \"status\": \"INVALID_ARGUMENT\"" |
| + "}"); |
| + } else { |
| + fetcher->SetResponseString(nullptr); |
| + } |
| + fetcher->delegate()->OnURLFetchComplete(fetcher); |
| + } |
| + |
| + base::MessageLoop message_loop_; |
| + content::TestBrowserThread io_thread_; |
| + net::TestURLFetcherFactory url_fetcher_factory_; |
| + |
| + 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.
|
| + std::unique_ptr<base::DictionaryValue> response_; |
| +}; |
| + |
| +TEST_F(DigitalAssetLinksHandlerTest, ValidResponse) { |
| + DigitalAssetLinksHandler handler(nullptr); |
| + handler.CheckDigitalAssetLinkRelationship( |
| + base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, |
| + base::Unretained(this)), |
| + "", "", "", ""); |
| + SendResponse(net::OK, net::HTTP_OK, true); |
| + |
| + bool verified = false; |
| + EXPECT_EQ(1, num_invocations_); |
| + EXPECT_FALSE(!response_); |
| + response_->GetBoolean( |
| + digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); |
| + EXPECT_TRUE(verified); |
| +} |
| + |
| +TEST_F(DigitalAssetLinksHandlerTest, InvalidResponse) { |
| + DigitalAssetLinksHandler handler(nullptr); |
| + handler.CheckDigitalAssetLinkRelationship( |
| + base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, |
| + base::Unretained(this)), |
| + "", "", "", ""); |
| + SendResponse(net::OK, net::HTTP_OK, false); |
| + |
| + bool verified = false; |
| + EXPECT_EQ(1, num_invocations_); |
| + EXPECT_FALSE(!response_); |
|
Benoit L
2017/04/18 16:14:31
nit: Why not EXPECT_TRUE?
Yusuf
2017/04/18 19:54:14
Done.
|
| + response_->GetBoolean( |
| + digital_asset_links::kDigitalAssetLinksCheckResponseKeyLinked, &verified); |
| + EXPECT_FALSE(verified); |
| +} |
| + |
| +TEST_F(DigitalAssetLinksHandlerTest, BadRequest) { |
| + DigitalAssetLinksHandler handler(nullptr); |
| + handler.CheckDigitalAssetLinkRelationship( |
| + base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, |
| + base::Unretained(this)), |
| + "", "", "", ""); |
| + SendResponse(net::OK, net::HTTP_BAD_REQUEST, true); |
| + |
| + EXPECT_EQ(1, num_invocations_); |
| + EXPECT_TRUE(!response_); |
| +} |
| + |
| +TEST_F(DigitalAssetLinksHandlerTest, NetworkError) { |
| + DigitalAssetLinksHandler handler(nullptr); |
| + handler.CheckDigitalAssetLinkRelationship( |
| + base::Bind(&DigitalAssetLinksHandlerTest::OnRelationshipCheckComplete, |
| + base::Unretained(this)), |
| + "", "", "", ""); |
| + SendResponse(net::ERR_ABORTED, net::HTTP_OK, true); |
| + |
| + EXPECT_EQ(1, num_invocations_); |
| + EXPECT_TRUE(!response_); |
| +} |
| +} // namespace digital_asset_links |