| OLD | NEW | 
|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "net/url_request/url_request_file_dir_job.h" | 5 #include "net/url_request/url_request_file_dir_job.h" | 
| 6 | 6 | 
| 7 #include <memory> | 7 #include <memory> | 
| 8 #include <string> | 8 #include <string> | 
| 9 | 9 | 
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" | 
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 118 | 118 | 
| 119   request->Start(); | 119   request->Start(); | 
| 120   ASSERT_TRUE(directory.Delete()); | 120   ASSERT_TRUE(directory.Delete()); | 
| 121 | 121 | 
| 122   // Since the DirectoryLister is running on the network thread, this | 122   // Since the DirectoryLister is running on the network thread, this | 
| 123   // will spin the message loop until the read error is returned to the | 123   // will spin the message loop until the read error is returned to the | 
| 124   // URLRequestFileDirJob. | 124   // URLRequestFileDirJob. | 
| 125   base::RunLoop().RunUntilIdle(); | 125   base::RunLoop().RunUntilIdle(); | 
| 126   ASSERT_TRUE(delegate_.got_response_started()); | 126   ASSERT_TRUE(delegate_.got_response_started()); | 
| 127 | 127 | 
| 128   int bytes_read = request->Read(buffer_.get(), kBufferSize); | 128   int bytes_read = 0; | 
|  | 129   EXPECT_FALSE(request->Read(buffer_.get(), kBufferSize, &bytes_read)); | 
| 129 | 130 | 
| 130   // The URLRequestFileDirJobShould return the cached read error synchronously. | 131   // The URLRequestFileDirJobShould return the cached read error synchronously. | 
| 131   // If it's not returned synchronously, the code path this is intended to test | 132   // If it's not returned synchronously, the code path this is intended to test | 
| 132   // was not executed. | 133   // was not executed. | 
| 133   EXPECT_THAT(bytes_read, IsError(ERR_FILE_NOT_FOUND)); | 134   EXPECT_THAT(request->status().ToNetError(), IsError(ERR_FILE_NOT_FOUND)); | 
| 134 } | 135 } | 
| 135 | 136 | 
| 136 // Test the case where reading the response completes synchronously. | 137 // Test the case where reading the response completes synchronously. | 
| 137 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) { | 138 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) { | 
| 138   base::ScopedTempDir directory; | 139   base::ScopedTempDir directory; | 
| 139   ASSERT_TRUE(directory.CreateUniqueTempDir()); | 140   ASSERT_TRUE(directory.CreateUniqueTempDir()); | 
| 140   base::FilePath path; | 141   base::FilePath path; | 
| 141   base::CreateTemporaryFileInDir(directory.path(), &path); | 142   base::CreateTemporaryFileInDir(directory.path(), &path); | 
| 142 | 143 | 
| 143   TestJobFactory factory(directory.path()); | 144   TestJobFactory factory(directory.path()); | 
| 144   context_.set_job_factory(&factory); | 145   context_.set_job_factory(&factory); | 
| 145 | 146 | 
| 146   std::unique_ptr<URLRequest> request(context_.CreateRequest( | 147   std::unique_ptr<URLRequest> request(context_.CreateRequest( | 
| 147       FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); | 148       FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); | 
| 148   request->Start(); | 149   request->Start(); | 
| 149   EXPECT_TRUE(request->is_pending()); | 150   EXPECT_TRUE(request->is_pending()); | 
| 150 | 151 | 
| 151   // Since the DirectoryLister is running on the network thread, this will spin | 152   // Since the DirectoryLister is running on the network thread, this will spin | 
| 152   // the message loop until the URLRequetsFileDirJob has received the | 153   // the message loop until the URLRequetsFileDirJob has received the | 
| 153   // entire directory listing and cached it. | 154   // entire directory listing and cached it. | 
| 154   base::RunLoop().RunUntilIdle(); | 155   base::RunLoop().RunUntilIdle(); | 
| 155 | 156 | 
|  | 157   int bytes_read = 0; | 
| 156   // This will complete synchronously, since the URLRequetsFileDirJob had | 158   // This will complete synchronously, since the URLRequetsFileDirJob had | 
| 157   // directory listing cached in memory. | 159   // directory listing cached in memory. | 
| 158   int bytes_read = request->Read(buffer_.get(), kBufferSize); | 160   EXPECT_TRUE(request->Read(buffer_.get(), kBufferSize, &bytes_read)); | 
|  | 161 | 
|  | 162   EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); | 
| 159 | 163 | 
| 160   ASSERT_GT(bytes_read, 0); | 164   ASSERT_GT(bytes_read, 0); | 
| 161   ASSERT_LE(bytes_read, kBufferSize); | 165   ASSERT_LE(bytes_read, kBufferSize); | 
| 162   std::string data(buffer_->data(), bytes_read); | 166   std::string data(buffer_->data(), bytes_read); | 
| 163   EXPECT_TRUE(data.find(directory.path().BaseName().MaybeAsASCII()) != | 167   EXPECT_TRUE(data.find(directory.path().BaseName().MaybeAsASCII()) != | 
| 164               std::string::npos); | 168               std::string::npos); | 
| 165   EXPECT_TRUE(data.find(path.BaseName().MaybeAsASCII()) != std::string::npos); | 169   EXPECT_TRUE(data.find(path.BaseName().MaybeAsASCII()) != std::string::npos); | 
| 166 } | 170 } | 
| 167 | 171 | 
| 168 // Test the case where reading the response completes asynchronously. | 172 // Test the case where reading the response completes asynchronously. | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 244   ASSERT_GT(delegate.bytes_received(), 0); | 248   ASSERT_GT(delegate.bytes_received(), 0); | 
| 245   ASSERT_LE(delegate.bytes_received(), kBufferSize); | 249   ASSERT_LE(delegate.bytes_received(), kBufferSize); | 
| 246   EXPECT_TRUE(delegate.data_received().find( | 250   EXPECT_TRUE(delegate.data_received().find( | 
| 247                   directory.path().BaseName().MaybeAsASCII()) != | 251                   directory.path().BaseName().MaybeAsASCII()) != | 
| 248               std::string::npos); | 252               std::string::npos); | 
| 249 } | 253 } | 
| 250 | 254 | 
| 251 }  // namespace | 255 }  // namespace | 
| 252 | 256 | 
| 253 }  // namespace net | 257 }  // namespace net | 
| OLD | NEW | 
|---|