| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 virtual void SetUp(); | 180 virtual void SetUp(); |
| 181 virtual void TearDown(); | 181 virtual void TearDown(); |
| 182 | 182 |
| 183 // Run the message loop until UploadDone has been invoked |num_uploads| times. | 183 // Run the message loop until UploadDone has been invoked |num_uploads| times. |
| 184 void RunAndWaitFor(int num_uploads); | 184 void RunAndWaitFor(int num_uploads); |
| 185 | 185 |
| 186 scoped_ptr<AttachmentUploader>& uploader(); | 186 scoped_ptr<AttachmentUploader>& uploader(); |
| 187 const AttachmentUploader::UploadCallback& upload_callback() const; | 187 const AttachmentUploader::UploadCallback& upload_callback() const; |
| 188 std::vector<HttpRequest>& http_requests_received(); | 188 std::vector<HttpRequest>& http_requests_received(); |
| 189 std::vector<AttachmentUploader::UploadResult>& upload_results(); | 189 std::vector<AttachmentUploader::UploadResult>& upload_results(); |
| 190 std::vector<AttachmentId>& updated_attachment_ids(); | 190 std::vector<AttachmentId>& attachment_ids(); |
| 191 MockOAuth2TokenService& token_service(); | 191 MockOAuth2TokenService& token_service(); |
| 192 base::MessageLoopForIO& message_loop(); | 192 base::MessageLoopForIO& message_loop(); |
| 193 RequestHandler& request_handler(); | 193 RequestHandler& request_handler(); |
| 194 | 194 |
| 195 private: | 195 private: |
| 196 // An UploadCallback invoked by AttachmentUploaderImpl. | 196 // An UploadCallback invoked by AttachmentUploaderImpl. |
| 197 void UploadDone(const AttachmentUploader::UploadResult& result, | 197 void UploadDone(const AttachmentUploader::UploadResult& result, |
| 198 const AttachmentId& updated_attachment_id); | 198 const AttachmentId& attachment_id); |
| 199 | 199 |
| 200 base::MessageLoopForIO message_loop_; | 200 base::MessageLoopForIO message_loop_; |
| 201 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 201 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 202 scoped_ptr<RequestHandler> request_handler_; | 202 scoped_ptr<RequestHandler> request_handler_; |
| 203 scoped_ptr<AttachmentUploader> uploader_; | 203 scoped_ptr<AttachmentUploader> uploader_; |
| 204 AttachmentUploader::UploadCallback upload_callback_; | 204 AttachmentUploader::UploadCallback upload_callback_; |
| 205 net::test_server::EmbeddedTestServer server_; | 205 net::test_server::EmbeddedTestServer server_; |
| 206 // A closure that signals an upload has finished. | 206 // A closure that signals an upload has finished. |
| 207 base::Closure signal_upload_done_; | 207 base::Closure signal_upload_done_; |
| 208 std::vector<HttpRequest> http_requests_received_; | 208 std::vector<HttpRequest> http_requests_received_; |
| 209 std::vector<AttachmentUploader::UploadResult> upload_results_; | 209 std::vector<AttachmentUploader::UploadResult> upload_results_; |
| 210 std::vector<AttachmentId> updated_attachment_ids_; | 210 std::vector<AttachmentId> attachment_ids_; |
| 211 scoped_ptr<MockOAuth2TokenService> token_service_; | 211 scoped_ptr<MockOAuth2TokenService> token_service_; |
| 212 | 212 |
| 213 // Must be last data member. | 213 // Must be last data member. |
| 214 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_; | 214 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_; |
| 215 }; | 215 }; |
| 216 | 216 |
| 217 // Handles HTTP requests received by the EmbeddedTestServer. | 217 // Handles HTTP requests received by the EmbeddedTestServer. |
| 218 // | 218 // |
| 219 // Responds with HTTP_OK by default. See |SetStatusCode|. | 219 // Responds with HTTP_OK by default. See |SetStatusCode|. |
| 220 class RequestHandler : public base::NonThreadSafe { | 220 class RequestHandler : public base::NonThreadSafe { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 std::vector<HttpRequest>& AttachmentUploaderImplTest::http_requests_received() { | 308 std::vector<HttpRequest>& AttachmentUploaderImplTest::http_requests_received() { |
| 309 return http_requests_received_; | 309 return http_requests_received_; |
| 310 } | 310 } |
| 311 | 311 |
| 312 std::vector<AttachmentUploader::UploadResult>& | 312 std::vector<AttachmentUploader::UploadResult>& |
| 313 AttachmentUploaderImplTest::upload_results() { | 313 AttachmentUploaderImplTest::upload_results() { |
| 314 return upload_results_; | 314 return upload_results_; |
| 315 } | 315 } |
| 316 | 316 |
| 317 std::vector<AttachmentId>& | 317 std::vector<AttachmentId>& |
| 318 AttachmentUploaderImplTest::updated_attachment_ids() { | 318 AttachmentUploaderImplTest::attachment_ids() { |
| 319 return updated_attachment_ids_; | 319 return attachment_ids_; |
| 320 } | 320 } |
| 321 | 321 |
| 322 MockOAuth2TokenService& AttachmentUploaderImplTest::token_service() { | 322 MockOAuth2TokenService& AttachmentUploaderImplTest::token_service() { |
| 323 return *token_service_; | 323 return *token_service_; |
| 324 } | 324 } |
| 325 | 325 |
| 326 base::MessageLoopForIO& AttachmentUploaderImplTest::message_loop() { | 326 base::MessageLoopForIO& AttachmentUploaderImplTest::message_loop() { |
| 327 return message_loop_; | 327 return message_loop_; |
| 328 } | 328 } |
| 329 | 329 |
| 330 RequestHandler& AttachmentUploaderImplTest::request_handler() { | 330 RequestHandler& AttachmentUploaderImplTest::request_handler() { |
| 331 return *request_handler_; | 331 return *request_handler_; |
| 332 } | 332 } |
| 333 | 333 |
| 334 void AttachmentUploaderImplTest::UploadDone( | 334 void AttachmentUploaderImplTest::UploadDone( |
| 335 const AttachmentUploader::UploadResult& result, | 335 const AttachmentUploader::UploadResult& result, |
| 336 const AttachmentId& updated_attachment_id) { | 336 const AttachmentId& attachment_id) { |
| 337 DCHECK(CalledOnValidThread()); | 337 DCHECK(CalledOnValidThread()); |
| 338 upload_results_.push_back(result); | 338 upload_results_.push_back(result); |
| 339 updated_attachment_ids_.push_back(updated_attachment_id); | 339 attachment_ids_.push_back(attachment_id); |
| 340 DCHECK(!signal_upload_done_.is_null()); | 340 DCHECK(!signal_upload_done_.is_null()); |
| 341 signal_upload_done_.Run(); | 341 signal_upload_done_.Run(); |
| 342 } | 342 } |
| 343 | 343 |
| 344 RequestHandler::RequestHandler( | 344 RequestHandler::RequestHandler( |
| 345 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, | 345 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, |
| 346 const base::WeakPtr<AttachmentUploaderImplTest>& test) | 346 const base::WeakPtr<AttachmentUploaderImplTest>& test) |
| 347 : status_code_(net::HTTP_OK), | 347 : status_code_(net::HTTP_OK), |
| 348 test_task_runner_(test_task_runner), | 348 test_task_runner_(test_task_runner), |
| 349 test_(test) { | 349 test_(test) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 some_data->data() = kAttachmentData; | 425 some_data->data() = kAttachmentData; |
| 426 Attachment attachment = Attachment::Create(some_data); | 426 Attachment attachment = Attachment::Create(some_data); |
| 427 uploader()->UploadAttachment(attachment, upload_callback()); | 427 uploader()->UploadAttachment(attachment, upload_callback()); |
| 428 | 428 |
| 429 // Run until the done callback is invoked. | 429 // Run until the done callback is invoked. |
| 430 RunAndWaitFor(1); | 430 RunAndWaitFor(1); |
| 431 | 431 |
| 432 // See that the done callback was invoked with the right arguments. | 432 // See that the done callback was invoked with the right arguments. |
| 433 ASSERT_EQ(1U, upload_results().size()); | 433 ASSERT_EQ(1U, upload_results().size()); |
| 434 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results()[0]); | 434 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results()[0]); |
| 435 ASSERT_EQ(1U, updated_attachment_ids().size()); | 435 ASSERT_EQ(1U, attachment_ids().size()); |
| 436 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 436 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 437 | 437 |
| 438 // See that the HTTP server received one request. | 438 // See that the HTTP server received one request. |
| 439 ASSERT_EQ(1U, http_requests_received().size()); | 439 ASSERT_EQ(1U, http_requests_received().size()); |
| 440 const HttpRequest& http_request = http_requests_received().front(); | 440 const HttpRequest& http_request = http_requests_received().front(); |
| 441 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 441 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
| 442 std::string expected_relative_url(kAttachments + | 442 std::string expected_relative_url(kAttachments + |
| 443 attachment.GetId().GetProto().unique_id()); | 443 attachment.GetId().GetProto().unique_id()); |
| 444 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 444 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
| 445 EXPECT_TRUE(http_request.has_content); | 445 EXPECT_TRUE(http_request.has_content); |
| 446 EXPECT_EQ(kAttachmentData, http_request.content); | 446 EXPECT_EQ(kAttachmentData, http_request.content); |
| 447 const std::string header_name(kAuthorization); | 447 const std::string header_name(kAuthorization); |
| 448 const std::string header_value(std::string("Bearer ") + kAccessToken); | 448 const std::string header_value(std::string("Bearer ") + kAccessToken); |
| 449 EXPECT_THAT(http_request.headers, | 449 EXPECT_THAT(http_request.headers, |
| 450 testing::Contains(testing::Pair(header_name, header_value))); | 450 testing::Contains(testing::Pair(header_name, header_value))); |
| 451 | |
| 452 // TODO(maniscalco): Once AttachmentUploaderImpl is capable of updating the | |
| 453 // AttachmentId with server address information about the attachment, add some | |
| 454 // checks here to verify it works properly (bug 371522). | |
| 455 } | 451 } |
| 456 | 452 |
| 457 // Verify two overlapping calls to upload the same attachment result in only one | 453 // Verify two overlapping calls to upload the same attachment result in only one |
| 458 // HTTP request. | 454 // HTTP request. |
| 459 TEST_F(AttachmentUploaderImplTest, UploadAttachment_Collapse) { | 455 TEST_F(AttachmentUploaderImplTest, UploadAttachment_Collapse) { |
| 460 token_service().AddAccount(kAccountId); | 456 token_service().AddAccount(kAccountId); |
| 461 request_handler().SetStatusCode(net::HTTP_OK); | 457 request_handler().SetStatusCode(net::HTTP_OK); |
| 462 | 458 |
| 463 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 459 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 464 some_data->data() = kAttachmentData; | 460 some_data->data() = kAttachmentData; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 502 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 507 some_data->data() = kAttachmentData; | 503 some_data->data() = kAttachmentData; |
| 508 Attachment attachment = Attachment::Create(some_data); | 504 Attachment attachment = Attachment::Create(some_data); |
| 509 uploader()->UploadAttachment(attachment, upload_callback()); | 505 uploader()->UploadAttachment(attachment, upload_callback()); |
| 510 | 506 |
| 511 RunAndWaitFor(1); | 507 RunAndWaitFor(1); |
| 512 | 508 |
| 513 // See that the done callback was invoked. | 509 // See that the done callback was invoked. |
| 514 ASSERT_EQ(1U, upload_results().size()); | 510 ASSERT_EQ(1U, upload_results().size()); |
| 515 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 511 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
| 516 ASSERT_EQ(1U, updated_attachment_ids().size()); | 512 ASSERT_EQ(1U, attachment_ids().size()); |
| 517 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 513 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 518 | 514 |
| 519 // See that no HTTP request was received. | 515 // See that no HTTP request was received. |
| 520 ASSERT_EQ(0U, http_requests_received().size()); | 516 ASSERT_EQ(0U, http_requests_received().size()); |
| 521 } | 517 } |
| 522 | 518 |
| 523 // Verify behavior when the server returns "503 Service Unavailable". | 519 // Verify behavior when the server returns "503 Service Unavailable". |
| 524 TEST_F(AttachmentUploaderImplTest, UploadAttachment_ServiceUnavilable) { | 520 TEST_F(AttachmentUploaderImplTest, UploadAttachment_ServiceUnavilable) { |
| 525 token_service().AddAccount(kAccountId); | 521 token_service().AddAccount(kAccountId); |
| 526 request_handler().SetStatusCode(net::HTTP_SERVICE_UNAVAILABLE); | 522 request_handler().SetStatusCode(net::HTTP_SERVICE_UNAVAILABLE); |
| 527 | 523 |
| 528 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 524 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 529 some_data->data() = kAttachmentData; | 525 some_data->data() = kAttachmentData; |
| 530 Attachment attachment = Attachment::Create(some_data); | 526 Attachment attachment = Attachment::Create(some_data); |
| 531 uploader()->UploadAttachment(attachment, upload_callback()); | 527 uploader()->UploadAttachment(attachment, upload_callback()); |
| 532 | 528 |
| 533 RunAndWaitFor(1); | 529 RunAndWaitFor(1); |
| 534 | 530 |
| 535 // See that the done callback was invoked. | 531 // See that the done callback was invoked. |
| 536 ASSERT_EQ(1U, upload_results().size()); | 532 ASSERT_EQ(1U, upload_results().size()); |
| 537 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 533 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
| 538 ASSERT_EQ(1U, updated_attachment_ids().size()); | 534 ASSERT_EQ(1U, attachment_ids().size()); |
| 539 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 535 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 540 | 536 |
| 541 // See that the HTTP server received one request. | 537 // See that the HTTP server received one request. |
| 542 ASSERT_EQ(1U, http_requests_received().size()); | 538 ASSERT_EQ(1U, http_requests_received().size()); |
| 543 const HttpRequest& http_request = http_requests_received().front(); | 539 const HttpRequest& http_request = http_requests_received().front(); |
| 544 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 540 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
| 545 std::string expected_relative_url(kAttachments + | 541 std::string expected_relative_url(kAttachments + |
| 546 attachment.GetId().GetProto().unique_id()); | 542 attachment.GetId().GetProto().unique_id()); |
| 547 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 543 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
| 548 EXPECT_TRUE(http_request.has_content); | 544 EXPECT_TRUE(http_request.has_content); |
| 549 EXPECT_EQ(kAttachmentData, http_request.content); | 545 EXPECT_EQ(kAttachmentData, http_request.content); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 566 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 562 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 567 some_data->data() = kAttachmentData; | 563 some_data->data() = kAttachmentData; |
| 568 Attachment attachment = Attachment::Create(some_data); | 564 Attachment attachment = Attachment::Create(some_data); |
| 569 uploader()->UploadAttachment(attachment, upload_callback()); | 565 uploader()->UploadAttachment(attachment, upload_callback()); |
| 570 | 566 |
| 571 RunAndWaitFor(1); | 567 RunAndWaitFor(1); |
| 572 | 568 |
| 573 // See that the done callback was invoked. | 569 // See that the done callback was invoked. |
| 574 ASSERT_EQ(1U, upload_results().size()); | 570 ASSERT_EQ(1U, upload_results().size()); |
| 575 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 571 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
| 576 ASSERT_EQ(1U, updated_attachment_ids().size()); | 572 ASSERT_EQ(1U, attachment_ids().size()); |
| 577 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 573 EXPECT_EQ(attachment.GetId(), attachment_ids()[0]); |
| 578 | 574 |
| 579 // See that the HTTP server received one request. | 575 // See that the HTTP server received one request. |
| 580 ASSERT_EQ(1U, http_requests_received().size()); | 576 ASSERT_EQ(1U, http_requests_received().size()); |
| 581 const HttpRequest& http_request = http_requests_received().front(); | 577 const HttpRequest& http_request = http_requests_received().front(); |
| 582 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 578 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
| 583 std::string expected_relative_url(kAttachments + | 579 std::string expected_relative_url(kAttachments + |
| 584 attachment.GetId().GetProto().unique_id()); | 580 attachment.GetId().GetProto().unique_id()); |
| 585 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 581 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
| 586 EXPECT_TRUE(http_request.has_content); | 582 EXPECT_TRUE(http_request.has_content); |
| 587 EXPECT_EQ(kAttachmentData, http_request.content); | 583 EXPECT_EQ(kAttachmentData, http_request.content); |
| 588 std::string expected_header(kAuthorization); | 584 std::string expected_header(kAuthorization); |
| 589 const std::string header_name(kAuthorization); | 585 const std::string header_name(kAuthorization); |
| 590 const std::string header_value(std::string("Bearer ") + kAccessToken); | 586 const std::string header_value(std::string("Bearer ") + kAccessToken); |
| 591 EXPECT_THAT(http_request.headers, | 587 EXPECT_THAT(http_request.headers, |
| 592 testing::Contains(testing::Pair(header_name, header_value))); | 588 testing::Contains(testing::Pair(header_name, header_value))); |
| 593 | 589 |
| 594 // See that we invalidated the token. | 590 // See that we invalidated the token. |
| 595 ASSERT_EQ(1, token_service().num_invalidate_token()); | 591 ASSERT_EQ(1, token_service().num_invalidate_token()); |
| 596 } | 592 } |
| 597 | 593 |
| 598 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 594 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
| 599 // already exists. 409 Conflict? (bug 379825) | 595 // already exists. 409 Conflict? (bug 379825) |
| 600 | 596 |
| 601 } // namespace syncer | 597 } // namespace syncer |
| OLD | NEW |