| 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" | 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" | 9 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
| 10 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 10 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 std::string content; | 173 std::string content; |
| 174 while (content.size() < content_size) { | 174 while (content.size() < content_size) { |
| 175 int result = reader->Read(buffer.get(), kBufferSize, callback.callback()); | 175 int result = reader->Read(buffer.get(), kBufferSize, callback.callback()); |
| 176 result = callback.GetResult(result); | 176 result = callback.GetResult(result); |
| 177 content.append(buffer->data(), result); | 177 content.append(buffer->data(), result); |
| 178 } | 178 } |
| 179 | 179 |
| 180 return content; | 180 return content; |
| 181 } | 181 } |
| 182 | 182 |
| 183 MessageLoopForIO message_loop_; | 183 base::MessageLoopForIO message_loop_; |
| 184 content::TestBrowserThread ui_thread_; | 184 content::TestBrowserThread ui_thread_; |
| 185 content::TestBrowserThread io_thread_; | 185 content::TestBrowserThread io_thread_; |
| 186 | 186 |
| 187 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; | 187 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
| 188 scoped_ptr<test_util::FakeDriveFileSystem> fake_drive_file_system_; | 188 scoped_ptr<test_util::FakeDriveFileSystem> fake_drive_file_system_; |
| 189 | 189 |
| 190 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; | 190 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; |
| 191 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; | 191 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; |
| 192 scoped_ptr<net::URLRequestContext> url_request_context_; | 192 scoped_ptr<net::URLRequestContext> url_request_context_; |
| 193 scoped_ptr<TestDelegate> test_delegate_; | 193 scoped_ptr<TestDelegate> test_delegate_; |
| 194 }; | 194 }; |
| 195 | 195 |
| 196 TEST_F(DriveURLRequestJobTest, NonGetMethod) { | 196 TEST_F(DriveURLRequestJobTest, NonGetMethod) { |
| 197 net::URLRequest request( | 197 net::URLRequest request( |
| 198 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 198 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
| 199 url_request_context_.get(), test_network_delegate_.get()); | 199 url_request_context_.get(), test_network_delegate_.get()); |
| 200 request.set_method("POST"); // Set non "GET" method. | 200 request.set_method("POST"); // Set non "GET" method. |
| 201 request.Start(); | 201 request.Start(); |
| 202 | 202 |
| 203 MessageLoop::current()->Run(); | 203 base::MessageLoop::current()->Run(); |
| 204 | 204 |
| 205 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 205 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 206 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); | 206 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); |
| 207 } | 207 } |
| 208 | 208 |
| 209 TEST_F(DriveURLRequestJobTest, RegularFile) { | 209 TEST_F(DriveURLRequestJobTest, RegularFile) { |
| 210 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 210 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
| 211 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 211 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
| 212 | 212 |
| 213 // For the first time, the file should be fetched from the server. | 213 // For the first time, the file should be fetched from the server. |
| 214 { | 214 { |
| 215 net::URLRequest request( | 215 net::URLRequest request( |
| 216 kTestUrl, test_delegate_.get(), | 216 kTestUrl, test_delegate_.get(), |
| 217 url_request_context_.get(), test_network_delegate_.get()); | 217 url_request_context_.get(), test_network_delegate_.get()); |
| 218 request.Start(); | 218 request.Start(); |
| 219 | 219 |
| 220 MessageLoop::current()->Run(); | 220 base::MessageLoop::current()->Run(); |
| 221 | 221 |
| 222 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 222 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
| 223 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" | 223 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" |
| 224 // on the server. | 224 // on the server. |
| 225 std::string mime_type; | 225 std::string mime_type; |
| 226 request.GetMimeType(&mime_type); | 226 request.GetMimeType(&mime_type); |
| 227 EXPECT_EQ("audio/mpeg", mime_type); | 227 EXPECT_EQ("audio/mpeg", mime_type); |
| 228 // Reading file must be done after |request| runs, otherwise | 228 // Reading file must be done after |request| runs, otherwise |
| 229 // it'll create a local cache file, and we cannot test correctly. | 229 // it'll create a local cache file, and we cannot test correctly. |
| 230 EXPECT_EQ(ReadDriveFileSync(kTestFilePath), | 230 EXPECT_EQ(ReadDriveFileSync(kTestFilePath), |
| 231 test_delegate_->data_received()); | 231 test_delegate_->data_received()); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // For the second time, the locally cached file should be used. | 234 // For the second time, the locally cached file should be used. |
| 235 // The caching emulation is done by FakeDriveFileSystem. | 235 // The caching emulation is done by FakeDriveFileSystem. |
| 236 { | 236 { |
| 237 test_delegate_.reset(new TestDelegate); | 237 test_delegate_.reset(new TestDelegate); |
| 238 net::URLRequest request( | 238 net::URLRequest request( |
| 239 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 239 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
| 240 url_request_context_.get(), test_network_delegate_.get()); | 240 url_request_context_.get(), test_network_delegate_.get()); |
| 241 request.Start(); | 241 request.Start(); |
| 242 | 242 |
| 243 MessageLoop::current()->Run(); | 243 base::MessageLoop::current()->Run(); |
| 244 | 244 |
| 245 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 245 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
| 246 std::string mime_type; | 246 std::string mime_type; |
| 247 request.GetMimeType(&mime_type); | 247 request.GetMimeType(&mime_type); |
| 248 EXPECT_EQ("audio/mpeg", mime_type); | 248 EXPECT_EQ("audio/mpeg", mime_type); |
| 249 EXPECT_EQ(ReadDriveFileSync(kTestFilePath), | 249 EXPECT_EQ(ReadDriveFileSync(kTestFilePath), |
| 250 test_delegate_->data_received()); | 250 test_delegate_->data_received()); |
| 251 } | 251 } |
| 252 } | 252 } |
| 253 | 253 |
| 254 TEST_F(DriveURLRequestJobTest, HostedDocument) { | 254 TEST_F(DriveURLRequestJobTest, HostedDocument) { |
| 255 // Open a gdoc file. | 255 // Open a gdoc file. |
| 256 test_delegate_->set_quit_on_redirect(true); | 256 test_delegate_->set_quit_on_redirect(true); |
| 257 net::URLRequest request( | 257 net::URLRequest request( |
| 258 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), | 258 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), |
| 259 test_delegate_.get(), | 259 test_delegate_.get(), |
| 260 url_request_context_.get(), test_network_delegate_.get()); | 260 url_request_context_.get(), test_network_delegate_.get()); |
| 261 request.Start(); | 261 request.Start(); |
| 262 | 262 |
| 263 MessageLoop::current()->Run(); | 263 base::MessageLoop::current()->Run(); |
| 264 | 264 |
| 265 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 265 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
| 266 // Make sure that a hosted document triggers redirection. | 266 // Make sure that a hosted document triggers redirection. |
| 267 EXPECT_TRUE(request.is_redirecting()); | 267 EXPECT_TRUE(request.is_redirecting()); |
| 268 EXPECT_EQ(GURL("https://3_document_alternate_link"), | 268 EXPECT_EQ(GURL("https://3_document_alternate_link"), |
| 269 test_delegate_->redirect_url()); | 269 test_delegate_->redirect_url()); |
| 270 } | 270 } |
| 271 | 271 |
| 272 TEST_F(DriveURLRequestJobTest, RootDirectory) { | 272 TEST_F(DriveURLRequestJobTest, RootDirectory) { |
| 273 net::URLRequest request( | 273 net::URLRequest request( |
| 274 GURL("drive:drive/root"), test_delegate_.get(), | 274 GURL("drive:drive/root"), test_delegate_.get(), |
| 275 url_request_context_.get(), test_network_delegate_.get()); | 275 url_request_context_.get(), test_network_delegate_.get()); |
| 276 request.Start(); | 276 request.Start(); |
| 277 | 277 |
| 278 MessageLoop::current()->Run(); | 278 base::MessageLoop::current()->Run(); |
| 279 | 279 |
| 280 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 280 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 281 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 281 EXPECT_EQ(net::ERR_FAILED, request.status().error()); |
| 282 } | 282 } |
| 283 | 283 |
| 284 TEST_F(DriveURLRequestJobTest, Directory) { | 284 TEST_F(DriveURLRequestJobTest, Directory) { |
| 285 net::URLRequest request( | 285 net::URLRequest request( |
| 286 GURL("drive:drive/root/Directory 1"), test_delegate_.get(), | 286 GURL("drive:drive/root/Directory 1"), test_delegate_.get(), |
| 287 url_request_context_.get(), test_network_delegate_.get()); | 287 url_request_context_.get(), test_network_delegate_.get()); |
| 288 request.Start(); | 288 request.Start(); |
| 289 | 289 |
| 290 MessageLoop::current()->Run(); | 290 base::MessageLoop::current()->Run(); |
| 291 | 291 |
| 292 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 292 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 293 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 293 EXPECT_EQ(net::ERR_FAILED, request.status().error()); |
| 294 } | 294 } |
| 295 | 295 |
| 296 TEST_F(DriveURLRequestJobTest, NonExistingFile) { | 296 TEST_F(DriveURLRequestJobTest, NonExistingFile) { |
| 297 net::URLRequest request( | 297 net::URLRequest request( |
| 298 GURL("drive:drive/root/non-existing-file.txt"), test_delegate_.get(), | 298 GURL("drive:drive/root/non-existing-file.txt"), test_delegate_.get(), |
| 299 url_request_context_.get(), test_network_delegate_.get()); | 299 url_request_context_.get(), test_network_delegate_.get()); |
| 300 request.Start(); | 300 request.Start(); |
| 301 | 301 |
| 302 MessageLoop::current()->Run(); | 302 base::MessageLoop::current()->Run(); |
| 303 | 303 |
| 304 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 304 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 305 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); | 305 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); |
| 306 } | 306 } |
| 307 | 307 |
| 308 TEST_F(DriveURLRequestJobTest, WrongFormat) { | 308 TEST_F(DriveURLRequestJobTest, WrongFormat) { |
| 309 net::URLRequest request( | 309 net::URLRequest request( |
| 310 GURL("drive:"), test_delegate_.get(), | 310 GURL("drive:"), test_delegate_.get(), |
| 311 url_request_context_.get(), test_network_delegate_.get()); | 311 url_request_context_.get(), test_network_delegate_.get()); |
| 312 request.Start(); | 312 request.Start(); |
| 313 | 313 |
| 314 MessageLoop::current()->Run(); | 314 base::MessageLoop::current()->Run(); |
| 315 | 315 |
| 316 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 316 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
| 317 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); | 317 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); |
| 318 } | 318 } |
| 319 | 319 |
| 320 TEST_F(DriveURLRequestJobTest, Cancel) { | 320 TEST_F(DriveURLRequestJobTest, Cancel) { |
| 321 net::URLRequest request( | 321 net::URLRequest request( |
| 322 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 322 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
| 323 url_request_context_.get(), test_network_delegate_.get()); | 323 url_request_context_.get(), test_network_delegate_.get()); |
| 324 | 324 |
| 325 // Start the request, and cancel it immediately after it. | 325 // Start the request, and cancel it immediately after it. |
| 326 request.Start(); | 326 request.Start(); |
| 327 request.Cancel(); | 327 request.Cancel(); |
| 328 | 328 |
| 329 MessageLoop::current()->Run(); | 329 base::MessageLoop::current()->Run(); |
| 330 | 330 |
| 331 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); | 331 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); |
| 332 } | 332 } |
| 333 | 333 |
| 334 } // namespace drive | 334 } // namespace drive |
| OLD | NEW |