Make |context| passed to queryRequiredElement optional
[chromium-blink-merge.git] / components / captive_portal / captive_portal_detector_unittest.cc
blobbff38212bfb6bf3f933fd59793d2c09855b6ff95
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 "components/captive_portal/captive_portal_detector.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h"
14 #include "components/captive_portal/captive_portal_testing_utils.h"
15 #include "net/base/net_errors.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
20 namespace captive_portal {
22 namespace {
24 class CaptivePortalClient {
25 public:
26 explicit CaptivePortalClient(CaptivePortalDetector* 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::Results results_;
44 int num_results_received_;
46 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient);
49 } // namespace
51 class CaptivePortalDetectorTest : public testing::Test,
52 public CaptivePortalDetectorTestBase {
53 public:
54 CaptivePortalDetectorTest() {}
55 ~CaptivePortalDetectorTest() override {}
57 void SetUp() override {
58 scoped_refptr<net::URLRequestContextGetter> request_context_getter(
59 new net::TestURLRequestContextGetter(
60 base::ThreadTaskRunnerHandle::Get()));
62 detector_.reset(new CaptivePortalDetector(request_context_getter.get()));
63 set_detector(detector_.get());
66 void TearDown() override { detector_.reset(); }
68 void RunTest(const CaptivePortalDetector::Results& expected_results,
69 int net_error,
70 int status_code,
71 const char* response_headers) {
72 ASSERT_FALSE(FetchingURL());
74 GURL url(CaptivePortalDetector::kDefaultURL);
75 CaptivePortalClient client(detector());
77 detector()->DetectCaptivePortal(url,
78 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted,
79 base::Unretained(&client)));
81 ASSERT_TRUE(FetchingURL());
82 base::RunLoop().RunUntilIdle();
84 CompleteURLFetch(net_error, status_code, response_headers);
86 EXPECT_FALSE(FetchingURL());
87 EXPECT_EQ(1, client.num_results_received());
88 EXPECT_EQ(expected_results.result, client.captive_portal_results().result);
89 EXPECT_EQ(expected_results.response_code,
90 client.captive_portal_results().response_code);
91 EXPECT_EQ(expected_results.retry_after_delta,
92 client.captive_portal_results().retry_after_delta);
95 void RunCancelTest() {
96 ASSERT_FALSE(FetchingURL());
98 GURL url(CaptivePortalDetector::kDefaultURL);
99 CaptivePortalClient client(detector());
101 detector()->DetectCaptivePortal(url,
102 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted,
103 base::Unretained(&client)));
105 ASSERT_TRUE(FetchingURL());
106 base::RunLoop().RunUntilIdle();
108 detector()->Cancel();
110 ASSERT_FALSE(FetchingURL());
111 EXPECT_EQ(0, client.num_results_received());
114 private:
115 base::MessageLoop message_loop_;
116 scoped_ptr<CaptivePortalDetector> detector_;
119 // Test that the CaptivePortalDetector returns the expected result
120 // codes in response to a variety of probe results.
121 TEST_F(CaptivePortalDetectorTest, CaptivePortalResultCodes) {
122 CaptivePortalDetector::Results results;
123 results.result = captive_portal::RESULT_INTERNET_CONNECTED;
124 results.response_code = 204;
126 RunTest(results, net::OK, 204, NULL);
128 // The server may return an HTTP error when it's acting up.
129 results.result = captive_portal::RESULT_NO_RESPONSE;
130 results.response_code = 500;
131 RunTest(results, net::OK, 500, NULL);
133 // Generic network error case.
134 results.result = captive_portal::RESULT_NO_RESPONSE;
135 results.response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
136 RunTest(results, net::ERR_TIMED_OUT, net::URLFetcher::RESPONSE_CODE_INVALID,
137 NULL);
139 // In the general captive portal case, the portal will return a page with a
140 // 200 status.
141 results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL;
142 results.response_code = 200;
143 RunTest(results, net::OK, 200, NULL);
145 // Some captive portals return 511 instead, to advertise their captive
146 // portal-ness.
147 results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL;
148 results.response_code = 511;
149 RunTest(results, net::OK, 511, NULL);
152 // Check a Retry-After header that contains a delay in seconds.
153 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterSeconds) {
154 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n";
155 CaptivePortalDetector::Results results;
157 // Check that Retry-After headers work both on the first request to return a
158 // result and on subsequent requests.
159 results.result = captive_portal::RESULT_NO_RESPONSE;
160 results.response_code = 503;
161 results.retry_after_delta = base::TimeDelta::FromSeconds(101);
162 RunTest(results, net::OK, 503, retry_after);
164 results.result = captive_portal::RESULT_INTERNET_CONNECTED;
165 results.response_code = 204;
166 results.retry_after_delta = base::TimeDelta();
167 RunTest(results, net::OK, 204, NULL);
170 // Check a Retry-After header that contains a date.
171 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterDate) {
172 const char* retry_after =
173 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n";
174 CaptivePortalDetector::Results results;
176 // base has a function to get a time in the right format from a string, but
177 // not the other way around.
178 base::Time start_time;
179 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT",
180 &start_time));
181 base::Time retry_after_time;
182 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT",
183 &retry_after_time));
185 SetTime(start_time);
187 results.result = captive_portal::RESULT_NO_RESPONSE;
188 results.response_code = 503;
189 results.retry_after_delta = retry_after_time - start_time;
190 RunTest(results, net::OK, 503, retry_after);
193 // Check invalid Retry-After headers are ignored.
194 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterInvalid) {
195 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n";
196 CaptivePortalDetector::Results results;
198 results.result = captive_portal::RESULT_NO_RESPONSE;
199 results.response_code = 503;
200 RunTest(results, net::OK, 503, retry_after);
203 TEST_F(CaptivePortalDetectorTest, Cancel) {
204 RunCancelTest();
205 CaptivePortalDetector::Results results;
206 results.result = captive_portal::RESULT_INTERNET_CONNECTED;
207 results.response_code = 204;
208 RunTest(results, net::OK, 204, NULL);
211 } // namespace captive_portal