| 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/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/common/net/url_fetcher.h" | 15 #include "content/common/net/url_fetcher.h" |
| 16 #include "content/public/common/url_fetcher_delegate.h" |
| 16 #include "content/test/test_url_fetcher_factory.h" | 17 #include "content/test/test_url_fetcher_factory.h" |
| 17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 19 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using ::testing::_; | 24 using ::testing::_; |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 // Responds as though OAuth returned from the server. | 27 // Responds as though OAuth returned from the server. |
| 27 class MockOAuthFetcher : public URLFetcher { | 28 class MockOAuthFetcher : public URLFetcher { |
| 28 public: | 29 public: |
| 29 MockOAuthFetcher(int response_code, | 30 MockOAuthFetcher(int response_code, |
| 30 int max_failure_count, | 31 int max_failure_count, |
| 31 const GURL& url, | 32 const GURL& url, |
| 32 const std::string& results, | 33 const std::string& results, |
| 33 URLFetcher::RequestType request_type, | 34 URLFetcher::RequestType request_type, |
| 34 URLFetcher::Delegate* d) | 35 content::URLFetcherDelegate* d) |
| 35 : URLFetcher(url, request_type, d), | 36 : URLFetcher(url, request_type, d), |
| 36 response_code_(response_code), | 37 response_code_(response_code), |
| 37 max_failure_count_(max_failure_count), | 38 max_failure_count_(max_failure_count), |
| 38 current_failure_count_(0), | 39 current_failure_count_(0), |
| 39 url_(url), | 40 url_(url), |
| 40 results_(results) { } | 41 results_(results) { } |
| 41 virtual ~MockOAuthFetcher() { } | 42 virtual ~MockOAuthFetcher() { } |
| 42 | 43 |
| 43 virtual void Start() { | 44 virtual void Start() { |
| 44 if ((response_code_ != RC_REQUEST_OK) && (max_failure_count_ != -1) && | 45 if ((response_code_ != RC_REQUEST_OK) && (max_failure_count_ != -1) && |
| 45 (current_failure_count_ == max_failure_count_)) { | 46 (current_failure_count_ == max_failure_count_)) { |
| 46 response_code_ = RC_REQUEST_OK; | 47 response_code_ = RC_REQUEST_OK; |
| 47 } | 48 } |
| 48 | 49 |
| 49 net::URLRequestStatus::Status code = net::URLRequestStatus::SUCCESS; | 50 net::URLRequestStatus::Status code = net::URLRequestStatus::SUCCESS; |
| 50 if (response_code_ != RC_REQUEST_OK) { | 51 if (response_code_ != RC_REQUEST_OK) { |
| 51 code = net::URLRequestStatus::FAILED; | 52 code = net::URLRequestStatus::FAILED; |
| 52 current_failure_count_++; | 53 current_failure_count_++; |
| 53 } | 54 } |
| 54 net::URLRequestStatus status(code, 0); | 55 status_ = net::URLRequestStatus(code, 0); |
| 55 delegate()->OnURLFetchComplete(this, | 56 |
| 56 url_, | 57 delegate()->OnURLFetchComplete(this); |
| 57 status, | 58 } |
| 58 response_code_, | 59 |
| 59 net::ResponseCookies(), | 60 virtual const GURL& url() const { |
| 60 results_); | 61 return url_; |
| 62 } |
| 63 |
| 64 virtual const net::URLRequestStatus& status() const { |
| 65 return status_; |
| 66 } |
| 67 |
| 68 virtual int response_code() const { |
| 69 return response_code_; |
| 70 } |
| 71 |
| 72 virtual bool GetResponseAsString(std::string* out_response_string) const { |
| 73 *out_response_string = results_; |
| 74 return true; |
| 61 } | 75 } |
| 62 | 76 |
| 63 private: | 77 private: |
| 78 net::URLRequestStatus status_; |
| 64 int response_code_; | 79 int response_code_; |
| 65 int max_failure_count_; | 80 int max_failure_count_; |
| 66 int current_failure_count_; | 81 int current_failure_count_; |
| 67 GURL url_; | 82 GURL url_; |
| 68 std::string results_; | 83 std::string results_; |
| 69 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcher); | 84 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcher); |
| 70 }; | 85 }; |
| 71 | 86 |
| 72 class MockOAuthFetcherFactory : public URLFetcher::Factory, | 87 class MockOAuthFetcherFactory : public URLFetcher::Factory, |
| 73 public ScopedURLFetcherFactory { | 88 public ScopedURLFetcherFactory { |
| 74 public: | 89 public: |
| 75 MockOAuthFetcherFactory() | 90 MockOAuthFetcherFactory() |
| 76 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 91 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 77 response_code_(RC_REQUEST_OK) { | 92 response_code_(RC_REQUEST_OK) { |
| 78 } | 93 } |
| 79 ~MockOAuthFetcherFactory() {} | 94 ~MockOAuthFetcherFactory() {} |
| 80 virtual URLFetcher* CreateURLFetcher( | 95 virtual URLFetcher* CreateURLFetcher( |
| 81 int id, | 96 int id, |
| 82 const GURL& url, | 97 const GURL& url, |
| 83 URLFetcher::RequestType request_type, | 98 URLFetcher::RequestType request_type, |
| 84 URLFetcher::Delegate* d) { | 99 content::URLFetcherDelegate* d) { |
| 85 return new MockOAuthFetcher( | 100 return new MockOAuthFetcher( |
| 86 response_code_, | 101 response_code_, |
| 87 max_failure_count_, | 102 max_failure_count_, |
| 88 url, | 103 url, |
| 89 results_, | 104 results_, |
| 90 request_type, | 105 request_type, |
| 91 d); | 106 d); |
| 92 } | 107 } |
| 93 void set_response_code(int response_code) { | 108 void set_response_code(int response_code) { |
| 94 response_code_ = response_code; | 109 response_code_ = response_code; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 factory.set_results(kDummyRefreshTokenResult); | 254 factory.set_results(kDummyRefreshTokenResult); |
| 240 | 255 |
| 241 OAuthClientInfo client_info; | 256 OAuthClientInfo client_info; |
| 242 client_info.client_id = "test_client_id"; | 257 client_info.client_id = "test_client_id"; |
| 243 client_info.client_secret = "test_client_secret"; | 258 client_info.client_secret = "test_client_secret"; |
| 244 GaiaOAuthClient auth(kGaiaOAuth2Url, | 259 GaiaOAuthClient auth(kGaiaOAuth2Url, |
| 245 profile_.GetRequestContext()); | 260 profile_.GetRequestContext()); |
| 246 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | 261 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); |
| 247 } | 262 } |
| 248 } // namespace gaia | 263 } // namespace gaia |
| OLD | NEW |