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/message_loop.h"
10 #include "base/time.h"
11 #include "chrome/browser/captive_portal/testing_utils.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "googleurl/src/gurl.h"
14 #include "net/base/net_errors.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace captive_portal
{
22 class CaptivePortalClient
{
24 explicit CaptivePortalClient(CaptivePortalDetector
* captive_portal_detector
)
25 : captive_portal_detector_(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
* captive_portal_detector_
;
44 CaptivePortalDetector::Results results_
;
45 int num_results_received_
;
47 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient
);
52 class CaptivePortalDetectorTest
: public testing::Test
,
53 public CaptivePortalDetectorTestBase
{
55 CaptivePortalDetectorTest() : detector_(profile_
.GetRequestContext()) {
56 set_detector(&detector_
);
59 virtual ~CaptivePortalDetectorTest() {}
61 void RunTest(const CaptivePortalDetector::Results
& expected_results
,
64 const char* response_headers
) {
65 ASSERT_FALSE(FetchingURL());
67 GURL
url(CaptivePortalDetector::kDefaultURL
);
68 CaptivePortalClient
client(detector());
70 detector()->DetectCaptivePortal(url
,
71 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted
,
72 base::Unretained(&client
)));
74 ASSERT_TRUE(FetchingURL());
75 MessageLoop::current()->RunUntilIdle();
77 CompleteURLFetch(net_error
, status_code
, response_headers
);
79 EXPECT_FALSE(FetchingURL());
80 EXPECT_EQ(1, client
.num_results_received());
81 EXPECT_EQ(expected_results
.result
, client
.captive_portal_results().result
);
82 EXPECT_EQ(expected_results
.response_code
,
83 client
.captive_portal_results().response_code
);
84 EXPECT_EQ(expected_results
.retry_after_delta
,
85 client
.captive_portal_results().retry_after_delta
);
88 void RunCancelTest() {
89 ASSERT_FALSE(FetchingURL());
91 GURL
url(CaptivePortalDetector::kDefaultURL
);
92 CaptivePortalClient
client(detector());
94 detector()->DetectCaptivePortal(url
,
95 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted
,
96 base::Unretained(&client
)));
98 ASSERT_TRUE(FetchingURL());
99 MessageLoop::current()->RunUntilIdle();
101 detector()->Cancel();
103 ASSERT_FALSE(FetchingURL());
104 EXPECT_EQ(0, client
.num_results_received());
108 MessageLoop message_loop_
;
110 // Definition order does matter.
111 TestingProfile profile_
;
112 CaptivePortalDetector detector_
;
115 // Test that the CaptivePortalDetector returns the expected result
116 // codes in response to a variety of probe results.
117 TEST_F(CaptivePortalDetectorTest
, CaptivePortalResultCodes
) {
118 CaptivePortalDetector::Results results
;
119 results
.result
= RESULT_INTERNET_CONNECTED
;
120 results
.response_code
= 204;
122 RunTest(results
, net::OK
, 204, NULL
);
124 // The server may return an HTTP error when it's acting up.
125 results
.result
= RESULT_NO_RESPONSE
;
126 results
.response_code
= 500;
127 RunTest(results
, net::OK
, 500, NULL
);
129 // Generic network error case.
130 results
.result
= RESULT_NO_RESPONSE
;
131 results
.response_code
= net::URLFetcher::RESPONSE_CODE_INVALID
;
132 RunTest(results
, net::ERR_TIMED_OUT
, net::URLFetcher::RESPONSE_CODE_INVALID
,
135 // In the general captive portal case, the portal will return a page with a
137 results
.result
= RESULT_BEHIND_CAPTIVE_PORTAL
;
138 results
.response_code
= 200;
139 RunTest(results
, net::OK
, 200, NULL
);
141 // Some captive portals return 511 instead, to advertise their captive
143 results
.result
= RESULT_BEHIND_CAPTIVE_PORTAL
;
144 results
.response_code
= 511;
145 RunTest(results
, net::OK
, 511, NULL
);
148 // Check a Retry-After header that contains a delay in seconds.
149 TEST_F(CaptivePortalDetectorTest
, CaptivePortalRetryAfterSeconds
) {
150 const char* retry_after
= "HTTP/1.1 503 OK\nRetry-After: 101\n\n";
151 CaptivePortalDetector::Results results
;
153 // Check that Retry-After headers work both on the first request to return a
154 // result and on subsequent requests.
155 results
.result
= RESULT_NO_RESPONSE
;
156 results
.response_code
= 503;
157 results
.retry_after_delta
= base::TimeDelta::FromSeconds(101);
158 RunTest(results
, net::OK
, 503, retry_after
);
160 results
.result
= RESULT_INTERNET_CONNECTED
;
161 results
.response_code
= 204;
162 results
.retry_after_delta
= base::TimeDelta();
163 RunTest(results
, net::OK
, 204, NULL
);
166 // Check a Retry-After header that contains a date.
167 TEST_F(CaptivePortalDetectorTest
, CaptivePortalRetryAfterDate
) {
168 const char* retry_after
=
169 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n";
170 CaptivePortalDetector::Results results
;
172 // base has a function to get a time in the right format from a string, but
173 // not the other way around.
174 base::Time start_time
;
175 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT",
177 base::Time retry_after_time
;
178 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT",
183 results
.result
= RESULT_NO_RESPONSE
;
184 results
.response_code
= 503;
185 results
.retry_after_delta
= retry_after_time
- start_time
;
186 RunTest(results
, net::OK
, 503, retry_after
);
189 // Check invalid Retry-After headers are ignored.
190 TEST_F(CaptivePortalDetectorTest
, CaptivePortalRetryAfterInvalid
) {
191 const char* retry_after
= "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n";
192 CaptivePortalDetector::Results results
;
194 results
.result
= RESULT_NO_RESPONSE
;
195 results
.response_code
= 503;
196 RunTest(results
, net::OK
, 503, retry_after
);
199 TEST_F(CaptivePortalDetectorTest
, Cancel
) {
201 CaptivePortalDetector::Results results
;
202 results
.result
= RESULT_INTERNET_CONNECTED
;
203 results
.response_code
= 204;
204 RunTest(results
, net::OK
, 204, NULL
);
207 } // namespace captive_portal