OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/captive_portal/captive_portal_detector.h" | 5 #include "components/captive_portal/captive_portal_detector.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
11 #include "chrome/browser/captive_portal/testing_utils.h" | 12 #include "components/captive_portal/captive_portal_testing_utils.h" |
12 #include "chrome/test/base/testing_profile.h" | |
13 #include "content/public/test/test_browser_thread_bundle.h" | |
14 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
15 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_request_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/gurl.h" | 16 #include "url/gurl.h" |
18 | 17 |
19 namespace captive_portal { | 18 namespace captive_portal { |
20 | 19 |
21 namespace { | 20 namespace { |
22 | 21 |
23 class CaptivePortalClient { | 22 class CaptivePortalClient { |
24 public: | 23 public: |
25 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector) | 24 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector) |
(...skipping 17 matching lines...) Expand all Loading... | |
43 int num_results_received_; | 42 int num_results_received_; |
44 | 43 |
45 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient); | 44 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient); |
46 }; | 45 }; |
47 | 46 |
48 } // namespace | 47 } // namespace |
49 | 48 |
50 class CaptivePortalDetectorTest : public testing::Test, | 49 class CaptivePortalDetectorTest : public testing::Test, |
51 public CaptivePortalDetectorTestBase { | 50 public CaptivePortalDetectorTestBase { |
52 public: | 51 public: |
53 CaptivePortalDetectorTest() : detector_(profile_.GetRequestContext()) { | 52 CaptivePortalDetectorTest() {} |
54 set_detector(&detector_); | 53 virtual ~CaptivePortalDetectorTest() {} |
54 | |
55 virtual void SetUp() OVERRIDE { | |
56 CHECK(base::MessageLoopProxy::current()); | |
57 scoped_refptr<net::URLRequestContextGetter> request_context_getter( | |
mmenke
2014/04/21 14:32:43
Should include ref_counted.h (And may as well add
stevenjb
2014/04/21 16:42:27
Done.
| |
58 new net::TestURLRequestContextGetter( | |
59 base::MessageLoopProxy::current())); | |
60 | |
61 detector_.reset(new CaptivePortalDetector(request_context_getter.get())); | |
62 set_detector(detector_.get()); | |
55 } | 63 } |
56 | 64 |
57 virtual ~CaptivePortalDetectorTest() {} | 65 virtual void TearDown() OVERRIDE { |
66 detector_.reset(); | |
67 } | |
58 | 68 |
59 void RunTest(const CaptivePortalDetector::Results& expected_results, | 69 void RunTest(const CaptivePortalDetector::Results& expected_results, |
60 int net_error, | 70 int net_error, |
61 int status_code, | 71 int status_code, |
62 const char* response_headers) { | 72 const char* response_headers) { |
63 ASSERT_FALSE(FetchingURL()); | 73 ASSERT_FALSE(FetchingURL()); |
64 | 74 |
65 GURL url(CaptivePortalDetector::kDefaultURL); | 75 GURL url(CaptivePortalDetector::kDefaultURL); |
66 CaptivePortalClient client(detector()); | 76 CaptivePortalClient client(detector()); |
67 | 77 |
(...skipping 28 matching lines...) Expand all Loading... | |
96 ASSERT_TRUE(FetchingURL()); | 106 ASSERT_TRUE(FetchingURL()); |
97 base::RunLoop().RunUntilIdle(); | 107 base::RunLoop().RunUntilIdle(); |
98 | 108 |
99 detector()->Cancel(); | 109 detector()->Cancel(); |
100 | 110 |
101 ASSERT_FALSE(FetchingURL()); | 111 ASSERT_FALSE(FetchingURL()); |
102 EXPECT_EQ(0, client.num_results_received()); | 112 EXPECT_EQ(0, client.num_results_received()); |
103 } | 113 } |
104 | 114 |
105 private: | 115 private: |
106 content::TestBrowserThreadBundle thread_bundle_; | 116 base::MessageLoop message_loop_; |
107 | 117 scoped_ptr<CaptivePortalDetector> detector_; |
108 // Definition order does matter. | |
109 TestingProfile profile_; | |
110 CaptivePortalDetector detector_; | |
111 }; | 118 }; |
112 | 119 |
113 // Test that the CaptivePortalDetector returns the expected result | 120 // Test that the CaptivePortalDetector returns the expected result |
114 // codes in response to a variety of probe results. | 121 // codes in response to a variety of probe results. |
115 TEST_F(CaptivePortalDetectorTest, CaptivePortalResultCodes) { | 122 TEST_F(CaptivePortalDetectorTest, CaptivePortalResultCodes) { |
116 CaptivePortalDetector::Results results; | 123 CaptivePortalDetector::Results results; |
117 results.result = RESULT_INTERNET_CONNECTED; | 124 results.result = captive_portal::RESULT_INTERNET_CONNECTED; |
118 results.response_code = 204; | 125 results.response_code = 204; |
119 | 126 |
120 RunTest(results, net::OK, 204, NULL); | 127 RunTest(results, net::OK, 204, NULL); |
121 | 128 |
122 // The server may return an HTTP error when it's acting up. | 129 // The server may return an HTTP error when it's acting up. |
123 results.result = RESULT_NO_RESPONSE; | 130 results.result = captive_portal::RESULT_NO_RESPONSE; |
124 results.response_code = 500; | 131 results.response_code = 500; |
125 RunTest(results, net::OK, 500, NULL); | 132 RunTest(results, net::OK, 500, NULL); |
126 | 133 |
127 // Generic network error case. | 134 // Generic network error case. |
128 results.result = RESULT_NO_RESPONSE; | 135 results.result = captive_portal::RESULT_NO_RESPONSE; |
129 results.response_code = net::URLFetcher::RESPONSE_CODE_INVALID; | 136 results.response_code = net::URLFetcher::RESPONSE_CODE_INVALID; |
130 RunTest(results, net::ERR_TIMED_OUT, net::URLFetcher::RESPONSE_CODE_INVALID, | 137 RunTest(results, net::ERR_TIMED_OUT, net::URLFetcher::RESPONSE_CODE_INVALID, |
131 NULL); | 138 NULL); |
132 | 139 |
133 // In the general captive portal case, the portal will return a page with a | 140 // In the general captive portal case, the portal will return a page with a |
134 // 200 status. | 141 // 200 status. |
135 results.result = RESULT_BEHIND_CAPTIVE_PORTAL; | 142 results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; |
136 results.response_code = 200; | 143 results.response_code = 200; |
137 RunTest(results, net::OK, 200, NULL); | 144 RunTest(results, net::OK, 200, NULL); |
138 | 145 |
139 // Some captive portals return 511 instead, to advertise their captive | 146 // Some captive portals return 511 instead, to advertise their captive |
140 // portal-ness. | 147 // portal-ness. |
141 results.result = RESULT_BEHIND_CAPTIVE_PORTAL; | 148 results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL; |
142 results.response_code = 511; | 149 results.response_code = 511; |
143 RunTest(results, net::OK, 511, NULL); | 150 RunTest(results, net::OK, 511, NULL); |
144 } | 151 } |
145 | 152 |
146 // Check a Retry-After header that contains a delay in seconds. | 153 // Check a Retry-After header that contains a delay in seconds. |
147 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterSeconds) { | 154 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterSeconds) { |
148 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; | 155 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; |
149 CaptivePortalDetector::Results results; | 156 CaptivePortalDetector::Results results; |
150 | 157 |
151 // Check that Retry-After headers work both on the first request to return a | 158 // Check that Retry-After headers work both on the first request to return a |
152 // result and on subsequent requests. | 159 // result and on subsequent requests. |
153 results.result = RESULT_NO_RESPONSE; | 160 results.result = captive_portal::RESULT_NO_RESPONSE; |
154 results.response_code = 503; | 161 results.response_code = 503; |
155 results.retry_after_delta = base::TimeDelta::FromSeconds(101); | 162 results.retry_after_delta = base::TimeDelta::FromSeconds(101); |
156 RunTest(results, net::OK, 503, retry_after); | 163 RunTest(results, net::OK, 503, retry_after); |
157 | 164 |
158 results.result = RESULT_INTERNET_CONNECTED; | 165 results.result = captive_portal::RESULT_INTERNET_CONNECTED; |
159 results.response_code = 204; | 166 results.response_code = 204; |
160 results.retry_after_delta = base::TimeDelta(); | 167 results.retry_after_delta = base::TimeDelta(); |
161 RunTest(results, net::OK, 204, NULL); | 168 RunTest(results, net::OK, 204, NULL); |
162 } | 169 } |
163 | 170 |
164 // Check a Retry-After header that contains a date. | 171 // Check a Retry-After header that contains a date. |
165 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterDate) { | 172 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterDate) { |
166 const char* retry_after = | 173 const char* retry_after = |
167 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"; | 174 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"; |
168 CaptivePortalDetector::Results results; | 175 CaptivePortalDetector::Results results; |
169 | 176 |
170 // base has a function to get a time in the right format from a string, but | 177 // base has a function to get a time in the right format from a string, but |
171 // not the other way around. | 178 // not the other way around. |
172 base::Time start_time; | 179 base::Time start_time; |
173 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", | 180 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", |
174 &start_time)); | 181 &start_time)); |
175 base::Time retry_after_time; | 182 base::Time retry_after_time; |
176 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT", | 183 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT", |
177 &retry_after_time)); | 184 &retry_after_time)); |
178 | 185 |
179 SetTime(start_time); | 186 SetTime(start_time); |
180 | 187 |
181 results.result = RESULT_NO_RESPONSE; | 188 results.result = captive_portal::RESULT_NO_RESPONSE; |
182 results.response_code = 503; | 189 results.response_code = 503; |
183 results.retry_after_delta = retry_after_time - start_time; | 190 results.retry_after_delta = retry_after_time - start_time; |
184 RunTest(results, net::OK, 503, retry_after); | 191 RunTest(results, net::OK, 503, retry_after); |
185 } | 192 } |
186 | 193 |
187 // Check invalid Retry-After headers are ignored. | 194 // Check invalid Retry-After headers are ignored. |
188 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterInvalid) { | 195 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterInvalid) { |
189 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"; | 196 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"; |
190 CaptivePortalDetector::Results results; | 197 CaptivePortalDetector::Results results; |
191 | 198 |
192 results.result = RESULT_NO_RESPONSE; | 199 results.result = captive_portal::RESULT_NO_RESPONSE; |
193 results.response_code = 503; | 200 results.response_code = 503; |
194 RunTest(results, net::OK, 503, retry_after); | 201 RunTest(results, net::OK, 503, retry_after); |
195 } | 202 } |
196 | 203 |
197 TEST_F(CaptivePortalDetectorTest, Cancel) { | 204 TEST_F(CaptivePortalDetectorTest, Cancel) { |
198 RunCancelTest(); | 205 RunCancelTest(); |
199 CaptivePortalDetector::Results results; | 206 CaptivePortalDetector::Results results; |
200 results.result = RESULT_INTERNET_CONNECTED; | 207 results.result = captive_portal::RESULT_INTERNET_CONNECTED; |
201 results.response_code = 204; | 208 results.response_code = 204; |
202 RunTest(results, net::OK, 204, NULL); | 209 RunTest(results, net::OK, 204, NULL); |
203 } | 210 } |
204 | 211 |
205 } // namespace captive_portal | 212 } // namespace captive_portal |
OLD | NEW |