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