- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / captive_portal / captive_portal_detector_unittest.cc
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.
4
5 #include "chrome/browser/captive_portal/captive_portal_detector.h"
6
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"
18
19 namespace captive_portal {
20
21 namespace {
22
23 class CaptivePortalClient {
24  public:
25   explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector)
26       : captive_portal_detector_(captive_portal_detector),
27         num_results_received_(0) {
28   }
29
30   void OnPortalDetectionCompleted(
31       const CaptivePortalDetector::Results& results) {
32     results_ = results;
33     ++num_results_received_;
34   }
35
36   const CaptivePortalDetector::Results& captive_portal_results() const {
37     return results_;
38   }
39
40   int num_results_received() const { return num_results_received_; }
41
42  private:
43   CaptivePortalDetector* captive_portal_detector_;
44
45   CaptivePortalDetector::Results results_;
46   int num_results_received_;
47
48   DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient);
49 };
50
51 }  // namespace
52
53 class CaptivePortalDetectorTest : public testing::Test,
54                                   public CaptivePortalDetectorTestBase {
55  public:
56   CaptivePortalDetectorTest() : detector_(profile_.GetRequestContext()) {
57     set_detector(&detector_);
58   }
59
60   virtual ~CaptivePortalDetectorTest() {}
61
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());
67
68     GURL url(CaptivePortalDetector::kDefaultURL);
69     CaptivePortalClient client(detector());
70
71     detector()->DetectCaptivePortal(url,
72         base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted,
73                    base::Unretained(&client)));
74
75     ASSERT_TRUE(FetchingURL());
76     base::RunLoop().RunUntilIdle();
77
78     CompleteURLFetch(net_error, status_code, response_headers);
79
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);
87   }
88
89   void RunCancelTest() {
90     ASSERT_FALSE(FetchingURL());
91
92     GURL url(CaptivePortalDetector::kDefaultURL);
93     CaptivePortalClient client(detector());
94
95     detector()->DetectCaptivePortal(url,
96         base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted,
97                    base::Unretained(&client)));
98
99     ASSERT_TRUE(FetchingURL());
100     base::RunLoop().RunUntilIdle();
101
102     detector()->Cancel();
103
104     ASSERT_FALSE(FetchingURL());
105     EXPECT_EQ(0, client.num_results_received());
106   }
107
108  private:
109   content::TestBrowserThreadBundle thread_bundle_;
110
111   // Definition order does matter.
112   TestingProfile profile_;
113   CaptivePortalDetector detector_;
114 };
115
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;
122
123   RunTest(results, net::OK, 204, NULL);
124
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);
129
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);
135
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);
141
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);
147 }
148
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;
153
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);
160
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);
165 }
166
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;
172
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));
181
182   SetTime(start_time);
183
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);
188 }
189
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;
194
195   results.result = RESULT_NO_RESPONSE;
196   results.response_code = 503;
197   RunTest(results, net::OK, 503, retry_after);
198 }
199
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);
206 }
207
208 }  // namespace captive_portal