Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(587)

Side by Side Diff: components/captive_portal/captive_portal_detector_unittest.cc

Issue 242483003: Move CaptivePortalDetector to src/components/captive_portal (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/captive_portal/captive_portal_detector.h" 5 #include "components/captive_portal/captive_portal_detector.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 12 #include "base/run_loop.h"
10 #include "base/time/time.h" 13 #include "base/time/time.h"
11 #include "chrome/browser/captive_portal/testing_utils.h" 14 #include "components/captive_portal/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/base/net_errors.h"
15 #include "net/url_request/url_fetcher.h" 16 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h" 18 #include "url/gurl.h"
18 19
19 namespace captive_portal { 20 namespace captive_portal {
20 21
21 namespace { 22 namespace {
22 23
23 class CaptivePortalClient { 24 class CaptivePortalClient {
24 public: 25 public:
25 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector) 26 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector)
(...skipping 17 matching lines...) Expand all
43 int num_results_received_; 44 int num_results_received_;
44 45
45 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient); 46 DISALLOW_COPY_AND_ASSIGN(CaptivePortalClient);
46 }; 47 };
47 48
48 } // namespace 49 } // namespace
49 50
50 class CaptivePortalDetectorTest : public testing::Test, 51 class CaptivePortalDetectorTest : public testing::Test,
51 public CaptivePortalDetectorTestBase { 52 public CaptivePortalDetectorTestBase {
52 public: 53 public:
53 CaptivePortalDetectorTest() : detector_(profile_.GetRequestContext()) { 54 CaptivePortalDetectorTest() {}
54 set_detector(&detector_); 55 virtual ~CaptivePortalDetectorTest() {}
56
57 virtual void SetUp() OVERRIDE {
58 CHECK(base::MessageLoopProxy::current());
59 scoped_refptr<net::URLRequestContextGetter> request_context_getter(
60 new net::TestURLRequestContextGetter(
61 base::MessageLoopProxy::current()));
62
63 detector_.reset(new CaptivePortalDetector(request_context_getter.get()));
64 set_detector(detector_.get());
55 } 65 }
56 66
57 virtual ~CaptivePortalDetectorTest() {} 67 virtual void TearDown() OVERRIDE {
68 detector_.reset();
69 }
58 70
59 void RunTest(const CaptivePortalDetector::Results& expected_results, 71 void RunTest(const CaptivePortalDetector::Results& expected_results,
60 int net_error, 72 int net_error,
61 int status_code, 73 int status_code,
62 const char* response_headers) { 74 const char* response_headers) {
63 ASSERT_FALSE(FetchingURL()); 75 ASSERT_FALSE(FetchingURL());
64 76
65 GURL url(CaptivePortalDetector::kDefaultURL); 77 GURL url(CaptivePortalDetector::kDefaultURL);
66 CaptivePortalClient client(detector()); 78 CaptivePortalClient client(detector());
67 79
(...skipping 28 matching lines...) Expand all
96 ASSERT_TRUE(FetchingURL()); 108 ASSERT_TRUE(FetchingURL());
97 base::RunLoop().RunUntilIdle(); 109 base::RunLoop().RunUntilIdle();
98 110
99 detector()->Cancel(); 111 detector()->Cancel();
100 112
101 ASSERT_FALSE(FetchingURL()); 113 ASSERT_FALSE(FetchingURL());
102 EXPECT_EQ(0, client.num_results_received()); 114 EXPECT_EQ(0, client.num_results_received());
103 } 115 }
104 116
105 private: 117 private:
106 content::TestBrowserThreadBundle thread_bundle_; 118 base::MessageLoop message_loop_;
107 119 scoped_ptr<CaptivePortalDetector> detector_;
108 // Definition order does matter.
109 TestingProfile profile_;
110 CaptivePortalDetector detector_;
111 }; 120 };
112 121
113 // Test that the CaptivePortalDetector returns the expected result 122 // Test that the CaptivePortalDetector returns the expected result
114 // codes in response to a variety of probe results. 123 // codes in response to a variety of probe results.
115 TEST_F(CaptivePortalDetectorTest, CaptivePortalResultCodes) { 124 TEST_F(CaptivePortalDetectorTest, CaptivePortalResultCodes) {
116 CaptivePortalDetector::Results results; 125 CaptivePortalDetector::Results results;
117 results.result = RESULT_INTERNET_CONNECTED; 126 results.result = captive_portal::RESULT_INTERNET_CONNECTED;
118 results.response_code = 204; 127 results.response_code = 204;
119 128
120 RunTest(results, net::OK, 204, NULL); 129 RunTest(results, net::OK, 204, NULL);
121 130
122 // The server may return an HTTP error when it's acting up. 131 // The server may return an HTTP error when it's acting up.
123 results.result = RESULT_NO_RESPONSE; 132 results.result = captive_portal::RESULT_NO_RESPONSE;
124 results.response_code = 500; 133 results.response_code = 500;
125 RunTest(results, net::OK, 500, NULL); 134 RunTest(results, net::OK, 500, NULL);
126 135
127 // Generic network error case. 136 // Generic network error case.
128 results.result = RESULT_NO_RESPONSE; 137 results.result = captive_portal::RESULT_NO_RESPONSE;
129 results.response_code = net::URLFetcher::RESPONSE_CODE_INVALID; 138 results.response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
130 RunTest(results, net::ERR_TIMED_OUT, net::URLFetcher::RESPONSE_CODE_INVALID, 139 RunTest(results, net::ERR_TIMED_OUT, net::URLFetcher::RESPONSE_CODE_INVALID,
131 NULL); 140 NULL);
132 141
133 // In the general captive portal case, the portal will return a page with a 142 // In the general captive portal case, the portal will return a page with a
134 // 200 status. 143 // 200 status.
135 results.result = RESULT_BEHIND_CAPTIVE_PORTAL; 144 results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL;
136 results.response_code = 200; 145 results.response_code = 200;
137 RunTest(results, net::OK, 200, NULL); 146 RunTest(results, net::OK, 200, NULL);
138 147
139 // Some captive portals return 511 instead, to advertise their captive 148 // Some captive portals return 511 instead, to advertise their captive
140 // portal-ness. 149 // portal-ness.
141 results.result = RESULT_BEHIND_CAPTIVE_PORTAL; 150 results.result = captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL;
142 results.response_code = 511; 151 results.response_code = 511;
143 RunTest(results, net::OK, 511, NULL); 152 RunTest(results, net::OK, 511, NULL);
144 } 153 }
145 154
146 // Check a Retry-After header that contains a delay in seconds. 155 // Check a Retry-After header that contains a delay in seconds.
147 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterSeconds) { 156 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterSeconds) {
148 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; 157 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n";
149 CaptivePortalDetector::Results results; 158 CaptivePortalDetector::Results results;
150 159
151 // Check that Retry-After headers work both on the first request to return a 160 // Check that Retry-After headers work both on the first request to return a
152 // result and on subsequent requests. 161 // result and on subsequent requests.
153 results.result = RESULT_NO_RESPONSE; 162 results.result = captive_portal::RESULT_NO_RESPONSE;
154 results.response_code = 503; 163 results.response_code = 503;
155 results.retry_after_delta = base::TimeDelta::FromSeconds(101); 164 results.retry_after_delta = base::TimeDelta::FromSeconds(101);
156 RunTest(results, net::OK, 503, retry_after); 165 RunTest(results, net::OK, 503, retry_after);
157 166
158 results.result = RESULT_INTERNET_CONNECTED; 167 results.result = captive_portal::RESULT_INTERNET_CONNECTED;
159 results.response_code = 204; 168 results.response_code = 204;
160 results.retry_after_delta = base::TimeDelta(); 169 results.retry_after_delta = base::TimeDelta();
161 RunTest(results, net::OK, 204, NULL); 170 RunTest(results, net::OK, 204, NULL);
162 } 171 }
163 172
164 // Check a Retry-After header that contains a date. 173 // Check a Retry-After header that contains a date.
165 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterDate) { 174 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterDate) {
166 const char* retry_after = 175 const char* retry_after =
167 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"; 176 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n";
168 CaptivePortalDetector::Results results; 177 CaptivePortalDetector::Results results;
169 178
170 // base has a function to get a time in the right format from a string, but 179 // base has a function to get a time in the right format from a string, but
171 // not the other way around. 180 // not the other way around.
172 base::Time start_time; 181 base::Time start_time;
173 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", 182 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT",
174 &start_time)); 183 &start_time));
175 base::Time retry_after_time; 184 base::Time retry_after_time;
176 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT", 185 ASSERT_TRUE(base::Time::FromString("Tue, 17 Apr 2012 18:02:51 GMT",
177 &retry_after_time)); 186 &retry_after_time));
178 187
179 SetTime(start_time); 188 SetTime(start_time);
180 189
181 results.result = RESULT_NO_RESPONSE; 190 results.result = captive_portal::RESULT_NO_RESPONSE;
182 results.response_code = 503; 191 results.response_code = 503;
183 results.retry_after_delta = retry_after_time - start_time; 192 results.retry_after_delta = retry_after_time - start_time;
184 RunTest(results, net::OK, 503, retry_after); 193 RunTest(results, net::OK, 503, retry_after);
185 } 194 }
186 195
187 // Check invalid Retry-After headers are ignored. 196 // Check invalid Retry-After headers are ignored.
188 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterInvalid) { 197 TEST_F(CaptivePortalDetectorTest, CaptivePortalRetryAfterInvalid) {
189 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"; 198 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n";
190 CaptivePortalDetector::Results results; 199 CaptivePortalDetector::Results results;
191 200
192 results.result = RESULT_NO_RESPONSE; 201 results.result = captive_portal::RESULT_NO_RESPONSE;
193 results.response_code = 503; 202 results.response_code = 503;
194 RunTest(results, net::OK, 503, retry_after); 203 RunTest(results, net::OK, 503, retry_after);
195 } 204 }
196 205
197 TEST_F(CaptivePortalDetectorTest, Cancel) { 206 TEST_F(CaptivePortalDetectorTest, Cancel) {
198 RunCancelTest(); 207 RunCancelTest();
199 CaptivePortalDetector::Results results; 208 CaptivePortalDetector::Results results;
200 results.result = RESULT_INTERNET_CONNECTED; 209 results.result = captive_portal::RESULT_INTERNET_CONNECTED;
201 results.response_code = 204; 210 results.response_code = 204;
202 RunTest(results, net::OK, 204, NULL); 211 RunTest(results, net::OK, 204, NULL);
203 } 212 }
204 213
205 } // namespace captive_portal 214 } // namespace captive_portal
OLDNEW
« no previous file with comments | « components/captive_portal/captive_portal_detector.cc ('k') | components/captive_portal/captive_portal_export.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698