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
3 // found in the LICENSE file.
5 #include "chrome/browser/captive_portal/captive_portal_detector.h"
7 #include "base/basictypes.h"
9 #include "base/run_loop.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/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"
15 #include "net/url_request/url_fetcher.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 namespace captive_portal
{
23 class CaptivePortalClient
{
25 explicit CaptivePortalClient(CaptivePortalDetector
* captive_portal_detector
)
26 : num_results_received_(0) {
29 void OnPortalDetectionCompleted(
30 const CaptivePortalDetector::Results
& results
) {
32 ++num_results_received_
;
35 const CaptivePortalDetector::Results
& captive_portal_results() const {
39 int num_results_received() const { return num_results_received_
; }
42 CaptivePortalDetector::Results results_
;
43 int num_results_received_
;
45 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient
);
50 class CaptivePortalDetectorTest
: public testing::Test
,
51 public CaptivePortalDetectorTestBase
{
53 CaptivePortalDetectorTest() : detector_(profile_
.GetRequestContext()) {
54 set_detector(&detector_
);
57 virtual ~CaptivePortalDetectorTest() {}
59 void RunTest(const CaptivePortalDetector::Results
& expected_results
,
62 const char* response_headers
) {
63 ASSERT_FALSE(FetchingURL());
65 GURL
url(CaptivePortalDetector::kDefaultURL
);
66 CaptivePortalClient
client(detector());
68 detector()->DetectCaptivePortal(url
,
69 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted
,
70 base::Unretained(&client
)));
72 ASSERT_TRUE(FetchingURL());
73 base::RunLoop().RunUntilIdle();
75 CompleteURLFetch(net_error
, status_code
, response_headers
);
77 EXPECT_FALSE(FetchingURL());
78 EXPECT_EQ(1, client
.num_results_received());
79 EXPECT_EQ(expected_results
.result
, client
.captive_portal_results().result
);
80 EXPECT_EQ(expected_results
.response_code
,
81 client
.captive_portal_results().response_code
);
82 EXPECT_EQ(expected_results
.retry_after_delta
,
83 client
.captive_portal_results().retry_after_delta
);
86 void RunCancelTest() {
87 ASSERT_FALSE(FetchingURL());
89 GURL
url(CaptivePortalDetector::kDefaultURL
);
90 CaptivePortalClient
client(detector());
92 detector()->DetectCaptivePortal(url
,
93 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted
,
94 base::Unretained(&client
)));
96 ASSERT_TRUE(FetchingURL());
97 base::RunLoop().RunUntilIdle();
101 ASSERT_FALSE(FetchingURL());
102 EXPECT_EQ(0, client
.num_results_received());
106 content::TestBrowserThreadBundle thread_bundle_
;
108 // Definition order does matter.
109 TestingProfile profile_
;
110 CaptivePortalDetector detector_
;
113 // Test that the CaptivePortalDetector returns the expected result
114 // codes in response to a variety of probe results.
115 TEST_F(CaptivePortalDetectorTest
, CaptivePortalResultCodes
) {
116 CaptivePortalDetector::Results results
;
117 results
.result
= RESULT_INTERNET_CONNECTED
;
118 results
.response_code
= 204;
120 RunTest(results
, net::OK
, 204, NULL
);
122 // The server may return an HTTP error when it's acting up.
123 results
.result
= RESULT_NO_RESPONSE
;
124 results
.response_code
= 500;
125 RunTest(results
, net::OK
, 500, NULL
);
127 // Generic network error case.
128 results
.result
= RESULT_NO_RESPONSE
;
129 results
.response_code
= net::URLFetcher::RESPONSE_CODE_INVALID
;
130 RunTest(results
, net::ERR_TIMED_OUT
, net::URLFetcher::RESPONSE_CODE_INVALID
,
133 // In the general captive portal case, the portal will return a page with a
135 results
.result
= RESULT_BEHIND_CAPTIVE_PORTAL
;
136 results
.response_code
= 200;
137 RunTest(results
, net::OK
, 200, NULL
);
139 // Some captive portals return 511 instead, to advertise their captive
141 results
.result
= RESULT_BEHIND_CAPTIVE_PORTAL
;
142 results
.response_code
= 511;
143 RunTest(results
, net::OK
, 511, NULL
);
146 // Check a Retry-After header that contains a delay in seconds.
147 TEST_F(CaptivePortalDetectorTest
, CaptivePortalRetryAfterSeconds
) {
148 const char* retry_after
= "HTTP/1.1 503 OK\nRetry-After: 101\n\n";
149 CaptivePortalDetector::Results results
;
151 // Check that Retry-After headers work both on the first request to return a
152 // result and on subsequent requests.
153 results
.result
= RESULT_NO_RESPONSE
;
154 results
.response_code
= 503;
155 results
.retry_after_delta
= base::TimeDelta::FromSeconds(101);
156 RunTest(results
, net::OK
, 503, retry_after
);
158 results
.result
= RESULT_INTERNET_CONNECTED
;
159 results
.response_code
= 204;
160 results
.retry_after_delta
= base::TimeDelta();
161 RunTest(results
, net::OK
, 204, NULL
);
164 // Check a Retry-After header that contains a date.
165 TEST_F(CaptivePortalDetectorTest
, CaptivePortalRetryAfterDate
) {
166 const char* retry_after
=
167 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n";
168 CaptivePortalDetector::Results results
;
170 // base has a function to get a time in the right format from a string, but
171 // not the other way around.
172 base::Time start_time
;
173 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT",
175 base::Time retry_after_time
;
176 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT",
181 results
.result
= RESULT_NO_RESPONSE
;
182 results
.response_code
= 503;
183 results
.retry_after_delta
= retry_after_time
- start_time
;
184 RunTest(results
, net::OK
, 503, retry_after
);
187 // Check invalid Retry-After headers are ignored.
188 TEST_F(CaptivePortalDetectorTest
, CaptivePortalRetryAfterInvalid
) {
189 const char* retry_after
= "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n";
190 CaptivePortalDetector::Results results
;
192 results
.result
= RESULT_NO_RESPONSE
;
193 results
.response_code
= 503;
194 RunTest(results
, net::OK
, 503, retry_after
);
197 TEST_F(CaptivePortalDetectorTest
, Cancel
) {
199 CaptivePortalDetector::Results results
;
200 results
.result
= RESULT_INTERNET_CONNECTED
;
201 results
.response_code
= 204;
202 RunTest(results
, net::OK
, 204, NULL
);
205 } // namespace captive_portal