Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: chrome/browser/extensions/api/dial/device_description_fetcher_unittest.cc

Issue 2583853004: [chrome.dial] Adds chrome.dial.fetchDeviceDecription API. (Closed)
Patch Set: Respond to imcheng@ comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "chrome/browser/extensions/api/dial/device_description_fetcher.h"
12 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "net/http/http_response_headers.h"
16 #include "net/http/http_status_code.h"
17 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h"
21
22 namespace extensions {
23
24 class DeviceDescriptionFetcherTest : public testing::Test {
25 public:
26 DeviceDescriptionFetcherTest() : url_("http://127.0.0.1/description.xml") {}
27
28 void TearDown() override { EXPECT_TRUE(callback_was_run_); }
29
30 void ExpectSuccess(const GURL& expected_app_url,
31 const std::string& expected_description) {
32 success_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::AssertSuccess,
33 base::Unretained(this), expected_app_url,
34 expected_description);
35 }
36
37 void ExpectFailure(const std::string& expected_message) {
38 expect_success_ = false;
39 error_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::AssertFailure,
40 base::Unretained(this), expected_message);
41 }
42
43 net::TestURLFetcher* StartRequest() {
44 fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>(
45 url_, &profile_, std::move(success_cb_), std::move(error_cb_));
46 fetcher_->Start();
47 return factory_.GetFetcherByID(DeviceDescriptionFetcher::kURLFetcherID);
48 }
49
50 protected:
51 const content::TestBrowserThreadBundle thread_bundle_;
52 TestingProfile profile_;
53 const net::TestURLFetcherFactory factory_;
54 const GURL url_;
55 base::OnceCallback<void(const DialDeviceDescription&)> success_cb_;
56 base::OnceCallback<void(const std::string&)> error_cb_;
57 std::unique_ptr<DeviceDescriptionFetcher> fetcher_;
58 bool callback_was_run_ = false;
59 bool expect_success_ = true;
60
61 private:
62 void AssertSuccess(const GURL& expected_app_url,
63 const std::string& expected_description,
64 const DialDeviceDescription& description) {
65 EXPECT_TRUE(expect_success_);
66 callback_was_run_ = true;
67 EXPECT_EQ(expected_app_url, description.app_url);
68 EXPECT_EQ(expected_description, description.device_description);
69 }
70
71 void AssertFailure(const std::string& expected_message,
72 const std::string& message) {
73 EXPECT_FALSE(expect_success_);
74 callback_was_run_ = true;
75 EXPECT_TRUE(message.find(expected_message) == 0);
76 }
77 };
78
79 TEST_F(DeviceDescriptionFetcherTest, TestFetchSuccessful) {
80 ExpectSuccess(GURL("http://127.0.0.1/apps"), "<xml>description</xml>");
81 net::TestURLFetcher* test_fetcher = StartRequest();
82
83 test_fetcher->set_response_code(net::HTTP_OK);
84 scoped_refptr<net::HttpResponseHeaders> headers =
85 new net::HttpResponseHeaders("");
86 headers->AddHeader("Application-URL: http://127.0.0.1/apps");
87 test_fetcher->set_response_headers(headers);
88 test_fetcher->SetResponseString("<xml>description</xml>");
89 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
90 }
91
92 TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnHttp404) {
93 ExpectFailure("HTTP 404:");
94 net::TestURLFetcher* test_fetcher = StartRequest();
95
96 test_fetcher->set_response_code(net::HTTP_NOT_FOUND);
97 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
98 }
99
100 TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnMissingAppUrl) {
101 ExpectFailure("Missing or empty Application-URL:");
102 net::TestURLFetcher* test_fetcher = StartRequest();
103
104 test_fetcher->set_response_code(net::HTTP_OK);
105 scoped_refptr<net::HttpResponseHeaders> headers =
106 new net::HttpResponseHeaders("");
107 test_fetcher->set_response_headers(headers);
108 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
109 }
110
111 TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnEmptyAppUrl) {
112 ExpectFailure("Missing or empty Application-URL:");
113 net::TestURLFetcher* test_fetcher = StartRequest();
114
115 test_fetcher->set_response_code(net::HTTP_OK);
116 scoped_refptr<net::HttpResponseHeaders> headers =
117 new net::HttpResponseHeaders("");
118 headers->AddHeader("Application-URL:");
119 test_fetcher->set_response_headers(headers);
120 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
121 }
122
123 TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnInvalidAppUrl) {
124 ExpectFailure("Invalid Application-URL:");
125 net::TestURLFetcher* test_fetcher = StartRequest();
126
127 test_fetcher->set_response_code(net::HTTP_OK);
128 scoped_refptr<net::HttpResponseHeaders> headers =
129 new net::HttpResponseHeaders("");
130 headers->AddHeader("Application-URL: http://www.example.com");
131 test_fetcher->set_response_headers(headers);
132 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
133 }
134
135 TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnEmptyDescription) {
136 ExpectFailure("Missing or empty response");
137 net::TestURLFetcher* test_fetcher = StartRequest();
138
139 test_fetcher->set_response_code(net::HTTP_OK);
140 scoped_refptr<net::HttpResponseHeaders> headers =
141 new net::HttpResponseHeaders("");
142 headers->AddHeader("Application-URL: http://127.0.0.1/apps");
143 test_fetcher->set_response_headers(headers);
144 test_fetcher->SetResponseString("");
145 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
146 }
147
148 TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnBadDescription) {
149 ExpectFailure("Invalid response encoding");
150 net::TestURLFetcher* test_fetcher = StartRequest();
151
152 test_fetcher->set_response_code(net::HTTP_OK);
153 scoped_refptr<net::HttpResponseHeaders> headers =
154 new net::HttpResponseHeaders("");
155 headers->AddHeader("Application-URL: http://127.0.0.1/apps");
156 test_fetcher->set_response_headers(headers);
157 test_fetcher->SetResponseString("\xfc\x9c\xbf\x80\xbf\x80");
158 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
159 }
160
161 TEST_F(DeviceDescriptionFetcherTest, TestFetchFailsOnResponseTooLarge) {
162 ExpectFailure("Response too large");
163 net::TestURLFetcher* test_fetcher = StartRequest();
164
165 test_fetcher->set_response_code(net::HTTP_OK);
166 scoped_refptr<net::HttpResponseHeaders> headers =
167 new net::HttpResponseHeaders("");
168 headers->AddHeader("Application-URL: http://127.0.0.1/apps");
169 test_fetcher->set_response_headers(headers);
170 test_fetcher->SetResponseString(std::string(262145, 'd'));
171 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
172 }
173
174 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698