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 "base/message_loop/message_loop.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
11 #include "content/public/test/test_browser_thread.h" | |
12 #include "google_apis/gaia/gaia_urls.h" | 11 #include "google_apis/gaia/gaia_urls.h" |
13 #include "google_apis/gaia/google_service_auth_error.h" | 12 #include "google_apis/gaia/google_service_auth_error.h" |
14 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 13 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
15 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 14 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
16 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
17 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
18 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
19 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
20 #include "net/url_request/url_fetcher_factory.h" | 19 #include "net/url_request/url_fetcher_factory.h" |
21 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
22 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
23 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "url/gurl.h" | 25 #include "url/gurl.h" |
27 | 26 |
28 using content::BrowserThread; | |
29 using net::ResponseCookies; | 27 using net::ResponseCookies; |
30 using net::ScopedURLFetcherFactory; | 28 using net::ScopedURLFetcherFactory; |
31 using net::TestURLFetcher; | 29 using net::TestURLFetcher; |
32 using net::URLFetcher; | 30 using net::URLFetcher; |
33 using net::URLFetcherDelegate; | 31 using net::URLFetcherDelegate; |
34 using net::URLFetcherFactory; | 32 using net::URLFetcherFactory; |
35 using net::URLRequestStatus; | 33 using net::URLRequestStatus; |
36 using testing::_; | 34 using testing::_; |
37 using testing::Return; | 35 using testing::Return; |
38 | 36 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 const base::Time& expiration_time)); | 75 const base::Time& expiration_time)); |
78 MOCK_METHOD1(OnGetTokenFailure, | 76 MOCK_METHOD1(OnGetTokenFailure, |
79 void(const GoogleServiceAuthError& error)); | 77 void(const GoogleServiceAuthError& error)); |
80 }; | 78 }; |
81 | 79 |
82 } // namespace | 80 } // namespace |
83 | 81 |
84 class OAuth2AccessTokenFetcherTest : public testing::Test { | 82 class OAuth2AccessTokenFetcherTest : public testing::Test { |
85 public: | 83 public: |
86 OAuth2AccessTokenFetcherTest() | 84 OAuth2AccessTokenFetcherTest() |
87 : ui_thread_(BrowserThread::UI, &message_loop_), | 85 : request_context_getter_(new net::TestURLRequestContextGetter( |
88 request_context_getter_(new net::TestURLRequestContextGetter( | 86 base::MessageLoopProxy::current())), |
89 message_loop_.message_loop_proxy())), | |
90 fetcher_(&consumer_, request_context_getter_) { | 87 fetcher_(&consumer_, request_context_getter_) { |
91 } | 88 } |
92 | 89 |
93 virtual ~OAuth2AccessTokenFetcherTest() {} | 90 virtual ~OAuth2AccessTokenFetcherTest() {} |
94 | 91 |
95 virtual TestURLFetcher* SetupGetAccessToken( | 92 virtual TestURLFetcher* SetupGetAccessToken( |
96 bool fetch_succeeds, int response_code, const std::string& body) { | 93 bool fetch_succeeds, int response_code, const std::string& body) { |
97 GURL url(GaiaUrls::GetInstance()->oauth2_token_url()); | 94 GURL url(GaiaUrls::GetInstance()->oauth2_token_url()); |
98 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_); | 95 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_); |
99 URLRequestStatus::Status status = | 96 URLRequestStatus::Status status = |
100 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; | 97 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; |
101 url_fetcher->set_status(URLRequestStatus(status, 0)); | 98 url_fetcher->set_status(URLRequestStatus(status, 0)); |
102 | 99 |
103 if (response_code != 0) | 100 if (response_code != 0) |
104 url_fetcher->set_response_code(response_code); | 101 url_fetcher->set_response_code(response_code); |
105 | 102 |
106 if (!body.empty()) | 103 if (!body.empty()) |
107 url_fetcher->SetResponseString(body); | 104 url_fetcher->SetResponseString(body); |
108 | 105 |
109 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) | 106 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) |
110 .WillOnce(Return(url_fetcher)); | 107 .WillOnce(Return(url_fetcher)); |
111 return url_fetcher; | 108 return url_fetcher; |
112 } | 109 } |
113 | 110 |
114 protected: | 111 protected: |
115 base::MessageLoop message_loop_; | 112 content::TestBrowserThreadBundle thread_bundle_; |
116 content::TestBrowserThread ui_thread_; | |
117 MockUrlFetcherFactory factory_; | 113 MockUrlFetcherFactory factory_; |
118 MockOAuth2AccessTokenConsumer consumer_; | 114 MockOAuth2AccessTokenConsumer consumer_; |
119 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 115 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
120 OAuth2AccessTokenFetcher fetcher_; | 116 OAuth2AccessTokenFetcher fetcher_; |
121 }; | 117 }; |
122 | 118 |
123 // These four tests time out, see http://crbug.com/113446. | 119 // These four tests time out, see http://crbug.com/113446. |
124 TEST_F(OAuth2AccessTokenFetcherTest, DISABLED_GetAccessTokenRequestFailure) { | 120 TEST_F(OAuth2AccessTokenFetcherTest, DISABLED_GetAccessTokenRequestFailure) { |
125 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, std::string()); | 121 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, std::string()); |
126 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | 122 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 url_fetcher.SetResponseString(kValidTokenResponse); | 224 url_fetcher.SetResponseString(kValidTokenResponse); |
229 | 225 |
230 std::string at; | 226 std::string at; |
231 int expires_in; | 227 int expires_in; |
232 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 228 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( |
233 &url_fetcher, &at, &expires_in)); | 229 &url_fetcher, &at, &expires_in)); |
234 EXPECT_EQ("at1", at); | 230 EXPECT_EQ("at1", at); |
235 EXPECT_EQ(3600, expires_in); | 231 EXPECT_EQ(3600, expires_in); |
236 } | 232 } |
237 } | 233 } |
OLD | NEW |