OLD | NEW |
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/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
| 14 #include "net/base/request_priority.h" |
14 #include "net/test/spawned_test_server/spawned_test_server.h" | 15 #include "net/test/spawned_test_server/spawned_test_server.h" |
15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 using base::DictionaryValue; | 19 using base::DictionaryValue; |
19 using base::FundamentalValue; | 20 using base::FundamentalValue; |
20 using base::ListValue; | 21 using base::ListValue; |
21 using base::StringValue; | 22 using base::StringValue; |
22 using base::Value; | 23 using base::Value; |
23 | 24 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 resource_types.Append(new base::StringValue("sub_frame")); | 82 resource_types.Append(new base::StringValue("sub_frame")); |
82 | 83 |
83 scoped_refptr<const WebRequestConditionAttribute> attribute = | 84 scoped_refptr<const WebRequestConditionAttribute> attribute = |
84 WebRequestConditionAttribute::Create( | 85 WebRequestConditionAttribute::Create( |
85 keys::kResourceTypeKey, &resource_types, &error); | 86 keys::kResourceTypeKey, &resource_types, &error); |
86 EXPECT_EQ("", error); | 87 EXPECT_EQ("", error); |
87 ASSERT_TRUE(attribute.get()); | 88 ASSERT_TRUE(attribute.get()); |
88 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); | 89 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); |
89 | 90 |
90 net::TestURLRequestContext context; | 91 net::TestURLRequestContext context; |
91 net::TestURLRequest url_request_ok( | 92 net::TestURLRequest url_request_ok(GURL("http://www.example.com"), |
92 GURL("http://www.example.com"), NULL, &context, NULL); | 93 net::DEFAULT_PRIORITY, |
| 94 NULL, |
| 95 &context, |
| 96 NULL); |
93 content::ResourceRequestInfo::AllocateForTesting( | 97 content::ResourceRequestInfo::AllocateForTesting( |
94 &url_request_ok, ResourceType::SUB_FRAME, NULL, -1, -1, false); | 98 &url_request_ok, ResourceType::SUB_FRAME, NULL, -1, -1, false); |
95 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(&url_request_ok, | 99 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(&url_request_ok, |
96 ON_BEFORE_REQUEST))); | 100 ON_BEFORE_REQUEST))); |
97 | 101 |
98 net::TestURLRequest url_request_fail( | 102 net::TestURLRequest url_request_fail(GURL("http://www.example.com"), |
99 GURL("http://www.example.com"), NULL, &context, NULL); | 103 net::DEFAULT_PRIORITY, |
| 104 NULL, |
| 105 &context, |
| 106 NULL); |
100 content::ResourceRequestInfo::AllocateForTesting( | 107 content::ResourceRequestInfo::AllocateForTesting( |
101 &url_request_fail, ResourceType::MAIN_FRAME, NULL, -1, -1, false); | 108 &url_request_fail, ResourceType::MAIN_FRAME, NULL, -1, -1, false); |
102 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, | 109 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, |
103 ON_BEFORE_REQUEST))); | 110 ON_BEFORE_REQUEST))); |
104 } | 111 } |
105 | 112 |
106 TEST(WebRequestConditionAttributeTest, ContentType) { | 113 TEST(WebRequestConditionAttributeTest, ContentType) { |
107 // Necessary for TestURLRequest. | 114 // Necessary for TestURLRequest. |
108 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 115 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
109 | 116 |
110 std::string error; | 117 std::string error; |
111 scoped_refptr<const WebRequestConditionAttribute> result; | 118 scoped_refptr<const WebRequestConditionAttribute> result; |
112 | 119 |
113 net::SpawnedTestServer test_server( | 120 net::SpawnedTestServer test_server( |
114 net::SpawnedTestServer::TYPE_HTTP, | 121 net::SpawnedTestServer::TYPE_HTTP, |
115 net::SpawnedTestServer::kLocalhost, | 122 net::SpawnedTestServer::kLocalhost, |
116 base::FilePath(FILE_PATH_LITERAL( | 123 base::FilePath(FILE_PATH_LITERAL( |
117 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 124 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
118 ASSERT_TRUE(test_server.Start()); | 125 ASSERT_TRUE(test_server.Start()); |
119 | 126 |
120 net::TestURLRequestContext context; | 127 net::TestURLRequestContext context; |
121 net::TestDelegate delegate; | 128 net::TestDelegate delegate; |
122 net::TestURLRequest url_request( | 129 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
123 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); | 130 net::DEFAULT_PRIORITY, |
| 131 &delegate, |
| 132 &context, |
| 133 NULL); |
124 url_request.Start(); | 134 url_request.Start(); |
125 base::MessageLoop::current()->Run(); | 135 base::MessageLoop::current()->Run(); |
126 | 136 |
127 ListValue content_types; | 137 ListValue content_types; |
128 content_types.Append(new base::StringValue("text/plain")); | 138 content_types.Append(new base::StringValue("text/plain")); |
129 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 139 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
130 WebRequestConditionAttribute::Create( | 140 WebRequestConditionAttribute::Create( |
131 keys::kContentTypeKey, &content_types, &error); | 141 keys::kContentTypeKey, &content_types, &error); |
132 EXPECT_EQ("", error); | 142 EXPECT_EQ("", error); |
133 ASSERT_TRUE(attribute_include.get()); | 143 ASSERT_TRUE(attribute_include.get()); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 ASSERT_EQ("", error); | 206 ASSERT_EQ("", error); |
197 ASSERT_TRUE(first_party_attribute.get()); | 207 ASSERT_TRUE(first_party_attribute.get()); |
198 EXPECT_EQ(std::string(keys::kThirdPartyKey), | 208 EXPECT_EQ(std::string(keys::kThirdPartyKey), |
199 first_party_attribute->GetName()); | 209 first_party_attribute->GetName()); |
200 | 210 |
201 const GURL url_empty; | 211 const GURL url_empty; |
202 const GURL url_a("http://a.com"); | 212 const GURL url_a("http://a.com"); |
203 const GURL url_b("http://b.com"); | 213 const GURL url_b("http://b.com"); |
204 net::TestURLRequestContext context; | 214 net::TestURLRequestContext context; |
205 net::TestDelegate delegate; | 215 net::TestDelegate delegate; |
206 net::TestURLRequest url_request(url_a, &delegate, &context, NULL); | 216 net::TestURLRequest url_request( |
| 217 url_a, net::DEFAULT_PRIORITY, &delegate, &context, NULL); |
207 | 218 |
208 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { | 219 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { |
209 if (!(kActiveStages & i)) | 220 if (!(kActiveStages & i)) |
210 continue; | 221 continue; |
211 const RequestStage stage = static_cast<RequestStage>(i); | 222 const RequestStage stage = static_cast<RequestStage>(i); |
212 url_request.set_first_party_for_cookies(url_empty); | 223 url_request.set_first_party_for_cookies(url_empty); |
213 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestData(&url_request, | 224 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestData(&url_request, |
214 stage))); | 225 stage))); |
215 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestData(&url_request, | 226 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestData(&url_request, |
216 stage))); | 227 stage))); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 WebRequestConditionAttribute::Create(keys::kStagesKey, | 297 WebRequestConditionAttribute::Create(keys::kStagesKey, |
287 &single_stage_list, | 298 &single_stage_list, |
288 &error)); | 299 &error)); |
289 EXPECT_EQ("", error); | 300 EXPECT_EQ("", error); |
290 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); | 301 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); |
291 } | 302 } |
292 | 303 |
293 const GURL url_empty; | 304 const GURL url_empty; |
294 net::TestURLRequestContext context; | 305 net::TestURLRequestContext context; |
295 net::TestDelegate delegate; | 306 net::TestDelegate delegate; |
296 net::TestURLRequest url_request(url_empty, &delegate, &context, NULL); | 307 net::TestURLRequest url_request( |
| 308 url_empty, net::DEFAULT_PRIORITY, &delegate, &context, NULL); |
297 | 309 |
298 for (size_t i = 0; i < arraysize(active_stages); ++i) { | 310 for (size_t i = 0; i < arraysize(active_stages); ++i) { |
299 EXPECT_FALSE(empty_attribute->IsFulfilled( | 311 EXPECT_FALSE(empty_attribute->IsFulfilled( |
300 WebRequestData(&url_request, active_stages[i].first))); | 312 WebRequestData(&url_request, active_stages[i].first))); |
301 | 313 |
302 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { | 314 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { |
303 EXPECT_EQ(i == j, | 315 EXPECT_EQ(i == j, |
304 one_stage_attributes[j]->IsFulfilled( | 316 one_stage_attributes[j]->IsFulfilled( |
305 WebRequestData(&url_request, active_stages[i].first))); | 317 WebRequestData(&url_request, active_stages[i].first))); |
306 } | 318 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 // correctly against request headers. This test is not as extensive as | 422 // correctly against request headers. This test is not as extensive as |
411 // "ResponseHeaders" (below), because the header-matching code is shared | 423 // "ResponseHeaders" (below), because the header-matching code is shared |
412 // by both types of condition attributes, so it is enough to test it once. | 424 // by both types of condition attributes, so it is enough to test it once. |
413 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 425 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
414 // Necessary for TestURLRequest. | 426 // Necessary for TestURLRequest. |
415 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 427 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
416 | 428 |
417 net::TestURLRequestContext context; | 429 net::TestURLRequestContext context; |
418 net::TestDelegate delegate; | 430 net::TestDelegate delegate; |
419 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. | 431 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. |
420 &delegate, &context, NULL); | 432 net::DEFAULT_PRIORITY, |
| 433 &delegate, |
| 434 &context, |
| 435 NULL); |
421 url_request.SetExtraRequestHeaderByName( | 436 url_request.SetExtraRequestHeaderByName( |
422 "Custom-header", "custom/value", true /* overwrite */); | 437 "Custom-header", "custom/value", true /* overwrite */); |
423 url_request.Start(); | 438 url_request.Start(); |
424 base::MessageLoop::current()->Run(); | 439 base::MessageLoop::current()->Run(); |
425 | 440 |
426 std::vector<std::vector<const std::string*> > tests; | 441 std::vector<std::vector<const std::string*> > tests; |
427 bool result = false; | 442 bool result = false; |
428 | 443 |
429 const RequestStage stage = ON_BEFORE_SEND_HEADERS; | 444 const RequestStage stage = ON_BEFORE_SEND_HEADERS; |
430 | 445 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 net::SpawnedTestServer test_server( | 510 net::SpawnedTestServer test_server( |
496 net::SpawnedTestServer::TYPE_HTTP, | 511 net::SpawnedTestServer::TYPE_HTTP, |
497 net::SpawnedTestServer::kLocalhost, | 512 net::SpawnedTestServer::kLocalhost, |
498 base::FilePath(FILE_PATH_LITERAL( | 513 base::FilePath(FILE_PATH_LITERAL( |
499 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 514 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
500 ASSERT_TRUE(test_server.Start()); | 515 ASSERT_TRUE(test_server.Start()); |
501 | 516 |
502 net::TestURLRequestContext context; | 517 net::TestURLRequestContext context; |
503 net::TestDelegate delegate; | 518 net::TestDelegate delegate; |
504 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 519 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
505 &delegate, &context, NULL); | 520 net::DEFAULT_PRIORITY, |
| 521 &delegate, |
| 522 &context, |
| 523 NULL); |
506 url_request.Start(); | 524 url_request.Start(); |
507 base::MessageLoop::current()->Run(); | 525 base::MessageLoop::current()->Run(); |
508 | 526 |
509 // In all the tests below we assume that the server includes the headers | 527 // In all the tests below we assume that the server includes the headers |
510 // Custom-Header: custom/value | 528 // Custom-Header: custom/value |
511 // Custom-Header-B: valueA | 529 // Custom-Header-B: valueA |
512 // Custom-Header-B: valueB | 530 // Custom-Header-B: valueB |
513 // Custom-Header-C: valueC, valueD | 531 // Custom-Header-C: valueC, valueD |
514 // Custom-Header-D: | 532 // Custom-Header-D: |
515 // in the response, but does not include "Non-existing: void". | 533 // in the response, but does not include "Non-existing: void". |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 keys::kValueEqualsKey, "valueB" | 682 keys::kValueEqualsKey, "valueB" |
665 }; | 683 }; |
666 const size_t kExistingSize[] = { arraysize(kExisting) }; | 684 const size_t kExistingSize[] = { arraysize(kExisting) }; |
667 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 685 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
668 MatchAndCheck( | 686 MatchAndCheck( |
669 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 687 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
670 EXPECT_FALSE(result); | 688 EXPECT_FALSE(result); |
671 } | 689 } |
672 | 690 |
673 } // namespace extensions | 691 } // namespace extensions |
OLD | NEW |