Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / captive_portal / captive_portal_detector_unittest.cc
blob49bd2b99e09a8267157c0320ea2803342d5c84b5
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"
8 #include "base/bind.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"
17 #include "url/gurl.h"
19 namespace captive_portal {
21 namespace {
23 class CaptivePortalClient {
24 public:
25 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector)
26 : captive_portal_detector_(captive_portal_detector),
27 num_results_received_(0) {
30 void OnPortalDetectionCompleted(
31 const CaptivePortalDetector::Results& results) {
32 results_ = results;
33 ++num_results_received_;
36 const CaptivePortalDetector::Results& captive_portal_results() const {
37 return results_;
40 int num_results_received() const { return num_results_received_; }
42 private:
43 CaptivePortalDetector* captive_portal_detector_;
45 CaptivePortalDetector::Results results_;
46 int num_results_received_;
48 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient);
51 } // namespace
53 class CaptivePortalDetectorTest : public testing::Test,
54 public CaptivePortalDetectorTestBase {
55 public:
56 CaptivePortalDetectorTest() : detector_(profile_.GetRequestContext()) {
57 set_detector(&detector_);
60 virtual ~CaptivePortalDetectorTest() {}
62 void RunTest(const CaptivePortalDetector::Results& expected_results,
63 int net_error,
64 int status_code,
65 const char* response_headers) {
66 ASSERT_FALSE(FetchingURL());
68 GURL url(CaptivePortalDetector::kDefaultURL);
69 CaptivePortalClient client(detector());
71 detector()->DetectCaptivePortal(url,
72 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted,
73 base::Unretained(&client)));
75 ASSERT_TRUE(FetchingURL());
76 base::RunLoop().RunUntilIdle();
78 CompleteURLFetch(net_error, status_code, response_headers);
80 EXPECT_FALSE(FetchingURL());
81 EXPECT_EQ(1, client.num_results_received());
82 EXPECT_EQ(expected_results.result, client.captive_portal_results().result);
83 EXPECT_EQ(expected_results.response_code,
84 client.captive_portal_results().response_code);
85 EXPECT_EQ(expected_results.retry_after_delta,
86 client.captive_portal_results().retry_after_delta);
89 void RunCancelTest() {
90 ASSERT_FALSE(FetchingURL());
92 GURL url(CaptivePortalDetector::kDefaultURL);
93 CaptivePortalClient client(detector());
95 detector()->DetectCaptivePortal(url,
96 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted,
97 base::Unretained(&client)));
99 ASSERT_TRUE(FetchingURL());
100 base::RunLoop().RunUntilIdle();
102 detector()->Cancel();
104 ASSERT_FALSE(FetchingURL());
105 EXPECT_EQ(0, client.num_results_received());
108 private:
109 content::TestBrowserThreadBundle thread_bundle_;
111 // Definition order does matter.
112 TestingProfile profile_;
113 CaptivePortalDetector detector_;
116 // Test that the CaptivePortalDetector returns the expected result
117 // codes in response to a variety of probe results.
118 TEST_F(CaptivePortalDetectorTest, CaptivePortalResultCodes) {
119 CaptivePortalDetector::Results results;
120 results.result = RESULT_INTERNET_CONNECTED;
121 results.response_code = 204;
123 RunTest(results, net::OK, 204, NULL);
125 // The server may return an HTTP error when it's acting up.
126 results.result = RESULT_NO_RESPONSE;
127 results.response_code = 500;
128 RunTest(results, net::OK, 500, NULL);
130 // Generic network error case.
131 results.result = RESULT_NO_RESPONSE;
132 results.response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
133 RunTest(results, net::ERR_TIMED_OUT, net::URLFetcher::RESPONSE_CODE_INVALID,
134 NULL);
136 // In the general captive portal case, the portal will return a page with a
137 // 200 status.
138 results.result = RESULT_BEHIND_CAPTIVE_PORTAL;
139 results.response_code = 200;
140 RunTest(results, net::OK, 200, NULL);
142 // Some captive portals return 511 instead, to advertise their captive
143 // portal-ness.
144 results.result = RESULT_BEHIND_CAPTIVE_PORTAL;
145 results.response_code = 511;
146 RunTest(results, net::OK, 511, NULL);
149 // Check a Retry-After header that contains a delay in seconds.
150 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterSeconds) {
151 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n";
152 CaptivePortalDetector::Results results;
154 // Check that Retry-After headers work both on the first request to return a
155 // result and on subsequent requests.
156 results.result = RESULT_NO_RESPONSE;
157 results.response_code = 503;
158 results.retry_after_delta = base::TimeDelta::FromSeconds(101);
159 RunTest(results, net::OK, 503, retry_after);
161 results.result = RESULT_INTERNET_CONNECTED;
162 results.response_code = 204;
163 results.retry_after_delta = base::TimeDelta();
164 RunTest(results, net::OK, 204, NULL);
167 // Check a Retry-After header that contains a date.
168 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterDate) {
169 const char* retry_after =
170 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n";
171 CaptivePortalDetector::Results results;
173 // base has a function to get a time in the right format from a string, but
174 // not the other way around.
175 base::Time start_time;
176 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT",
177 &start_time));
178 base::Time retry_after_time;
179 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT",
180 &retry_after_time));
182 SetTime(start_time);
184 results.result = RESULT_NO_RESPONSE;
185 results.response_code = 503;
186 results.retry_after_delta = retry_after_time - start_time;
187 RunTest(results, net::OK, 503, retry_after);
190 // Check invalid Retry-After headers are ignored.
191 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterInvalid) {
192 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n";
193 CaptivePortalDetector::Results results;
195 results.result = RESULT_NO_RESPONSE;
196 results.response_code = 503;
197 RunTest(results, net::OK, 503, retry_after);
200 TEST_F(CaptivePortalDetectorTest, Cancel) {
201 RunCancelTest();
202 CaptivePortalDetector::Results results;
203 results.result = RESULT_INTERNET_CONNECTED;
204 results.response_code = 204;
205 RunTest(results, net::OK, 204, NULL);
208 } // namespace captive_portal