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_action
.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action
.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" | 9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // Test success | 50 // Test success |
51 input.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 51 input.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
52 error.clear(); | 52 error.clear(); |
53 result = WebRequestAction::Create(input, &error, &bad_message); | 53 result = WebRequestAction::Create(input, &error, &bad_message); |
54 EXPECT_EQ("", error); | 54 EXPECT_EQ("", error); |
55 EXPECT_FALSE(bad_message); | 55 EXPECT_FALSE(bad_message); |
56 ASSERT_TRUE(result.get()); | 56 ASSERT_TRUE(result.get()); |
57 EXPECT_EQ(WebRequestAction::ACTION_CANCEL_REQUEST, result->GetType()); | 57 EXPECT_EQ(WebRequestAction::ACTION_CANCEL_REQUEST, result->GetType()); |
58 } | 58 } |
59 | 59 |
| 60 TEST(WebRequestActionTest, CreateActionSet) { |
| 61 std::string error; |
| 62 bool bad_message = false; |
| 63 scoped_ptr<WebRequestActionSet> result; |
| 64 |
| 65 WebRequestActionSet::AnyVector input; |
| 66 |
| 67 // Test empty input. |
| 68 error.clear(); |
| 69 result = WebRequestActionSet::Create(input, &error, &bad_message); |
| 70 EXPECT_TRUE(error.empty()) << error; |
| 71 EXPECT_FALSE(bad_message); |
| 72 ASSERT_TRUE(result.get()); |
| 73 EXPECT_TRUE(result->actions().empty()); |
| 74 EXPECT_EQ(std::numeric_limits<int>::min(), result->GetMinimumPriority()); |
| 75 |
| 76 DictionaryValue correct_action; |
| 77 correct_action.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); |
| 78 correct_action.SetInteger(keys::kLowerPriorityThanKey, 10); |
| 79 DictionaryValue incorrect_action; |
| 80 incorrect_action.SetString(keys::kInstanceTypeKey, kUnknownActionType); |
| 81 |
| 82 // Test success. |
| 83 input.push_back(linked_ptr<base::Value>(correct_action.DeepCopy())); |
| 84 error.clear(); |
| 85 result = WebRequestActionSet::Create(input, &error, &bad_message); |
| 86 EXPECT_TRUE(error.empty()) << error; |
| 87 EXPECT_FALSE(bad_message); |
| 88 ASSERT_TRUE(result.get()); |
| 89 ASSERT_EQ(1u, result->actions().size()); |
| 90 EXPECT_EQ(WebRequestAction::ACTION_IGNORE_RULES, |
| 91 result->actions()[0]->GetType()); |
| 92 EXPECT_EQ(10, result->GetMinimumPriority()); |
| 93 |
| 94 // Test failure. |
| 95 input.push_back(linked_ptr<base::Value>(incorrect_action.DeepCopy())); |
| 96 error.clear(); |
| 97 result = WebRequestActionSet::Create(input, &error, &bad_message); |
| 98 EXPECT_NE("", error); |
| 99 EXPECT_FALSE(result.get()); |
| 100 } |
| 101 |
60 // Test capture group syntax conversions of WebRequestRedirectByRegExAction | 102 // Test capture group syntax conversions of WebRequestRedirectByRegExAction |
61 TEST(WebRequestActionTest, PerlToRe2Style) { | 103 TEST(WebRequestActionTest, PerlToRe2Style) { |
62 #define CallPerlToRe2Style WebRequestRedirectByRegExAction::PerlToRe2Style | 104 #define CallPerlToRe2Style WebRequestRedirectByRegExAction::PerlToRe2Style |
63 // foo$1bar -> foo\1bar | 105 // foo$1bar -> foo\1bar |
64 EXPECT_EQ("foo\\1bar", CallPerlToRe2Style("foo$1bar")); | 106 EXPECT_EQ("foo\\1bar", CallPerlToRe2Style("foo$1bar")); |
65 // foo\$1bar -> foo$1bar | 107 // foo\$1bar -> foo$1bar |
66 EXPECT_EQ("foo$1bar", CallPerlToRe2Style("foo\\$1bar")); | 108 EXPECT_EQ("foo$1bar", CallPerlToRe2Style("foo\\$1bar")); |
67 // foo\\$1bar -> foo\\\1bar | 109 // foo\\$1bar -> foo\\\1bar |
68 EXPECT_EQ("foo\\\\\\1bar", CallPerlToRe2Style("foo\\\\$1bar")); | 110 EXPECT_EQ("foo\\\\\\1bar", CallPerlToRe2Style("foo\\\\$1bar")); |
69 // foo\bar -> foobar | 111 // foo\bar -> foobar |
(...skipping 10 matching lines...) Expand all Loading... |
80 | 122 |
81 std::string error; | 123 std::string error; |
82 bool bad_message = false; | 124 bool bad_message = false; |
83 scoped_ptr<WebRequestActionSet> action_set; | 125 scoped_ptr<WebRequestActionSet> action_set; |
84 | 126 |
85 // Setup redirect to http://www.foobar.com. | 127 // Setup redirect to http://www.foobar.com. |
86 base::DictionaryValue redirect; | 128 base::DictionaryValue redirect; |
87 redirect.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); | 129 redirect.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); |
88 redirect.SetString(keys::kRedirectUrlKey, "http://www.foobar.com"); | 130 redirect.SetString(keys::kRedirectUrlKey, "http://www.foobar.com"); |
89 | 131 |
90 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | |
91 new json_schema_compiler::any::Any); | |
92 action->Init(redirect); | |
93 WebRequestActionSet::AnyVector actions; | 132 WebRequestActionSet::AnyVector actions; |
94 actions.push_back(action); | 133 actions.push_back(linked_ptr<base::Value>(redirect.DeepCopy())); |
95 | 134 |
96 action_set = WebRequestActionSet::Create(actions, &error, &bad_message); | 135 action_set = WebRequestActionSet::Create(actions, &error, &bad_message); |
97 EXPECT_EQ("", error); | 136 EXPECT_EQ("", error); |
98 EXPECT_FALSE(bad_message); | 137 EXPECT_FALSE(bad_message); |
99 ASSERT_TRUE(action.get()); | |
100 | 138 |
101 // Check that redirect works on regular URLs but not on protected URLs. | 139 // Check that redirect works on regular URLs but not on protected URLs. |
102 net::TestURLRequest regular_request(GURL("http://test.com"), NULL, &context); | 140 net::TestURLRequest regular_request(GURL("http://test.com"), NULL, &context); |
103 std::list<LinkedPtrEventResponseDelta> deltas; | 141 std::list<LinkedPtrEventResponseDelta> deltas; |
104 DeclarativeWebRequestData request_data(®ular_request, ON_BEFORE_REQUEST); | 142 DeclarativeWebRequestData request_data(®ular_request, ON_BEFORE_REQUEST); |
105 WebRequestAction::ApplyInfo apply_info = { | 143 WebRequestAction::ApplyInfo apply_info = { |
106 NULL, request_data, false, &deltas | 144 NULL, request_data, false, &deltas |
107 }; | 145 }; |
108 action_set->Apply("ext1", base::Time(), &apply_info); | 146 action_set->Apply("ext1", base::Time(), &apply_info); |
109 EXPECT_EQ(1u, deltas.size()); | 147 EXPECT_EQ(1u, deltas.size()); |
110 | 148 |
111 net::TestURLRequest protected_request(GURL("http://clients1.google.com"), | 149 net::TestURLRequest protected_request(GURL("http://clients1.google.com"), |
112 NULL, &context); | 150 NULL, &context); |
113 deltas.clear(); | 151 deltas.clear(); |
114 request_data = | 152 request_data = |
115 DeclarativeWebRequestData(&protected_request, ON_BEFORE_REQUEST); | 153 DeclarativeWebRequestData(&protected_request, ON_BEFORE_REQUEST); |
116 action_set->Apply("ext1", base::Time(), &apply_info); | 154 action_set->Apply("ext1", base::Time(), &apply_info); |
117 EXPECT_EQ(0u, deltas.size()); | 155 EXPECT_EQ(0u, deltas.size()); |
118 } | 156 } |
119 | 157 |
120 } // namespace extensions | 158 } // namespace extensions |
OLD | NEW |