Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/local_discovery/privet_confirm_api_flow_unittest.cc

Issue 23530007: Move API flows for cloud print into common class CloudPrintBaseApiFlow (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: AppendQueryParameter Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h" 7 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h"
8 #include "content/public/test/test_browser_thread.h" 8 #include "content/public/test/test_browser_thread.h"
9 #include "google_apis/gaia/google_service_auth_error.h" 9 #include "google_apis/gaia/google_service_auth_error.h"
10 #include "net/base/host_port_pair.h" 10 #include "net/base/host_port_pair.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { 46 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE {
47 return request_context_.get(); 47 return request_context_.get();
48 } 48 }
49 49
50 private: 50 private:
51 scoped_refptr<net::URLRequestContextGetter> request_context_; 51 scoped_refptr<net::URLRequestContextGetter> request_context_;
52 }; 52 };
53 53
54 class MockableConfirmCallback { 54 class MockableConfirmCallback {
55 public: 55 public:
56 MOCK_METHOD1(ConfirmCallback, void(PrivetConfirmApiCallFlow::Status)); 56 MOCK_METHOD1(ConfirmCallback, void(CloudPrintBaseApiFlow::Status));
57 57
58 PrivetConfirmApiCallFlow::ResponseCallback callback() { 58 PrivetConfirmApiCallFlow::ResponseCallback callback() {
59 return base::Bind(&MockableConfirmCallback::ConfirmCallback, 59 return base::Bind(&MockableConfirmCallback::ConfirmCallback,
60 base::Unretained(this)); 60 base::Unretained(this));
61 } 61 }
62 }; 62 };
63 63
64 class PrivetConfirmApiFlowTest : public testing::Test { 64 class PrivetConfirmApiFlowTest : public testing::Test {
65 public: 65 public:
66 PrivetConfirmApiFlowTest() 66 PrivetConfirmApiFlowTest()
(...skipping 15 matching lines...) Expand all
82 net::TestURLFetcherFactory fetcher_factory_; 82 net::TestURLFetcherFactory fetcher_factory_;
83 TestOAuth2TokenService token_service_; 83 TestOAuth2TokenService token_service_;
84 MockableConfirmCallback callback_; 84 MockableConfirmCallback callback_;
85 }; 85 };
86 86
87 TEST_F(PrivetConfirmApiFlowTest, SuccessOAuth2) { 87 TEST_F(PrivetConfirmApiFlowTest, SuccessOAuth2) {
88 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 88 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
89 &token_service_, 89 &token_service_,
90 GURL("http://SoMeUrL.com"), 90 GURL("http://SoMeUrL.com"),
91 callback_.callback()); 91 callback_.callback());
92 CloudPrintBaseApiFlow* cloudprint_flow =
93 confirm_flow.GetBaseApiFlowForTests();
92 94
93 confirm_flow.Start(); 95 confirm_flow.Start();
94 96
95 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 97 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
96 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 98 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
97 99
98 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 100 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
99 101
100 net::HttpRequestHeaders headers; 102 net::HttpRequestHeaders headers;
101 fetcher->GetExtraRequestHeaders(&headers); 103 fetcher->GetExtraRequestHeaders(&headers);
102 std::string oauth_header; 104 std::string oauth_header;
103 std::string proxy; 105 std::string proxy;
104 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header)); 106 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header));
105 EXPECT_EQ("Bearer SomeToken", oauth_header); 107 EXPECT_EQ("Bearer SomeToken", oauth_header);
106 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy)); 108 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
107 EXPECT_EQ("Chrome", proxy); 109 EXPECT_EQ("Chrome", proxy);
108 110
109 fetcher->SetResponseString(kSampleConfirmResponse); 111 fetcher->SetResponseString(kSampleConfirmResponse);
110 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 112 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
111 net::OK)); 113 net::OK));
112 fetcher->set_response_code(200); 114 fetcher->set_response_code(200);
113 115
114 EXPECT_CALL(callback_, ConfirmCallback(PrivetConfirmApiCallFlow::SUCCESS)); 116 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS));
115 117
116 fetcher->delegate()->OnURLFetchComplete(fetcher); 118 fetcher->delegate()->OnURLFetchComplete(fetcher);
117 } 119 }
118 120
119 TEST_F(PrivetConfirmApiFlowTest, SuccessCookies) { 121 TEST_F(PrivetConfirmApiFlowTest, SuccessCookies) {
120 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 122 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
121 1, 123 1,
122 "SomeToken", 124 "SomeToken",
123 GURL("http://SoMeUrL.com?token=tkn"), 125 GURL("http://SoMeUrL.com?token=tkn"),
124 callback_.callback()); 126 callback_.callback());
125 127
126 confirm_flow.Start(); 128 confirm_flow.Start();
127 129
128 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 130 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
129 131
130 EXPECT_EQ(GURL("http://SoMeUrL.com?token=tkn&xsrf=SomeToken&user=1"), 132 EXPECT_EQ(GURL("http://SoMeUrL.com?token=tkn&xsrf=SomeToken&user=1"),
131 fetcher->GetOriginalURL()); 133 fetcher->GetOriginalURL());
132 134
133 net::HttpRequestHeaders headers; 135 net::HttpRequestHeaders headers;
134 fetcher->GetExtraRequestHeaders(&headers); 136 fetcher->GetExtraRequestHeaders(&headers);
135 std::string proxy; 137 std::string proxy;
136 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy)); 138 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
137 EXPECT_EQ("Chrome", proxy); 139 EXPECT_EQ("Chrome", proxy);
138 140
139 fetcher->SetResponseString(kSampleConfirmResponse); 141 fetcher->SetResponseString(kSampleConfirmResponse);
140 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 142 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
141 net::OK)); 143 net::OK));
142 fetcher->set_response_code(200); 144 fetcher->set_response_code(200);
143 145
144 EXPECT_CALL(callback_, ConfirmCallback(PrivetConfirmApiCallFlow::SUCCESS)); 146 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS));
145 147
146 fetcher->delegate()->OnURLFetchComplete(fetcher); 148 fetcher->delegate()->OnURLFetchComplete(fetcher);
147 } 149 }
148 150
149 TEST_F(PrivetConfirmApiFlowTest, BadToken) { 151 TEST_F(PrivetConfirmApiFlowTest, BadToken) {
150 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 152 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
151 &token_service_, 153 &token_service_,
152 GURL("http://SoMeUrL.com"), 154 GURL("http://SoMeUrL.com"),
153 callback_.callback()); 155 callback_.callback());
154 156
155 confirm_flow.Start(); 157 confirm_flow.Start();
156 158
159 CloudPrintBaseApiFlow* cloudprint_flow =
160 confirm_flow.GetBaseApiFlowForTests();
161
157 EXPECT_CALL(callback_, 162 EXPECT_CALL(callback_,
158 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_TOKEN)); 163 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_TOKEN));
159 confirm_flow.OnGetTokenFailure(NULL, GoogleServiceAuthError( 164 cloudprint_flow->OnGetTokenFailure(NULL, GoogleServiceAuthError(
160 GoogleServiceAuthError::USER_NOT_SIGNED_UP)); 165 GoogleServiceAuthError::USER_NOT_SIGNED_UP));
161 } 166 }
162 167
163 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) { 168 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) {
164 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 169 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
165 &token_service_, 170 &token_service_,
166 GURL("http://SoMeUrL.com"), 171 GURL("http://SoMeUrL.com"),
167 callback_.callback()); 172 callback_.callback());
168 173
169 confirm_flow.Start(); 174 confirm_flow.Start();
170 175
171 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 176 CloudPrintBaseApiFlow* cloudprint_flow =
177 confirm_flow.GetBaseApiFlowForTests();
178
179 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
172 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 180 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
173 181
174 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 182 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
175 183
176 fetcher->SetResponseString(kFailedConfirmResponse); 184 fetcher->SetResponseString(kFailedConfirmResponse);
177 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 185 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
178 net::OK)); 186 net::OK));
179 fetcher->set_response_code(200); 187 fetcher->set_response_code(200);
180 188
181 EXPECT_CALL(callback_, 189 EXPECT_CALL(callback_,
182 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_FROM_SERVER)); 190 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_FROM_SERVER));
183 191
184 fetcher->delegate()->OnURLFetchComplete(fetcher); 192 fetcher->delegate()->OnURLFetchComplete(fetcher);
185 } 193 }
186 194
187 TEST_F(PrivetConfirmApiFlowTest, BadJson) { 195 TEST_F(PrivetConfirmApiFlowTest, BadJson) {
188 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 196 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
189 &token_service_, 197 &token_service_,
190 GURL("http://SoMeUrL.com"), 198 GURL("http://SoMeUrL.com"),
191 callback_.callback()); 199 callback_.callback());
192 200
193 confirm_flow.Start(); 201 confirm_flow.Start();
194 202
195 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 203 CloudPrintBaseApiFlow* cloudprint_flow =
204 confirm_flow.GetBaseApiFlowForTests();
205
206 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
196 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 207 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
197 208
198 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 209 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
199 210
200 fetcher->SetResponseString(kFailedConfirmResponseBadJson); 211 fetcher->SetResponseString(kFailedConfirmResponseBadJson);
201 fetcher->set_status(net::URLRequestStatus( 212 fetcher->set_status(net::URLRequestStatus(
202 net::URLRequestStatus::SUCCESS, 213 net::URLRequestStatus::SUCCESS,
203 net::OK)); 214 net::OK));
204 fetcher->set_response_code(200); 215 fetcher->set_response_code(200);
205 216
206 EXPECT_CALL(callback_, ConfirmCallback 217 EXPECT_CALL(callback_, ConfirmCallback
207 (PrivetConfirmApiCallFlow::ERROR_MALFORMED_RESPONSE)); 218 (CloudPrintBaseApiFlow::ERROR_MALFORMED_RESPONSE));
208 219
209 fetcher->delegate()->OnURLFetchComplete(fetcher); 220 fetcher->delegate()->OnURLFetchComplete(fetcher);
210 } 221 }
211 222
212 } // namespace 223 } // namespace
213 224
214 } // namespace local_discovery 225 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/local_discovery/privet_confirm_api_flow.cc ('k') | chrome/browser/local_discovery/privet_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698