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/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " |
13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
14 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
15 #include "net/test/test_server.h" | 15 #include "net/test/test_server.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using base::DictionaryValue; | 18 using base::DictionaryValue; |
19 using base::FundamentalValue; | |
19 using base::ListValue; | 20 using base::ListValue; |
20 using base::StringValue; | 21 using base::StringValue; |
21 using base::Value; | 22 using base::Value; |
22 | 23 |
23 namespace { | 24 namespace { |
24 const char kUnknownConditionName[] = "unknownType"; | 25 const char kUnknownConditionName[] = "unknownType"; |
25 } // namespace | 26 } // namespace |
26 | 27 |
27 namespace extensions { | 28 namespace extensions { |
28 | 29 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 scoped_ptr<WebRequestConditionAttribute> attribute_unexcluded = | 156 scoped_ptr<WebRequestConditionAttribute> attribute_unexcluded = |
156 WebRequestConditionAttribute::Create( | 157 WebRequestConditionAttribute::Create( |
157 keys::kExcludeContentTypeKey, &content_types, &error); | 158 keys::kExcludeContentTypeKey, &content_types, &error); |
158 EXPECT_EQ("", error); | 159 EXPECT_EQ("", error); |
159 ASSERT_TRUE(attribute_unexcluded.get()); | 160 ASSERT_TRUE(attribute_unexcluded.get()); |
160 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( | 161 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( |
161 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, | 162 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, |
162 url_request.response_headers()))); | 163 url_request.response_headers()))); |
163 } | 164 } |
164 | 165 |
166 // Testing WebRequestConditionAttributeThirdParty. | |
167 TEST(WebRequestConditionAttributeTest, ThirdParty) { | |
168 // Necessary for TestURLRequest. | |
169 MessageLoop message_loop(MessageLoop::TYPE_IO); | |
170 | |
171 std::string error; | |
172 const FundamentalValue value_true(true); | |
173 // This attribute matches only third party requests. | |
174 scoped_ptr<WebRequestConditionAttribute> third_party_attribute = | |
175 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, | |
176 &value_true, | |
177 &error); | |
178 ASSERT_EQ("", error); | |
179 ASSERT_TRUE(third_party_attribute.get() != NULL); | |
180 const FundamentalValue value_false(false); | |
181 // This attribute matches only first party requests. | |
182 scoped_ptr<WebRequestConditionAttribute> first_party_attribute = | |
183 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, | |
184 &value_false, | |
185 &error); | |
186 ASSERT_EQ("", error); | |
187 ASSERT_TRUE(first_party_attribute.get() != NULL); | |
188 | |
189 const GURL url_a("http://a.com"); | |
190 const GURL url_b("http://b.com"); | |
191 TestURLRequestContext context; | |
192 TestDelegate delegate; | |
193 TestURLRequest url_request(url_a, &delegate, &context); | |
194 | |
195 static const RequestStage request_stages[] = { | |
196 ON_BEFORE_REQUEST, | |
197 ON_BEFORE_SEND_HEADERS, | |
198 ON_SEND_HEADERS, | |
199 ON_HEADERS_RECEIVED, | |
200 ON_AUTH_REQUIRED, | |
201 ON_BEFORE_REDIRECT, | |
202 ON_RESPONSE_STARTED, | |
203 ON_COMPLETED, | |
204 ON_ERROR | |
205 }; | |
206 | |
207 for (size_t i = 0; i < arraysize(request_stages); ++i) { | |
battre
2012/10/15 07:19:51
maybe you want to add a test case for empty first
vabr (Chromium)
2012/10/15 12:01:43
Done.
| |
208 url_request.set_first_party_for_cookies(url_b); | |
209 EXPECT_TRUE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( | |
210 &url_request, request_stages[i]))); | |
211 | |
212 url_request.set_first_party_for_cookies(url_a); | |
213 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( | |
214 &url_request, request_stages[i]))); | |
215 | |
216 url_request.set_first_party_for_cookies(url_b); | |
217 EXPECT_FALSE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( | |
218 &url_request, request_stages[i]))); | |
219 | |
220 url_request.set_first_party_for_cookies(url_a); | |
221 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( | |
222 &url_request, request_stages[i]))); | |
223 } | |
224 } | |
225 | |
165 namespace { | 226 namespace { |
166 | 227 |
167 // Builds a vector of vectors of string pointers from an array of strings. | 228 // Builds a vector of vectors of string pointers from an array of strings. |
168 // |array| is in fact a sequence of arrays. The array |sizes| captures the sizes | 229 // |array| is in fact a sequence of arrays. The array |sizes| captures the sizes |
169 // of all parts of |array|, and |size| is the length of |sizes| itself. | 230 // of all parts of |array|, and |size| is the length of |sizes| itself. |
170 // Example (this is pseudo-code, not C++): | 231 // Example (this is pseudo-code, not C++): |
171 // array = { "a", "b", "c", "d", "e", "f" } | 232 // array = { "a", "b", "c", "d", "e", "f" } |
172 // sizes = { 2, 0, 4 } | 233 // sizes = { 2, 0, 4 } |
173 // size = 3 | 234 // size = 3 |
174 // results in out == { {&"a", &"b"}, {}, {&"c", &"d", &"e", &"f"} } | 235 // results in out == { {&"a", &"b"}, {}, {&"c", &"d", &"e", &"f"} } |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 keys::kNameEqualsKey, "custom-header-b", | 492 keys::kNameEqualsKey, "custom-header-b", |
432 keys::kValueEqualsKey, "valueB" | 493 keys::kValueEqualsKey, "valueB" |
433 }; | 494 }; |
434 const size_t kExistingSize[] = { arraysize(kExisting) }; | 495 const size_t kExistingSize[] = { arraysize(kExisting) }; |
435 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 496 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
436 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, &url_request, &result); | 497 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, &url_request, &result); |
437 EXPECT_FALSE(result); | 498 EXPECT_FALSE(result); |
438 } | 499 } |
439 | 500 |
440 } // namespace extensions | 501 } // namespace extensions |
OLD | NEW |