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

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc

Issue 9820003: Implementation of beginning of Declarative Web Request API backend (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h"
6
7 #include <set>
8
9 #include "base/message_loop.h"
10 #include "base/values.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15 const char kRequestMatcher[] = "experimental.webRequest.RequestMatcher";
16 const char kInstanceType[] = "instanceType";
17 }
18
19 namespace extensions {
20 namespace declarative_webrequest {
21
22 TEST(WebRequestConditionFactoryTest, CreateCondition) {
23 // Necessary for TestURLRequest.
24 MessageLoop message_loop(MessageLoop::TYPE_IO);
25 URLMatcher matcher;
26
27 std::string error;
28 scoped_ptr<WebRequestCondition> result;
29
30 DictionaryValue invalid_condition;
31 invalid_condition.SetString("invalid", "foobar");
32 invalid_condition.SetString("host_suffix", "example.com");
33 invalid_condition.SetString(kInstanceType, kRequestMatcher);
34
35 DictionaryValue invalid_condition2;
36 invalid_condition2.Set("host_suffix", new ListValue);
37 invalid_condition2.SetString(kInstanceType, kRequestMatcher);
38
39 DictionaryValue valid_condition;
40 valid_condition.SetString("scheme", "http");
41 valid_condition.SetString("host_suffix", "example.com");
42 valid_condition.SetString(kInstanceType, kRequestMatcher);
43
44 // Test wrong condition name passed.
45 error.clear();
46 result = WebRequestConditionFactory::CreateCondition(
47 matcher.condition_factory(), invalid_condition, &error);
48 EXPECT_FALSE(error.empty());
49 EXPECT_FALSE(result.get());
50
51 // Test wrong datatype in host_suffix.
52 error.clear();
53 result = WebRequestConditionFactory::CreateCondition(
54 matcher.condition_factory(), invalid_condition2, &error);
55 EXPECT_FALSE(error.empty());
56 EXPECT_FALSE(result.get());
57
58 // Test success (can we support multiple criteria?)
59 error.clear();
60 result = WebRequestConditionFactory::CreateCondition(
61 matcher.condition_factory(), valid_condition, &error);
62 EXPECT_TRUE(error.empty());
63 ASSERT_TRUE(result.get());
64
65 // Tell the URLMatcher about our shiny new patterns.
66 std::vector<URLMatcherConditionSet> url_matcher_condition_set;
67 result->AppendURLMatcherConditionSet(&url_matcher_condition_set);
68 matcher.AddConditionSets(url_matcher_condition_set);
69
70 URLMatcherConditionSet::ID url_matcher_id =
71 result->url_matcher_condition_set_id();
72
73 TestURLRequest match_request(GURL("http://www.example.com"), NULL);
74 EXPECT_TRUE(result->IsFulfilled(url_matcher_id, &match_request));
75
76 EXPECT_FALSE(result->IsFulfilled(url_matcher_id + 1, &match_request));
77
78 TestURLRequest wrong_scheme(GURL("https://www.example.com"), NULL);
79 EXPECT_FALSE(result->IsFulfilled(url_matcher_id, &wrong_scheme));
80 }
81
82 TEST(WebRequestConditionFactoryTest, CreateConditionCollection) {
83 // Necessary for TestURLRequest.
84 MessageLoop message_loop(MessageLoop::TYPE_IO);
85 URLMatcher matcher;
86
87 DictionaryValue http_condition;
88 http_condition.SetString("scheme", "http");
89 http_condition.SetString("host_suffix", "example.com");
90 http_condition.SetString(kInstanceType, kRequestMatcher);
91
92 DictionaryValue https_condition;
93 https_condition.SetString("scheme", "https");
94 https_condition.SetString("host_suffix", "example.com");
95 https_condition.SetString("host_prefix", "www");
96 https_condition.SetString(kInstanceType, kRequestMatcher);
97
98 WebRequestConditionFactory::AnyVector conditions;
99
100 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr(
101 new json_schema_compiler::any::Any);
102 condition1->Init(http_condition);
103 conditions.push_back(condition1);
104
105 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr(
106 new json_schema_compiler::any::Any);
107 condition2->Init(https_condition);
108 conditions.push_back(condition2);
109
110 // Test insertion
111 std::string error;
112 scoped_ptr<WebRequestConditionCollection> result =
113 WebRequestConditionFactory::CreateConditionCollection(
114 matcher.condition_factory(), conditions, &error);
115 EXPECT_TRUE(error.empty());
116 ASSERT_TRUE(result.get());
117 EXPECT_EQ(2u, result->conditions().size());
118
119 // Tell the URLMatcher about our shiny new patterns.
120 std::vector<URLMatcherConditionSet> url_matcher_condition_set;
121 result->AppendURLMatcherConditionSets(&url_matcher_condition_set);
122 matcher.AddConditionSets(url_matcher_condition_set);
123
124 std::set<URLMatcherConditionSet::ID> url_match_ids;
125
126 // Test that the result is correct and matches http://www.example.com and
127 // https://www.example.com
128 GURL http_url("http://www.example.com");
129 TestURLRequest http_request(http_url, NULL);
130 url_match_ids = matcher.MatchURL(http_url);
131 EXPECT_TRUE(result->IsFulfilled(url_match_ids, &http_request));
132
133 GURL https_url("https://www.example.com");
134 url_match_ids = matcher.MatchURL(https_url);
135 TestURLRequest https_request(https_url, NULL);
136 EXPECT_TRUE(result->IsFulfilled(url_match_ids, &https_request));
137
138 // Check that both, host_prefix and host_suffix are evaluated.
139 GURL https_foo_url("https://foo.example.com");
140 url_match_ids = matcher.MatchURL(https_foo_url);
141 TestURLRequest https_foo_request(https_foo_url, NULL);
142 EXPECT_FALSE(result->IsFulfilled(url_match_ids, &https_foo_request));
143 }
144
145 // This tests that an internal array of all supported path condition attributes
146 // remains sorted. This is crucial so that we can execute a binary search on the
147 // array.
148 TEST(WebRequestConditionFactoryTest, GetAllPathAttributesSorted) {
149 std::vector<std::string> array =
150 WebRequestConditionFactory::GetAllPathAttributes();
151 std::vector<std::string> sorted_array = array;
152 std::sort(sorted_array.begin(), sorted_array.end());
153 EXPECT_EQ(sorted_array, array);
154 }
155
156 } // namespace declarative_webrequest
157 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698