| OLD | NEW |
| 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 // A complete set of unit tests for OAuth2AccessTokenFetcher. | 5 // A complete set of unit tests for OAuth2AccessTokenFetcher. |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 " \"access_token\": \"at1\"," | 43 " \"access_token\": \"at1\"," |
| 44 " \"expires_in\": 3600," | 44 " \"expires_in\": 3600," |
| 45 " \"token_type\": \"Bearer\"" | 45 " \"token_type\": \"Bearer\"" |
| 46 "}"; | 46 "}"; |
| 47 static const char kTokenResponseNoAccessToken[] = | 47 static const char kTokenResponseNoAccessToken[] = |
| 48 "{" | 48 "{" |
| 49 " \"expires_in\": 3600," | 49 " \"expires_in\": 3600," |
| 50 " \"token_type\": \"Bearer\"" | 50 " \"token_type\": \"Bearer\"" |
| 51 "}"; | 51 "}"; |
| 52 | 52 |
| 53 static const char kValidFailureTokenResponse[] = |
| 54 "{" |
| 55 " \"error\": \"invalid_grant\"" |
| 56 "}"; |
| 57 |
| 53 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, | 58 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, |
| 54 public URLFetcherFactory { | 59 public URLFetcherFactory { |
| 55 public: | 60 public: |
| 56 MockUrlFetcherFactory() | 61 MockUrlFetcherFactory() |
| 57 : ScopedURLFetcherFactory(this) { | 62 : ScopedURLFetcherFactory(this) { |
| 58 } | 63 } |
| 59 virtual ~MockUrlFetcherFactory() {} | 64 virtual ~MockUrlFetcherFactory() {} |
| 60 | 65 |
| 61 MOCK_METHOD4( | 66 MOCK_METHOD4( |
| 62 CreateURLFetcher, | 67 CreateURLFetcher, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 #define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse | 193 #define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse |
| 189 #else | 194 #else |
| 190 #define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse | 195 #define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse |
| 191 #endif // defined(OS_WIN) | 196 #endif // defined(OS_WIN) |
| 192 TEST_F(OAuth2AccessTokenFetcherTest, MAYBE_ParseGetAccessTokenResponse) { | 197 TEST_F(OAuth2AccessTokenFetcherTest, MAYBE_ParseGetAccessTokenResponse) { |
| 193 { // No body. | 198 { // No body. |
| 194 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | 199 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 195 | 200 |
| 196 std::string at; | 201 std::string at; |
| 197 int expires_in; | 202 int expires_in; |
| 198 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 203 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenSuccessResponse( |
| 199 &url_fetcher, &at, &expires_in)); | 204 &url_fetcher, &at, &expires_in)); |
| 200 EXPECT_TRUE(at.empty()); | 205 EXPECT_TRUE(at.empty()); |
| 201 } | 206 } |
| 202 { // Bad json. | 207 { // Bad json. |
| 203 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | 208 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 204 url_fetcher.SetResponseString("foo"); | 209 url_fetcher.SetResponseString("foo"); |
| 205 | 210 |
| 206 std::string at; | 211 std::string at; |
| 207 int expires_in; | 212 int expires_in; |
| 208 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 213 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenSuccessResponse( |
| 209 &url_fetcher, &at, &expires_in)); | 214 &url_fetcher, &at, &expires_in)); |
| 210 EXPECT_TRUE(at.empty()); | 215 EXPECT_TRUE(at.empty()); |
| 211 } | 216 } |
| 212 { // Valid json: access token missing. | 217 { // Valid json: access token missing. |
| 213 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | 218 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 214 url_fetcher.SetResponseString(kTokenResponseNoAccessToken); | 219 url_fetcher.SetResponseString(kTokenResponseNoAccessToken); |
| 215 | 220 |
| 216 std::string at; | 221 std::string at; |
| 217 int expires_in; | 222 int expires_in; |
| 218 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 223 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenSuccessResponse( |
| 219 &url_fetcher, &at, &expires_in)); | 224 &url_fetcher, &at, &expires_in)); |
| 220 EXPECT_TRUE(at.empty()); | 225 EXPECT_TRUE(at.empty()); |
| 221 } | 226 } |
| 222 { // Valid json: all good. | 227 { // Valid json: all good. |
| 223 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | 228 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 224 url_fetcher.SetResponseString(kValidTokenResponse); | 229 url_fetcher.SetResponseString(kValidTokenResponse); |
| 225 | 230 |
| 226 std::string at; | 231 std::string at; |
| 227 int expires_in; | 232 int expires_in; |
| 228 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 233 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenSuccessResponse( |
| 229 &url_fetcher, &at, &expires_in)); | 234 &url_fetcher, &at, &expires_in)); |
| 230 EXPECT_EQ("at1", at); | 235 EXPECT_EQ("at1", at); |
| 231 EXPECT_EQ(3600, expires_in); | 236 EXPECT_EQ(3600, expires_in); |
| 232 } | 237 } |
| 233 } | 238 { // Valid json: invalid error response. |
| 239 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 240 url_fetcher.SetResponseString(kTokenResponseNoAccessToken); |
| 241 |
| 242 std::string error; |
| 243 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenFailureResponse( |
| 244 &url_fetcher, &error)); |
| 245 EXPECT_TRUE(error.empty()); |
| 246 } |
| 247 { // Valid json: error response. |
| 248 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); |
| 249 url_fetcher.SetResponseString(kValidFailureTokenResponse); |
| 250 |
| 251 std::string error; |
| 252 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenFailureResponse( |
| 253 &url_fetcher, &error)); |
| 254 EXPECT_EQ("invalid_grant", error); |
| 255 } |
| 256 } |
| OLD | NEW |