OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // A complete set of unit tests for GaiaOAuthClient. | 5 // A complete set of unit tests for GaiaOAuthClient. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "chrome/common/net/gaia/gaia_oauth_client.h" | 12 #include "chrome/common/net/gaia/gaia_oauth_client.h" |
13 #include "chrome/common/net/http_return.h" | 13 #include "chrome/common/net/http_return.h" |
14 #include "chrome/test/testing_profile.h" | 14 #include "chrome/test/testing_profile.h" |
15 #include "content/common/test_url_fetcher_factory.h" | |
16 #include "content/common/url_fetcher.h" | 15 #include "content/common/url_fetcher.h" |
| 16 #include "content/test/test_url_fetcher_factory.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
19 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 using ::testing::_; | 23 using ::testing::_; |
24 | 24 |
25 namespace { | 25 namespace { |
26 // Responds as though OAuth returned from the server. | 26 // Responds as though OAuth returned from the server. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 62 |
63 private: | 63 private: |
64 int response_code_; | 64 int response_code_; |
65 int max_failure_count_; | 65 int max_failure_count_; |
66 int current_failure_count_; | 66 int current_failure_count_; |
67 GURL url_; | 67 GURL url_; |
68 std::string results_; | 68 std::string results_; |
69 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcher); | 69 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcher); |
70 }; | 70 }; |
71 | 71 |
72 class MockOAuthFetcherFactory : public URLFetcher::Factory { | 72 class MockOAuthFetcherFactory : public URLFetcher::Factory, |
| 73 public ScopedURLFetcherFactory { |
73 public: | 74 public: |
74 MockOAuthFetcherFactory() | 75 MockOAuthFetcherFactory() |
75 : response_code_(RC_REQUEST_OK) {} | 76 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 77 response_code_(RC_REQUEST_OK) { |
| 78 } |
76 ~MockOAuthFetcherFactory() {} | 79 ~MockOAuthFetcherFactory() {} |
77 virtual URLFetcher* CreateURLFetcher( | 80 virtual URLFetcher* CreateURLFetcher( |
78 int id, | 81 int id, |
79 const GURL& url, | 82 const GURL& url, |
80 URLFetcher::RequestType request_type, | 83 URLFetcher::RequestType request_type, |
81 URLFetcher::Delegate* d) { | 84 URLFetcher::Delegate* d) { |
82 return new MockOAuthFetcher( | 85 return new MockOAuthFetcher( |
83 response_code_, | 86 response_code_, |
84 max_failure_count_, | 87 max_failure_count_, |
85 url, | 88 url, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 TEST_F(GaiaOAuthClientTest, NetworkFailure) { | 148 TEST_F(GaiaOAuthClientTest, NetworkFailure) { |
146 int response_code = RC_INTERNAL_SERVER_ERROR; | 149 int response_code = RC_INTERNAL_SERVER_ERROR; |
147 | 150 |
148 MockGaiaOAuthClientDelegate delegate; | 151 MockGaiaOAuthClientDelegate delegate; |
149 EXPECT_CALL(delegate, OnNetworkError(response_code)) | 152 EXPECT_CALL(delegate, OnNetworkError(response_code)) |
150 .Times(1); | 153 .Times(1); |
151 | 154 |
152 TestingProfile profile; | 155 TestingProfile profile; |
153 | 156 |
154 MockOAuthFetcherFactory factory; | 157 MockOAuthFetcherFactory factory; |
155 URLFetcher::set_factory(&factory); | |
156 factory.set_response_code(response_code); | 158 factory.set_response_code(response_code); |
157 factory.set_max_failure_count(4); | 159 factory.set_max_failure_count(4); |
158 | 160 |
159 OAuthClientInfo client_info; | 161 OAuthClientInfo client_info; |
160 client_info.client_id = "test_client_id"; | 162 client_info.client_id = "test_client_id"; |
161 client_info.client_secret = "test_client_secret"; | 163 client_info.client_secret = "test_client_secret"; |
162 GaiaOAuthClient auth(kGaiaOAuth2Url, | 164 GaiaOAuthClient auth(kGaiaOAuth2Url, |
163 profile_.GetRequestContext()); | 165 profile_.GetRequestContext()); |
164 auth.GetTokensFromAuthCode(client_info, "auth_code", 2, &delegate); | 166 auth.GetTokensFromAuthCode(client_info, "auth_code", 2, &delegate); |
165 URLFetcher::set_factory(NULL); | |
166 } | 167 } |
167 | 168 |
168 TEST_F(GaiaOAuthClientTest, NetworkFailureRecover) { | 169 TEST_F(GaiaOAuthClientTest, NetworkFailureRecover) { |
169 int response_code = RC_INTERNAL_SERVER_ERROR; | 170 int response_code = RC_INTERNAL_SERVER_ERROR; |
170 | 171 |
171 MockGaiaOAuthClientDelegate delegate; | 172 MockGaiaOAuthClientDelegate delegate; |
172 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken, | 173 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken, |
173 kTestExpiresIn)).Times(1); | 174 kTestExpiresIn)).Times(1); |
174 | 175 |
175 TestingProfile profile; | 176 TestingProfile profile; |
176 | 177 |
177 MockOAuthFetcherFactory factory; | 178 MockOAuthFetcherFactory factory; |
178 URLFetcher::set_factory(&factory); | |
179 factory.set_response_code(response_code); | 179 factory.set_response_code(response_code); |
180 factory.set_max_failure_count(4); | 180 factory.set_max_failure_count(4); |
181 factory.set_results(kDummyGetTokensResult); | 181 factory.set_results(kDummyGetTokensResult); |
182 | 182 |
183 OAuthClientInfo client_info; | 183 OAuthClientInfo client_info; |
184 client_info.client_id = "test_client_id"; | 184 client_info.client_id = "test_client_id"; |
185 client_info.client_secret = "test_client_secret"; | 185 client_info.client_secret = "test_client_secret"; |
186 GaiaOAuthClient auth(kGaiaOAuth2Url, | 186 GaiaOAuthClient auth(kGaiaOAuth2Url, |
187 profile_.GetRequestContext()); | 187 profile_.GetRequestContext()); |
188 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | 188 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); |
189 URLFetcher::set_factory(NULL); | |
190 } | 189 } |
191 | 190 |
192 TEST_F(GaiaOAuthClientTest, OAuthFailure) { | 191 TEST_F(GaiaOAuthClientTest, OAuthFailure) { |
193 int response_code = RC_BAD_REQUEST; | 192 int response_code = RC_BAD_REQUEST; |
194 | 193 |
195 MockGaiaOAuthClientDelegate delegate; | 194 MockGaiaOAuthClientDelegate delegate; |
196 EXPECT_CALL(delegate, OnOAuthError()).Times(1); | 195 EXPECT_CALL(delegate, OnOAuthError()).Times(1); |
197 | 196 |
198 TestingProfile profile; | 197 TestingProfile profile; |
199 | 198 |
200 MockOAuthFetcherFactory factory; | 199 MockOAuthFetcherFactory factory; |
201 URLFetcher::set_factory(&factory); | |
202 factory.set_response_code(response_code); | 200 factory.set_response_code(response_code); |
203 factory.set_max_failure_count(-1); | 201 factory.set_max_failure_count(-1); |
204 factory.set_results(kDummyGetTokensResult); | 202 factory.set_results(kDummyGetTokensResult); |
205 | 203 |
206 OAuthClientInfo client_info; | 204 OAuthClientInfo client_info; |
207 client_info.client_id = "test_client_id"; | 205 client_info.client_id = "test_client_id"; |
208 client_info.client_secret = "test_client_secret"; | 206 client_info.client_secret = "test_client_secret"; |
209 GaiaOAuthClient auth(kGaiaOAuth2Url, | 207 GaiaOAuthClient auth(kGaiaOAuth2Url, |
210 profile_.GetRequestContext()); | 208 profile_.GetRequestContext()); |
211 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | 209 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); |
212 URLFetcher::set_factory(NULL); | |
213 } | 210 } |
214 | 211 |
215 | 212 |
216 TEST_F(GaiaOAuthClientTest, GetTokensSuccess) { | 213 TEST_F(GaiaOAuthClientTest, GetTokensSuccess) { |
217 MockGaiaOAuthClientDelegate delegate; | 214 MockGaiaOAuthClientDelegate delegate; |
218 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken, | 215 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken, |
219 kTestExpiresIn)).Times(1); | 216 kTestExpiresIn)).Times(1); |
220 | 217 |
221 TestingProfile profile; | 218 TestingProfile profile; |
222 | 219 |
223 MockOAuthFetcherFactory factory; | 220 MockOAuthFetcherFactory factory; |
224 URLFetcher::set_factory(&factory); | |
225 factory.set_results(kDummyGetTokensResult); | 221 factory.set_results(kDummyGetTokensResult); |
226 | 222 |
227 OAuthClientInfo client_info; | 223 OAuthClientInfo client_info; |
228 client_info.client_id = "test_client_id"; | 224 client_info.client_id = "test_client_id"; |
229 client_info.client_secret = "test_client_secret"; | 225 client_info.client_secret = "test_client_secret"; |
230 GaiaOAuthClient auth(kGaiaOAuth2Url, | 226 GaiaOAuthClient auth(kGaiaOAuth2Url, |
231 profile_.GetRequestContext()); | 227 profile_.GetRequestContext()); |
232 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | 228 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); |
233 URLFetcher::set_factory(NULL); | |
234 } | 229 } |
235 | 230 |
236 TEST_F(GaiaOAuthClientTest, RefreshTokenSuccess) { | 231 TEST_F(GaiaOAuthClientTest, RefreshTokenSuccess) { |
237 MockGaiaOAuthClientDelegate delegate; | 232 MockGaiaOAuthClientDelegate delegate; |
238 EXPECT_CALL(delegate, OnRefreshTokenResponse(kTestAccessToken, | 233 EXPECT_CALL(delegate, OnRefreshTokenResponse(kTestAccessToken, |
239 kTestExpiresIn)).Times(1); | 234 kTestExpiresIn)).Times(1); |
240 | 235 |
241 TestingProfile profile; | 236 TestingProfile profile; |
242 | 237 |
243 MockOAuthFetcherFactory factory; | 238 MockOAuthFetcherFactory factory; |
244 URLFetcher::set_factory(&factory); | |
245 factory.set_results(kDummyRefreshTokenResult); | 239 factory.set_results(kDummyRefreshTokenResult); |
246 | 240 |
247 OAuthClientInfo client_info; | 241 OAuthClientInfo client_info; |
248 client_info.client_id = "test_client_id"; | 242 client_info.client_id = "test_client_id"; |
249 client_info.client_secret = "test_client_secret"; | 243 client_info.client_secret = "test_client_secret"; |
250 GaiaOAuthClient auth(kGaiaOAuth2Url, | 244 GaiaOAuthClient auth(kGaiaOAuth2Url, |
251 profile_.GetRequestContext()); | 245 profile_.GetRequestContext()); |
252 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | 246 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); |
253 URLFetcher::set_factory(NULL); | |
254 } | 247 } |
255 } // namespace gaia | 248 } // namespace gaia |
OLD | NEW |