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_rule.h
" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/memory/linked_ptr.h" | 8 #include "base/memory/linked_ptr.h" |
9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action
.h" | 9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action
.h" |
10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" | 10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
12 #include "chrome/common/extensions/matcher/url_matcher.h" | 12 #include "chrome/common/extensions/matcher/url_matcher.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 namespace { | 17 namespace { |
18 const char kExtId[] = "ext1"; | 18 const char kExtId[] = "ext1"; |
19 } // namespace | 19 } // namespace |
20 | 20 |
21 TEST(WebRequestRuleTest, Create) { | 21 TEST(WebRequestRuleTest, Create) { |
22 const char kRule[] = | 22 const char kRule[] = |
23 "{ \n" | 23 "{ \n" |
24 " \"id\": \"rule1\", \n" | 24 " \"id\": \"rule1\", \n" |
25 " \"conditions\": [ \n" | 25 " \"conditions\": [ \n" |
26 " { \n" | 26 " { \n" |
27 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 27 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
28 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n" | 28 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n" |
| 29 " \"firstPartyForCookiesUrl\": {\"hostSuffix\": \"foo.com\"}, \n" |
29 " \"contentType\": [\"image/jpeg\"] \n" | 30 " \"contentType\": [\"image/jpeg\"] \n" |
30 " } \n" | 31 " } \n" |
31 " ], \n" | 32 " ], \n" |
32 " \"actions\": [ \n" | 33 " \"actions\": [ \n" |
33 " { \n" | 34 " { \n" |
34 " \"instanceType\": \"declarativeWebRequest.CancelRequest\" \n" | 35 " \"instanceType\": \"declarativeWebRequest.CancelRequest\" \n" |
35 " } \n" | 36 " } \n" |
36 " ], \n" | 37 " ], \n" |
37 " \"priority\": 200 \n" | 38 " \"priority\": 200 \n" |
38 "} "; | 39 "} "; |
39 | 40 |
40 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); | 41 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); |
41 ASSERT_TRUE(value.get()); | 42 ASSERT_TRUE(value.get()); |
42 | 43 |
43 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | 44 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
44 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rule.get())); | 45 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rule.get())); |
45 | 46 |
46 URLMatcher matcher; | 47 URLMatcher matcher; |
| 48 URLMatcher first_party_matcher; |
47 std::string error; | 49 std::string error; |
48 scoped_ptr<WebRequestRule> web_request_rule( | 50 scoped_ptr<WebRequestRule> web_request_rule( |
49 WebRequestRule::Create(matcher.condition_factory(), kExtId, base::Time(), | 51 WebRequestRule::Create( |
50 rule, &error)); | 52 matcher.condition_factory(), first_party_matcher.condition_factory(), |
| 53 kExtId, base::Time(), rule, &error)); |
51 ASSERT_TRUE(web_request_rule.get()); | 54 ASSERT_TRUE(web_request_rule.get()); |
52 EXPECT_TRUE(error.empty()); | 55 EXPECT_TRUE(error.empty()); |
53 | 56 |
54 EXPECT_EQ(kExtId, web_request_rule->id().first); | 57 EXPECT_EQ(kExtId, web_request_rule->id().first); |
55 EXPECT_EQ("rule1", web_request_rule->id().second); | 58 EXPECT_EQ("rule1", web_request_rule->id().second); |
56 | 59 |
57 EXPECT_EQ(200, web_request_rule->priority()); | 60 EXPECT_EQ(200, web_request_rule->priority()); |
58 | 61 |
59 const WebRequestConditionSet& condition_set = web_request_rule->conditions(); | 62 const WebRequestConditionSet& condition_set = web_request_rule->conditions(); |
60 const WebRequestConditionSet::Conditions conditions = | 63 const WebRequestConditionSet::Conditions conditions = |
61 condition_set.conditions(); | 64 condition_set.conditions(); |
62 ASSERT_EQ(1u, conditions.size()); | 65 ASSERT_EQ(1u, conditions.size()); |
63 linked_ptr<WebRequestCondition> condition = conditions[0]; | 66 linked_ptr<WebRequestCondition> condition = conditions[0]; |
64 const WebRequestConditionAttributes& condition_attributes = | 67 const WebRequestConditionAttributes& condition_attributes = |
65 condition->condition_attributes(); | 68 condition->condition_attributes(); |
66 ASSERT_EQ(1u, condition_attributes.size()); | 69 ASSERT_EQ(1u, condition_attributes.size()); |
| 70 ASSERT_EQ(1u, condition->url_matcher_condition_set()->conditions().size()); |
| 71 ASSERT_EQ(1u, |
| 72 condition->first_party_url_matcher_condition_set()-> |
| 73 conditions().size()); |
67 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_CONTENT_TYPE, | 74 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_CONTENT_TYPE, |
68 condition_attributes[0]->GetType()); | 75 condition_attributes[0]->GetType()); |
69 | 76 |
70 const WebRequestActionSet& action_set = web_request_rule->actions(); | 77 const WebRequestActionSet& action_set = web_request_rule->actions(); |
71 const WebRequestActionSet::Actions& actions = action_set.actions(); | 78 const WebRequestActionSet::Actions& actions = action_set.actions(); |
72 ASSERT_EQ(1u, actions.size()); | 79 ASSERT_EQ(1u, actions.size()); |
73 EXPECT_EQ(WebRequestAction::ACTION_CANCEL_REQUEST, actions[0]->GetType()); | 80 EXPECT_EQ(WebRequestAction::ACTION_CANCEL_REQUEST, actions[0]->GetType()); |
74 } | 81 } |
75 | 82 |
76 TEST(WebRequestRuleTest, CheckConsistency) { | 83 TEST(WebRequestRuleTest, CheckConsistency) { |
(...skipping 19 matching lines...) Expand all Loading... |
96 " \"priority\": 200 \n" | 103 " \"priority\": 200 \n" |
97 "} "; | 104 "} "; |
98 | 105 |
99 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); | 106 scoped_ptr<Value> value(base::JSONReader::Read(kRule)); |
100 ASSERT_TRUE(value.get()); | 107 ASSERT_TRUE(value.get()); |
101 | 108 |
102 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | 109 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
103 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rule.get())); | 110 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rule.get())); |
104 | 111 |
105 URLMatcher matcher; | 112 URLMatcher matcher; |
| 113 URLMatcher first_party_matcher; |
106 std::string error; | 114 std::string error; |
107 scoped_ptr<WebRequestRule> web_request_rule( | 115 scoped_ptr<WebRequestRule> web_request_rule( |
108 WebRequestRule::Create(matcher.condition_factory(), kExtId, base::Time(), | 116 WebRequestRule::Create( |
109 rule, &error)); | 117 matcher.condition_factory(), first_party_matcher.condition_factory(), |
| 118 kExtId, base::Time(), rule, &error)); |
110 EXPECT_FALSE(web_request_rule.get()); | 119 EXPECT_FALSE(web_request_rule.get()); |
111 EXPECT_FALSE(error.empty()); | 120 EXPECT_FALSE(error.empty()); |
112 } | 121 } |
113 | 122 |
114 } // namespace extensions | 123 } // namespace extensions |
OLD | NEW |