OLD | NEW |
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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "sync/protocol/sync.pb.h" | 26 #include "sync/protocol/sync.pb.h" |
27 #include "testing/gmock/include/gmock/gmock-matchers.h" | 27 #include "testing/gmock/include/gmock/gmock-matchers.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 const char kAttachmentData[] = "some data"; | 32 const char kAttachmentData[] = "some data"; |
33 const char kAccountId[] = "some-account-id"; | 33 const char kAccountId[] = "some-account-id"; |
34 const char kAccessToken[] = "some-access-token"; | 34 const char kAccessToken[] = "some-access-token"; |
35 const char kAuthorization[] = "Authorization"; | 35 const char kAuthorization[] = "Authorization"; |
36 const char kAttachments[] = "/attachments/"; | |
37 | 36 |
38 } // namespace | 37 } // namespace |
39 | 38 |
40 namespace syncer { | 39 namespace syncer { |
41 | 40 |
42 using net::test_server::BasicHttpResponse; | 41 using net::test_server::BasicHttpResponse; |
43 using net::test_server::HttpRequest; | 42 using net::test_server::HttpRequest; |
44 using net::test_server::HttpResponse; | 43 using net::test_server::HttpResponse; |
45 | 44 |
46 class RequestHandler; | 45 class RequestHandler; |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 request_handler_.reset(new RequestHandler(message_loop_.message_loop_proxy(), | 257 request_handler_.reset(new RequestHandler(message_loop_.message_loop_proxy(), |
259 weak_ptr_factory_.GetWeakPtr())); | 258 weak_ptr_factory_.GetWeakPtr())); |
260 url_request_context_getter_ = | 259 url_request_context_getter_ = |
261 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 260 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
262 | 261 |
263 ASSERT_TRUE(server_.InitializeAndWaitUntilReady()); | 262 ASSERT_TRUE(server_.InitializeAndWaitUntilReady()); |
264 server_.RegisterRequestHandler( | 263 server_.RegisterRequestHandler( |
265 base::Bind(&RequestHandler::HandleRequest, | 264 base::Bind(&RequestHandler::HandleRequest, |
266 base::Unretained(request_handler_.get()))); | 265 base::Unretained(request_handler_.get()))); |
267 | 266 |
268 GURL url(base::StringPrintf("http://localhost:%d/", server_.port())); | 267 std::string url_prefix( |
| 268 base::StringPrintf("http://localhost:%d/uploads/", server_.port())); |
269 | 269 |
270 token_service_.reset(new MockOAuth2TokenService); | 270 token_service_.reset(new MockOAuth2TokenService); |
271 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> | 271 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> |
272 token_service_provider(new TokenServiceProvider(token_service_.get())); | 272 token_service_provider(new TokenServiceProvider(token_service_.get())); |
273 | 273 |
274 OAuth2TokenService::ScopeSet scopes; | 274 OAuth2TokenService::ScopeSet scopes; |
275 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 275 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
276 uploader().reset(new AttachmentUploaderImpl(url, | 276 uploader().reset(new AttachmentUploaderImpl(url_prefix, |
277 url_request_context_getter_, | 277 url_request_context_getter_, |
278 kAccountId, | 278 kAccountId, |
279 scopes, | 279 scopes, |
280 token_service_provider.Pass())); | 280 token_service_provider.Pass())); |
281 | 281 |
282 upload_callback_ = base::Bind(&AttachmentUploaderImplTest::UploadDone, | 282 upload_callback_ = base::Bind(&AttachmentUploaderImplTest::UploadDone, |
283 base::Unretained(this)); | 283 base::Unretained(this)); |
284 } | 284 } |
285 | 285 |
286 void AttachmentUploaderImplTest::TearDown() { | 286 void AttachmentUploaderImplTest::TearDown() { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { | 370 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { |
371 base::AutoLock lock(mutex_); | 371 base::AutoLock lock(mutex_); |
372 status_code_ = status_code; | 372 status_code_ = status_code; |
373 } | 373 } |
374 | 374 |
375 net::HttpStatusCode RequestHandler::GetStatusCode() const { | 375 net::HttpStatusCode RequestHandler::GetStatusCode() const { |
376 base::AutoLock lock(mutex_); | 376 base::AutoLock lock(mutex_); |
377 return status_code_; | 377 return status_code_; |
378 } | 378 } |
379 | 379 |
380 TEST_F(AttachmentUploaderImplTest, GetURLForAttachmentId_NoPath) { | |
381 AttachmentId id = AttachmentId::Create(); | |
382 std::string unique_id = id.GetProto().unique_id(); | |
383 GURL sync_service_url("https://example.com"); | |
384 EXPECT_EQ("https://example.com/attachments/" + unique_id, | |
385 AttachmentUploaderImpl::GetURLForAttachmentId(sync_service_url, id) | |
386 .spec()); | |
387 } | |
388 | |
389 TEST_F(AttachmentUploaderImplTest, GetURLForAttachmentId_JustSlash) { | |
390 AttachmentId id = AttachmentId::Create(); | |
391 std::string unique_id = id.GetProto().unique_id(); | |
392 GURL sync_service_url("https://example.com/"); | |
393 EXPECT_EQ("https://example.com/attachments/" + unique_id, | |
394 AttachmentUploaderImpl::GetURLForAttachmentId(sync_service_url, id) | |
395 .spec()); | |
396 } | |
397 | |
398 TEST_F(AttachmentUploaderImplTest, GetURLForAttachmentId_Path) { | |
399 AttachmentId id = AttachmentId::Create(); | |
400 std::string unique_id = id.GetProto().unique_id(); | |
401 GURL sync_service_url("https://example.com/service"); | |
402 EXPECT_EQ("https://example.com/service/attachments/" + unique_id, | |
403 AttachmentUploaderImpl::GetURLForAttachmentId(sync_service_url, id) | |
404 .spec()); | |
405 } | |
406 | |
407 TEST_F(AttachmentUploaderImplTest, GetURLForAttachmentId_PathAndSlash) { | |
408 AttachmentId id = AttachmentId::Create(); | |
409 std::string unique_id = id.GetProto().unique_id(); | |
410 GURL sync_service_url("https://example.com/service/"); | |
411 EXPECT_EQ("https://example.com/service/attachments/" + unique_id, | |
412 AttachmentUploaderImpl::GetURLForAttachmentId(sync_service_url, id) | |
413 .spec()); | |
414 } | |
415 | |
416 // Verify the "happy case" of uploading an attachment. | 380 // Verify the "happy case" of uploading an attachment. |
417 // | 381 // |
418 // Token is requested, token is returned, HTTP request is made, attachment is | 382 // Token is requested, token is returned, HTTP request is made, attachment is |
419 // received by server. | 383 // received by server. |
420 TEST_F(AttachmentUploaderImplTest, UploadAttachment_HappyCase) { | 384 TEST_F(AttachmentUploaderImplTest, UploadAttachment_HappyCase) { |
421 token_service().AddAccount(kAccountId); | 385 token_service().AddAccount(kAccountId); |
422 request_handler().SetStatusCode(net::HTTP_OK); | 386 request_handler().SetStatusCode(net::HTTP_OK); |
423 | 387 |
424 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 388 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
425 some_data->data() = kAttachmentData; | 389 some_data->data() = kAttachmentData; |
426 Attachment attachment = Attachment::Create(some_data); | 390 Attachment attachment = Attachment::Create(some_data); |
427 uploader()->UploadAttachment(attachment, upload_callback()); | 391 uploader()->UploadAttachment(attachment, upload_callback()); |
428 | 392 |
429 // Run until the done callback is invoked. | 393 // Run until the done callback is invoked. |
430 RunAndWaitFor(1); | 394 RunAndWaitFor(1); |
431 | 395 |
432 // See that the done callback was invoked with the right arguments. | 396 // See that the done callback was invoked with the right arguments. |
433 ASSERT_EQ(1U, upload_results().size()); | 397 ASSERT_EQ(1U, upload_results().size()); |
434 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results()[0]); | 398 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results()[0]); |
435 ASSERT_EQ(1U, updated_attachment_ids().size()); | 399 ASSERT_EQ(1U, updated_attachment_ids().size()); |
436 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 400 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); |
437 | 401 |
438 // See that the HTTP server received one request. | 402 // See that the HTTP server received one request. |
439 ASSERT_EQ(1U, http_requests_received().size()); | 403 ASSERT_EQ(1U, http_requests_received().size()); |
440 const HttpRequest& http_request = http_requests_received().front(); | 404 const HttpRequest& http_request = http_requests_received().front(); |
441 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 405 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
442 std::string expected_relative_url(kAttachments + | 406 std::string expected_relative_url("/uploads/" + |
443 attachment.GetId().GetProto().unique_id()); | 407 attachment.GetId().GetProto().unique_id()); |
444 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 408 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
445 EXPECT_TRUE(http_request.has_content); | 409 EXPECT_TRUE(http_request.has_content); |
446 EXPECT_EQ(kAttachmentData, http_request.content); | 410 EXPECT_EQ(kAttachmentData, http_request.content); |
447 const std::string header_name(kAuthorization); | 411 const std::string header_name(kAuthorization); |
448 const std::string header_value(std::string("Bearer ") + kAccessToken); | 412 const std::string header_value(std::string("Bearer ") + kAccessToken); |
449 EXPECT_THAT(http_request.headers, | 413 EXPECT_THAT(http_request.headers, |
450 testing::Contains(testing::Pair(header_name, header_value))); | 414 testing::Contains(testing::Pair(header_name, header_value))); |
451 | 415 |
452 // TODO(maniscalco): Once AttachmentUploaderImpl is capable of updating the | 416 // TODO(maniscalco): Once AttachmentUploaderImpl is capable of updating the |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 // See that the done callback was invoked. | 499 // See that the done callback was invoked. |
536 ASSERT_EQ(1U, upload_results().size()); | 500 ASSERT_EQ(1U, upload_results().size()); |
537 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 501 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
538 ASSERT_EQ(1U, updated_attachment_ids().size()); | 502 ASSERT_EQ(1U, updated_attachment_ids().size()); |
539 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 503 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); |
540 | 504 |
541 // See that the HTTP server received one request. | 505 // See that the HTTP server received one request. |
542 ASSERT_EQ(1U, http_requests_received().size()); | 506 ASSERT_EQ(1U, http_requests_received().size()); |
543 const HttpRequest& http_request = http_requests_received().front(); | 507 const HttpRequest& http_request = http_requests_received().front(); |
544 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 508 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
545 std::string expected_relative_url(kAttachments + | 509 std::string expected_relative_url("/uploads/" + |
546 attachment.GetId().GetProto().unique_id()); | 510 attachment.GetId().GetProto().unique_id()); |
547 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 511 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
548 EXPECT_TRUE(http_request.has_content); | 512 EXPECT_TRUE(http_request.has_content); |
549 EXPECT_EQ(kAttachmentData, http_request.content); | 513 EXPECT_EQ(kAttachmentData, http_request.content); |
550 std::string expected_header(kAuthorization); | 514 std::string expected_header(kAuthorization); |
551 const std::string header_name(kAuthorization); | 515 const std::string header_name(kAuthorization); |
552 const std::string header_value(std::string("Bearer ") + kAccessToken); | 516 const std::string header_value(std::string("Bearer ") + kAccessToken); |
553 EXPECT_THAT(http_request.headers, | 517 EXPECT_THAT(http_request.headers, |
554 testing::Contains(testing::Pair(header_name, header_value))); | 518 testing::Contains(testing::Pair(header_name, header_value))); |
555 | 519 |
(...skipping 17 matching lines...) Expand all Loading... |
573 // See that the done callback was invoked. | 537 // See that the done callback was invoked. |
574 ASSERT_EQ(1U, upload_results().size()); | 538 ASSERT_EQ(1U, upload_results().size()); |
575 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 539 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
576 ASSERT_EQ(1U, updated_attachment_ids().size()); | 540 ASSERT_EQ(1U, updated_attachment_ids().size()); |
577 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 541 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); |
578 | 542 |
579 // See that the HTTP server received one request. | 543 // See that the HTTP server received one request. |
580 ASSERT_EQ(1U, http_requests_received().size()); | 544 ASSERT_EQ(1U, http_requests_received().size()); |
581 const HttpRequest& http_request = http_requests_received().front(); | 545 const HttpRequest& http_request = http_requests_received().front(); |
582 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 546 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
583 std::string expected_relative_url(kAttachments + | 547 std::string expected_relative_url("/uploads/" + |
584 attachment.GetId().GetProto().unique_id()); | 548 attachment.GetId().GetProto().unique_id()); |
585 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 549 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
586 EXPECT_TRUE(http_request.has_content); | 550 EXPECT_TRUE(http_request.has_content); |
587 EXPECT_EQ(kAttachmentData, http_request.content); | 551 EXPECT_EQ(kAttachmentData, http_request.content); |
588 std::string expected_header(kAuthorization); | 552 std::string expected_header(kAuthorization); |
589 const std::string header_name(kAuthorization); | 553 const std::string header_name(kAuthorization); |
590 const std::string header_value(std::string("Bearer ") + kAccessToken); | 554 const std::string header_value(std::string("Bearer ") + kAccessToken); |
591 EXPECT_THAT(http_request.headers, | 555 EXPECT_THAT(http_request.headers, |
592 testing::Contains(testing::Pair(header_name, header_value))); | 556 testing::Contains(testing::Pair(header_name, header_value))); |
593 | 557 |
594 // See that we invalidated the token. | 558 // See that we invalidated the token. |
595 ASSERT_EQ(1, token_service().num_invalidate_token()); | 559 ASSERT_EQ(1, token_service().num_invalidate_token()); |
596 } | 560 } |
597 | 561 |
598 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 562 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
599 // already exists. 409 Conflict? (bug 379825) | 563 // already exists. 409 Conflict? (bug 379825) |
600 | 564 |
601 } // namespace syncer | 565 } // namespace syncer |
OLD | NEW |