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