| 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 <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 void TearDown() override; | 201 void TearDown() override; |
| 202 | 202 |
| 203 // Run the message loop until UploadDone has been invoked |num_uploads| times. | 203 // Run the message loop until UploadDone has been invoked |num_uploads| times. |
| 204 void RunAndWaitFor(int num_uploads); | 204 void RunAndWaitFor(int num_uploads); |
| 205 | 205 |
| 206 // Upload an attachment and have the server respond with |status_code|. | 206 // Upload an attachment and have the server respond with |status_code|. |
| 207 // | 207 // |
| 208 // Returns the attachment that was uploaded. | 208 // Returns the attachment that was uploaded. |
| 209 Attachment UploadAndRespondWith(const net::HttpStatusCode& status_code); | 209 Attachment UploadAndRespondWith(const net::HttpStatusCode& status_code); |
| 210 | 210 |
| 211 scoped_ptr<AttachmentUploader>& uploader(); | 211 std::unique_ptr<AttachmentUploader>& uploader(); |
| 212 const AttachmentUploader::UploadCallback& upload_callback() const; | 212 const AttachmentUploader::UploadCallback& upload_callback() const; |
| 213 std::vector<HttpRequest>& http_requests_received(); | 213 std::vector<HttpRequest>& http_requests_received(); |
| 214 std::vector<AttachmentUploader::UploadResult>& upload_results(); | 214 std::vector<AttachmentUploader::UploadResult>& upload_results(); |
| 215 std::vector<AttachmentId>& attachment_ids(); | 215 std::vector<AttachmentId>& attachment_ids(); |
| 216 MockOAuth2TokenService& token_service(); | 216 MockOAuth2TokenService& token_service(); |
| 217 base::MessageLoopForIO& message_loop(); | 217 base::MessageLoopForIO& message_loop(); |
| 218 RequestHandler& request_handler(); | 218 RequestHandler& request_handler(); |
| 219 | 219 |
| 220 private: | 220 private: |
| 221 // An UploadCallback invoked by AttachmentUploaderImpl. | 221 // An UploadCallback invoked by AttachmentUploaderImpl. |
| 222 void UploadDone(const AttachmentUploader::UploadResult& result, | 222 void UploadDone(const AttachmentUploader::UploadResult& result, |
| 223 const AttachmentId& attachment_id); | 223 const AttachmentId& attachment_id); |
| 224 | 224 |
| 225 base::MessageLoopForIO message_loop_; | 225 base::MessageLoopForIO message_loop_; |
| 226 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 226 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 227 scoped_ptr<RequestHandler> request_handler_; | 227 std::unique_ptr<RequestHandler> request_handler_; |
| 228 scoped_ptr<AttachmentUploader> uploader_; | 228 std::unique_ptr<AttachmentUploader> uploader_; |
| 229 AttachmentUploader::UploadCallback upload_callback_; | 229 AttachmentUploader::UploadCallback upload_callback_; |
| 230 net::EmbeddedTestServer server_; | 230 net::EmbeddedTestServer server_; |
| 231 // A closure that signals an upload has finished. | 231 // A closure that signals an upload has finished. |
| 232 base::Closure signal_upload_done_; | 232 base::Closure signal_upload_done_; |
| 233 std::vector<HttpRequest> http_requests_received_; | 233 std::vector<HttpRequest> http_requests_received_; |
| 234 std::vector<AttachmentUploader::UploadResult> upload_results_; | 234 std::vector<AttachmentUploader::UploadResult> upload_results_; |
| 235 std::vector<AttachmentId> attachment_ids_; | 235 std::vector<AttachmentId> attachment_ids_; |
| 236 scoped_ptr<MockOAuth2TokenService> token_service_; | 236 std::unique_ptr<MockOAuth2TokenService> token_service_; |
| 237 | 237 |
| 238 // Must be last data member. | 238 // Must be last data member. |
| 239 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_; | 239 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_; |
| 240 }; | 240 }; |
| 241 | 241 |
| 242 // Handles HTTP requests received by the EmbeddedTestServer. | 242 // Handles HTTP requests received by the EmbeddedTestServer. |
| 243 // | 243 // |
| 244 // Responds with HTTP_OK by default. See |SetStatusCode|. | 244 // Responds with HTTP_OK by default. See |SetStatusCode|. |
| 245 class RequestHandler : public base::NonThreadSafe { | 245 class RequestHandler : public base::NonThreadSafe { |
| 246 public: | 246 public: |
| 247 // Construct a RequestHandler that will PostTask to |test| using | 247 // Construct a RequestHandler that will PostTask to |test| using |
| 248 // |test_task_runner|. | 248 // |test_task_runner|. |
| 249 RequestHandler( | 249 RequestHandler( |
| 250 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, | 250 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, |
| 251 const base::WeakPtr<AttachmentUploaderImplTest>& test); | 251 const base::WeakPtr<AttachmentUploaderImplTest>& test); |
| 252 | 252 |
| 253 ~RequestHandler(); | 253 ~RequestHandler(); |
| 254 | 254 |
| 255 scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request); | 255 std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request); |
| 256 | 256 |
| 257 // Set the HTTP status code to respond with. | 257 // Set the HTTP status code to respond with. |
| 258 void SetStatusCode(const net::HttpStatusCode& status_code); | 258 void SetStatusCode(const net::HttpStatusCode& status_code); |
| 259 | 259 |
| 260 // Returns the HTTP status code that will be used in responses. | 260 // Returns the HTTP status code that will be used in responses. |
| 261 net::HttpStatusCode GetStatusCode() const; | 261 net::HttpStatusCode GetStatusCode() const; |
| 262 | 262 |
| 263 private: | 263 private: |
| 264 // Protects status_code_. | 264 // Protects status_code_. |
| 265 mutable base::Lock mutex_; | 265 mutable base::Lock mutex_; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 const net::HttpStatusCode& status_code) { | 323 const net::HttpStatusCode& status_code) { |
| 324 token_service().AddAccount(kAccountId); | 324 token_service().AddAccount(kAccountId); |
| 325 request_handler().SetStatusCode(status_code); | 325 request_handler().SetStatusCode(status_code); |
| 326 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); | 326 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString); |
| 327 some_data->data() = kAttachmentData; | 327 some_data->data() = kAttachmentData; |
| 328 Attachment attachment = Attachment::Create(some_data); | 328 Attachment attachment = Attachment::Create(some_data); |
| 329 uploader()->UploadAttachment(attachment, upload_callback()); | 329 uploader()->UploadAttachment(attachment, upload_callback()); |
| 330 return attachment; | 330 return attachment; |
| 331 } | 331 } |
| 332 | 332 |
| 333 scoped_ptr<AttachmentUploader>& AttachmentUploaderImplTest::uploader() { | 333 std::unique_ptr<AttachmentUploader>& AttachmentUploaderImplTest::uploader() { |
| 334 return uploader_; | 334 return uploader_; |
| 335 } | 335 } |
| 336 | 336 |
| 337 const AttachmentUploader::UploadCallback& | 337 const AttachmentUploader::UploadCallback& |
| 338 AttachmentUploaderImplTest::upload_callback() const { | 338 AttachmentUploaderImplTest::upload_callback() const { |
| 339 return upload_callback_; | 339 return upload_callback_; |
| 340 } | 340 } |
| 341 | 341 |
| 342 std::vector<HttpRequest>& AttachmentUploaderImplTest::http_requests_received() { | 342 std::vector<HttpRequest>& AttachmentUploaderImplTest::http_requests_received() { |
| 343 return http_requests_received_; | 343 return http_requests_received_; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 : status_code_(net::HTTP_OK), | 381 : status_code_(net::HTTP_OK), |
| 382 test_task_runner_(test_task_runner), | 382 test_task_runner_(test_task_runner), |
| 383 test_(test) { | 383 test_(test) { |
| 384 DetachFromThread(); | 384 DetachFromThread(); |
| 385 } | 385 } |
| 386 | 386 |
| 387 RequestHandler::~RequestHandler() { | 387 RequestHandler::~RequestHandler() { |
| 388 DetachFromThread(); | 388 DetachFromThread(); |
| 389 } | 389 } |
| 390 | 390 |
| 391 scoped_ptr<HttpResponse> RequestHandler::HandleRequest( | 391 std::unique_ptr<HttpResponse> RequestHandler::HandleRequest( |
| 392 const HttpRequest& request) { | 392 const HttpRequest& request) { |
| 393 DCHECK(CalledOnValidThread()); | 393 DCHECK(CalledOnValidThread()); |
| 394 test_task_runner_->PostTask( | 394 test_task_runner_->PostTask( |
| 395 FROM_HERE, | 395 FROM_HERE, |
| 396 base::Bind( | 396 base::Bind( |
| 397 &AttachmentUploaderImplTest::OnRequestReceived, test_, request)); | 397 &AttachmentUploaderImplTest::OnRequestReceived, test_, request)); |
| 398 scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse); | 398 std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse); |
| 399 response->set_code(GetStatusCode()); | 399 response->set_code(GetStatusCode()); |
| 400 response->set_content_type("text/plain"); | 400 response->set_content_type("text/plain"); |
| 401 return std::move(response); | 401 return std::move(response); |
| 402 } | 402 } |
| 403 | 403 |
| 404 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { | 404 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { |
| 405 base::AutoLock lock(mutex_); | 405 base::AutoLock lock(mutex_); |
| 406 status_code_ = status_code; | 406 status_code_ = status_code; |
| 407 } | 407 } |
| 408 | 408 |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); | 674 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); |
| 675 hello_world->data() = "hello world"; | 675 hello_world->data() = "hello world"; |
| 676 EXPECT_EQ("yZRlqg==", AttachmentUploaderImpl::FormatCrc32cHash( | 676 EXPECT_EQ("yZRlqg==", AttachmentUploaderImpl::FormatCrc32cHash( |
| 677 ComputeCrc32c(hello_world))); | 677 ComputeCrc32c(hello_world))); |
| 678 } | 678 } |
| 679 | 679 |
| 680 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 680 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
| 681 // already exists. 409 Conflict? (bug 379825) | 681 // already exists. 409 Conflict? (bug 379825) |
| 682 | 682 |
| 683 } // namespace syncer | 683 } // namespace syncer |
| OLD | NEW |