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

Side by Side Diff: chrome/browser/extensions/api/web_request/web_request_api_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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <map> 5 #include <map>
6 #include <queue> 6 #include <queue>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 24 matching lines...) Expand all
35 #include "chrome/test/base/testing_browser_process.h" 35 #include "chrome/test/base/testing_browser_process.h"
36 #include "chrome/test/base/testing_pref_service_syncable.h" 36 #include "chrome/test/base/testing_pref_service_syncable.h"
37 #include "chrome/test/base/testing_profile.h" 37 #include "chrome/test/base/testing_profile.h"
38 #include "chrome/test/base/testing_profile_manager.h" 38 #include "chrome/test/base/testing_profile_manager.h"
39 #include "content/public/common/url_constants.h" 39 #include "content/public/common/url_constants.h"
40 #include "content/public/test/test_browser_thread_bundle.h" 40 #include "content/public/test/test_browser_thread_bundle.h"
41 #include "extensions/common/features/feature.h" 41 #include "extensions/common/features/feature.h"
42 #include "net/base/auth.h" 42 #include "net/base/auth.h"
43 #include "net/base/capturing_net_log.h" 43 #include "net/base/capturing_net_log.h"
44 #include "net/base/net_util.h" 44 #include "net/base/net_util.h"
45 #include "net/base/request_priority.h"
45 #include "net/base/upload_bytes_element_reader.h" 46 #include "net/base/upload_bytes_element_reader.h"
46 #include "net/base/upload_data_stream.h" 47 #include "net/base/upload_data_stream.h"
47 #include "net/base/upload_file_element_reader.h" 48 #include "net/base/upload_file_element_reader.h"
48 #include "net/dns/mock_host_resolver.h" 49 #include "net/dns/mock_host_resolver.h"
49 #include "net/url_request/url_request_job_factory_impl.h" 50 #include "net/url_request/url_request_job_factory_impl.h"
50 #include "net/url_request/url_request_test_util.h" 51 #include "net/url_request/url_request_test_util.h"
51 #include "testing/gtest/include/gtest/gtest-message.h" 52 #include "testing/gtest/include/gtest/gtest-message.h"
52 #include "testing/gtest/include/gtest/gtest.h" 53 #include "testing/gtest/include/gtest/gtest.h"
53 54
54 namespace helpers = extension_web_request_api_helpers; 55 namespace helpers = extension_web_request_api_helpers;
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 234
234 net::URLRequestJobFactoryImpl job_factory; 235 net::URLRequestJobFactoryImpl job_factory;
235 job_factory.SetProtocolHandler( 236 job_factory.SetProtocolHandler(
236 chrome::kAboutScheme, 237 chrome::kAboutScheme,
237 new chrome_browser_net::AboutProtocolHandler()); 238 new chrome_browser_net::AboutProtocolHandler());
238 context_->set_job_factory(&job_factory); 239 context_->set_job_factory(&job_factory);
239 240
240 GURL redirect_url("about:redirected"); 241 GURL redirect_url("about:redirected");
241 GURL not_chosen_redirect_url("about:not_chosen"); 242 GURL not_chosen_redirect_url("about:not_chosen");
242 243
243 net::URLRequest request(GURL("about:blank"), &delegate_, context_.get()); 244 net::URLRequest request(GURL("about:blank"),
245 net::DEFAULT_PRIORITY,
246 &delegate_,
247 context_.get(),
248 network_delegate_.get());
244 { 249 {
245 // onBeforeRequest will be dispatched twice initially. The second response - 250 // onBeforeRequest will be dispatched twice initially. The second response -
246 // the redirect - should win, since it has a later |install_time|. The 251 // the redirect - should win, since it has a later |install_time|. The
247 // redirect will dispatch another pair of onBeforeRequest. There, the first 252 // redirect will dispatch another pair of onBeforeRequest. There, the first
248 // response should win (later |install_time|). 253 // response should win (later |install_time|).
249 ExtensionWebRequestEventRouter::EventResponse* response = NULL; 254 ExtensionWebRequestEventRouter::EventResponse* response = NULL;
250 255
251 // Extension1 response. Arrives first, but ignored due to install_time. 256 // Extension1 response. Arrives first, but ignored due to install_time.
252 response = new ExtensionWebRequestEventRouter::EventResponse( 257 response = new ExtensionWebRequestEventRouter::EventResponse(
253 extension1_id, base::Time::FromDoubleT(1)); 258 extension1_id, base::Time::FromDoubleT(1));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 292
288 EXPECT_TRUE(!request.is_pending()); 293 EXPECT_TRUE(!request.is_pending());
289 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); 294 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
290 EXPECT_EQ(0, request.status().error()); 295 EXPECT_EQ(0, request.status().error());
291 EXPECT_EQ(redirect_url, request.url()); 296 EXPECT_EQ(redirect_url, request.url());
292 EXPECT_EQ(2U, request.url_chain().size()); 297 EXPECT_EQ(2U, request.url_chain().size());
293 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); 298 EXPECT_EQ(0U, ipc_sender_.GetNumTasks());
294 } 299 }
295 300
296 // Now test the same thing but the extensions answer in reverse order. 301 // Now test the same thing but the extensions answer in reverse order.
297 net::URLRequest request2(GURL("about:blank"), &delegate_, context_.get()); 302 net::URLRequest request2(GURL("about:blank"),
303 net::DEFAULT_PRIORITY,
304 &delegate_,
305 context_.get(),
306 network_delegate_.get());
298 { 307 {
299 ExtensionWebRequestEventRouter::EventResponse* response = NULL; 308 ExtensionWebRequestEventRouter::EventResponse* response = NULL;
300 309
301 // Extension2 response. Arrives first, and chosen because of install_time. 310 // Extension2 response. Arrives first, and chosen because of install_time.
302 response = new ExtensionWebRequestEventRouter::EventResponse( 311 response = new ExtensionWebRequestEventRouter::EventResponse(
303 extension2_id, base::Time::FromDoubleT(2)); 312 extension2_id, base::Time::FromDoubleT(2));
304 response->new_url = redirect_url; 313 response->new_url = redirect_url;
305 ipc_sender_.PushTask( 314 ipc_sender_.PushTask(
306 base::Bind(&EventHandledOnIOThread, 315 base::Bind(&EventHandledOnIOThread,
307 &profile_, extension2_id, kEventName, kEventName + "/2", 316 &profile_, extension2_id, kEventName, kEventName + "/2",
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 369 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
361 &profile_, extension1_id, extension1_id, kEventName, kEventName + "/1", 370 &profile_, extension1_id, extension1_id, kEventName, kEventName + "/1",
362 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, 371 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1,
363 ipc_sender_factory.GetWeakPtr()); 372 ipc_sender_factory.GetWeakPtr());
364 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 373 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
365 &profile_, extension2_id, extension2_id, kEventName, kEventName + "/2", 374 &profile_, extension2_id, extension2_id, kEventName, kEventName + "/2",
366 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, 375 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1,
367 ipc_sender_factory.GetWeakPtr()); 376 ipc_sender_factory.GetWeakPtr());
368 377
369 GURL request_url("about:blank"); 378 GURL request_url("about:blank");
370 net::URLRequest request(request_url, &delegate_, context_.get()); 379 net::URLRequest request(request_url,
380 net::DEFAULT_PRIORITY,
381 &delegate_,
382 context_.get(),
383 network_delegate_.get());
371 384
372 // onBeforeRequest will be dispatched twice. The second response - 385 // onBeforeRequest will be dispatched twice. The second response -
373 // the redirect - would win, since it has a later |install_time|, but 386 // the redirect - would win, since it has a later |install_time|, but
374 // the first response takes precedence because cancel >> redirect. 387 // the first response takes precedence because cancel >> redirect.
375 GURL redirect_url("about:redirected"); 388 GURL redirect_url("about:redirected");
376 ExtensionWebRequestEventRouter::EventResponse* response = NULL; 389 ExtensionWebRequestEventRouter::EventResponse* response = NULL;
377 390
378 // Extension1 response. Arrives first, would be ignored in principle due to 391 // Extension1 response. Arrives first, would be ignored in principle due to
379 // install_time but "cancel" always wins. 392 // install_time but "cancel" always wins.
380 response = new ExtensionWebRequestEventRouter::EventResponse( 393 response = new ExtensionWebRequestEventRouter::EventResponse(
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); 440 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_);
428 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 441 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
429 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", 442 &profile_, extension_id, extension_id, kEventName, kEventName + "/1",
430 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1, 443 filter, ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, -1, -1,
431 ipc_sender_factory.GetWeakPtr()); 444 ipc_sender_factory.GetWeakPtr());
432 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 445 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
433 &profile_, extension_id, extension_id, kEventName2, kEventName2 + "/1", 446 &profile_, extension_id, extension_id, kEventName2, kEventName2 + "/1",
434 filter, 0, -1, -1, ipc_sender_factory.GetWeakPtr()); 447 filter, 0, -1, -1, ipc_sender_factory.GetWeakPtr());
435 448
436 GURL request_url("about:blank"); 449 GURL request_url("about:blank");
437 net::URLRequest request(request_url, &delegate_, context_.get()); 450 net::URLRequest request(request_url,
451 net::DEFAULT_PRIORITY,
452 &delegate_,
453 context_.get(),
454 network_delegate_.get());
438 455
439 ExtensionWebRequestEventRouter::EventResponse* response = NULL; 456 ExtensionWebRequestEventRouter::EventResponse* response = NULL;
440 457
441 // Extension response for the OnBeforeRequest handler. This should not be 458 // Extension response for the OnBeforeRequest handler. This should not be
442 // processed because request is canceled before the handler responds. 459 // processed because request is canceled before the handler responds.
443 response = new ExtensionWebRequestEventRouter::EventResponse( 460 response = new ExtensionWebRequestEventRouter::EventResponse(
444 extension_id, base::Time::FromDoubleT(1)); 461 extension_id, base::Time::FromDoubleT(1));
445 GURL redirect_url("about:redirected"); 462 GURL redirect_url("about:redirected");
446 response->new_url = redirect_url; 463 response->new_url = redirect_url;
447 ipc_sender_.PushTask( 464 ipc_sender_.PushTask(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 507
491 } // namespace 508 } // namespace
492 509
493 void ExtensionWebRequestTest::FireURLRequestWithData( 510 void ExtensionWebRequestTest::FireURLRequestWithData(
494 const std::string& method, 511 const std::string& method,
495 const char* content_type, 512 const char* content_type,
496 const std::vector<char>& bytes_1, 513 const std::vector<char>& bytes_1,
497 const std::vector<char>& bytes_2) { 514 const std::vector<char>& bytes_2) {
498 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme. 515 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme.
499 GURL request_url("http://www.example.com"); 516 GURL request_url("http://www.example.com");
500 net::URLRequest request(request_url, &delegate_, context_.get()); 517 net::URLRequest request(request_url,
518 net::DEFAULT_PRIORITY,
519 &delegate_,
520 context_.get(),
521 network_delegate_.get());
501 request.set_method(method); 522 request.set_method(method);
502 if (content_type != NULL) 523 if (content_type != NULL)
503 request.SetExtraRequestHeaderByName(net::HttpRequestHeaders::kContentType, 524 request.SetExtraRequestHeaderByName(net::HttpRequestHeaders::kContentType,
504 content_type, 525 content_type,
505 true /* overwrite */); 526 true /* overwrite */);
506 ScopedVector<net::UploadElementReader> element_readers; 527 ScopedVector<net::UploadElementReader> element_readers;
507 element_readers.push_back(new net::UploadBytesElementReader( 528 element_readers.push_back(new net::UploadBytesElementReader(
508 &(bytes_1[0]), bytes_1.size())); 529 &(bytes_1[0]), bytes_1.size()));
509 element_readers.push_back( 530 element_readers.push_back(
510 new net::UploadFileElementReader(base::MessageLoopProxy::current().get(), 531 new net::UploadFileElementReader(base::MessageLoopProxy::current().get(),
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 724
704 // Subscribe to OnBeforeRequest with requestBody requirement. 725 // Subscribe to OnBeforeRequest with requestBody requirement.
705 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 726 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
706 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", 727 &profile_, extension_id, extension_id, kEventName, kEventName + "/1",
707 filter, extra_info_spec, -1, -1, ipc_sender_factory.GetWeakPtr()); 728 filter, extra_info_spec, -1, -1, ipc_sender_factory.GetWeakPtr());
708 729
709 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme. 730 // The request URL can be arbitrary but must have an HTTP or HTTPS scheme.
710 const GURL request_url("http://www.example.com"); 731 const GURL request_url("http://www.example.com");
711 732
712 for (size_t i = 0; i < arraysize(kMethods); ++i) { 733 for (size_t i = 0; i < arraysize(kMethods); ++i) {
713 net::URLRequest request(request_url, &delegate_, context_.get()); 734 net::URLRequest request(request_url,
735 net::DEFAULT_PRIORITY,
736 &delegate_,
737 context_.get(),
738 network_delegate_.get());
714 request.set_method(kMethods[i]); 739 request.set_method(kMethods[i]);
715 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); 740 ipc_sender_.PushTask(base::Bind(&base::DoNothing));
716 request.Start(); 741 request.Start();
717 } 742 }
718 743
719 // We inspect the result in the message list of |ipc_sender_| later. 744 // We inspect the result in the message list of |ipc_sender_| later.
720 base::MessageLoop::current()->RunUntilIdle(); 745 base::MessageLoop::current()->RunUntilIdle();
721 746
722 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 747 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(
723 &profile_, extension_id, kEventName + "/1"); 748 &profile_, extension_id, kEventName + "/1");
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 ipc_sender_factory.GetWeakPtr()); 847 ipc_sender_factory.GetWeakPtr());
823 848
824 // Install one extension that observes the final headers. 849 // Install one extension that observes the final headers.
825 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 850 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
826 &profile_, extension3_id, extension3_id, keys::kOnSendHeadersEvent, 851 &profile_, extension3_id, extension3_id, keys::kOnSendHeadersEvent,
827 std::string(keys::kOnSendHeadersEvent) + "/3", filter, 852 std::string(keys::kOnSendHeadersEvent) + "/3", filter,
828 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_HEADERS, -1, -1, 853 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_HEADERS, -1, -1,
829 ipc_sender_factory.GetWeakPtr()); 854 ipc_sender_factory.GetWeakPtr());
830 855
831 GURL request_url("http://doesnotexist/does_not_exist.html"); 856 GURL request_url("http://doesnotexist/does_not_exist.html");
832 net::URLRequest request(request_url, &delegate_, context_.get()); 857 net::URLRequest request(request_url,
858 net::DEFAULT_PRIORITY,
859 &delegate_,
860 context_.get(),
861 network_delegate_.get());
833 862
834 // Initialize headers available before extensions are notified of the 863 // Initialize headers available before extensions are notified of the
835 // onBeforeSendHeaders event. 864 // onBeforeSendHeaders event.
836 HeaderModificationTest test = GetParam(); 865 HeaderModificationTest test = GetParam();
837 net::HttpRequestHeaders before_headers; 866 net::HttpRequestHeaders before_headers;
838 for (int i = 0; i < test.before_size; ++i) 867 for (int i = 0; i < test.before_size; ++i)
839 before_headers.SetHeader(test.before[i].name, test.before[i].value); 868 before_headers.SetHeader(test.before[i].name, test.before[i].value);
840 request.SetExtraRequestHeaders(before_headers); 869 request.SetExtraRequestHeaders(before_headers);
841 870
842 // Gather the modifications to the headers for the respective extensions. 871 // Gather the modifications to the headers for the respective extensions.
(...skipping 1287 matching lines...) Expand 10 before | Expand all | Expand 10 after
2130 EXPECT_TRUE(credentials_set); 2159 EXPECT_TRUE(credentials_set);
2131 EXPECT_FALSE(auth3.Empty()); 2160 EXPECT_FALSE(auth3.Empty());
2132 EXPECT_EQ(username, auth1.username()); 2161 EXPECT_EQ(username, auth1.username());
2133 EXPECT_EQ(password, auth1.password()); 2162 EXPECT_EQ(password, auth1.password());
2134 EXPECT_EQ(1u, warning_set.size()); 2163 EXPECT_EQ(1u, warning_set.size());
2135 EXPECT_TRUE(HasWarning(warning_set, "extid2")); 2164 EXPECT_TRUE(HasWarning(warning_set, "extid2"));
2136 EXPECT_EQ(3u, capturing_net_log.GetSize()); 2165 EXPECT_EQ(3u, capturing_net_log.GetSize());
2137 } 2166 }
2138 2167
2139 } // namespace extensions 2168 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698