Index: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc |
diff --git a/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..07e6e1ecc0c92114a647f79e88b61d0b30303ded |
--- /dev/null |
+++ b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc |
@@ -0,0 +1,157 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.h" |
+ |
+#include <set> |
+ |
+#include "base/message_loop.h" |
+#include "base/values.h" |
+#include "net/url_request/url_request_test_util.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+const char kRequestMatcher[] = "experimental.webRequest.RequestMatcher"; |
+const char kInstanceType[] = "instanceType"; |
+} |
+ |
+namespace extensions { |
+namespace declarative_webrequest { |
+ |
+TEST(WebRequestConditionFactoryTest, CreateCondition) { |
+ // Necessary for TestURLRequest. |
+ MessageLoop message_loop(MessageLoop::TYPE_IO); |
+ URLMatcher matcher; |
+ |
+ std::string error; |
+ scoped_ptr<WebRequestCondition> result; |
+ |
+ DictionaryValue invalid_condition; |
+ invalid_condition.SetString("invalid", "foobar"); |
+ invalid_condition.SetString("host_suffix", "example.com"); |
+ invalid_condition.SetString(kInstanceType, kRequestMatcher); |
+ |
+ DictionaryValue invalid_condition2; |
+ invalid_condition2.Set("host_suffix", new ListValue); |
+ invalid_condition2.SetString(kInstanceType, kRequestMatcher); |
+ |
+ DictionaryValue valid_condition; |
+ valid_condition.SetString("scheme", "http"); |
+ valid_condition.SetString("host_suffix", "example.com"); |
+ valid_condition.SetString(kInstanceType, kRequestMatcher); |
+ |
+ // Test wrong condition name passed. |
+ error.clear(); |
+ result = WebRequestConditionFactory::CreateCondition( |
+ matcher.condition_factory(), invalid_condition, &error); |
+ EXPECT_FALSE(error.empty()); |
+ EXPECT_FALSE(result.get()); |
+ |
+ // Test wrong datatype in host_suffix. |
+ error.clear(); |
+ result = WebRequestConditionFactory::CreateCondition( |
+ matcher.condition_factory(), invalid_condition2, &error); |
+ EXPECT_FALSE(error.empty()); |
+ EXPECT_FALSE(result.get()); |
+ |
+ // Test success (can we support multiple criteria?) |
+ error.clear(); |
+ result = WebRequestConditionFactory::CreateCondition( |
+ matcher.condition_factory(), valid_condition, &error); |
+ EXPECT_TRUE(error.empty()); |
+ ASSERT_TRUE(result.get()); |
+ |
+ // Tell the URLMatcher about our shiny new patterns. |
+ std::vector<URLMatcherConditionSet> url_matcher_condition_set; |
+ result->AppendURLMatcherConditionSet(&url_matcher_condition_set); |
+ matcher.AddConditionSets(url_matcher_condition_set); |
+ |
+ URLMatcherConditionSet::ID url_matcher_id = |
+ result->url_matcher_condition_set_id(); |
+ |
+ TestURLRequest match_request(GURL("http://www.example.com"), NULL); |
+ EXPECT_TRUE(result->IsFulfilled(url_matcher_id, &match_request)); |
+ |
+ EXPECT_FALSE(result->IsFulfilled(url_matcher_id + 1, &match_request)); |
+ |
+ TestURLRequest wrong_scheme(GURL("https://www.example.com"), NULL); |
+ EXPECT_FALSE(result->IsFulfilled(url_matcher_id, &wrong_scheme)); |
+} |
+ |
+TEST(WebRequestConditionFactoryTest, CreateConditionCollection) { |
+ // Necessary for TestURLRequest. |
+ MessageLoop message_loop(MessageLoop::TYPE_IO); |
+ URLMatcher matcher; |
+ |
+ DictionaryValue http_condition; |
+ http_condition.SetString("scheme", "http"); |
+ http_condition.SetString("host_suffix", "example.com"); |
+ http_condition.SetString(kInstanceType, kRequestMatcher); |
+ |
+ DictionaryValue https_condition; |
+ https_condition.SetString("scheme", "https"); |
+ https_condition.SetString("host_suffix", "example.com"); |
+ https_condition.SetString("host_prefix", "www"); |
+ https_condition.SetString(kInstanceType, kRequestMatcher); |
+ |
+ WebRequestConditionFactory::AnyVector conditions; |
+ |
+ linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
+ new json_schema_compiler::any::Any); |
+ condition1->Init(http_condition); |
+ conditions.push_back(condition1); |
+ |
+ linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( |
+ new json_schema_compiler::any::Any); |
+ condition2->Init(https_condition); |
+ conditions.push_back(condition2); |
+ |
+ // Test insertion |
+ std::string error; |
+ scoped_ptr<WebRequestConditionCollection> result = |
+ WebRequestConditionFactory::CreateConditionCollection( |
+ matcher.condition_factory(), conditions, &error); |
+ EXPECT_TRUE(error.empty()); |
+ ASSERT_TRUE(result.get()); |
+ EXPECT_EQ(2u, result->conditions().size()); |
+ |
+ // Tell the URLMatcher about our shiny new patterns. |
+ std::vector<URLMatcherConditionSet> url_matcher_condition_set; |
+ result->AppendURLMatcherConditionSets(&url_matcher_condition_set); |
+ matcher.AddConditionSets(url_matcher_condition_set); |
+ |
+ std::set<URLMatcherConditionSet::ID> url_match_ids; |
+ |
+ // Test that the result is correct and matches http://www.example.com and |
+ // https://www.example.com |
+ GURL http_url("http://www.example.com"); |
+ TestURLRequest http_request(http_url, NULL); |
+ url_match_ids = matcher.MatchURL(http_url); |
+ EXPECT_TRUE(result->IsFulfilled(url_match_ids, &http_request)); |
+ |
+ GURL https_url("https://www.example.com"); |
+ url_match_ids = matcher.MatchURL(https_url); |
+ TestURLRequest https_request(https_url, NULL); |
+ EXPECT_TRUE(result->IsFulfilled(url_match_ids, &https_request)); |
+ |
+ // Check that both, host_prefix and host_suffix are evaluated. |
+ GURL https_foo_url("https://foo.example.com"); |
+ url_match_ids = matcher.MatchURL(https_foo_url); |
+ TestURLRequest https_foo_request(https_foo_url, NULL); |
+ EXPECT_FALSE(result->IsFulfilled(url_match_ids, &https_foo_request)); |
+} |
+ |
+// This tests that an internal array of all supported path condition attributes |
+// remains sorted. This is crucial so that we can execute a binary search on the |
+// array. |
+TEST(WebRequestConditionFactoryTest, GetAllPathAttributesSorted) { |
+ std::vector<std::string> array = |
+ WebRequestConditionFactory::GetAllPathAttributes(); |
+ std::vector<std::string> sorted_array = array; |
+ std::sort(sorted_array.begin(), sorted_array.end()); |
+ EXPECT_EQ(sorted_array, array); |
+} |
+ |
+} // namespace declarative_webrequest |
+} // namespace extensions |