Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1148)

Unified Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc

Issue 11038031: Adding condition attributes for request headers to Declarative WebRequest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Dominic's comments addressed Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc
diff --git a/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc
index 1f3e26b9a69ebc19eba57c99cfd07ea57a7f1515..479cfeb8acc84769c76f923d2d39c2eb07cc6a10 100644
--- a/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc
+++ b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute_unittest.cc
@@ -234,6 +234,7 @@ scoped_ptr<DictionaryValue> GetDictionaryFromArray(
// doesNotContainHeaders.
void MatchAndCheck(const std::vector< std::vector<const std::string*> >& tests,
const std::string& key,
+ RequestStage stage,
net::URLRequest* url_request,
bool* result) {
ListValue contains_headers;
@@ -250,16 +251,94 @@ void MatchAndCheck(const std::vector< std::vector<const std::string*> >& tests,
ASSERT_TRUE(attribute.get() != NULL);
*result = attribute->IsFulfilled(WebRequestRule::RequestData(
- url_request, ON_HEADERS_RECEIVED, url_request->response_headers()));
+ url_request, stage, url_request->response_headers()));
}
} // namespace
+// Here we test WebRequestConditionAttributeRequestHeaders for matching
+// correctly against request headers. This test is not as extensive as
+// "ResponseHeaders" (below), because the header-matching code is shared
+// by both types of condition attributes, so it is enough to test it once.
+TEST(WebRequestConditionAttributeTest, RequestHeaders) {
+ // Necessary for TestURLRequest.
+ MessageLoop message_loop(MessageLoop::TYPE_IO);
+
+ TestURLRequestContext context;
+ TestDelegate delegate;
+ TestURLRequest url_request(GURL("http://example.com"), // Dummy URL.
+ &delegate, &context);
+ url_request.SetExtraRequestHeaderByName(
+ "Custom-header", "custom/value", true /* overwrite */);
+ url_request.Start();
+ MessageLoop::current()->Run();
+
+ std::vector<std::vector<const std::string*> > tests;
+ bool result = false;
+
+ const RequestStage stage = ON_BEFORE_SEND_HEADERS;
+
+ // First set of test data -- passing conjunction.
+ const std::string kPassingCondition[] = {
+ keys::kNameContainsKey, "CuStOm", // Header names are case insensitive.
+ keys::kNameEqualsKey, "custom-header",
+ keys::kValueSuffixKey, "alue",
+ keys::kValuePrefixKey, "custom/value"
+ };
+ const size_t kPassingConditionSizes[] = { arraysize(kPassingCondition) };
+ GetArrayAsVector(kPassingCondition, kPassingConditionSizes, 1u, &tests);
+ // Positive filter, passing (conjunction of tests).
+ MatchAndCheck(tests, keys::kRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_TRUE(result);
+ // Negative filter, failing (conjunction of tests).
+ MatchAndCheck(
+ tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_FALSE(result);
+
+ // Second set of test data -- failing disjunction.
+ const std::string kFailCondition[] = {
+ keys::kNameSuffixKey, "Custom", // Test 1.
+ keys::kNameEqualsKey, "ustom-valu", // Test 2.
+ keys::kValuePrefixKey, "custom ", // Test 3.
+ keys::kValueContainsKey, " value" // Test 4.
+ };
+ const size_t kFailConditionSizes[] = { 2u, 2u, 2u, 2u };
+ GetArrayAsVector(kFailCondition, kFailConditionSizes, 4u, &tests);
+ // Positive filter, failing (disjunction of tests).
+ MatchAndCheck(tests, keys::kRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_FALSE(result);
+ // Negative filter, passing (disjunction of tests).
+ MatchAndCheck(
+ tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_TRUE(result);
+
+ // Third set of test data, corner case -- empty disjunction.
+ GetArrayAsVector(NULL, NULL, 0u, &tests);
+ // Positive filter, failing (no test to pass).
+ MatchAndCheck(tests, keys::kRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_FALSE(result);
+ // Negative filter, passing (no test to fail).
+ MatchAndCheck(
+ tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_TRUE(result);
+
+ // Fourth set of test data, corner case -- empty conjunction.
+ const size_t kEmptyConjunctionSizes[] = { 0u };
+ GetArrayAsVector(NULL, kEmptyConjunctionSizes, 1u, &tests);
+ // Positive filter, passing (trivial test).
+ MatchAndCheck(tests, keys::kRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_TRUE(result);
+ // Negative filter, failing.
+ MatchAndCheck(
+ tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result);
+ EXPECT_FALSE(result);
+}
+
// Here we test WebRequestConditionAttributeResponseHeaders for:
// 1. Correct implementation of prefix/suffix/contains/equals matching.
// 2. Performing logical disjunction (||) between multiple specifications.
// 3. Negating the match in case of 'doesNotContainHeaders'.
-TEST(WebRequestConditionAttributeTest, Headers) {
+TEST(WebRequestConditionAttributeTest, ResponseHeaders) {
// Necessary for TestURLRequest.
MessageLoop message_loop(MessageLoop::TYPE_IO);
@@ -288,6 +367,8 @@ TEST(WebRequestConditionAttributeTest, Headers) {
std::vector< std::vector<const std::string*> > tests;
bool result;
+ const RequestStage stage = ON_HEADERS_RECEIVED;
+
// 1.a. -- All these tests should pass.
const std::string kPassingCondition[] = {
keys::kNamePrefixKey, "Custom",
@@ -297,7 +378,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kPassingConditionSizes[] = { arraysize(kPassingCondition) };
GetArrayAsVector(kPassingCondition, kPassingConditionSizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// 1.b. -- None of the following tests in the discjunction should pass.
@@ -309,7 +390,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kFailConditionSizes[] = { 2u, 2u, 2u, 2u };
GetArrayAsVector(kFailCondition, kFailConditionSizes, 4u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_FALSE(result);
// 1.c. -- This should fail (mixing name and value from different headers)
@@ -319,7 +400,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kMixingConditionSizes[] = { arraysize(kMixingCondition) };
GetArrayAsVector(kMixingCondition, kMixingConditionSizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_FALSE(result);
// 1.d. -- Test handling multiple values for one header (both should pass).
@@ -329,7 +410,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kMoreValues1Sizes[] = { arraysize(kMoreValues1) };
GetArrayAsVector(kMoreValues1, kMoreValues1Sizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
const std::string kMoreValues2[] = {
keys::kNameEqualsKey, "Custom-header-b",
@@ -337,7 +418,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kMoreValues2Sizes[] = { arraysize(kMoreValues2) };
GetArrayAsVector(kMoreValues2, kMoreValues2Sizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// 1.e. -- This should fail as conjunction but pass as disjunction.
@@ -348,12 +429,12 @@ TEST(WebRequestConditionAttributeTest, Headers) {
// First disjunction, no conflict.
const size_t kNoConflictSizes[] = { 2u, 2u };
GetArrayAsVector(kConflict, kNoConflictSizes, 2u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// Then conjunction, conflict.
const size_t kConflictSizes[] = { arraysize(kConflict) };
GetArrayAsVector(kConflict, kConflictSizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_FALSE(result);
// 1.f. -- This should pass, checking for correct treatment of ',' in values.
@@ -363,7 +444,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kCommaSizes[] = { arraysize(kComma) };
GetArrayAsVector(kComma, kCommaSizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// 1.g. -- This should pass, empty values are values as well.
@@ -373,7 +454,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kEmptySizes[] = { arraysize(kEmpty) };
GetArrayAsVector(kEmpty, kEmptySizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// 1.h. -- Values are case-sensitive, this should fail.
@@ -389,7 +470,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kLowercaseSizes[] = { 4u, 4u, 4u, 4u }; // As disjunction.
GetArrayAsVector(kLowercase, kLowercaseSizes, 4u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_FALSE(result);
// 1.i. -- Names are case-insensitive, this should pass.
@@ -401,7 +482,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kUppercaseSizes[] = { arraysize(kUppercase) }; // Conjunction.
GetArrayAsVector(kUppercase, kUppercaseSizes, 1u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// 2.a. -- This should pass as disjunction, because one of the tests passes.
@@ -413,7 +494,7 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kDisjunctionSizes[] = { 2u, 2u, 2u, 2u };
GetArrayAsVector(kDisjunction, kDisjunctionSizes, 4u, &tests);
- MatchAndCheck(tests, keys::kResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(tests, keys::kResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// 3.a. -- This should pass.
@@ -423,7 +504,8 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kNonExistentSizes[] = { arraysize(kNonExistent) };
GetArrayAsVector(kNonExistent, kNonExistentSizes, 1u, &tests);
- MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(
+ tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result);
EXPECT_TRUE(result);
// 3.b. -- This should fail.
@@ -433,7 +515,8 @@ TEST(WebRequestConditionAttributeTest, Headers) {
};
const size_t kExistingSize[] = { arraysize(kExisting) };
GetArrayAsVector(kExisting, kExistingSize, 1u, &tests);
- MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, &url_request, &result);
+ MatchAndCheck(
+ tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result);
EXPECT_FALSE(result);
}

Powered by Google App Engine
This is Rietveld 408576698