OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // A complete set of unit tests for OAuth2RevocationFetcher. |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "chrome/common/net/gaia/gaia_urls.h" |
| 12 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 13 #include "chrome/common/net/http_return.h" |
| 14 #include "chrome/common/net/gaia/oauth2_revocation_consumer.h" |
| 15 #include "chrome/common/net/gaia/oauth2_revocation_fetcher.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/common/url_fetcher.h" |
| 18 #include "content/public/common/url_fetcher_delegate.h" |
| 19 #include "content/public/common/url_fetcher_factory.h" |
| 20 #include "content/test/test_browser_thread.h" |
| 21 #include "content/test/test_url_fetcher_factory.h" |
| 22 #include "googleurl/src/gurl.h" |
| 23 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_status.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 |
| 28 using content::BrowserThread; |
| 29 using content::URLFetcher; |
| 30 using content::URLFetcherDelegate; |
| 31 using content::URLFetcherFactory; |
| 32 using net::ResponseCookies; |
| 33 using net::URLRequestStatus; |
| 34 using testing::_; |
| 35 using testing::Return; |
| 36 |
| 37 namespace { |
| 38 |
| 39 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, |
| 40 public URLFetcherFactory { |
| 41 public: |
| 42 MockUrlFetcherFactory() |
| 43 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 44 } |
| 45 virtual ~MockUrlFetcherFactory() {} |
| 46 |
| 47 MOCK_METHOD4( |
| 48 CreateURLFetcher, |
| 49 URLFetcher* (int id, |
| 50 const GURL& url, |
| 51 URLFetcher::RequestType request_type, |
| 52 URLFetcherDelegate* d)); |
| 53 }; |
| 54 |
| 55 class MockOAuth2RevocationConsumer : public OAuth2RevocationConsumer { |
| 56 public: |
| 57 MockOAuth2RevocationConsumer() {} |
| 58 ~MockOAuth2RevocationConsumer() {} |
| 59 |
| 60 MOCK_METHOD1(OnRevocationSuccess, void()); |
| 61 MOCK_METHOD1(OnRevocationFailure, |
| 62 void(const GoogleServiceAuthError& error)); |
| 63 }; |
| 64 |
| 65 class OAuth2RevocationFetcherTest : public testing::Test { |
| 66 public: |
| 67 OAuth2RevocationFetcherTest() |
| 68 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 69 fetcher_(&consumer_, profile_.GetRequestContext()) { |
| 70 } |
| 71 |
| 72 virtual ~OAuth2RevocationFetcherTest() { } |
| 73 |
| 74 virtual TestURLFetcher* SetupRevocation( |
| 75 bool fetch_succeeds, int response_code) { |
| 76 GURL url = OAuth2RevocationFetcher::MakeRevocationUrl(); |
| 77 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_); |
| 78 URLRequestStatus::Status status = |
| 79 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; |
| 80 url_fetcher->set_status(URLRequestStatus(status, 0)); |
| 81 |
| 82 if (response_code != 0) |
| 83 url_fetcher->set_response_code(response_code); |
| 84 |
| 85 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) |
| 86 .WillOnce(Return(url_fetcher)); |
| 87 return url_fetcher; |
| 88 } |
| 89 |
| 90 protected: |
| 91 MessageLoop message_loop_; |
| 92 content::TestBrowserThread ui_thread_; |
| 93 MockUrlFetcherFactory factory_; |
| 94 MockOAuth2RevocationConsumer consumer_; |
| 95 TestingProfile profile_; |
| 96 OAuth2RevocationFetcher fetcher_; |
| 97 }; |
| 98 |
| 99 TEST_F(OAuth2RevocationFetcherTest, RequestFailure) { |
| 100 TestURLFetcher* url_fetcher = SetupRevocation(false, 0); |
| 101 EXPECT_CALL(consumer_, OnRevocationFailure(_)).Times(1); |
| 102 fetcher_.Start("access_token", "client_id", "origin"); |
| 103 fetcher_.OnURLFetchComplete(url_fetcher); |
| 104 } |
| 105 |
| 106 TEST_F(OAuth2RevocationFetcherTest, ResponseCodeFailure) { |
| 107 TestURLFetcher* url_fetcher = SetupRevocation(true, RC_FORBIDDEN); |
| 108 EXPECT_CALL(consumer_, OnRevocationFailure(_)).Times(1); |
| 109 fetcher_.Start("access_token", "client_id", "origin"); |
| 110 fetcher_.OnURLFetchComplete(url_fetcher); |
| 111 } |
| 112 |
| 113 TEST_F(OAuth2RevocationFetcherTest, Success) { |
| 114 TestURLFetcher* url_fetcher = SetupRevocation(true, RC_REQUEST_OK); |
| 115 EXPECT_CALL(consumer_, OnRevocationSuccess()).Times(1); |
| 116 fetcher_.Start("access_token", "client_id", "origin"); |
| 117 fetcher_.OnURLFetchComplete(url_fetcher); |
| 118 } |
OLD | NEW |