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_, std::move(success_cb_), std::move(error_cb_)); |
| 51 fetcher_->Start(); |
| 52 return factory_.GetFetcherByID( |
| 53 DeviceDescriptionFetcher::kURLFetcherIDForTest); |
| 54 } |
| 55 |
| 56 protected: |
| 57 const content::TestBrowserThreadBundle thread_bundle_; |
| 58 TestingProfile profile_; |
| 59 const net::TestURLFetcherFactory factory_; |
| 60 const GURL url_; |
| 61 base::OnceCallback<void(const DialDeviceDescriptionData&)> success_cb_; |
| 62 base::OnceCallback<void(const std::string&)> error_cb_; |
| 63 std::unique_ptr<DeviceDescriptionFetcher> fetcher_; |
| 64 |
| 65 private: |
| 66 void OnSuccess(const GURL& expected_app_url, |
| 67 const std::string& expected_description, |
| 68 const DialDeviceDescriptionData& description) { |
| 69 EXPECT_EQ(expected_app_url, description.app_url); |
| 70 EXPECT_EQ(expected_description, description.device_description); |
| 71 } |
| 72 |
| 73 void OnError(const std::string& expected_message, |
| 74 const std::string& message) { |
| 75 EXPECT_TRUE(message.find(expected_message) == 0); |
| 76 } |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(DeviceDescriptionFetcherTest); |
| 79 }; |
| 80 |
| 81 TEST_F(DeviceDescriptionFetcherTest, FetchSuccessful) { |
| 82 ExpectSuccess(GURL("http://127.0.0.1/apps"), "<xml>description</xml>"); |
| 83 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 84 |
| 85 test_fetcher->set_response_code(net::HTTP_OK); |
| 86 scoped_refptr<net::HttpResponseHeaders> headers = |
| 87 new net::HttpResponseHeaders(""); |
| 88 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); |
| 89 test_fetcher->set_response_headers(headers); |
| 90 test_fetcher->SetResponseString("<xml>description</xml>"); |
| 91 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 92 } |
| 93 |
| 94 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnMissingDescription) { |
| 95 ExpectError("HTTP 404:"); |
| 96 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 97 |
| 98 test_fetcher->set_response_code(net::HTTP_NOT_FOUND); |
| 99 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 100 } |
| 101 |
| 102 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnMissingAppUrl) { |
| 103 ExpectError("Missing or empty Application-URL:"); |
| 104 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 105 |
| 106 test_fetcher->set_response_code(net::HTTP_OK); |
| 107 scoped_refptr<net::HttpResponseHeaders> headers = |
| 108 new net::HttpResponseHeaders(""); |
| 109 test_fetcher->set_response_headers(headers); |
| 110 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 111 } |
| 112 |
| 113 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnEmptyAppUrl) { |
| 114 ExpectError("Missing or empty Application-URL:"); |
| 115 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 116 |
| 117 test_fetcher->set_response_code(net::HTTP_OK); |
| 118 scoped_refptr<net::HttpResponseHeaders> headers = |
| 119 new net::HttpResponseHeaders(""); |
| 120 headers->AddHeader("Application-URL:"); |
| 121 test_fetcher->set_response_headers(headers); |
| 122 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 123 } |
| 124 |
| 125 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnInvalidAppUrl) { |
| 126 ExpectError("Invalid Application-URL:"); |
| 127 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 128 |
| 129 test_fetcher->set_response_code(net::HTTP_OK); |
| 130 scoped_refptr<net::HttpResponseHeaders> headers = |
| 131 new net::HttpResponseHeaders(""); |
| 132 headers->AddHeader("Application-URL: http://www.example.com"); |
| 133 test_fetcher->set_response_headers(headers); |
| 134 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 135 } |
| 136 |
| 137 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnEmptyDescription) { |
| 138 ExpectError("Missing or empty response"); |
| 139 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 140 |
| 141 test_fetcher->set_response_code(net::HTTP_OK); |
| 142 scoped_refptr<net::HttpResponseHeaders> headers = |
| 143 new net::HttpResponseHeaders(""); |
| 144 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); |
| 145 test_fetcher->set_response_headers(headers); |
| 146 test_fetcher->SetResponseString(""); |
| 147 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 148 } |
| 149 |
| 150 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnBadDescription) { |
| 151 ExpectError("Invalid response encoding"); |
| 152 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 153 |
| 154 test_fetcher->set_response_code(net::HTTP_OK); |
| 155 scoped_refptr<net::HttpResponseHeaders> headers = |
| 156 new net::HttpResponseHeaders(""); |
| 157 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); |
| 158 test_fetcher->set_response_headers(headers); |
| 159 test_fetcher->SetResponseString("\xfc\x9c\xbf\x80\xbf\x80"); |
| 160 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 161 } |
| 162 |
| 163 TEST_F(DeviceDescriptionFetcherTest, FetchFailsOnResponseTooLarge) { |
| 164 ExpectError("Response too large"); |
| 165 net::TestURLFetcher* test_fetcher = StartRequest(); |
| 166 |
| 167 test_fetcher->set_response_code(net::HTTP_OK); |
| 168 scoped_refptr<net::HttpResponseHeaders> headers = |
| 169 new net::HttpResponseHeaders(""); |
| 170 headers->AddHeader("Application-URL: http://127.0.0.1/apps"); |
| 171 test_fetcher->set_response_headers(headers); |
| 172 test_fetcher->SetResponseString(std::string(262145, 'd')); |
| 173 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 174 } |
| 175 |
| 176 } // namespace dial |
| 177 } // namespace api |
| 178 } // namespace extensions |
OLD | NEW |