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/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 namespace api { | |
24 namespace dial { | |
25 | |
26 class DeviceDescriptionFetcherTest : public testing::Test { | |
27 public: | |
28 DeviceDescriptionFetcherTest() : url_("http://127.0.0.1/description.xml") {} | |
29 | |
30 void TearDown() override { EXPECT_TRUE(callback_was_run_); } | |
Wez
2017/01/06 01:24:03
Rather than maintain a separate boolean, would it
| |
31 | |
32 void ExpectSuccess(const GURL& expected_app_url, | |
33 const std::string& expected_description) { | |
34 success_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::AssertSuccess, | |
35 base::Unretained(this), expected_app_url, | |
36 expected_description); | |
37 } | |
38 | |
39 void ExpectFailure(const std::string& expected_message) { | |
40 expect_success_ = false; | |
41 error_cb_ = base::BindOnce(&DeviceDescriptionFetcherTest::AssertFailure, | |
42 base::Unretained(this), expected_message); | |
43 } | |
44 | |
45 net::TestURLFetcher* StartRequest() { | |
46 fetcher_ = base::MakeUnique<DeviceDescriptionFetcher>( | |
47 url_, &profile_, std::move(success_cb_), std::move(error_cb_)); | |
48 fetcher_->Start(); | |
49 return factory_.GetFetcherByID(DeviceDescriptionFetcher::kURLFetcherID); | |
50 } | |
51 | |
52 protected: | |
53 const content::TestBrowserThreadBundle thread_bundle_; | |
54 TestingProfile profile_; | |
55 const net::TestURLFetcherFactory factory_; | |
56 const GURL url_; | |
57 base::OnceCallback<void(const DialDeviceDescriptionData&)> success_cb_; | |
58 base::OnceCallback<void(const std::string&)> error_cb_; | |
59 std::unique_ptr<DeviceDescriptionFetcher> fetcher_; | |
60 bool callback_was_run_ = false; | |
61 bool expect_success_ = true; | |
62 | |
63 private: | |
64 void AssertSuccess(const GURL& expected_app_url, | |
65 const std::string& expected_description, | |
66 const DialDeviceDescriptionData& description) { | |
Wez
2017/01/06 01:24:03
I'd suggest renaming these to OnSuccess/SuccessCal
mark a. foltz
2017/01/09 21:38:07
Done.
| |
67 EXPECT_TRUE(expect_success_); | |
Wez
2017/01/06 01:24:03
Will this expectation ever fire? If we ExpectFailu
mark a. foltz
2017/01/09 21:38:07
Removed expectations.
| |
68 callback_was_run_ = true; | |
69 EXPECT_EQ(expected_app_url, description.app_url); | |
70 EXPECT_EQ(expected_description, description.device_description); | |
71 } | |
72 | |
73 void AssertFailure(const std::string& expected_message, | |
74 const std::string& message) { | |
75 EXPECT_FALSE(expect_success_); | |
76 callback_was_run_ = true; | |
77 EXPECT_TRUE(message.find(expected_message) == 0); | |
78 } | |
79 }; | |
Wez
2017/01/06 01:24:03
nit: DISALLOW_COPY_AND_ASSIGN, or ~Foo() etc = del
mark a. foltz
2017/01/09 21:38:07
Done.
| |
80 | |
81 TEST_F(DeviceDescriptionFetcherTest, TestFetchSuccessful) { | |
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, TestFetchFailsOnHttp404) { | |
Wez
2017/01/06 01:24:03
nit: The name matches the expectation failure; can
mark a. foltz
2017/01/09 21:38:07
Renamed.
| |
95 ExpectFailure("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, TestFetchFailsOnMissingAppUrl) { | |
103 ExpectFailure("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, TestFetchFailsOnEmptyAppUrl) { | |
114 ExpectFailure("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, TestFetchFailsOnInvalidAppUrl) { | |
126 ExpectFailure("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, TestFetchFailsOnEmptyDescription) { | |
138 ExpectFailure("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, TestFetchFailsOnBadDescription) { | |
151 ExpectFailure("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, TestFetchFailsOnResponseTooLarge) { | |
164 ExpectFailure("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 |