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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 namespace { | 25 namespace { |
26 const char kUnknownConditionName[] = "unknownType"; | 26 const char kUnknownConditionName[] = "unknownType"; |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 namespace extensions { | 29 namespace extensions { |
30 | 30 |
31 namespace keys = declarative_webrequest_constants; | 31 namespace keys = declarative_webrequest_constants; |
32 | 32 |
33 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { | 33 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { |
34 // Necessary for TestURLRequest. | 34 // Necessary for TestURLRequest. |
35 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 35 base::MessageLoopForIO message_loop; |
36 | 36 |
37 std::string error; | 37 std::string error; |
38 scoped_refptr<const WebRequestConditionAttribute> result; | 38 scoped_refptr<const WebRequestConditionAttribute> result; |
39 base::StringValue string_value("main_frame"); | 39 base::StringValue string_value("main_frame"); |
40 base::ListValue resource_types; | 40 base::ListValue resource_types; |
41 resource_types.Append(new base::StringValue("main_frame")); | 41 resource_types.Append(new base::StringValue("main_frame")); |
42 | 42 |
43 // Test wrong condition name passed. | 43 // Test wrong condition name passed. |
44 error.clear(); | 44 error.clear(); |
45 result = WebRequestConditionAttribute::Create( | 45 result = WebRequestConditionAttribute::Create( |
(...skipping 20 matching lines...) Expand all Loading... |
66 keys::kResourceTypeKey, &resource_types, &error); | 66 keys::kResourceTypeKey, &resource_types, &error); |
67 EXPECT_EQ("", error); | 67 EXPECT_EQ("", error); |
68 ASSERT_TRUE(result.get()); | 68 ASSERT_TRUE(result.get()); |
69 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, | 69 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, |
70 result->GetType()); | 70 result->GetType()); |
71 EXPECT_EQ(std::string(keys::kResourceTypeKey), result->GetName()); | 71 EXPECT_EQ(std::string(keys::kResourceTypeKey), result->GetName()); |
72 } | 72 } |
73 | 73 |
74 TEST(WebRequestConditionAttributeTest, ResourceType) { | 74 TEST(WebRequestConditionAttributeTest, ResourceType) { |
75 // Necessary for TestURLRequest. | 75 // Necessary for TestURLRequest. |
76 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 76 base::MessageLoopForIO message_loop; |
77 | 77 |
78 std::string error; | 78 std::string error; |
79 base::ListValue resource_types; | 79 base::ListValue resource_types; |
80 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding | 80 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding |
81 // ResourceType::Type is not 0, the default value. | 81 // ResourceType::Type is not 0, the default value. |
82 resource_types.Append(new base::StringValue("sub_frame")); | 82 resource_types.Append(new base::StringValue("sub_frame")); |
83 | 83 |
84 scoped_refptr<const WebRequestConditionAttribute> attribute = | 84 scoped_refptr<const WebRequestConditionAttribute> attribute = |
85 WebRequestConditionAttribute::Create( | 85 WebRequestConditionAttribute::Create( |
86 keys::kResourceTypeKey, &resource_types, &error); | 86 keys::kResourceTypeKey, &resource_types, &error); |
(...skipping 12 matching lines...) Expand all Loading... |
99 net::TestURLRequest url_request_fail( | 99 net::TestURLRequest url_request_fail( |
100 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, &context); | 100 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, &context); |
101 content::ResourceRequestInfo::AllocateForTesting( | 101 content::ResourceRequestInfo::AllocateForTesting( |
102 &url_request_fail, ResourceType::MAIN_FRAME, NULL, -1, -1, -1, false); | 102 &url_request_fail, ResourceType::MAIN_FRAME, NULL, -1, -1, -1, false); |
103 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, | 103 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, |
104 ON_BEFORE_REQUEST))); | 104 ON_BEFORE_REQUEST))); |
105 } | 105 } |
106 | 106 |
107 TEST(WebRequestConditionAttributeTest, ContentType) { | 107 TEST(WebRequestConditionAttributeTest, ContentType) { |
108 // Necessary for TestURLRequest. | 108 // Necessary for TestURLRequest. |
109 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 109 base::MessageLoopForIO message_loop; |
110 | 110 |
111 std::string error; | 111 std::string error; |
112 scoped_refptr<const WebRequestConditionAttribute> result; | 112 scoped_refptr<const WebRequestConditionAttribute> result; |
113 | 113 |
114 net::SpawnedTestServer test_server( | 114 net::SpawnedTestServer test_server( |
115 net::SpawnedTestServer::TYPE_HTTP, | 115 net::SpawnedTestServer::TYPE_HTTP, |
116 net::SpawnedTestServer::kLocalhost, | 116 net::SpawnedTestServer::kLocalhost, |
117 base::FilePath(FILE_PATH_LITERAL( | 117 base::FilePath(FILE_PATH_LITERAL( |
118 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 118 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
119 ASSERT_TRUE(test_server.Start()); | 119 ASSERT_TRUE(test_server.Start()); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( | 170 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( |
171 WebRequestData(&url_request, ON_HEADERS_RECEIVED, | 171 WebRequestData(&url_request, ON_HEADERS_RECEIVED, |
172 url_request.response_headers()))); | 172 url_request.response_headers()))); |
173 EXPECT_EQ(std::string(keys::kExcludeContentTypeKey), | 173 EXPECT_EQ(std::string(keys::kExcludeContentTypeKey), |
174 attribute_unexcluded->GetName()); | 174 attribute_unexcluded->GetName()); |
175 } | 175 } |
176 | 176 |
177 // Testing WebRequestConditionAttributeThirdParty. | 177 // Testing WebRequestConditionAttributeThirdParty. |
178 TEST(WebRequestConditionAttributeTest, ThirdParty) { | 178 TEST(WebRequestConditionAttributeTest, ThirdParty) { |
179 // Necessary for TestURLRequest. | 179 // Necessary for TestURLRequest. |
180 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 180 base::MessageLoopForIO message_loop; |
181 | 181 |
182 std::string error; | 182 std::string error; |
183 const FundamentalValue value_true(true); | 183 const FundamentalValue value_true(true); |
184 // This attribute matches only third party requests. | 184 // This attribute matches only third party requests. |
185 scoped_refptr<const WebRequestConditionAttribute> third_party_attribute = | 185 scoped_refptr<const WebRequestConditionAttribute> third_party_attribute = |
186 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, | 186 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, |
187 &value_true, | 187 &value_true, |
188 &error); | 188 &error); |
189 ASSERT_EQ("", error); | 189 ASSERT_EQ("", error); |
190 ASSERT_TRUE(third_party_attribute.get()); | 190 ASSERT_TRUE(third_party_attribute.get()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 stage))); | 232 stage))); |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 // Testing WebRequestConditionAttributeStages. This iterates over all stages, | 236 // Testing WebRequestConditionAttributeStages. This iterates over all stages, |
237 // and tests a couple of "stage" attributes -- one created with an empty set of | 237 // and tests a couple of "stage" attributes -- one created with an empty set of |
238 // applicable stages, one for each stage applicable for that stage, and one | 238 // applicable stages, one for each stage applicable for that stage, and one |
239 // applicable in all stages. | 239 // applicable in all stages. |
240 TEST(WebRequestConditionAttributeTest, Stages) { | 240 TEST(WebRequestConditionAttributeTest, Stages) { |
241 // Necessary for TestURLRequest. | 241 // Necessary for TestURLRequest. |
242 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 242 base::MessageLoopForIO message_loop; |
243 | 243 |
244 typedef std::pair<RequestStage, const char*> StageNamePair; | 244 typedef std::pair<RequestStage, const char*> StageNamePair; |
245 static const StageNamePair active_stages[] = { | 245 static const StageNamePair active_stages[] = { |
246 StageNamePair(ON_BEFORE_REQUEST, keys::kOnBeforeRequestEnum), | 246 StageNamePair(ON_BEFORE_REQUEST, keys::kOnBeforeRequestEnum), |
247 StageNamePair(ON_BEFORE_SEND_HEADERS, keys::kOnBeforeSendHeadersEnum), | 247 StageNamePair(ON_BEFORE_SEND_HEADERS, keys::kOnBeforeSendHeadersEnum), |
248 StageNamePair(ON_HEADERS_RECEIVED, keys::kOnHeadersReceivedEnum), | 248 StageNamePair(ON_HEADERS_RECEIVED, keys::kOnHeadersReceivedEnum), |
249 StageNamePair(ON_AUTH_REQUIRED, keys::kOnAuthRequiredEnum) | 249 StageNamePair(ON_AUTH_REQUIRED, keys::kOnAuthRequiredEnum) |
250 }; | 250 }; |
251 | 251 |
252 // Check that exactly all active stages are considered in this test. | 252 // Check that exactly all active stages are considered in this test. |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 } | 411 } |
412 | 412 |
413 } // namespace | 413 } // namespace |
414 | 414 |
415 // Here we test WebRequestConditionAttributeRequestHeaders for matching | 415 // Here we test WebRequestConditionAttributeRequestHeaders for matching |
416 // correctly against request headers. This test is not as extensive as | 416 // correctly against request headers. This test is not as extensive as |
417 // "ResponseHeaders" (below), because the header-matching code is shared | 417 // "ResponseHeaders" (below), because the header-matching code is shared |
418 // by both types of condition attributes, so it is enough to test it once. | 418 // by both types of condition attributes, so it is enough to test it once. |
419 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 419 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
420 // Necessary for TestURLRequest. | 420 // Necessary for TestURLRequest. |
421 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 421 base::MessageLoopForIO message_loop; |
422 | 422 |
423 net::TestURLRequestContext context; | 423 net::TestURLRequestContext context; |
424 net::TestDelegate delegate; | 424 net::TestDelegate delegate; |
425 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. | 425 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. |
426 net::DEFAULT_PRIORITY, | 426 net::DEFAULT_PRIORITY, |
427 &delegate, | 427 &delegate, |
428 &context); | 428 &context); |
429 url_request.SetExtraRequestHeaderByName( | 429 url_request.SetExtraRequestHeaderByName( |
430 "Custom-header", "custom/value", true /* overwrite */); | 430 "Custom-header", "custom/value", true /* overwrite */); |
431 url_request.Start(); | 431 url_request.Start(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result); | 491 tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result); |
492 EXPECT_FALSE(result); | 492 EXPECT_FALSE(result); |
493 } | 493 } |
494 | 494 |
495 // Here we test WebRequestConditionAttributeResponseHeaders for: | 495 // Here we test WebRequestConditionAttributeResponseHeaders for: |
496 // 1. Correct implementation of prefix/suffix/contains/equals matching. | 496 // 1. Correct implementation of prefix/suffix/contains/equals matching. |
497 // 2. Performing logical disjunction (||) between multiple specifications. | 497 // 2. Performing logical disjunction (||) between multiple specifications. |
498 // 3. Negating the match in case of 'doesNotContainHeaders'. | 498 // 3. Negating the match in case of 'doesNotContainHeaders'. |
499 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { | 499 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { |
500 // Necessary for TestURLRequest. | 500 // Necessary for TestURLRequest. |
501 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 501 base::MessageLoopForIO message_loop; |
502 | 502 |
503 net::SpawnedTestServer test_server( | 503 net::SpawnedTestServer test_server( |
504 net::SpawnedTestServer::TYPE_HTTP, | 504 net::SpawnedTestServer::TYPE_HTTP, |
505 net::SpawnedTestServer::kLocalhost, | 505 net::SpawnedTestServer::kLocalhost, |
506 base::FilePath(FILE_PATH_LITERAL( | 506 base::FilePath(FILE_PATH_LITERAL( |
507 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 507 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
508 ASSERT_TRUE(test_server.Start()); | 508 ASSERT_TRUE(test_server.Start()); |
509 | 509 |
510 net::TestURLRequestContext context; | 510 net::TestURLRequestContext context; |
511 net::TestDelegate delegate; | 511 net::TestDelegate delegate; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 keys::kValueEqualsKey, "valueB" | 674 keys::kValueEqualsKey, "valueB" |
675 }; | 675 }; |
676 const size_t kExistingSize[] = { arraysize(kExisting) }; | 676 const size_t kExistingSize[] = { arraysize(kExisting) }; |
677 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 677 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
678 MatchAndCheck( | 678 MatchAndCheck( |
679 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 679 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
680 EXPECT_FALSE(result); | 680 EXPECT_FALSE(result); |
681 } | 681 } |
682 | 682 |
683 } // namespace extensions | 683 } // namespace extensions |
OLD | NEW |