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/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.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 MessageLoop message_loop(MessageLoop::TYPE_IO); | 35 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
36 | 36 |
37 std::string error; | 37 std::string error; |
38 scoped_ptr<WebRequestConditionAttribute> result; | 38 scoped_ptr<WebRequestConditionAttribute> result; |
39 StringValue string_value("main_frame"); | 39 StringValue string_value("main_frame"); |
40 ListValue resource_types; | 40 ListValue resource_types; |
41 resource_types.Append(Value::CreateStringValue("main_frame")); | 41 resource_types.Append(Value::CreateStringValue("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 19 matching lines...) Expand all Loading... |
65 result = WebRequestConditionAttribute::Create( | 65 result = WebRequestConditionAttribute::Create( |
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 } | 71 } |
72 | 72 |
73 TEST(WebRequestConditionAttributeTest, ResourceType) { | 73 TEST(WebRequestConditionAttributeTest, ResourceType) { |
74 // Necessary for TestURLRequest. | 74 // Necessary for TestURLRequest. |
75 MessageLoop message_loop(MessageLoop::TYPE_IO); | 75 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
76 | 76 |
77 std::string error; | 77 std::string error; |
78 ListValue resource_types; | 78 ListValue resource_types; |
79 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding | 79 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding |
80 // ResourceType::Type is not 0, the default value. | 80 // ResourceType::Type is not 0, the default value. |
81 resource_types.Append(Value::CreateStringValue("sub_frame")); | 81 resource_types.Append(Value::CreateStringValue("sub_frame")); |
82 | 82 |
83 scoped_ptr<WebRequestConditionAttribute> attribute = | 83 scoped_ptr<WebRequestConditionAttribute> attribute = |
84 WebRequestConditionAttribute::Create( | 84 WebRequestConditionAttribute::Create( |
85 keys::kResourceTypeKey, &resource_types, &error); | 85 keys::kResourceTypeKey, &resource_types, &error); |
(...skipping 11 matching lines...) Expand all Loading... |
97 net::TestURLRequest url_request_fail( | 97 net::TestURLRequest url_request_fail( |
98 GURL("http://www.example.com"), NULL, &context, NULL); | 98 GURL("http://www.example.com"), NULL, &context, NULL); |
99 content::ResourceRequestInfo::AllocateForTesting(&url_request_fail, | 99 content::ResourceRequestInfo::AllocateForTesting(&url_request_fail, |
100 ResourceType::MAIN_FRAME, NULL, -1, -1); | 100 ResourceType::MAIN_FRAME, NULL, -1, -1); |
101 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, | 101 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, |
102 ON_BEFORE_REQUEST))); | 102 ON_BEFORE_REQUEST))); |
103 } | 103 } |
104 | 104 |
105 TEST(WebRequestConditionAttributeTest, ContentType) { | 105 TEST(WebRequestConditionAttributeTest, ContentType) { |
106 // Necessary for TestURLRequest. | 106 // Necessary for TestURLRequest. |
107 MessageLoop message_loop(MessageLoop::TYPE_IO); | 107 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
108 | 108 |
109 std::string error; | 109 std::string error; |
110 scoped_ptr<WebRequestConditionAttribute> result; | 110 scoped_ptr<WebRequestConditionAttribute> result; |
111 | 111 |
112 net::TestServer test_server( | 112 net::TestServer test_server( |
113 net::TestServer::TYPE_HTTP, | 113 net::TestServer::TYPE_HTTP, |
114 net::TestServer::kLocalhost, | 114 net::TestServer::kLocalhost, |
115 base::FilePath(FILE_PATH_LITERAL( | 115 base::FilePath(FILE_PATH_LITERAL( |
116 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 116 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
117 ASSERT_TRUE(test_server.Start()); | 117 ASSERT_TRUE(test_server.Start()); |
118 | 118 |
119 net::TestURLRequestContext context; | 119 net::TestURLRequestContext context; |
120 net::TestDelegate delegate; | 120 net::TestDelegate delegate; |
121 net::TestURLRequest url_request( | 121 net::TestURLRequest url_request( |
122 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); | 122 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); |
123 url_request.Start(); | 123 url_request.Start(); |
124 MessageLoop::current()->Run(); | 124 base::MessageLoop::current()->Run(); |
125 | 125 |
126 ListValue content_types; | 126 ListValue content_types; |
127 content_types.Append(Value::CreateStringValue("text/plain")); | 127 content_types.Append(Value::CreateStringValue("text/plain")); |
128 scoped_ptr<WebRequestConditionAttribute> attribute_include = | 128 scoped_ptr<WebRequestConditionAttribute> attribute_include = |
129 WebRequestConditionAttribute::Create( | 129 WebRequestConditionAttribute::Create( |
130 keys::kContentTypeKey, &content_types, &error); | 130 keys::kContentTypeKey, &content_types, &error); |
131 EXPECT_EQ("", error); | 131 EXPECT_EQ("", error); |
132 ASSERT_TRUE(attribute_include.get()); | 132 ASSERT_TRUE(attribute_include.get()); |
133 EXPECT_FALSE(attribute_include->IsFulfilled( | 133 EXPECT_FALSE(attribute_include->IsFulfilled( |
134 WebRequestData(&url_request, ON_BEFORE_REQUEST, | 134 WebRequestData(&url_request, ON_BEFORE_REQUEST, |
(...skipping 28 matching lines...) Expand all Loading... |
163 EXPECT_EQ("", error); | 163 EXPECT_EQ("", error); |
164 ASSERT_TRUE(attribute_unexcluded.get()); | 164 ASSERT_TRUE(attribute_unexcluded.get()); |
165 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( | 165 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( |
166 WebRequestData(&url_request, ON_HEADERS_RECEIVED, | 166 WebRequestData(&url_request, ON_HEADERS_RECEIVED, |
167 url_request.response_headers()))); | 167 url_request.response_headers()))); |
168 } | 168 } |
169 | 169 |
170 // Testing WebRequestConditionAttributeThirdParty. | 170 // Testing WebRequestConditionAttributeThirdParty. |
171 TEST(WebRequestConditionAttributeTest, ThirdParty) { | 171 TEST(WebRequestConditionAttributeTest, ThirdParty) { |
172 // Necessary for TestURLRequest. | 172 // Necessary for TestURLRequest. |
173 MessageLoop message_loop(MessageLoop::TYPE_IO); | 173 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
174 | 174 |
175 std::string error; | 175 std::string error; |
176 const FundamentalValue value_true(true); | 176 const FundamentalValue value_true(true); |
177 // This attribute matches only third party requests. | 177 // This attribute matches only third party requests. |
178 scoped_ptr<WebRequestConditionAttribute> third_party_attribute = | 178 scoped_ptr<WebRequestConditionAttribute> third_party_attribute = |
179 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, | 179 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, |
180 &value_true, | 180 &value_true, |
181 &error); | 181 &error); |
182 ASSERT_EQ("", error); | 182 ASSERT_EQ("", error); |
183 ASSERT_TRUE(third_party_attribute.get()); | 183 ASSERT_TRUE(third_party_attribute.get()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 stage))); | 220 stage))); |
221 } | 221 } |
222 } | 222 } |
223 | 223 |
224 // Testing WebRequestConditionAttributeStages. This iterates over all stages, | 224 // Testing WebRequestConditionAttributeStages. This iterates over all stages, |
225 // and tests a couple of "stage" attributes -- one created with an empty set of | 225 // and tests a couple of "stage" attributes -- one created with an empty set of |
226 // applicable stages, one for each stage applicable for that stage, and one | 226 // applicable stages, one for each stage applicable for that stage, and one |
227 // applicable in all stages. | 227 // applicable in all stages. |
228 TEST(WebRequestConditionAttributeTest, Stages) { | 228 TEST(WebRequestConditionAttributeTest, Stages) { |
229 // Necessary for TestURLRequest. | 229 // Necessary for TestURLRequest. |
230 MessageLoop message_loop(MessageLoop::TYPE_IO); | 230 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
231 | 231 |
232 typedef std::pair<RequestStage, const char*> StageNamePair; | 232 typedef std::pair<RequestStage, const char*> StageNamePair; |
233 static const StageNamePair active_stages[] = { | 233 static const StageNamePair active_stages[] = { |
234 StageNamePair(ON_BEFORE_REQUEST, keys::kOnBeforeRequestEnum), | 234 StageNamePair(ON_BEFORE_REQUEST, keys::kOnBeforeRequestEnum), |
235 StageNamePair(ON_BEFORE_SEND_HEADERS, keys::kOnBeforeSendHeadersEnum), | 235 StageNamePair(ON_BEFORE_SEND_HEADERS, keys::kOnBeforeSendHeadersEnum), |
236 StageNamePair(ON_HEADERS_RECEIVED, keys::kOnHeadersReceivedEnum), | 236 StageNamePair(ON_HEADERS_RECEIVED, keys::kOnHeadersReceivedEnum), |
237 StageNamePair(ON_AUTH_REQUIRED, keys::kOnAuthRequiredEnum) | 237 StageNamePair(ON_AUTH_REQUIRED, keys::kOnAuthRequiredEnum) |
238 }; | 238 }; |
239 | 239 |
240 // Check that exactly all active stages are considered in this test. | 240 // Check that exactly all active stages are considered in this test. |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 392 } |
393 | 393 |
394 } // namespace | 394 } // namespace |
395 | 395 |
396 // Here we test WebRequestConditionAttributeRequestHeaders for matching | 396 // Here we test WebRequestConditionAttributeRequestHeaders for matching |
397 // correctly against request headers. This test is not as extensive as | 397 // correctly against request headers. This test is not as extensive as |
398 // "ResponseHeaders" (below), because the header-matching code is shared | 398 // "ResponseHeaders" (below), because the header-matching code is shared |
399 // by both types of condition attributes, so it is enough to test it once. | 399 // by both types of condition attributes, so it is enough to test it once. |
400 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 400 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
401 // Necessary for TestURLRequest. | 401 // Necessary for TestURLRequest. |
402 MessageLoop message_loop(MessageLoop::TYPE_IO); | 402 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
403 | 403 |
404 net::TestURLRequestContext context; | 404 net::TestURLRequestContext context; |
405 net::TestDelegate delegate; | 405 net::TestDelegate delegate; |
406 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. | 406 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. |
407 &delegate, &context, NULL); | 407 &delegate, &context, NULL); |
408 url_request.SetExtraRequestHeaderByName( | 408 url_request.SetExtraRequestHeaderByName( |
409 "Custom-header", "custom/value", true /* overwrite */); | 409 "Custom-header", "custom/value", true /* overwrite */); |
410 url_request.Start(); | 410 url_request.Start(); |
411 MessageLoop::current()->Run(); | 411 base::MessageLoop::current()->Run(); |
412 | 412 |
413 std::vector<std::vector<const std::string*> > tests; | 413 std::vector<std::vector<const std::string*> > tests; |
414 bool result = false; | 414 bool result = false; |
415 | 415 |
416 const RequestStage stage = ON_BEFORE_SEND_HEADERS; | 416 const RequestStage stage = ON_BEFORE_SEND_HEADERS; |
417 | 417 |
418 // First set of test data -- passing conjunction. | 418 // First set of test data -- passing conjunction. |
419 const std::string kPassingCondition[] = { | 419 const std::string kPassingCondition[] = { |
420 keys::kNameContainsKey, "CuStOm", // Header names are case insensitive. | 420 keys::kNameContainsKey, "CuStOm", // Header names are case insensitive. |
421 keys::kNameEqualsKey, "custom-header", | 421 keys::kNameEqualsKey, "custom-header", |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result); | 470 tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result); |
471 EXPECT_FALSE(result); | 471 EXPECT_FALSE(result); |
472 } | 472 } |
473 | 473 |
474 // Here we test WebRequestConditionAttributeResponseHeaders for: | 474 // Here we test WebRequestConditionAttributeResponseHeaders for: |
475 // 1. Correct implementation of prefix/suffix/contains/equals matching. | 475 // 1. Correct implementation of prefix/suffix/contains/equals matching. |
476 // 2. Performing logical disjunction (||) between multiple specifications. | 476 // 2. Performing logical disjunction (||) between multiple specifications. |
477 // 3. Negating the match in case of 'doesNotContainHeaders'. | 477 // 3. Negating the match in case of 'doesNotContainHeaders'. |
478 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { | 478 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { |
479 // Necessary for TestURLRequest. | 479 // Necessary for TestURLRequest. |
480 MessageLoop message_loop(MessageLoop::TYPE_IO); | 480 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
481 | 481 |
482 net::TestServer test_server( | 482 net::TestServer test_server( |
483 net::TestServer::TYPE_HTTP, | 483 net::TestServer::TYPE_HTTP, |
484 net::TestServer::kLocalhost, | 484 net::TestServer::kLocalhost, |
485 base::FilePath(FILE_PATH_LITERAL( | 485 base::FilePath(FILE_PATH_LITERAL( |
486 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 486 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
487 ASSERT_TRUE(test_server.Start()); | 487 ASSERT_TRUE(test_server.Start()); |
488 | 488 |
489 net::TestURLRequestContext context; | 489 net::TestURLRequestContext context; |
490 net::TestDelegate delegate; | 490 net::TestDelegate delegate; |
491 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 491 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
492 &delegate, &context, NULL); | 492 &delegate, &context, NULL); |
493 url_request.Start(); | 493 url_request.Start(); |
494 MessageLoop::current()->Run(); | 494 base::MessageLoop::current()->Run(); |
495 | 495 |
496 // In all the tests below we assume that the server includes the headers | 496 // In all the tests below we assume that the server includes the headers |
497 // Custom-Header: custom/value | 497 // Custom-Header: custom/value |
498 // Custom-Header-B: valueA | 498 // Custom-Header-B: valueA |
499 // Custom-Header-B: valueB | 499 // Custom-Header-B: valueB |
500 // Custom-Header-C: valueC, valueD | 500 // Custom-Header-C: valueC, valueD |
501 // Custom-Header-D: | 501 // Custom-Header-D: |
502 // in the response, but does not include "Non-existing: void". | 502 // in the response, but does not include "Non-existing: void". |
503 | 503 |
504 std::vector< std::vector<const std::string*> > tests; | 504 std::vector< std::vector<const std::string*> > tests; |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 keys::kValueEqualsKey, "valueB" | 651 keys::kValueEqualsKey, "valueB" |
652 }; | 652 }; |
653 const size_t kExistingSize[] = { arraysize(kExisting) }; | 653 const size_t kExistingSize[] = { arraysize(kExisting) }; |
654 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 654 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
655 MatchAndCheck( | 655 MatchAndCheck( |
656 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 656 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
657 EXPECT_FALSE(result); | 657 EXPECT_FALSE(result); |
658 } | 658 } |
659 | 659 |
660 } // namespace extensions | 660 } // namespace extensions |
OLD | NEW |