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/"; |
36 | 37 |
37 } // namespace | 38 } // namespace |
38 | 39 |
39 namespace syncer { | 40 namespace syncer { |
40 | 41 |
41 using net::test_server::BasicHttpResponse; | 42 using net::test_server::BasicHttpResponse; |
42 using net::test_server::HttpRequest; | 43 using net::test_server::HttpRequest; |
43 using net::test_server::HttpResponse; | 44 using net::test_server::HttpResponse; |
44 | 45 |
45 class RequestHandler; | 46 class RequestHandler; |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 request_handler_.reset(new RequestHandler(message_loop_.message_loop_proxy(), | 258 request_handler_.reset(new RequestHandler(message_loop_.message_loop_proxy(), |
258 weak_ptr_factory_.GetWeakPtr())); | 259 weak_ptr_factory_.GetWeakPtr())); |
259 url_request_context_getter_ = | 260 url_request_context_getter_ = |
260 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 261 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
261 | 262 |
262 ASSERT_TRUE(server_.InitializeAndWaitUntilReady()); | 263 ASSERT_TRUE(server_.InitializeAndWaitUntilReady()); |
263 server_.RegisterRequestHandler( | 264 server_.RegisterRequestHandler( |
264 base::Bind(&RequestHandler::HandleRequest, | 265 base::Bind(&RequestHandler::HandleRequest, |
265 base::Unretained(request_handler_.get()))); | 266 base::Unretained(request_handler_.get()))); |
266 | 267 |
267 std::string url_prefix( | 268 GURL url(base::StringPrintf("http://localhost:%d/", server_.port())); |
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_prefix, | 276 uploader().reset(new AttachmentUploaderImpl(url, |
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 |
380 // Verify the "happy case" of uploading an attachment. | 416 // Verify the "happy case" of uploading an attachment. |
381 // | 417 // |
382 // Token is requested, token is returned, HTTP request is made, attachment is | 418 // Token is requested, token is returned, HTTP request is made, attachment is |
383 // received by server. | 419 // received by server. |
384 TEST_F(AttachmentUploaderImplTest, UploadAttachment_HappyCase) { | 420 TEST_F(AttachmentUploaderImplTest, UploadAttachment_HappyCase) { |
385 token_service().AddAccount(kAccountId); | 421 token_service().AddAccount(kAccountId); |
386 request_handler().SetStatusCode(net::HTTP_OK); | 422 request_handler().SetStatusCode(net::HTTP_OK); |
387 | 423 |
388 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 424 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
389 some_data->data() = kAttachmentData; | 425 some_data->data() = kAttachmentData; |
390 Attachment attachment = Attachment::Create(some_data); | 426 Attachment attachment = Attachment::Create(some_data); |
391 uploader()->UploadAttachment(attachment, upload_callback()); | 427 uploader()->UploadAttachment(attachment, upload_callback()); |
392 | 428 |
393 // Run until the done callback is invoked. | 429 // Run until the done callback is invoked. |
394 RunAndWaitFor(1); | 430 RunAndWaitFor(1); |
395 | 431 |
396 // See that the done callback was invoked with the right arguments. | 432 // See that the done callback was invoked with the right arguments. |
397 ASSERT_EQ(1U, upload_results().size()); | 433 ASSERT_EQ(1U, upload_results().size()); |
398 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results()[0]); | 434 EXPECT_EQ(AttachmentUploader::UPLOAD_SUCCESS, upload_results()[0]); |
399 ASSERT_EQ(1U, updated_attachment_ids().size()); | 435 ASSERT_EQ(1U, updated_attachment_ids().size()); |
400 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 436 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); |
401 | 437 |
402 // See that the HTTP server received one request. | 438 // See that the HTTP server received one request. |
403 ASSERT_EQ(1U, http_requests_received().size()); | 439 ASSERT_EQ(1U, http_requests_received().size()); |
404 const HttpRequest& http_request = http_requests_received().front(); | 440 const HttpRequest& http_request = http_requests_received().front(); |
405 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 441 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
406 std::string expected_relative_url("/uploads/" + | 442 std::string expected_relative_url(kAttachments + |
407 attachment.GetId().GetProto().unique_id()); | 443 attachment.GetId().GetProto().unique_id()); |
408 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 444 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
409 EXPECT_TRUE(http_request.has_content); | 445 EXPECT_TRUE(http_request.has_content); |
410 EXPECT_EQ(kAttachmentData, http_request.content); | 446 EXPECT_EQ(kAttachmentData, http_request.content); |
411 const std::string header_name(kAuthorization); | 447 const std::string header_name(kAuthorization); |
412 const std::string header_value(std::string("Bearer ") + kAccessToken); | 448 const std::string header_value(std::string("Bearer ") + kAccessToken); |
413 EXPECT_THAT(http_request.headers, | 449 EXPECT_THAT(http_request.headers, |
414 testing::Contains(testing::Pair(header_name, header_value))); | 450 testing::Contains(testing::Pair(header_name, header_value))); |
415 | 451 |
416 // TODO(maniscalco): Once AttachmentUploaderImpl is capable of updating the | 452 // TODO(maniscalco): Once AttachmentUploaderImpl is capable of updating the |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 // See that the done callback was invoked. | 535 // See that the done callback was invoked. |
500 ASSERT_EQ(1U, upload_results().size()); | 536 ASSERT_EQ(1U, upload_results().size()); |
501 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 537 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
502 ASSERT_EQ(1U, updated_attachment_ids().size()); | 538 ASSERT_EQ(1U, updated_attachment_ids().size()); |
503 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 539 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); |
504 | 540 |
505 // See that the HTTP server received one request. | 541 // See that the HTTP server received one request. |
506 ASSERT_EQ(1U, http_requests_received().size()); | 542 ASSERT_EQ(1U, http_requests_received().size()); |
507 const HttpRequest& http_request = http_requests_received().front(); | 543 const HttpRequest& http_request = http_requests_received().front(); |
508 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 544 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
509 std::string expected_relative_url("/uploads/" + | 545 std::string expected_relative_url(kAttachments + |
510 attachment.GetId().GetProto().unique_id()); | 546 attachment.GetId().GetProto().unique_id()); |
511 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 547 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
512 EXPECT_TRUE(http_request.has_content); | 548 EXPECT_TRUE(http_request.has_content); |
513 EXPECT_EQ(kAttachmentData, http_request.content); | 549 EXPECT_EQ(kAttachmentData, http_request.content); |
514 std::string expected_header(kAuthorization); | 550 std::string expected_header(kAuthorization); |
515 const std::string header_name(kAuthorization); | 551 const std::string header_name(kAuthorization); |
516 const std::string header_value(std::string("Bearer ") + kAccessToken); | 552 const std::string header_value(std::string("Bearer ") + kAccessToken); |
517 EXPECT_THAT(http_request.headers, | 553 EXPECT_THAT(http_request.headers, |
518 testing::Contains(testing::Pair(header_name, header_value))); | 554 testing::Contains(testing::Pair(header_name, header_value))); |
519 | 555 |
(...skipping 17 matching lines...) Expand all Loading... |
537 // See that the done callback was invoked. | 573 // See that the done callback was invoked. |
538 ASSERT_EQ(1U, upload_results().size()); | 574 ASSERT_EQ(1U, upload_results().size()); |
539 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); | 575 EXPECT_EQ(AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR, upload_results()[0]); |
540 ASSERT_EQ(1U, updated_attachment_ids().size()); | 576 ASSERT_EQ(1U, updated_attachment_ids().size()); |
541 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); | 577 EXPECT_EQ(attachment.GetId(), updated_attachment_ids()[0]); |
542 | 578 |
543 // See that the HTTP server received one request. | 579 // See that the HTTP server received one request. |
544 ASSERT_EQ(1U, http_requests_received().size()); | 580 ASSERT_EQ(1U, http_requests_received().size()); |
545 const HttpRequest& http_request = http_requests_received().front(); | 581 const HttpRequest& http_request = http_requests_received().front(); |
546 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); | 582 EXPECT_EQ(net::test_server::METHOD_POST, http_request.method); |
547 std::string expected_relative_url("/uploads/" + | 583 std::string expected_relative_url(kAttachments + |
548 attachment.GetId().GetProto().unique_id()); | 584 attachment.GetId().GetProto().unique_id()); |
549 EXPECT_EQ(expected_relative_url, http_request.relative_url); | 585 EXPECT_EQ(expected_relative_url, http_request.relative_url); |
550 EXPECT_TRUE(http_request.has_content); | 586 EXPECT_TRUE(http_request.has_content); |
551 EXPECT_EQ(kAttachmentData, http_request.content); | 587 EXPECT_EQ(kAttachmentData, http_request.content); |
552 std::string expected_header(kAuthorization); | 588 std::string expected_header(kAuthorization); |
553 const std::string header_name(kAuthorization); | 589 const std::string header_name(kAuthorization); |
554 const std::string header_value(std::string("Bearer ") + kAccessToken); | 590 const std::string header_value(std::string("Bearer ") + kAccessToken); |
555 EXPECT_THAT(http_request.headers, | 591 EXPECT_THAT(http_request.headers, |
556 testing::Contains(testing::Pair(header_name, header_value))); | 592 testing::Contains(testing::Pair(header_name, header_value))); |
557 | 593 |
558 // See that we invalidated the token. | 594 // See that we invalidated the token. |
559 ASSERT_EQ(1, token_service().num_invalidate_token()); | 595 ASSERT_EQ(1, token_service().num_invalidate_token()); |
560 } | 596 } |
561 | 597 |
562 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 598 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
563 // already exists. 409 Conflict? (bug 379825) | 599 // already exists. 409 Conflict? (bug 379825) |
564 | 600 |
565 } // namespace syncer | 601 } // namespace syncer |
OLD | NEW |