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

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: Pacify clang 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
21 TEST(WebRequestConditionTest, CreateCondition) {
22 // Necessary for TestURLRequest.
23 MessageLoop message_loop(MessageLoop::TYPE_IO);
24 URLMatcher matcher;
25
26 std::string error;
27 scoped_ptr<WebRequestCondition> result;
28
29 DictionaryValue invalid_condition;
30 invalid_condition.SetString("invalid", "foobar");
31 invalid_condition.SetString("host_suffix", "example.com");
32 invalid_condition.SetString(kInstanceType, kRequestMatcher);
33
34 DictionaryValue invalid_condition2;
35 invalid_condition2.Set("host_suffix", new ListValue);
36 invalid_condition2.SetString(kInstanceType, kRequestMatcher);
37
38 DictionaryValue valid_condition;
39 valid_condition.SetString("scheme", "http");
40 valid_condition.SetString("host_suffix", "example.com");
41 valid_condition.SetString(kInstanceType, kRequestMatcher);
42
43 // Test wrong condition name passed.
44 error.clear();
45 result = WebRequestCondition::Create(matcher.condition_factory(),
46 invalid_condition, &error);
47 EXPECT_FALSE(error.empty());
48 EXPECT_FALSE(result.get());
49
50 // Test wrong datatype in host_suffix.
51 error.clear();
52 result = WebRequestCondition::Create(matcher.condition_factory(),
53 invalid_condition2, &error);
54 EXPECT_FALSE(error.empty());
55 EXPECT_FALSE(result.get());
56
57 // Test success (can we support multiple criteria?)
58 error.clear();
59 result = WebRequestCondition::Create(matcher.condition_factory(),
60 valid_condition, &error);
61 EXPECT_TRUE(error.empty());
62 ASSERT_TRUE(result.get());
63
64 TestURLRequest match_request(GURL("http://www.example.com"), NULL);
65 EXPECT_TRUE(result->IsFulfilled(&match_request));
66
67 TestURLRequest wrong_scheme(GURL("https://www.example.com"), NULL);
68 EXPECT_FALSE(result->IsFulfilled(&wrong_scheme));
69 }
70
71 TEST(WebRequestConditionTest, CreateConditionSet) {
72 // Necessary for TestURLRequest.
73 MessageLoop message_loop(MessageLoop::TYPE_IO);
74 URLMatcher matcher;
75
76 DictionaryValue http_condition;
77 http_condition.SetString("scheme", "http");
78 http_condition.SetString("host_suffix", "example.com");
79 http_condition.SetString(kInstanceType, kRequestMatcher);
80
81 DictionaryValue https_condition;
82 https_condition.SetString("scheme", "https");
83 https_condition.SetString("host_suffix", "example.com");
84 https_condition.SetString("host_prefix", "www");
85 https_condition.SetString(kInstanceType, kRequestMatcher);
86
87 WebRequestConditionSet::AnyVector conditions;
88
89 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr(
90 new json_schema_compiler::any::Any);
91 condition1->Init(http_condition);
92 conditions.push_back(condition1);
93
94 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr(
95 new json_schema_compiler::any::Any);
96 condition2->Init(https_condition);
97 conditions.push_back(condition2);
98
99 // Test insertion
100 std::string error;
101 scoped_ptr<WebRequestConditionSet> result =
102 WebRequestConditionSet::Create(matcher.condition_factory(),
103 conditions, &error);
104 EXPECT_TRUE(error.empty());
105 ASSERT_TRUE(result.get());
106 EXPECT_EQ(2u, result->conditions().size());
107
108 // Tell the URLMatcher about our shiny new patterns.
109 std::vector<URLMatcherConditionSet> url_matcher_condition_set;
110 result->GetURLMatcherConditionSets(&url_matcher_condition_set);
111 matcher.AddConditionSets(url_matcher_condition_set);
112
113 std::set<URLMatcherConditionSet::ID> url_match_ids;
114 int number_matches = 0;
115
116 // Test that the result is correct and matches http://www.example.com and
117 // https://www.example.com
118 GURL http_url("http://www.example.com");
119 TestURLRequest http_request(http_url, NULL);
120 url_match_ids = matcher.MatchURL(http_url);
121 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin();
122 i != url_match_ids.end(); ++i) {
123 if (result->IsFulfilled(*i, &http_request))
124 ++number_matches;
125 }
126 EXPECT_EQ(1, number_matches);
127
128 GURL https_url("https://www.example.com");
129 url_match_ids = matcher.MatchURL(https_url);
130 TestURLRequest https_request(https_url, NULL);
131 number_matches = 0;
132 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin();
133 i != url_match_ids.end(); ++i) {
134 if (result->IsFulfilled(*i, &https_request))
135 ++number_matches;
136 }
137 EXPECT_EQ(1, number_matches);
138
139 // Check that both, host_prefix and host_suffix are evaluated.
140 GURL https_foo_url("https://foo.example.com");
141 url_match_ids = matcher.MatchURL(https_foo_url);
142 TestURLRequest https_foo_request(https_foo_url, NULL);
143 number_matches = 0;
144 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin();
145 i != url_match_ids.end(); ++i) {
146 if (result->IsFulfilled(*i, &https_foo_request))
147 ++number_matches;
148 }
149 EXPECT_EQ(0, number_matches);
150 }
151
152 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698