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

Side by Side Diff: sync/internal_api/attachments/attachment_uploader_impl_unittest.cc

Issue 304253010: Add authentication support to AttachmentUploaderImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "sync/internal_api/public/attachments/attachment_uploader_impl.h" 5 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/ref_counted_memory.h" 10 #include "base/memory/ref_counted_memory.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/synchronization/lock.h"
15 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/non_thread_safe.h" 16 #include "base/threading/non_thread_safe.h"
15 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
18 #include "google_apis/gaia/fake_oauth2_token_service.h"
19 #include "google_apis/gaia/gaia_constants.h"
20 #include "google_apis/gaia/oauth2_token_service_request.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h" 21 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "net/test/embedded_test_server/http_request.h" 22 #include "net/test/embedded_test_server/http_request.h"
18 #include "net/test/embedded_test_server/http_response.h" 23 #include "net/test/embedded_test_server/http_response.h"
19 #include "net/url_request/url_request_test_util.h" 24 #include "net/url_request/url_request_test_util.h"
20 #include "sync/api/attachments/attachment.h" 25 #include "sync/api/attachments/attachment.h"
21 #include "sync/protocol/sync.pb.h" 26 #include "sync/protocol/sync.pb.h"
27 #include "testing/gmock/include/gmock/gmock-matchers.h"
22 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
23 29
24 namespace { 30 namespace {
25 31
26 const char kAttachmentData[] = "some data"; 32 const char kAttachmentData[] = "some data";
33 const char kAccountId[] = "some-account-id";
34 const char kAccessToken[] = "some-access-token";
35 const char kAuthorization[] = "Authorization";
27 36
28 } // namespace 37 } // namespace
29 38
30 namespace syncer { 39 namespace syncer {
31 40
32 using net::test_server::BasicHttpResponse; 41 using net::test_server::BasicHttpResponse;
33 using net::test_server::HttpRequest; 42 using net::test_server::HttpRequest;
34 using net::test_server::HttpResponse; 43 using net::test_server::HttpResponse;
35 44
36 class RequestHandler; 45 class RequestHandler;
37 46
47 // A mock implementation of an OAuth2TokenService.
48 //
49 // Use |SetResponse| to vary the response to token requests.
50 //
51 // Use |num_invalidate_token| and |last_token_invalidated| to check the number
52 // of invalidate token operations performed and the last token invalidated.
53 class MockOAuth2TokenService : public FakeOAuth2TokenService {
54 public:
55 MockOAuth2TokenService();
56 virtual ~MockOAuth2TokenService();
57
58 void SetResponse(const GoogleServiceAuthError& error,
59 const std::string& access_token,
60 const base::Time& expiration);
61
62 int num_invalidate_token() const { return num_invalidate_token_; }
63
64 const std::string& last_token_invalidated() const {
65 return last_token_invalidated_;
66 }
67
68 protected:
69 virtual void FetchOAuth2Token(RequestImpl* request,
70 const std::string& account_id,
71 net::URLRequestContextGetter* getter,
72 const std::string& client_id,
73 const std::string& client_secret,
74 const ScopeSet& scopes) OVERRIDE;
75
76 virtual void InvalidateOAuth2Token(const std::string& account_id,
77 const std::string& client_id,
78 const ScopeSet& scopes,
79 const std::string& access_token) OVERRIDE;
80
81 private:
82 GoogleServiceAuthError response_error_;
83 std::string response_access_token_;
84 base::Time response_expiration_;
85 int num_invalidate_token_;
86 std::string last_token_invalidated_;
87 };
88
89 MockOAuth2TokenService::MockOAuth2TokenService()
90 : response_error_(GoogleServiceAuthError::AuthErrorNone()),
91 response_access_token_(kAccessToken),
92 response_expiration_(base::Time::Max()),
93 num_invalidate_token_(0) {
94 }
95
96 MockOAuth2TokenService::~MockOAuth2TokenService() {
97 }
98
99 void MockOAuth2TokenService::SetResponse(const GoogleServiceAuthError& error,
100 const std::string& access_token,
101 const base::Time& expiration) {
102 response_error_ = error;
103 response_access_token_ = access_token;
104 response_expiration_ = expiration;
105 }
106
107 void MockOAuth2TokenService::FetchOAuth2Token(
108 RequestImpl* request,
109 const std::string& account_id,
110 net::URLRequestContextGetter* getter,
111 const std::string& client_id,
112 const std::string& client_secret,
113 const ScopeSet& scopes) {
114 base::MessageLoop::current()->PostTask(
115 FROM_HERE,
116 base::Bind(&OAuth2TokenService::RequestImpl::InformConsumer,
117 request->AsWeakPtr(),
118 response_error_,
119 response_access_token_,
120 response_expiration_));
121 }
122
123 void MockOAuth2TokenService::InvalidateOAuth2Token(
124 const std::string& account_id,
125 const std::string& client_id,
126 const ScopeSet& scopes,
127 const std::string& access_token) {
128 ++num_invalidate_token_;
129 last_token_invalidated_ = access_token;
130 }
131
132 class TokenServiceProvider
133 : public OAuth2TokenServiceRequest::TokenServiceProvider,
134 base::NonThreadSafe {
135 public:
136 TokenServiceProvider(OAuth2TokenService* token_service);
137 virtual ~TokenServiceProvider();
138
139 // OAuth2TokenService::TokenServiceProvider implementation.
140 virtual scoped_refptr<base::SingleThreadTaskRunner>
141 GetTokenServiceTaskRunner() OVERRIDE;
142 virtual OAuth2TokenService* GetTokenService() OVERRIDE;
143
144 private:
145 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
146 OAuth2TokenService* token_service_;
147 };
148
149 TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service)
150 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
151 token_service_(token_service) {
152 DCHECK(token_service_);
153 }
154
155 TokenServiceProvider::~TokenServiceProvider() {
156 }
157
158 scoped_refptr<base::SingleThreadTaskRunner>
159 TokenServiceProvider::GetTokenServiceTaskRunner() {
160 return task_runner_;
161 }
162
163 OAuth2TokenService* TokenServiceProvider::GetTokenService() {
164 DCHECK(task_runner_->BelongsToCurrentThread());
165 return token_service_;
166 }
167
38 // Text fixture for AttachmentUploaderImpl test. 168 // Text fixture for AttachmentUploaderImpl test.
39 // 169 //
40 // This fixture provides an embedded HTTP server for interacting with 170 // This fixture provides an embedded HTTP server and a mock OAuth2 token service
41 // AttachmentUploaderImpl. 171 // for interacting with AttachmentUploaderImpl
42 class AttachmentUploaderImplTest : public testing::Test, 172 class AttachmentUploaderImplTest : public testing::Test,
43 public base::NonThreadSafe { 173 public base::NonThreadSafe {
44 public: 174 public:
45 void OnRequestReceived(const HttpRequest& request); 175 void OnRequestReceived(const HttpRequest& request);
46 176
47 protected: 177 protected:
48 AttachmentUploaderImplTest(); 178 AttachmentUploaderImplTest();
49 virtual void SetUp(); 179 virtual void SetUp();
50 180
51 // Run the message loop until UploadDone has been invoked |num_uploads| times. 181 // Run the message loop until UploadDone has been invoked |num_uploads| times.
52 void RunAndWaitFor(int num_uploads); 182 void RunAndWaitFor(int num_uploads);
53 183
54 scoped_ptr<AttachmentUploader>& uploader(); 184 scoped_ptr<AttachmentUploader>& uploader();
55 const AttachmentUploader::UploadCallback& upload_callback() const; 185 const AttachmentUploader::UploadCallback& upload_callback() const;
56 std::vector<HttpRequest>& http_requests_received(); 186 std::vector<HttpRequest>& http_requests_received();
57 std::vector<AttachmentUploader::UploadResult>& upload_results(); 187 std::vector<AttachmentUploader::UploadResult>& upload_results();
58 std::vector<AttachmentId>& updated_attachment_ids(); 188 std::vector<AttachmentId>& updated_attachment_ids();
189 MockOAuth2TokenService& token_service();
190 base::MessageLoopForIO& message_loop();
191 RequestHandler& request_handler();
59 192
60 private: 193 private:
61 // An UploadCallback invoked by AttachmentUploaderImpl. 194 // An UploadCallback invoked by AttachmentUploaderImpl.
62 void UploadDone(const AttachmentUploader::UploadResult& result, 195 void UploadDone(const AttachmentUploader::UploadResult& result,
63 const AttachmentId& updated_attachment_id); 196 const AttachmentId& updated_attachment_id);
64 197
65 base::MessageLoopForIO message_loop_; 198 base::MessageLoopForIO message_loop_;
66 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 199 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
67 scoped_ptr<RequestHandler> request_handler_; 200 scoped_ptr<RequestHandler> request_handler_;
68 scoped_ptr<AttachmentUploader> uploader_; 201 scoped_ptr<AttachmentUploader> uploader_;
69 AttachmentUploader::UploadCallback upload_callback_; 202 AttachmentUploader::UploadCallback upload_callback_;
70 net::test_server::EmbeddedTestServer server_; 203 net::test_server::EmbeddedTestServer server_;
71
72 // A closure that signals an upload has finished. 204 // A closure that signals an upload has finished.
73 base::Closure signal_upload_done_; 205 base::Closure signal_upload_done_;
74 std::vector<HttpRequest> http_requests_received_; 206 std::vector<HttpRequest> http_requests_received_;
75 std::vector<AttachmentUploader::UploadResult> upload_results_; 207 std::vector<AttachmentUploader::UploadResult> upload_results_;
76 std::vector<AttachmentId> updated_attachment_ids_; 208 std::vector<AttachmentId> updated_attachment_ids_;
209 scoped_ptr<MockOAuth2TokenService> token_service_;
77 210
78 // Must be last data member. 211 // Must be last data member.
79 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_; 212 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_;
80 }; 213 };
81 214
82 // Handles HTTP requests received by the EmbeddedTestServer. 215 // Handles HTTP requests received by the EmbeddedTestServer.
216 //
217 // Responds with HTTP_OK by default. See |SetStatusCode|.
83 class RequestHandler : public base::NonThreadSafe { 218 class RequestHandler : public base::NonThreadSafe {
84 public: 219 public:
85 // Construct a RequestHandler that will PostTask to |test| using 220 // Construct a RequestHandler that will PostTask to |test| using
86 // |test_task_runner|. 221 // |test_task_runner|.
87 RequestHandler( 222 RequestHandler(
88 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, 223 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner,
89 const base::WeakPtr<AttachmentUploaderImplTest>& test); 224 const base::WeakPtr<AttachmentUploaderImplTest>& test);
90 225
91 ~RequestHandler(); 226 ~RequestHandler();
92 227
93 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request); 228 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request);
94 229
230 // Set the HTTP status code to respond with.
231 void SetStatusCode(const net::HttpStatusCode& status_code);
232
233 // Returns the HTTP status code that will be used in responses.
234 net::HttpStatusCode GetStatusCode() const;
235
95 private: 236 private:
237 // Protects status_code_.
238 mutable base::Lock mutex_;
239 net::HttpStatusCode status_code_;
240
96 scoped_refptr<base::SingleThreadTaskRunner> test_task_runner_; 241 scoped_refptr<base::SingleThreadTaskRunner> test_task_runner_;
97 base::WeakPtr<AttachmentUploaderImplTest> test_; 242 base::WeakPtr<AttachmentUploaderImplTest> test_;
98 }; 243 };
99 244
100 AttachmentUploaderImplTest::AttachmentUploaderImplTest() 245 AttachmentUploaderImplTest::AttachmentUploaderImplTest()
101 : weak_ptr_factory_(this) { 246 : weak_ptr_factory_(this) {
102 } 247 }
103 248
104 void AttachmentUploaderImplTest::OnRequestReceived(const HttpRequest& request) { 249 void AttachmentUploaderImplTest::OnRequestReceived(const HttpRequest& request) {
105 DCHECK(CalledOnValidThread()); 250 DCHECK(CalledOnValidThread());
106 http_requests_received_.push_back(request); 251 http_requests_received_.push_back(request);
107 } 252 }
108 253
109 void AttachmentUploaderImplTest::SetUp() { 254 void AttachmentUploaderImplTest::SetUp() {
110 DCHECK(CalledOnValidThread()); 255 DCHECK(CalledOnValidThread());
111 request_handler_.reset(new RequestHandler(message_loop_.message_loop_proxy(), 256 request_handler_.reset(new RequestHandler(message_loop_.message_loop_proxy(),
112 weak_ptr_factory_.GetWeakPtr())); 257 weak_ptr_factory_.GetWeakPtr()));
113 url_request_context_getter_ = 258 url_request_context_getter_ =
114 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()); 259 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy());
115 260
116 ASSERT_TRUE(server_.InitializeAndWaitUntilReady()); 261 ASSERT_TRUE(server_.InitializeAndWaitUntilReady());
117 server_.RegisterRequestHandler( 262 server_.RegisterRequestHandler(
118 base::Bind(&RequestHandler::HandleRequest, 263 base::Bind(&RequestHandler::HandleRequest,
119 base::Unretained(request_handler_.get()))); 264 base::Unretained(request_handler_.get())));
120 265
121 std::string url_prefix( 266 std::string url_prefix(
122 base::StringPrintf("http://localhost:%d/uploads/", server_.port())); 267 base::StringPrintf("http://localhost:%d/uploads/", server_.port()));
123 268
124 uploader().reset( 269 token_service_.reset(new MockOAuth2TokenService);
125 new AttachmentUploaderImpl(url_prefix, url_request_context_getter_)); 270 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider>
271 token_service_provider(new TokenServiceProvider(token_service_.get()));
272
273 uploader().reset(new AttachmentUploaderImpl(url_prefix,
274 url_request_context_getter_,
275 kAccountId,
276 token_service_provider.Pass()));
277
126 upload_callback_ = base::Bind(&AttachmentUploaderImplTest::UploadDone, 278 upload_callback_ = base::Bind(&AttachmentUploaderImplTest::UploadDone,
127 base::Unretained(this)); 279 base::Unretained(this));
128 } 280 }
129 281
130 void AttachmentUploaderImplTest::RunAndWaitFor(int num_uploads) { 282 void AttachmentUploaderImplTest::RunAndWaitFor(int num_uploads) {
131 for (int i = 0; i < num_uploads; ++i) { 283 for (int i = 0; i < num_uploads; ++i) {
132 // Run the loop until one upload completes. 284 // Run the loop until one upload completes.
133 base::RunLoop run_loop; 285 base::RunLoop run_loop;
134 signal_upload_done_ = run_loop.QuitClosure(); 286 signal_upload_done_ = run_loop.QuitClosure();
135 run_loop.Run(); 287 run_loop.Run();
(...skipping 16 matching lines...) Expand all
152 std::vector<AttachmentUploader::UploadResult>& 304 std::vector<AttachmentUploader::UploadResult>&
153 AttachmentUploaderImplTest::upload_results() { 305 AttachmentUploaderImplTest::upload_results() {
154 return upload_results_; 306 return upload_results_;
155 } 307 }
156 308
157 std::vector<AttachmentId>& 309 std::vector<AttachmentId>&
158 AttachmentUploaderImplTest::updated_attachment_ids() { 310 AttachmentUploaderImplTest::updated_attachment_ids() {
159 return updated_attachment_ids_; 311 return updated_attachment_ids_;
160 } 312 }
161 313
314 MockOAuth2TokenService& AttachmentUploaderImplTest::token_service() {
315 return *token_service_;
316 }
317
318 base::MessageLoopForIO& AttachmentUploaderImplTest::message_loop() {
319 return message_loop_;
320 }
321
322 RequestHandler& AttachmentUploaderImplTest::request_handler() {
323 return *request_handler_;
324 }
325
162 void AttachmentUploaderImplTest::UploadDone( 326 void AttachmentUploaderImplTest::UploadDone(
163 const AttachmentUploader::UploadResult& result, 327 const AttachmentUploader::UploadResult& result,
164 const AttachmentId& updated_attachment_id) { 328 const AttachmentId& updated_attachment_id) {
165 DCHECK(CalledOnValidThread()); 329 DCHECK(CalledOnValidThread());
166 upload_results_.push_back(result); 330 upload_results_.push_back(result);
167 updated_attachment_ids_.push_back(updated_attachment_id); 331 updated_attachment_ids_.push_back(updated_attachment_id);
168 signal_upload_done_.Run(); 332 signal_upload_done_.Run();
169 } 333 }
170 334
171 RequestHandler::RequestHandler( 335 RequestHandler::RequestHandler(
172 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, 336 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner,
173 const base::WeakPtr<AttachmentUploaderImplTest>& test) 337 const base::WeakPtr<AttachmentUploaderImplTest>& test)
174 : test_task_runner_(test_task_runner), test_(test) { 338 : status_code_(net::HTTP_OK),
339 test_task_runner_(test_task_runner),
340 test_(test) {
175 DetachFromThread(); 341 DetachFromThread();
176 } 342 }
177 343
178 RequestHandler::~RequestHandler() { 344 RequestHandler::~RequestHandler() {
179 DetachFromThread(); 345 DetachFromThread();
180 } 346 }
181 347
182 scoped_ptr<HttpResponse> RequestHandler::HandleRequest( 348 scoped_ptr<HttpResponse> RequestHandler::HandleRequest(
183 const HttpRequest& request) { 349 const HttpRequest& request) {
184 DCHECK(CalledOnValidThread()); 350 DCHECK(CalledOnValidThread());
185 test_task_runner_->PostTask( 351 test_task_runner_->PostTask(
186 FROM_HERE, 352 FROM_HERE,
187 base::Bind( 353 base::Bind(
188 &AttachmentUploaderImplTest::OnRequestReceived, test_, request)); 354 &AttachmentUploaderImplTest::OnRequestReceived, test_, request));
189 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); 355 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
190 http_response->set_code(net::HTTP_OK); 356 response->set_code(GetStatusCode());
191 http_response->set_content("hello"); 357 response->set_content_type("text/plain");
192 http_response->set_content_type("text/plain"); 358 return response.PassAs<HttpResponse>();
193 return http_response.PassAs<HttpResponse>(); 359 }
360
361 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) {
362 base::AutoLock lock(mutex_);
363 status_code_ = status_code;
364 }
365
366 net::HttpStatusCode RequestHandler::GetStatusCode() const {
367 base::AutoLock lock(mutex_);
368 return status_code_;
194 } 369 }
195 370
196 // Verify the "happy case" of uploading an attachment. 371 // Verify the "happy case" of uploading an attachment.
372 //
373 // Token is requested, token is returned, HTTP request is made, attachment is
374 // received by server.
197 TEST_F(AttachmentUploaderImplTest, UploadAttachment_HappyCase) { 375 TEST_F(AttachmentUploaderImplTest, UploadAttachment_HappyCase) {
376 token_service().AddAccount(kAccountId);
377 request_handler().SetStatusCode(net::HTTP_OK);
378
198 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); 379 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
199 some_data->data() = kAttachmentData; 380 some_data->data() = kAttachmentData;
200 Attachment attachment = Attachment::Create(some_data); 381 Attachment attachment = Attachment::Create(some_data);
201 uploader()->UploadAttachment(attachment, upload_callback()); 382 uploader()->UploadAttachment(attachment, upload_callback());
383
384 // Run until the done callback is invoked.
202 RunAndWaitFor(1); 385 RunAndWaitFor(1);
203 386
387 // See that the done callback was invoked with the right arguments.
388 ASSERT_EQ(1U, upload_results().size());
389 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results()[0]);
390 ASSERT_EQ(1U, updated_attachment_ids().size());
391 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]);
392
204 // See that the HTTP server received one request. 393 // See that the HTTP server received one request.
205 EXPECT_EQ(1U, http_requests_received().size()); 394 ASSERT_EQ(1U, http_requests_received().size());
206 const HttpRequest& http_request = http_requests_received().front(); 395 const HttpRequest& http_request = http_requests_received().front();
207 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); 396 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method);
208 std::string expected_relative_url("/uploads/" + 397 std::string expected_relative_url("/uploads/" +
209 attachment.GetId().GetProto().unique_id()); 398 attachment.GetId().GetProto().unique_id());
210 EXPECT_EQ(expected_relative_url, http_request.relative_url); 399 EXPECT_EQ(expected_relative_url, http_request.relative_url);
211 EXPECT_TRUE(http_request.has_content); 400 EXPECT_TRUE(http_request.has_content);
212 EXPECT_EQ(kAttachmentData, http_request.content); 401 EXPECT_EQ(kAttachmentData, http_request.content);
402 std::string expected_header(kAuthorization);
403 const std::string header_name(kAuthorization);
404 const std::string header_value(std::string("Bearer ") + kAccessToken);
405 EXPECT_THAT(http_request.headers,
406 testing::Contains(testing::Pair(header_name, header_value)));
213 407
214 // See that the UploadCallback received a result and updated AttachmentId.
215 EXPECT_EQ(1U, upload_results().size());
216 EXPECT_EQ(1U, updated_attachment_ids().size());
217 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results().front());
218 EXPECT_EQ(attachment.GetId(), updated_attachment_ids().front());
219 // TODO(maniscalco): Once AttachmentUploaderImpl is capable of updating the 408 // TODO(maniscalco): Once AttachmentUploaderImpl is capable of updating the
220 // AttachmentId with server address information about the attachment, add some 409 // AttachmentId with server address information about the attachment, add some
221 // checks here to verify it works properly (bug 371522). 410 // checks here to verify it works properly (bug 371522).
222 } 411 }
223 412
224 // Verify two overlapping calls to upload the same attachment result in only one 413 // Verify two overlapping calls to upload the same attachment result in only one
225 // HTTP request. 414 // HTTP request.
226 TEST_F(AttachmentUploaderImplTest, UploadAttachment_Collapse) { 415 TEST_F(AttachmentUploaderImplTest, UploadAttachment_Collapse) {
416 token_service().AddAccount(kAccountId);
417 request_handler().SetStatusCode(net::HTTP_OK);
418
227 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); 419 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
228 some_data->data() = kAttachmentData; 420 some_data->data() = kAttachmentData;
229 Attachment attachment1 = Attachment::Create(some_data); 421 Attachment attachment1 = Attachment::Create(some_data);
230 Attachment attachment2 = attachment1; 422 Attachment attachment2 = attachment1;
231 uploader()->UploadAttachment(attachment1, upload_callback()); 423 uploader()->UploadAttachment(attachment1, upload_callback());
232 uploader()->UploadAttachment(attachment2, upload_callback()); 424 uploader()->UploadAttachment(attachment2, upload_callback());
425 base::RunLoop().RunUntilIdle();
426
233 // Wait for upload_callback() to be invoked twice. 427 // Wait for upload_callback() to be invoked twice.
234 RunAndWaitFor(2); 428 RunAndWaitFor(2);
235 // See there was only one request. 429 // See there was only one request.
236 EXPECT_EQ(1U, http_requests_received().size()); 430 EXPECT_EQ(1U, http_requests_received().size());
237 } 431 }
238 432
239 // Verify that the internal state associated with an upload is removed when the 433 // Verify that the internal state associated with an upload is removed when the
240 // uplaod finishes. We do this by issuing two non-overlapping uploads for the 434 // uplaod finishes. We do this by issuing two non-overlapping uploads for the
241 // same attachment and see that it results in two HTTP requests. 435 // same attachment and see that it results in two HTTP requests.
242 TEST_F(AttachmentUploaderImplTest, UploadAttachment_CleanUpAfterUpload) { 436 TEST_F(AttachmentUploaderImplTest, UploadAttachment_CleanUpAfterUpload) {
437 token_service().AddAccount(kAccountId);
438 request_handler().SetStatusCode(net::HTTP_OK);
439
243 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); 440 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
244 some_data->data() = kAttachmentData; 441 some_data->data() = kAttachmentData;
245 Attachment attachment1 = Attachment::Create(some_data); 442 Attachment attachment1 = Attachment::Create(some_data);
246 Attachment attachment2 = attachment1; 443 Attachment attachment2 = attachment1;
247 uploader()->UploadAttachment(attachment1, upload_callback()); 444 uploader()->UploadAttachment(attachment1, upload_callback());
445 base::RunLoop().RunUntilIdle();
446
248 // Wait for upload_callback() to be invoked before starting the second upload. 447 // Wait for upload_callback() to be invoked before starting the second upload.
249 RunAndWaitFor(1); 448 RunAndWaitFor(1);
250 uploader()->UploadAttachment(attachment2, upload_callback()); 449 uploader()->UploadAttachment(attachment2, upload_callback());
450 base::RunLoop().RunUntilIdle();
451
251 // Wait for upload_callback() to be invoked a second time. 452 // Wait for upload_callback() to be invoked a second time.
252 RunAndWaitFor(1); 453 RunAndWaitFor(1);
253 // See there were two requests. 454 // See there were two requests.
254 EXPECT_EQ(2U, http_requests_received().size()); 455 ASSERT_EQ(2U, http_requests_received().size());
255 } 456 }
256 457
458 // Verify that we do not issue an HTTP request when we fail to receive an access
459 // token.
460 //
461 // Token is requested, no token is returned, no HTTP request is made
462 TEST_F(AttachmentUploaderImplTest, UploadAttachment_FailToGetToken) {
463 // Note, we won't receive a token because we did not add kAccountId to the
464 // token service.
465 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
466 some_data->data() = kAttachmentData;
467 Attachment attachment = Attachment::Create(some_data);
468 uploader()->UploadAttachment(attachment, upload_callback());
469
470 RunAndWaitFor(1);
471
472 // See that the done callback was invoked.
473 ASSERT_EQ(1U, upload_results().size());
474 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]);
475 ASSERT_EQ(1U, updated_attachment_ids().size());
476 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]);
477
478 // See that no HTTP request was received.
479 ASSERT_EQ(0U, http_requests_received().size());
480 }
481
482 // Verify behavior when the server returns "503 Service Unavailable".
483 TEST_F(AttachmentUploaderImplTest, UploadAttachment_ServiceUnavilable) {
484 token_service().AddAccount(kAccountId);
485 request_handler().SetStatusCode(net::HTTP_SERVICE_UNAVAILABLE);
486
487 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
488 some_data->data() = kAttachmentData;
489 Attachment attachment = Attachment::Create(some_data);
490 uploader()->UploadAttachment(attachment, upload_callback());
491
492 RunAndWaitFor(1);
493
494 // See that the done callback was invoked.
495 ASSERT_EQ(1U, upload_results().size());
496 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]);
497 ASSERT_EQ(1U, updated_attachment_ids().size());
498 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]);
499
500 // See that the HTTP server received one request.
501 ASSERT_EQ(1U, http_requests_received().size());
502 const HttpRequest& http_request = http_requests_received().front();
503 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method);
504 std::string expected_relative_url("/uploads/" +
505 attachment.GetId().GetProto().unique_id());
506 EXPECT_EQ(expected_relative_url, http_request.relative_url);
507 EXPECT_TRUE(http_request.has_content);
508 EXPECT_EQ(kAttachmentData, http_request.content);
509 std::string expected_header(kAuthorization);
510 const std::string header_name(kAuthorization);
511 const std::string header_value(std::string("Bearer ") + kAccessToken);
512 EXPECT_THAT(http_request.headers,
513 testing::Contains(testing::Pair(header_name, header_value)));
514
515 // See that we did not invalidate the token.
516 ASSERT_EQ(0, token_service().num_invalidate_token());
517 }
518
519 // Verify that when we receive an "401 Unauthorized" we invalidate the access
520 // token.
521 TEST_F(AttachmentUploaderImplTest, UploadAttachment_BadToken) {
522 token_service().AddAccount(kAccountId);
523 request_handler().SetStatusCode(net::HTTP_UNAUTHORIZED);
524
525 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString);
526 some_data->data() = kAttachmentData;
527 Attachment attachment = Attachment::Create(some_data);
528 uploader()->UploadAttachment(attachment, upload_callback());
529
530 RunAndWaitFor(1);
531
532 // See that the done callback was invoked.
533 ASSERT_EQ(1U, upload_results().size());
534 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]);
535 ASSERT_EQ(1U, updated_attachment_ids().size());
536 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]);
537
538 // See that the HTTP server received one request.
539 ASSERT_EQ(1U, http_requests_received().size());
540 const HttpRequest& http_request = http_requests_received().front();
541 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method);
542 std::string expected_relative_url("/uploads/" +
543 attachment.GetId().GetProto().unique_id());
544 EXPECT_EQ(expected_relative_url, http_request.relative_url);
545 EXPECT_TRUE(http_request.has_content);
546 EXPECT_EQ(kAttachmentData, http_request.content);
547 std::string expected_header(kAuthorization);
548 const std::string header_name(kAuthorization);
549 const std::string header_value(std::string("Bearer ") + kAccessToken);
550 EXPECT_THAT(http_request.headers,
551 testing::Contains(testing::Pair(header_name, header_value)));
552
553 // See that we invalidated the token.
554 ASSERT_EQ(1, token_service().num_invalidate_token());
555 }
556
557 // TODO(maniscalco): Add test case for when we are uploading an attachment that
558 // already exists. 409 Conflict? (bug 379825)
559
257 } // namespace syncer 560 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698