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 "extensions/browser/api/declarative_webrequest/webrequest_condition.h" | 5 #include "extensions/browser/api/declarative_webrequest/webrequest_condition.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/test/values_test_util.h" | 11 #include "base/test/values_test_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "components/url_matcher/url_matcher_constants.h" | 13 #include "components/url_matcher/url_matcher_constants.h" |
14 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 15 #include "content/public/common/previews_state.h" |
15 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 16 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
16 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
17 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
18 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 using content::ResourceType; | 22 using content::ResourceType; |
22 using url_matcher::URLMatcher; | 23 using url_matcher::URLMatcher; |
23 using url_matcher::URLMatcherConditionSet; | 24 using url_matcher::URLMatcherConditionSet; |
24 | 25 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 net::TestURLRequestContext context; | 82 net::TestURLRequestContext context; |
82 const GURL http_url("http://www.example.com"); | 83 const GURL http_url("http://www.example.com"); |
83 std::unique_ptr<net::URLRequest> match_request( | 84 std::unique_ptr<net::URLRequest> match_request( |
84 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); | 85 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
85 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); | 86 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); |
86 WebRequestDataWithMatchIds request_data(&data); | 87 WebRequestDataWithMatchIds request_data(&data); |
87 request_data.url_match_ids = matcher.MatchURL(http_url); | 88 request_data.url_match_ids = matcher.MatchURL(http_url); |
88 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 89 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
89 content::ResourceRequestInfo::AllocateForTesting( | 90 content::ResourceRequestInfo::AllocateForTesting( |
90 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, | 91 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, |
91 NULL, // context | 92 NULL, // context |
92 -1, // render_process_id | 93 -1, // render_process_id |
93 -1, // render_view_id | 94 -1, // render_view_id |
94 -1, // render_frame_id | 95 -1, // render_frame_id |
95 true, // is_main_frame | 96 true, // is_main_frame |
96 false, // parent_is_main_frame | 97 false, // parent_is_main_frame |
97 true, // allow_download | 98 true, // allow_download |
98 false, // is_async | 99 false, // is_async |
99 false); // is_using_lofi | 100 content::PREVIEWS_OFF); // previews_state |
100 EXPECT_TRUE(result->IsFulfilled(request_data)); | 101 EXPECT_TRUE(result->IsFulfilled(request_data)); |
101 | 102 |
102 const GURL https_url("https://www.example.com"); | 103 const GURL https_url("https://www.example.com"); |
103 std::unique_ptr<net::URLRequest> wrong_resource_type( | 104 std::unique_ptr<net::URLRequest> wrong_resource_type( |
104 context.CreateRequest(https_url, net::DEFAULT_PRIORITY, NULL)); | 105 context.CreateRequest(https_url, net::DEFAULT_PRIORITY, NULL)); |
105 data.request = wrong_resource_type.get(); | 106 data.request = wrong_resource_type.get(); |
106 request_data.url_match_ids = matcher.MatchURL(http_url); | 107 request_data.url_match_ids = matcher.MatchURL(http_url); |
107 // Make sure IsFulfilled does not fail because of URL matching. | 108 // Make sure IsFulfilled does not fail because of URL matching. |
108 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 109 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
109 content::ResourceRequestInfo::AllocateForTesting( | 110 content::ResourceRequestInfo::AllocateForTesting( |
110 wrong_resource_type.get(), content::RESOURCE_TYPE_SUB_FRAME, | 111 wrong_resource_type.get(), content::RESOURCE_TYPE_SUB_FRAME, |
111 NULL, // context | 112 NULL, // context |
112 -1, // render_process_id | 113 -1, // render_process_id |
113 -1, // render_view_id | 114 -1, // render_view_id |
114 -1, // render_frame_id | 115 -1, // render_frame_id |
115 false, // is_main_frame | 116 false, // is_main_frame |
116 false, // parent_is_main_frame | 117 false, // parent_is_main_frame |
117 true, // allow_download | 118 true, // allow_download |
118 false, // is_async | 119 false, // is_async |
119 false); // is_using_lofi | 120 content::PREVIEWS_OFF); // previews_state |
120 EXPECT_FALSE(result->IsFulfilled(request_data)); | 121 EXPECT_FALSE(result->IsFulfilled(request_data)); |
121 } | 122 } |
122 | 123 |
123 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { | 124 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { |
124 // Necessary for TestURLRequest. | 125 // Necessary for TestURLRequest. |
125 base::MessageLoopForIO message_loop; | 126 base::MessageLoopForIO message_loop; |
126 URLMatcher matcher; | 127 URLMatcher matcher; |
127 | 128 |
128 std::string error; | 129 std::string error; |
129 std::unique_ptr<WebRequestCondition> result; | 130 std::unique_ptr<WebRequestCondition> result; |
(...skipping 20 matching lines...) Expand all Loading... |
150 std::unique_ptr<net::URLRequest> match_request( | 151 std::unique_ptr<net::URLRequest> match_request( |
151 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); | 152 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
152 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); | 153 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); |
153 WebRequestDataWithMatchIds request_data(&data); | 154 WebRequestDataWithMatchIds request_data(&data); |
154 request_data.url_match_ids = matcher.MatchURL(http_url); | 155 request_data.url_match_ids = matcher.MatchURL(http_url); |
155 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 156 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
156 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); | 157 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); |
157 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); | 158 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); |
158 content::ResourceRequestInfo::AllocateForTesting( | 159 content::ResourceRequestInfo::AllocateForTesting( |
159 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, | 160 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, |
160 NULL, // context | 161 NULL, // context |
161 -1, // render_process_id | 162 -1, // render_process_id |
162 -1, // render_view_id | 163 -1, // render_view_id |
163 -1, // render_frame_id | 164 -1, // render_frame_id |
164 true, // is_main_frame | 165 true, // is_main_frame |
165 false, // parent_is_main_frame | 166 false, // parent_is_main_frame |
166 true, // allow_download | 167 true, // allow_download |
167 false, // is_async | 168 false, // is_async |
168 false); // is_using_lofi | 169 content::PREVIEWS_OFF); // previews_state |
169 EXPECT_TRUE(result->IsFulfilled(request_data)); | 170 EXPECT_TRUE(result->IsFulfilled(request_data)); |
170 } | 171 } |
171 | 172 |
172 // Conditions without UrlFilter attributes need to be independent of URL | 173 // Conditions without UrlFilter attributes need to be independent of URL |
173 // matching results. We test here that: | 174 // matching results. We test here that: |
174 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 175 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
175 // attributes are fulfilled. | 176 // attributes are fulfilled. |
176 // 2. An empty condition (in particular, without UrlFilter attributes) is | 177 // 2. An empty condition (in particular, without UrlFilter attributes) is |
177 // always fulfilled. | 178 // always fulfilled. |
178 TEST(WebRequestConditionTest, NoUrlAttributes) { | 179 TEST(WebRequestConditionTest, NoUrlAttributes) { |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 // filters. | 398 // filters. |
398 " \"requestHeaders\": [{}], \n" | 399 " \"requestHeaders\": [{}], \n" |
399 " \"responseHeaders\": [{}], \n" | 400 " \"responseHeaders\": [{}], \n" |
400 "}"), | 401 "}"), |
401 &error); | 402 &error); |
402 EXPECT_FALSE(error.empty()); | 403 EXPECT_FALSE(error.empty()); |
403 EXPECT_FALSE(result.get()); | 404 EXPECT_FALSE(result.get()); |
404 } | 405 } |
405 | 406 |
406 } // namespace extensions | 407 } // namespace extensions |
OLD | NEW |