| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016 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 <memory> | |
| 6 #include <string> | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "chrome/browser/extensions/api/dial/device_description_fetcher.h" | |
| 13 #include "chrome/browser/extensions/api/dial/dial_device_data.h" | |
| 14 #include "chrome/test/base/testing_profile.h" | |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | |
| 16 #include "net/http/http_response_headers.h" | |
| 17 #include "net/http/http_status_code.h" | |
| 18 #include "net/url_request/test_url_fetcher_factory.h" | |
| 19 #include "net/url_request/url_fetcher.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace extensions { | |
| 24 namespace api { | |
| 25 namespace dial { | |
| 26 | |
| 27 class DeviceDescriptionFetcherTest : public testing::Test { | |
| 28 public: | |
| 29 DeviceDescriptionFetcherTest() : url_("http://127.0.0.1/description.xml") {} | |
| 30 | |
| 31 void TearDown() override { | |
| 32 EXPECT_FALSE(error_cb_); | |
| 33 EXPECT_FALSE(success_cb_); | |
| 34 } | |
| 35 | |
| 36 void ExpectSuccess(const GURL& expected_app_url, | |
| 37 const std::string& expected_description) { | |
| 38 success_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::OnSuccess, | |
| 39 base::Unretained(this), expected_app_url, | |
| 40 expected_description); | |
| 41 } | |
| 42 | |
| 43 void ExpectError(const std::string& expected_message) { | |
| 44 error_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::OnError, | |
| 45 base::Unretained(this), expected_message); | |
| 46 } | |
| 47 | |
| 48 net::TestURLFetcher* StartRequest() { | |
| 49 fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>( | |
| 50 url_, profile_.GetRequestContext(), std::move(success_cb_), | |
| 51 std::move(error_cb_)); | |
| 52 fetcher_->Start(); | |
| 53 return factory_.GetFetcherByID( | |
| 54 DeviceDescriptionFetcher::kURLFetcherIDForTest); | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 const content::TestBrowserThreadBundle thread_bundle_; | |
| 59 TestingProfile profile_; | |
| 60 const net::TestURLFetcherFactory factory_; | |
| 61 const GURL url_; | |
| 62 base::OnceCallback<void(const DialDeviceDescriptionData&)> success_cb_; | |
| 63 base::OnceCallback<void(const std::string&)> error_cb_; | |
| 64 std::unique_ptr<DeviceDescriptionFetcher> fetcher_; | |
| 65 | |
| 66 private: | |
| 67 void OnSuccess(const GURL& expected_app_url, | |
| 68 const std::string& expected_description, | |
| 69 const DialDeviceDescriptionData& description) { | |
| 70 EXPECT_EQ(expected_app_url, description.app_url); | |
| 71 EXPECT_EQ(expected_description, description.device_description); | |
| 72 } | |
| 73 | |
| 74 void OnError(const std::string& expected_message, | |
| 75 const std::string& message) { | |
| 76 EXPECT_TRUE(message.find(expected_message) == 0); | |
| 77 } | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(DeviceDescriptionFetcherTest); | |
| 80 }; | |
| 81 | |
| 82 TEST_F(DeviceDescriptionFetcherTest, FetchSuccessful) { | |
| 83 ExpectSuccess(GURL("http://127.0.0.1/apps"), "<xml>description</xml>"); | |
| 84 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 85 | |
| 86 test_fetcher->set_response_code(net::HTTP_OK); | |
| 87 scoped_refptr<net::HttpResponseHeaders> headers = | |
| 88 new net::HttpResponseHeaders(""); | |
| 89 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); | |
| 90 test_fetcher->set_response_headers(headers); | |
| 91 test_fetcher->SetResponseString("<xml>description</xml>"); | |
| 92 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 93 } | |
| 94 | |
| 95 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnMissingDescription) { | |
| 96 ExpectError("HTTP 404:"); | |
| 97 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 98 | |
| 99 test_fetcher->set_response_code(net::HTTP_NOT_FOUND); | |
| 100 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 101 } | |
| 102 | |
| 103 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnMissingAppUrl) { | |
| 104 ExpectError("Missing or empty Application-URL:"); | |
| 105 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 106 | |
| 107 test_fetcher->set_response_code(net::HTTP_OK); | |
| 108 scoped_refptr<net::HttpResponseHeaders> headers = | |
| 109 new net::HttpResponseHeaders(""); | |
| 110 test_fetcher->set_response_headers(headers); | |
| 111 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 112 } | |
| 113 | |
| 114 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnEmptyAppUrl) { | |
| 115 ExpectError("Missing or empty Application-URL:"); | |
| 116 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 117 | |
| 118 test_fetcher->set_response_code(net::HTTP_OK); | |
| 119 scoped_refptr<net::HttpResponseHeaders> headers = | |
| 120 new net::HttpResponseHeaders(""); | |
| 121 headers->AddHeader("Application-URL:"); | |
| 122 test_fetcher->set_response_headers(headers); | |
| 123 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 124 } | |
| 125 | |
| 126 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnInvalidAppUrl) { | |
| 127 ExpectError("Invalid Application-URL:"); | |
| 128 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 129 | |
| 130 test_fetcher->set_response_code(net::HTTP_OK); | |
| 131 scoped_refptr<net::HttpResponseHeaders> headers = | |
| 132 new net::HttpResponseHeaders(""); | |
| 133 headers->AddHeader("Application-URL: http://www.example.com"); | |
| 134 test_fetcher->set_response_headers(headers); | |
| 135 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 136 } | |
| 137 | |
| 138 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnEmptyDescription) { | |
| 139 ExpectError("Missing or empty response"); | |
| 140 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 141 | |
| 142 test_fetcher->set_response_code(net::HTTP_OK); | |
| 143 scoped_refptr<net::HttpResponseHeaders> headers = | |
| 144 new net::HttpResponseHeaders(""); | |
| 145 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); | |
| 146 test_fetcher->set_response_headers(headers); | |
| 147 test_fetcher->SetResponseString(""); | |
| 148 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 149 } | |
| 150 | |
| 151 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnBadDescription) { | |
| 152 ExpectError("Invalid response encoding"); | |
| 153 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 154 | |
| 155 test_fetcher->set_response_code(net::HTTP_OK); | |
| 156 scoped_refptr<net::HttpResponseHeaders> headers = | |
| 157 new net::HttpResponseHeaders(""); | |
| 158 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); | |
| 159 test_fetcher->set_response_headers(headers); | |
| 160 test_fetcher->SetResponseString("\xfc\x9c\xbf\x80\xbf\x80"); | |
| 161 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 162 } | |
| 163 | |
| 164 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnResponseTooLarge) { | |
| 165 ExpectError("Response too large"); | |
| 166 net::TestURLFetcher* test_fetcher = StartRequest(); | |
| 167 | |
| 168 test_fetcher->set_response_code(net::HTTP_OK); | |
| 169 scoped_refptr<net::HttpResponseHeaders> headers = | |
| 170 new net::HttpResponseHeaders(""); | |
| 171 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); | |
| 172 test_fetcher->set_response_headers(headers); | |
| 173 test_fetcher->SetResponseString(std::string(262145, 'd')); | |
| 174 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | |
| 175 } | |
| 176 | |
| 177 } // namespace dial | |
| 178 } // namespace api | |
| 179 } // namespace extensions | |
| OLD | NEW |