| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "content/browser/fileapi/mock_url_request_delegate.h" |
| 15 #include "content/public/test/async_file_test_helper.h" | 16 #include "content/public/test/async_file_test_helper.h" |
| 16 #include "content/public/test/test_file_system_context.h" | 17 #include "content/public/test/test_file_system_context.h" |
| 17 #include "net/base/io_buffer.h" | |
| 18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
| 19 #include "net/http/http_byte_range.h" | 19 #include "net/http/http_byte_range.h" |
| 20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
| 21 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
| 22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 24 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "webkit/browser/blob/blob_url_request_job.h" | 26 #include "webkit/browser/blob/blob_url_request_job.h" |
| 27 #include "webkit/browser/fileapi/file_system_context.h" | 27 #include "webkit/browser/fileapi/file_system_context.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 47 const char kTestContentDisposition[] = "attachment; filename=foo.txt"; | 47 const char kTestContentDisposition[] = "attachment; filename=foo.txt"; |
| 48 | 48 |
| 49 const char kFileSystemURLOrigin[] = "http://remote"; | 49 const char kFileSystemURLOrigin[] = "http://remote"; |
| 50 const fileapi::FileSystemType kFileSystemType = | 50 const fileapi::FileSystemType kFileSystemType = |
| 51 fileapi::kFileSystemTypeTemporary; | 51 fileapi::kFileSystemTypeTemporary; |
| 52 | 52 |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 class BlobURLRequestJobTest : public testing::Test { | 55 class BlobURLRequestJobTest : public testing::Test { |
| 56 public: | 56 public: |
| 57 | |
| 58 // Test Harness ------------------------------------------------------------- | |
| 59 // TODO(jianli): share this test harness with AppCacheURLRequestJobTest | |
| 60 | |
| 61 class MockURLRequestDelegate : public net::URLRequest::Delegate { | |
| 62 public: | |
| 63 MockURLRequestDelegate() | |
| 64 : received_data_(new net::IOBuffer(kBufferSize)) {} | |
| 65 | |
| 66 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE { | |
| 67 if (request->status().is_success()) { | |
| 68 EXPECT_TRUE(request->response_headers()); | |
| 69 ReadSome(request); | |
| 70 } else { | |
| 71 RequestComplete(); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 virtual void OnReadCompleted(net::URLRequest* request, | |
| 76 int bytes_read) OVERRIDE { | |
| 77 if (bytes_read > 0) | |
| 78 ReceiveData(request, bytes_read); | |
| 79 else | |
| 80 RequestComplete(); | |
| 81 } | |
| 82 | |
| 83 const std::string& response_data() const { return response_data_; } | |
| 84 | |
| 85 private: | |
| 86 void ReadSome(net::URLRequest* request) { | |
| 87 if (!request->is_pending()) { | |
| 88 RequestComplete(); | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 int bytes_read = 0; | |
| 93 if (!request->Read(received_data_.get(), kBufferSize, &bytes_read)) { | |
| 94 if (!request->status().is_io_pending()) { | |
| 95 RequestComplete(); | |
| 96 } | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 ReceiveData(request, bytes_read); | |
| 101 } | |
| 102 | |
| 103 void ReceiveData(net::URLRequest* request, int bytes_read) { | |
| 104 if (bytes_read) { | |
| 105 response_data_.append(received_data_->data(), | |
| 106 static_cast<size_t>(bytes_read)); | |
| 107 ReadSome(request); | |
| 108 } else { | |
| 109 RequestComplete(); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void RequestComplete() { | |
| 114 base::MessageLoop::current()->Quit(); | |
| 115 } | |
| 116 | |
| 117 scoped_refptr<net::IOBuffer> received_data_; | |
| 118 std::string response_data_; | |
| 119 }; | |
| 120 | |
| 121 // A simple ProtocolHandler implementation to create BlobURLRequestJob. | 57 // A simple ProtocolHandler implementation to create BlobURLRequestJob. |
| 122 class MockProtocolHandler : | 58 class MockProtocolHandler : |
| 123 public net::URLRequestJobFactory::ProtocolHandler { | 59 public net::URLRequestJobFactory::ProtocolHandler { |
| 124 public: | 60 public: |
| 125 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} | 61 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} |
| 126 | 62 |
| 127 // net::URLRequestJobFactory::ProtocolHandler override. | 63 // net::URLRequestJobFactory::ProtocolHandler override. |
| 128 virtual net::URLRequestJob* MaybeCreateJob( | 64 virtual net::URLRequestJob* MaybeCreateJob( |
| 129 net::URLRequest* request, | 65 net::URLRequest* request, |
| 130 net::NetworkDelegate* network_delegate) const OVERRIDE { | 66 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); | 407 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
| 472 EXPECT_EQ(kTestContentType, content_type); | 408 EXPECT_EQ(kTestContentType, content_type); |
| 473 void* iter = NULL; | 409 void* iter = NULL; |
| 474 std::string content_disposition; | 410 std::string content_disposition; |
| 475 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( | 411 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
| 476 &iter, "Content-Disposition", &content_disposition)); | 412 &iter, "Content-Disposition", &content_disposition)); |
| 477 EXPECT_EQ(kTestContentDisposition, content_disposition); | 413 EXPECT_EQ(kTestContentDisposition, content_disposition); |
| 478 } | 414 } |
| 479 | 415 |
| 480 } // namespace content | 416 } // namespace content |
| OLD | NEW |