Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: chrome/browser/chromeos/drive/drive_url_request_job_unittest.cc

Issue 51953002: [Net] Add a priority parameter to URLRequest's constructor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile error from rebase Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/sequenced_task_runner.h" 11 #include "base/sequenced_task_runner.h"
12 #include "base/threading/sequenced_worker_pool.h" 12 #include "base/threading/sequenced_worker_pool.h"
13 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
14 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" 14 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h"
15 #include "chrome/browser/chromeos/drive/fake_file_system.h" 15 #include "chrome/browser/chromeos/drive/fake_file_system.h"
16 #include "chrome/browser/chromeos/drive/file_system_util.h" 16 #include "chrome/browser/chromeos/drive/file_system_util.h"
17 #include "chrome/browser/chromeos/drive/test_util.h" 17 #include "chrome/browser/chromeos/drive/test_util.h"
18 #include "chrome/browser/drive/fake_drive_service.h" 18 #include "chrome/browser/drive/fake_drive_service.h"
19 #include "chrome/browser/google_apis/test_util.h" 19 #include "chrome/browser/google_apis/test_util.h"
20 #include "chrome/common/url_constants.h" 20 #include "chrome/common/url_constants.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "net/base/request_priority.h"
23 #include "net/base/test_completion_callback.h" 24 #include "net/base/test_completion_callback.h"
24 #include "net/http/http_byte_range.h" 25 #include "net/http/http_byte_range.h"
25 #include "net/url_request/url_request_test_util.h" 26 #include "net/url_request/url_request_test_util.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 #include "url/gurl.h" 28 #include "url/gurl.h"
28 29
29 namespace drive { 30 namespace drive {
30 namespace { 31 namespace {
31 32
32 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob. 33 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob.
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 scoped_ptr<FakeDriveService> fake_drive_service_; 177 scoped_ptr<FakeDriveService> fake_drive_service_;
177 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; 178 scoped_ptr<test_util::FakeFileSystem> fake_file_system_;
178 179
179 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; 180 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_;
180 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; 181 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_;
181 scoped_ptr<net::URLRequestContext> url_request_context_; 182 scoped_ptr<net::URLRequestContext> url_request_context_;
182 scoped_ptr<TestDelegate> test_delegate_; 183 scoped_ptr<TestDelegate> test_delegate_;
183 }; 184 };
184 185
185 TEST_F(DriveURLRequestJobTest, NonGetMethod) { 186 TEST_F(DriveURLRequestJobTest, NonGetMethod) {
186 net::URLRequest request( 187 net::URLRequest request(GURL("drive:drive/root/File 1.txt"),
187 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), 188 net::DEFAULT_PRIORITY,
188 url_request_context_.get(), test_network_delegate_.get()); 189 test_delegate_.get(),
190 url_request_context_.get(),
191 test_network_delegate_.get());
189 request.set_method("POST"); // Set non "GET" method. 192 request.set_method("POST"); // Set non "GET" method.
190 request.Start(); 193 request.Start();
191 194
192 base::RunLoop().Run(); 195 base::RunLoop().Run();
193 196
194 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); 197 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
195 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); 198 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error());
196 } 199 }
197 200
198 TEST_F(DriveURLRequestJobTest, RegularFile) { 201 TEST_F(DriveURLRequestJobTest, RegularFile) {
199 const GURL kTestUrl("drive:drive/root/File 1.txt"); 202 const GURL kTestUrl("drive:drive/root/File 1.txt");
200 const base::FilePath kTestFilePath("drive/root/File 1.txt"); 203 const base::FilePath kTestFilePath("drive/root/File 1.txt");
201 204
202 // For the first time, the file should be fetched from the server. 205 // For the first time, the file should be fetched from the server.
203 { 206 {
204 net::URLRequest request( 207 net::URLRequest request(kTestUrl,
205 kTestUrl, test_delegate_.get(), 208 net::DEFAULT_PRIORITY,
206 url_request_context_.get(), test_network_delegate_.get()); 209 test_delegate_.get(),
210 url_request_context_.get(),
211 test_network_delegate_.get());
207 request.Start(); 212 request.Start();
208 213
209 base::RunLoop().Run(); 214 base::RunLoop().Run();
210 215
211 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); 216 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
212 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" 217 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg"
213 // on the server. 218 // on the server.
214 std::string mime_type; 219 std::string mime_type;
215 request.GetMimeType(&mime_type); 220 request.GetMimeType(&mime_type);
216 EXPECT_EQ("audio/mpeg", mime_type); 221 EXPECT_EQ("audio/mpeg", mime_type);
217 222
218 // Reading file must be done after |request| runs, otherwise 223 // Reading file must be done after |request| runs, otherwise
219 // it'll create a local cache file, and we cannot test correctly. 224 // it'll create a local cache file, and we cannot test correctly.
220 std::string expected_data; 225 std::string expected_data;
221 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); 226 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data));
222 EXPECT_EQ(expected_data, test_delegate_->data_received()); 227 EXPECT_EQ(expected_data, test_delegate_->data_received());
223 } 228 }
224 229
225 // For the second time, the locally cached file should be used. 230 // For the second time, the locally cached file should be used.
226 // The caching emulation is done by FakeFileSystem. 231 // The caching emulation is done by FakeFileSystem.
227 { 232 {
228 test_delegate_.reset(new TestDelegate); 233 test_delegate_.reset(new TestDelegate);
229 net::URLRequest request( 234 net::URLRequest request(GURL("drive:drive/root/File 1.txt"),
230 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), 235 net::DEFAULT_PRIORITY,
231 url_request_context_.get(), test_network_delegate_.get()); 236 test_delegate_.get(),
237 url_request_context_.get(),
238 test_network_delegate_.get());
232 request.Start(); 239 request.Start();
233 240
234 base::RunLoop().Run(); 241 base::RunLoop().Run();
235 242
236 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); 243 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
237 std::string mime_type; 244 std::string mime_type;
238 request.GetMimeType(&mime_type); 245 request.GetMimeType(&mime_type);
239 EXPECT_EQ("audio/mpeg", mime_type); 246 EXPECT_EQ("audio/mpeg", mime_type);
240 247
241 std::string expected_data; 248 std::string expected_data;
242 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); 249 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data));
243 EXPECT_EQ(expected_data, test_delegate_->data_received()); 250 EXPECT_EQ(expected_data, test_delegate_->data_received());
244 } 251 }
245 } 252 }
246 253
247 TEST_F(DriveURLRequestJobTest, HostedDocument) { 254 TEST_F(DriveURLRequestJobTest, HostedDocument) {
248 // Open a gdoc file. 255 // Open a gdoc file.
249 test_delegate_->set_quit_on_redirect(true); 256 test_delegate_->set_quit_on_redirect(true);
250 net::URLRequest request( 257 net::URLRequest request(
251 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), 258 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"),
259 net::DEFAULT_PRIORITY,
252 test_delegate_.get(), 260 test_delegate_.get(),
253 url_request_context_.get(), test_network_delegate_.get()); 261 url_request_context_.get(),
262 test_network_delegate_.get());
254 request.Start(); 263 request.Start();
255 264
256 base::RunLoop().Run(); 265 base::RunLoop().Run();
257 266
258 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); 267 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
259 // Make sure that a hosted document triggers redirection. 268 // Make sure that a hosted document triggers redirection.
260 EXPECT_TRUE(request.is_redirecting()); 269 EXPECT_TRUE(request.is_redirecting());
261 EXPECT_EQ(GURL("https://3_document_alternate_link"), 270 EXPECT_EQ(GURL("https://3_document_alternate_link"),
262 test_delegate_->redirect_url()); 271 test_delegate_->redirect_url());
263 } 272 }
264 273
265 TEST_F(DriveURLRequestJobTest, RootDirectory) { 274 TEST_F(DriveURLRequestJobTest, RootDirectory) {
266 net::URLRequest request( 275 net::URLRequest request(GURL("drive:drive/root"),
267 GURL("drive:drive/root"), test_delegate_.get(), 276 net::DEFAULT_PRIORITY,
268 url_request_context_.get(), test_network_delegate_.get()); 277 test_delegate_.get(),
278 url_request_context_.get(),
279 test_network_delegate_.get());
269 request.Start(); 280 request.Start();
270 281
271 base::RunLoop().Run(); 282 base::RunLoop().Run();
272 283
273 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); 284 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
274 EXPECT_EQ(net::ERR_FAILED, request.status().error()); 285 EXPECT_EQ(net::ERR_FAILED, request.status().error());
275 } 286 }
276 287
277 TEST_F(DriveURLRequestJobTest, Directory) { 288 TEST_F(DriveURLRequestJobTest, Directory) {
278 net::URLRequest request( 289 net::URLRequest request(GURL("drive:drive/root/Directory 1"),
279 GURL("drive:drive/root/Directory 1"), test_delegate_.get(), 290 net::DEFAULT_PRIORITY,
280 url_request_context_.get(), test_network_delegate_.get()); 291 test_delegate_.get(),
292 url_request_context_.get(),
293 test_network_delegate_.get());
281 request.Start(); 294 request.Start();
282 295
283 base::RunLoop().Run(); 296 base::RunLoop().Run();
284 297
285 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); 298 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
286 EXPECT_EQ(net::ERR_FAILED, request.status().error()); 299 EXPECT_EQ(net::ERR_FAILED, request.status().error());
287 } 300 }
288 301
289 TEST_F(DriveURLRequestJobTest, NonExistingFile) { 302 TEST_F(DriveURLRequestJobTest, NonExistingFile) {
290 net::URLRequest request( 303 net::URLRequest request(GURL("drive:drive/root/non-existing-file.txt"),
291 GURL("drive:drive/root/non-existing-file.txt"), test_delegate_.get(), 304 net::DEFAULT_PRIORITY,
292 url_request_context_.get(), test_network_delegate_.get()); 305 test_delegate_.get(),
306 url_request_context_.get(),
307 test_network_delegate_.get());
293 request.Start(); 308 request.Start();
294 309
295 base::RunLoop().Run(); 310 base::RunLoop().Run();
296 311
297 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); 312 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
298 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); 313 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error());
299 } 314 }
300 315
301 TEST_F(DriveURLRequestJobTest, WrongFormat) { 316 TEST_F(DriveURLRequestJobTest, WrongFormat) {
302 net::URLRequest request( 317 net::URLRequest request(GURL("drive:"),
303 GURL("drive:"), test_delegate_.get(), 318 net::DEFAULT_PRIORITY,
304 url_request_context_.get(), test_network_delegate_.get()); 319 test_delegate_.get(),
320 url_request_context_.get(),
321 test_network_delegate_.get());
305 request.Start(); 322 request.Start();
306 323
307 base::RunLoop().Run(); 324 base::RunLoop().Run();
308 325
309 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); 326 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
310 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); 327 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error());
311 } 328 }
312 329
313 TEST_F(DriveURLRequestJobTest, Cancel) { 330 TEST_F(DriveURLRequestJobTest, Cancel) {
314 net::URLRequest request( 331 net::URLRequest request(GURL("drive:drive/root/File 1.txt"),
315 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), 332 net::DEFAULT_PRIORITY,
316 url_request_context_.get(), test_network_delegate_.get()); 333 test_delegate_.get(),
334 url_request_context_.get(),
335 test_network_delegate_.get());
317 336
318 // Start the request, and cancel it immediately after it. 337 // Start the request, and cancel it immediately after it.
319 request.Start(); 338 request.Start();
320 request.Cancel(); 339 request.Cancel();
321 340
322 base::RunLoop().Run(); 341 base::RunLoop().Run();
323 342
324 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); 343 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status());
325 } 344 }
326 345
327 TEST_F(DriveURLRequestJobTest, RangeHeader) { 346 TEST_F(DriveURLRequestJobTest, RangeHeader) {
328 const GURL kTestUrl("drive:drive/root/File 1.txt"); 347 const GURL kTestUrl("drive:drive/root/File 1.txt");
329 const base::FilePath kTestFilePath("drive/root/File 1.txt"); 348 const base::FilePath kTestFilePath("drive/root/File 1.txt");
330 349
331 net::URLRequest request( 350 net::URLRequest request(kTestUrl,
332 kTestUrl, test_delegate_.get(), 351 net::DEFAULT_PRIORITY,
333 url_request_context_.get(), test_network_delegate_.get()); 352 test_delegate_.get(),
353 url_request_context_.get(),
354 test_network_delegate_.get());
334 355
335 // Set range header. 356 // Set range header.
336 request.SetExtraRequestHeaderByName( 357 request.SetExtraRequestHeaderByName(
337 "Range", "bytes=3-5", false /* overwrite */); 358 "Range", "bytes=3-5", false /* overwrite */);
338 request.Start(); 359 request.Start();
339 360
340 base::RunLoop().Run(); 361 base::RunLoop().Run();
341 362
342 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); 363 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
343 364
344 // Reading file must be done after |request| runs, otherwise 365 // Reading file must be done after |request| runs, otherwise
345 // it'll create a local cache file, and we cannot test correctly. 366 // it'll create a local cache file, and we cannot test correctly.
346 std::string expected_data; 367 std::string expected_data;
347 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); 368 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data));
348 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); 369 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received());
349 } 370 }
350 371
351 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) { 372 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) {
352 const GURL kTestUrl("drive:drive/root/File 1.txt"); 373 const GURL kTestUrl("drive:drive/root/File 1.txt");
353 374
354 net::URLRequest request( 375 net::URLRequest request(kTestUrl,
355 kTestUrl, test_delegate_.get(), 376 net::DEFAULT_PRIORITY,
356 url_request_context_.get(), test_network_delegate_.get()); 377 test_delegate_.get(),
378 url_request_context_.get(),
379 test_network_delegate_.get());
357 380
358 // Set range header. 381 // Set range header.
359 request.SetExtraRequestHeaderByName( 382 request.SetExtraRequestHeaderByName(
360 "Range", "Wrong Range Header Value", false /* overwrite */); 383 "Range", "Wrong Range Header Value", false /* overwrite */);
361 request.Start(); 384 request.Start();
362 385
363 base::RunLoop().Run(); 386 base::RunLoop().Run();
364 387
365 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); 388 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
366 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request.status().error()); 389 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request.status().error());
367 } 390 }
368 391
369 } // namespace drive 392 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698