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_empty; |
| 190 const GURL url_a("http://a.com"); |
| 191 const GURL url_b("http://b.com"); |
| 192 TestURLRequestContext context; |
| 193 TestDelegate delegate; |
| 194 TestURLRequest url_request(url_a, &delegate, &context); |
| 195 |
| 196 static const RequestStage request_stages[] = { |
| 197 ON_BEFORE_REQUEST, |
| 198 ON_BEFORE_SEND_HEADERS, |
| 199 ON_SEND_HEADERS, |
| 200 ON_HEADERS_RECEIVED, |
| 201 ON_AUTH_REQUIRED, |
| 202 ON_BEFORE_REDIRECT, |
| 203 ON_RESPONSE_STARTED, |
| 204 ON_COMPLETED, |
| 205 ON_ERROR |
| 206 }; |
| 207 |
| 208 for (size_t i = 0; i < arraysize(request_stages); ++i) { |
| 209 url_request.set_first_party_for_cookies(url_empty); |
| 210 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 211 &url_request, request_stages[i]))); |
| 212 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 213 &url_request, request_stages[i]))); |
| 214 |
| 215 url_request.set_first_party_for_cookies(url_b); |
| 216 EXPECT_TRUE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 217 &url_request, request_stages[i]))); |
| 218 EXPECT_FALSE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 219 &url_request, request_stages[i]))); |
| 220 |
| 221 url_request.set_first_party_for_cookies(url_a); |
| 222 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 223 &url_request, request_stages[i]))); |
| 224 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 225 &url_request, request_stages[i]))); |
| 226 } |
| 227 } |
| 228 |
165 namespace { | 229 namespace { |
166 | 230 |
167 // Builds a vector of vectors of string pointers from an array of strings. | 231 // 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 | 232 // |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. | 233 // of all parts of |array|, and |size| is the length of |sizes| itself. |
170 // Example (this is pseudo-code, not C++): | 234 // Example (this is pseudo-code, not C++): |
171 // array = { "a", "b", "c", "d", "e", "f" } | 235 // array = { "a", "b", "c", "d", "e", "f" } |
172 // sizes = { 2, 0, 4 } | 236 // sizes = { 2, 0, 4 } |
173 // size = 3 | 237 // size = 3 |
174 // results in out == { {&"a", &"b"}, {}, {&"c", &"d", &"e", &"f"} } | 238 // 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", | 495 keys::kNameEqualsKey, "custom-header-b", |
432 keys::kValueEqualsKey, "valueB" | 496 keys::kValueEqualsKey, "valueB" |
433 }; | 497 }; |
434 const size_t kExistingSize[] = { arraysize(kExisting) }; | 498 const size_t kExistingSize[] = { arraysize(kExisting) }; |
435 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 499 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
436 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, &url_request, &result); | 500 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, &url_request, &result); |
437 EXPECT_FALSE(result); | 501 EXPECT_FALSE(result); |
438 } | 502 } |
439 | 503 |
440 } // namespace extensions | 504 } // namespace extensions |
OLD | NEW |