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 OAuth2MintTokenFlow. | 5 // A complete set of unit tests for OAuth2MintTokenFlow. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 // Replaces the global URLFetcher factory so the test can return a custom | 54 // Replaces the global URLFetcher factory so the test can return a custom |
55 // URLFetcher to complete requests. | 55 // URLFetcher to complete requests. |
56 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, | 56 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, |
57 public URLFetcherFactory { | 57 public URLFetcherFactory { |
58 public: | 58 public: |
59 MockUrlFetcherFactory() | 59 MockUrlFetcherFactory() |
60 : ScopedURLFetcherFactory(this) { | 60 : ScopedURLFetcherFactory(this) { |
61 } | 61 } |
62 virtual ~MockUrlFetcherFactory() {} | 62 virtual ~MockUrlFetcherFactory() {} |
63 | 63 |
64 MOCK_METHOD4( | 64 MOCK_METHOD4(CreateURLFetcherMock, |
65 CreateURLFetcher, | 65 URLFetcher*(int id, |
66 URLFetcher* (int id, | 66 const GURL& url, |
67 const GURL& url, | 67 URLFetcher::RequestType request_type, |
68 URLFetcher::RequestType request_type, | 68 URLFetcherDelegate* d)); |
69 URLFetcherDelegate* d)); | 69 |
| 70 scoped_ptr<URLFetcher> CreateURLFetcher(int id, |
| 71 const GURL& url, |
| 72 URLFetcher::RequestType request_type, |
| 73 URLFetcherDelegate* d) override { |
| 74 return scoped_ptr<URLFetcher>( |
| 75 CreateURLFetcherMock(id, url, request_type, d)); |
| 76 } |
70 }; | 77 }; |
71 | 78 |
72 class MockApiCallFlow : public OAuth2ApiCallFlow { | 79 class MockApiCallFlow : public OAuth2ApiCallFlow { |
73 public: | 80 public: |
74 MockApiCallFlow() {} | 81 MockApiCallFlow() {} |
75 ~MockApiCallFlow() {} | 82 ~MockApiCallFlow() {} |
76 | 83 |
77 MOCK_METHOD0(CreateApiCallUrl, GURL ()); | 84 MOCK_METHOD0(CreateApiCallUrl, GURL ()); |
78 MOCK_METHOD0(CreateApiCallBody, std::string ()); | 85 MOCK_METHOD0(CreateApiCallBody, std::string ()); |
79 MOCK_METHOD1(ProcessApiCallSuccess, | 86 MOCK_METHOD1(ProcessApiCallSuccess, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return url_fetcher; | 118 return url_fetcher; |
112 } | 119 } |
113 | 120 |
114 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { | 121 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { |
115 std::string body(CreateBody()); | 122 std::string body(CreateBody()); |
116 GURL url(CreateApiUrl()); | 123 GURL url(CreateApiUrl()); |
117 EXPECT_CALL(flow_, CreateApiCallBody()).WillOnce(Return(body)); | 124 EXPECT_CALL(flow_, CreateApiCallBody()).WillOnce(Return(body)); |
118 EXPECT_CALL(flow_, CreateApiCallUrl()).WillOnce(Return(url)); | 125 EXPECT_CALL(flow_, CreateApiCallUrl()).WillOnce(Return(url)); |
119 TestURLFetcher* url_fetcher = | 126 TestURLFetcher* url_fetcher = |
120 CreateURLFetcher(url, succeeds, status, std::string()); | 127 CreateURLFetcher(url, succeeds, status, std::string()); |
121 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) | 128 EXPECT_CALL(factory_, CreateURLFetcherMock(_, url, _, _)) |
122 .WillOnce(Return(url_fetcher)); | 129 .WillOnce(Return(url_fetcher)); |
123 return url_fetcher; | 130 return url_fetcher; |
124 } | 131 } |
125 | 132 |
126 base::MessageLoop message_loop_; | 133 base::MessageLoop message_loop_; |
127 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 134 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
128 StrictMock<MockApiCallFlow> flow_; | 135 StrictMock<MockApiCallFlow> flow_; |
129 MockUrlFetcherFactory factory_; | 136 MockUrlFetcherFactory factory_; |
130 }; | 137 }; |
131 | 138 |
(...skipping 25 matching lines...) Expand all Loading... |
157 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); | 164 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); |
158 flow_.Start(request_context_getter_.get(), kAccessToken); | 165 flow_.Start(request_context_getter_.get(), kAccessToken); |
159 HttpRequestHeaders headers; | 166 HttpRequestHeaders headers; |
160 url_fetcher->GetExtraRequestHeaders(&headers); | 167 url_fetcher->GetExtraRequestHeaders(&headers); |
161 std::string auth_header; | 168 std::string auth_header; |
162 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); | 169 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); |
163 EXPECT_EQ("Bearer access_token", auth_header); | 170 EXPECT_EQ("Bearer access_token", auth_header); |
164 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); | 171 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); |
165 EXPECT_EQ(body, url_fetcher->upload_data()); | 172 EXPECT_EQ(body, url_fetcher->upload_data()); |
166 } | 173 } |
OLD | NEW |