| 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 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 // token service. | 502 // token service. |
| 503 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 503 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 504 some_data->data() = kAttachmentData; | 504 some_data->data() = kAttachmentData; |
| 505 Attachment attachment = Attachment::Create(some_data); | 505 Attachment attachment = Attachment::Create(some_data); |
| 506 uploader()->UploadAttachment(attachment, upload_callback()); | 506 uploader()->UploadAttachment(attachment, upload_callback()); |
| 507 | 507 |
| 508 RunAndWaitFor(1); | 508 RunAndWaitFor(1); |
| 509 | 509 |
| 510 // See that the done callback was invoked. | 510 // See that the done callback was invoked. |
| 511 ASSERT_EQ(1U, upload_results().size()); | 511 ASSERT_EQ(1U, upload_results().size()); |
| 512 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 512 EXPECT_EQ(AttachmentUploader::UPLOAD_TRANSIENT_ERROR, upload_results()[0]); |
| 513 ASSERT_EQ(1U, attachment_ids().size()); | 513 ASSERT_EQ(1U, attachment_ids().size()); |
| 514 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); | 514 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 515 | 515 |
| 516 // See that no HTTP request was received. | 516 // See that no HTTP request was received. |
| 517 ASSERT_EQ(0U, http_requests_received().size()); | 517 ASSERT_EQ(0U, http_requests_received().size()); |
| 518 } | 518 } |
| 519 | 519 |
| 520 // Verify behavior when the server returns "503 Service Unavailable". | 520 // Verify behavior when the server returns "503 Service Unavailable". |
| 521 TEST_F(AttachmentUploaderImplTest, UploadAttachment_ServiceUnavilable) { | 521 TEST_F(AttachmentUploaderImplTest, UploadAttachment_ServiceUnavilable) { |
| 522 token_service().AddAccount(kAccountId); | 522 token_service().AddAccount(kAccountId); |
| 523 request_handler().SetStatusCode(net::HTTP_SERVICE_UNAVAILABLE); | 523 request_handler().SetStatusCode(net::HTTP_SERVICE_UNAVAILABLE); |
| 524 | 524 |
| 525 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 525 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 526 some_data->data() = kAttachmentData; | 526 some_data->data() = kAttachmentData; |
| 527 Attachment attachment = Attachment::Create(some_data); | 527 Attachment attachment = Attachment::Create(some_data); |
| 528 uploader()->UploadAttachment(attachment, upload_callback()); | 528 uploader()->UploadAttachment(attachment, upload_callback()); |
| 529 | 529 |
| 530 RunAndWaitFor(1); | 530 RunAndWaitFor(1); |
| 531 | 531 |
| 532 // See that the done callback was invoked. | 532 // See that the done callback was invoked. |
| 533 ASSERT_EQ(1U, upload_results().size()); | 533 ASSERT_EQ(1U, upload_results().size()); |
| 534 EXPECT_EQ(AttachmentUploader::UPLOAD_TRANSIENT_ERROR, upload_results()[0]); |
| 535 ASSERT_EQ(1U, attachment_ids().size()); |
| 536 EXPECT_EQ(attachment.GetId(), 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(kAttachments + |
| 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 did not invalidate the token. |
| 554 ASSERT_EQ(0, token_service().num_invalidate_token()); |
| 555 } |
| 556 |
| 557 // Verify that we "403 Forbidden" as a non-transient error. |
| 558 TEST_F(AttachmentUploaderImplTest, UploadAttachment_Forbidden) { |
| 559 token_service().AddAccount(kAccountId); |
| 560 request_handler().SetStatusCode(net::HTTP_FORBIDDEN); |
| 561 |
| 562 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 563 some_data->data() = kAttachmentData; |
| 564 Attachment attachment = Attachment::Create(some_data); |
| 565 uploader()->UploadAttachment(attachment, upload_callback()); |
| 566 |
| 567 RunAndWaitFor(1); |
| 568 |
| 569 // See that the done callback was invoked. |
| 570 ASSERT_EQ(1U, upload_results().size()); |
| 534 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 571 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
| 535 ASSERT_EQ(1U, attachment_ids().size()); | 572 ASSERT_EQ(1U, attachment_ids().size()); |
| 536 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); | 573 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 537 | 574 |
| 538 // See that the HTTP server received one request. | 575 // See that the HTTP server received one request. |
| 539 ASSERT_EQ(1U, http_requests_received().size()); | 576 ASSERT_EQ(1U, http_requests_received().size()); |
| 540 const HttpRequest& http_request = http_requests_received().front(); | 577 const HttpRequest& http_request = http_requests_received().front(); |
| 541 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 578 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
| 542 std::string expected_relative_url(kAttachments + | 579 std::string expected_relative_url(kAttachments + |
| 543 attachment.GetId().GetProto().unique_id()); | 580 attachment.GetId().GetProto().unique_id()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 562 | 599 |
| 563 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 600 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 564 some_data->data() = kAttachmentData; | 601 some_data->data() = kAttachmentData; |
| 565 Attachment attachment = Attachment::Create(some_data); | 602 Attachment attachment = Attachment::Create(some_data); |
| 566 uploader()->UploadAttachment(attachment, upload_callback()); | 603 uploader()->UploadAttachment(attachment, upload_callback()); |
| 567 | 604 |
| 568 RunAndWaitFor(1); | 605 RunAndWaitFor(1); |
| 569 | 606 |
| 570 // See that the done callback was invoked. | 607 // See that the done callback was invoked. |
| 571 ASSERT_EQ(1U, upload_results().size()); | 608 ASSERT_EQ(1U, upload_results().size()); |
| 572 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 609 EXPECT_EQ(AttachmentUploader::UPLOAD_TRANSIENT_ERROR, upload_results()[0]); |
| 573 ASSERT_EQ(1U, attachment_ids().size()); | 610 ASSERT_EQ(1U, attachment_ids().size()); |
| 574 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); | 611 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 575 | 612 |
| 576 // See that the HTTP server received one request. | 613 // See that the HTTP server received one request. |
| 577 ASSERT_EQ(1U, http_requests_received().size()); | 614 ASSERT_EQ(1U, http_requests_received().size()); |
| 578 const HttpRequest& http_request = http_requests_received().front(); | 615 const HttpRequest& http_request = http_requests_received().front(); |
| 579 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 616 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
| 580 std::string expected_relative_url(kAttachments + | 617 std::string expected_relative_url(kAttachments + |
| 581 attachment.GetId().GetProto().unique_id()); | 618 attachment.GetId().GetProto().unique_id()); |
| 582 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 619 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
| 583 EXPECT_TRUE(http_request.has_content); | 620 EXPECT_TRUE(http_request.has_content); |
| 584 EXPECT_EQ(kAttachmentData, http_request.content); | 621 EXPECT_EQ(kAttachmentData, http_request.content); |
| 585 std::string expected_header(kAuthorization); | 622 std::string expected_header(kAuthorization); |
| 586 const std::string header_name(kAuthorization); | 623 const std::string header_name(kAuthorization); |
| 587 const std::string header_value(std::string("Bearer ") + kAccessToken); | 624 const std::string header_value(std::string("Bearer ") + kAccessToken); |
| 588 EXPECT_THAT(http_request.headers, | 625 EXPECT_THAT(http_request.headers, |
| 589 testing::Contains(testing::Pair(header_name, header_value))); | 626 testing::Contains(testing::Pair(header_name, header_value))); |
| 590 | 627 |
| 591 // See that we invalidated the token. | 628 // See that we invalidated the token. |
| 592 ASSERT_EQ(1, token_service().num_invalidate_token()); | 629 ASSERT_EQ(1, token_service().num_invalidate_token()); |
| 593 } | 630 } |
| 594 | 631 |
| 595 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 632 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
| 596 // already exists. 409 Conflict? (bug 379825) | 633 // already exists. 409 Conflict? (bug 379825) |
| 597 | 634 |
| 598 } // namespace syncer | 635 } // namespace syncer |
| OLD | NEW |