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

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc

Issue 11369179: Move url_request_test_util into net namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: patch for landing Created 8 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 "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h" 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 std::string error; 77 std::string error;
78 ListValue resource_types; 78 ListValue resource_types;
79 resource_types.Append(Value::CreateStringValue("main_frame")); 79 resource_types.Append(Value::CreateStringValue("main_frame"));
80 80
81 scoped_ptr<WebRequestConditionAttribute> attribute = 81 scoped_ptr<WebRequestConditionAttribute> attribute =
82 WebRequestConditionAttribute::Create( 82 WebRequestConditionAttribute::Create(
83 keys::kResourceTypeKey, &resource_types, &error); 83 keys::kResourceTypeKey, &resource_types, &error);
84 EXPECT_EQ("", error); 84 EXPECT_EQ("", error);
85 ASSERT_TRUE(attribute.get()); 85 ASSERT_TRUE(attribute.get());
86 86
87 TestURLRequestContext context; 87 net::TestURLRequestContext context;
88 TestURLRequest url_request_ok(GURL("http://www.example.com"), NULL, &context); 88 net::TestURLRequest url_request_ok(
89 GURL("http://www.example.com"), NULL, &context);
89 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, 90 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok,
90 ResourceType::MAIN_FRAME, NULL, -1, -1); 91 ResourceType::MAIN_FRAME, NULL, -1, -1);
91 EXPECT_TRUE(attribute->IsFulfilled( 92 EXPECT_TRUE(attribute->IsFulfilled(
92 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST))); 93 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST)));
93 94
94 TestURLRequest url_request_fail( 95 net::TestURLRequest url_request_fail(
95 GURL("http://www.example.com"), NULL, &context); 96 GURL("http://www.example.com"), NULL, &context);
96 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, 97 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok,
97 ResourceType::SUB_FRAME, NULL, -1, -1); 98 ResourceType::SUB_FRAME, NULL, -1, -1);
98 EXPECT_FALSE(attribute->IsFulfilled( 99 EXPECT_FALSE(attribute->IsFulfilled(
99 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST))); 100 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST)));
100 } 101 }
101 102
102 TEST(WebRequestConditionAttributeTest, ContentType) { 103 TEST(WebRequestConditionAttributeTest, ContentType) {
103 // Necessary for TestURLRequest. 104 // Necessary for TestURLRequest.
104 MessageLoop message_loop(MessageLoop::TYPE_IO); 105 MessageLoop message_loop(MessageLoop::TYPE_IO);
105 106
106 std::string error; 107 std::string error;
107 scoped_ptr<WebRequestConditionAttribute> result; 108 scoped_ptr<WebRequestConditionAttribute> result;
108 109
109 net::TestServer test_server( 110 net::TestServer test_server(
110 net::TestServer::TYPE_HTTP, 111 net::TestServer::TYPE_HTTP,
111 net::TestServer::kLocalhost, 112 net::TestServer::kLocalhost,
112 FilePath(FILE_PATH_LITERAL( 113 FilePath(FILE_PATH_LITERAL(
113 "chrome/test/data/extensions/api_test/webrequest/declarative"))); 114 "chrome/test/data/extensions/api_test/webrequest/declarative")));
114 ASSERT_TRUE(test_server.Start()); 115 ASSERT_TRUE(test_server.Start());
115 116
116 TestURLRequestContext context; 117 net::TestURLRequestContext context;
117 TestDelegate delegate; 118 net::TestDelegate delegate;
118 TestURLRequest url_request(test_server.GetURL("files/headers.html"), 119 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"),
119 &delegate, &context); 120 &delegate, &context);
120 url_request.Start(); 121 url_request.Start();
121 MessageLoop::current()->Run(); 122 MessageLoop::current()->Run();
122 123
123 ListValue content_types; 124 ListValue content_types;
124 content_types.Append(Value::CreateStringValue("text/plain")); 125 content_types.Append(Value::CreateStringValue("text/plain"));
125 scoped_ptr<WebRequestConditionAttribute> attribute_include = 126 scoped_ptr<WebRequestConditionAttribute> attribute_include =
126 WebRequestConditionAttribute::Create( 127 WebRequestConditionAttribute::Create(
127 keys::kContentTypeKey, &content_types, &error); 128 keys::kContentTypeKey, &content_types, &error);
128 EXPECT_EQ("", error); 129 EXPECT_EQ("", error);
129 ASSERT_TRUE(attribute_include.get()); 130 ASSERT_TRUE(attribute_include.get());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 scoped_ptr<WebRequestConditionAttribute> first_party_attribute = 184 scoped_ptr<WebRequestConditionAttribute> first_party_attribute =
184 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, 185 WebRequestConditionAttribute::Create(keys::kThirdPartyKey,
185 &value_false, 186 &value_false,
186 &error); 187 &error);
187 ASSERT_EQ("", error); 188 ASSERT_EQ("", error);
188 ASSERT_TRUE(first_party_attribute.get()); 189 ASSERT_TRUE(first_party_attribute.get());
189 190
190 const GURL url_empty; 191 const GURL url_empty;
191 const GURL url_a("http://a.com"); 192 const GURL url_a("http://a.com");
192 const GURL url_b("http://b.com"); 193 const GURL url_b("http://b.com");
193 TestURLRequestContext context; 194 net::TestURLRequestContext context;
194 TestDelegate delegate; 195 net::TestDelegate delegate;
195 TestURLRequest url_request(url_a, &delegate, &context); 196 net::TestURLRequest url_request(url_a, &delegate, &context);
196 197
197 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { 198 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) {
198 if (!(kActiveStages & i)) 199 if (!(kActiveStages & i))
199 continue; 200 continue;
200 const RequestStage stage = static_cast<RequestStage>(i); 201 const RequestStage stage = static_cast<RequestStage>(i);
201 url_request.set_first_party_for_cookies(url_empty); 202 url_request.set_first_party_for_cookies(url_empty);
202 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( 203 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData(
203 &url_request, stage))); 204 &url_request, stage)));
204 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( 205 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData(
205 &url_request, stage))); 206 &url_request, stage)));
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 single_stage_list.AppendString(active_stages[i].second); 271 single_stage_list.AppendString(active_stages[i].second);
271 one_stage_attributes.push_back( 272 one_stage_attributes.push_back(
272 WebRequestConditionAttribute::Create(keys::kStagesKey, 273 WebRequestConditionAttribute::Create(keys::kStagesKey,
273 &single_stage_list, 274 &single_stage_list,
274 &error).release()); 275 &error).release());
275 EXPECT_EQ("", error); 276 EXPECT_EQ("", error);
276 ASSERT_TRUE(one_stage_attributes.back() != NULL); 277 ASSERT_TRUE(one_stage_attributes.back() != NULL);
277 } 278 }
278 279
279 const GURL url_empty; 280 const GURL url_empty;
280 TestURLRequestContext context; 281 net::TestURLRequestContext context;
281 TestDelegate delegate; 282 net::TestDelegate delegate;
282 TestURLRequest url_request(url_empty, &delegate, &context); 283 net::TestURLRequest url_request(url_empty, &delegate, &context);
283 284
284 for (size_t i = 0; i < arraysize(active_stages); ++i) { 285 for (size_t i = 0; i < arraysize(active_stages); ++i) {
285 EXPECT_FALSE(empty_attribute->IsFulfilled(WebRequestRule::RequestData( 286 EXPECT_FALSE(empty_attribute->IsFulfilled(WebRequestRule::RequestData(
286 &url_request, active_stages[i].first))); 287 &url_request, active_stages[i].first)));
287 288
288 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { 289 for (size_t j = 0; j < one_stage_attributes.size(); ++j) {
289 EXPECT_EQ( 290 EXPECT_EQ(
290 i == j, 291 i == j,
291 one_stage_attributes[j]->IsFulfilled(WebRequestRule::RequestData( 292 one_stage_attributes[j]->IsFulfilled(WebRequestRule::RequestData(
292 &url_request, active_stages[i].first))); 293 &url_request, active_stages[i].first)));
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 } // namespace 393 } // namespace
393 394
394 // Here we test WebRequestConditionAttributeRequestHeaders for matching 395 // Here we test WebRequestConditionAttributeRequestHeaders for matching
395 // correctly against request headers. This test is not as extensive as 396 // correctly against request headers. This test is not as extensive as
396 // "ResponseHeaders" (below), because the header-matching code is shared 397 // "ResponseHeaders" (below), because the header-matching code is shared
397 // by both types of condition attributes, so it is enough to test it once. 398 // by both types of condition attributes, so it is enough to test it once.
398 TEST(WebRequestConditionAttributeTest, RequestHeaders) { 399 TEST(WebRequestConditionAttributeTest, RequestHeaders) {
399 // Necessary for TestURLRequest. 400 // Necessary for TestURLRequest.
400 MessageLoop message_loop(MessageLoop::TYPE_IO); 401 MessageLoop message_loop(MessageLoop::TYPE_IO);
401 402
402 TestURLRequestContext context; 403 net::TestURLRequestContext context;
403 TestDelegate delegate; 404 net::TestDelegate delegate;
404 TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. 405 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL.
405 &delegate, &context); 406 &delegate, &context);
406 url_request.SetExtraRequestHeaderByName( 407 url_request.SetExtraRequestHeaderByName(
407 "Custom-header", "custom/value", true /* overwrite */); 408 "Custom-header", "custom/value", true /* overwrite */);
408 url_request.Start(); 409 url_request.Start();
409 MessageLoop::current()->Run(); 410 MessageLoop::current()->Run();
410 411
411 std::vector<std::vector<const std::string*> > tests; 412 std::vector<std::vector<const std::string*> > tests;
412 bool result = false; 413 bool result = false;
413 414
414 const RequestStage stage = ON_BEFORE_SEND_HEADERS; 415 const RequestStage stage = ON_BEFORE_SEND_HEADERS;
415 416
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 // Necessary for TestURLRequest. 478 // Necessary for TestURLRequest.
478 MessageLoop message_loop(MessageLoop::TYPE_IO); 479 MessageLoop message_loop(MessageLoop::TYPE_IO);
479 480
480 net::TestServer test_server( 481 net::TestServer test_server(
481 net::TestServer::TYPE_HTTP, 482 net::TestServer::TYPE_HTTP,
482 net::TestServer::kLocalhost, 483 net::TestServer::kLocalhost,
483 FilePath(FILE_PATH_LITERAL( 484 FilePath(FILE_PATH_LITERAL(
484 "chrome/test/data/extensions/api_test/webrequest/declarative"))); 485 "chrome/test/data/extensions/api_test/webrequest/declarative")));
485 ASSERT_TRUE(test_server.Start()); 486 ASSERT_TRUE(test_server.Start());
486 487
487 TestURLRequestContext context; 488 net::TestURLRequestContext context;
488 TestDelegate delegate; 489 net::TestDelegate delegate;
489 TestURLRequest url_request(test_server.GetURL("files/headers.html"), 490 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"),
490 &delegate, &context); 491 &delegate, &context);
491 url_request.Start(); 492 url_request.Start();
492 MessageLoop::current()->Run(); 493 MessageLoop::current()->Run();
493 494
494 // In all the tests below we assume that the server includes the headers 495 // In all the tests below we assume that the server includes the headers
495 // Custom-Header: custom/value 496 // Custom-Header: custom/value
496 // Custom-Header-B: valueA 497 // Custom-Header-B: valueA
497 // Custom-Header-B: valueB 498 // Custom-Header-B: valueB
498 // Custom-Header-C: valueC, valueD 499 // Custom-Header-C: valueC, valueD
499 // Custom-Header-D: 500 // Custom-Header-D:
500 // in the response, but does not include "Non-existing: void". 501 // in the response, but does not include "Non-existing: void".
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 keys::kValueEqualsKey, "valueB" 650 keys::kValueEqualsKey, "valueB"
650 }; 651 };
651 const size_t kExistingSize[] = { arraysize(kExisting) }; 652 const size_t kExistingSize[] = { arraysize(kExisting) };
652 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); 653 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests);
653 MatchAndCheck( 654 MatchAndCheck(
654 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); 655 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result);
655 EXPECT_FALSE(result); 656 EXPECT_FALSE(result);
656 } 657 }
657 658
658 } // namespace extensions 659 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698