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

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

Issue 10825212: Support also excluding content types in declarative webrequest conditions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ' Created 8 years, 4 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
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 "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h" 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" 10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 // Test success 55 // Test success
56 error.clear(); 56 error.clear();
57 result = WebRequestConditionAttribute::Create( 57 result = WebRequestConditionAttribute::Create(
58 keys::kResourceTypeKey, &resource_types, &error); 58 keys::kResourceTypeKey, &resource_types, &error);
59 EXPECT_EQ("", error); 59 EXPECT_EQ("", error);
60 ASSERT_TRUE(result.get()); 60 ASSERT_TRUE(result.get());
61 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, 61 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE,
62 result->GetType()); 62 result->GetType());
63 } 63 }
64 64
65 TEST(WebRequestConditionAttributeTest, TestResourceType) { 65 TEST(WebRequestConditionAttributeTest, ResourceType) {
66 // Necessary for TestURLRequest. 66 // Necessary for TestURLRequest.
67 MessageLoop message_loop(MessageLoop::TYPE_IO); 67 MessageLoop message_loop(MessageLoop::TYPE_IO);
68 68
69 std::string error; 69 std::string error;
70 ListValue resource_types; 70 ListValue resource_types;
71 resource_types.Append(Value::CreateStringValue("main_frame")); 71 resource_types.Append(Value::CreateStringValue("main_frame"));
72 72
73 scoped_ptr<WebRequestConditionAttribute> attribute = 73 scoped_ptr<WebRequestConditionAttribute> attribute =
74 WebRequestConditionAttribute::Create( 74 WebRequestConditionAttribute::Create(
75 keys::kResourceTypeKey, &resource_types, &error); 75 keys::kResourceTypeKey, &resource_types, &error);
76 EXPECT_EQ("", error); 76 EXPECT_EQ("", error);
77 ASSERT_TRUE(attribute.get()); 77 ASSERT_TRUE(attribute.get());
78 78
79 TestURLRequestContext context; 79 TestURLRequestContext context;
80 TestURLRequest url_request_ok(GURL("http://www.example.com"), NULL, &context); 80 TestURLRequest url_request_ok(GURL("http://www.example.com"), NULL, &context);
81 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, 81 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok,
82 ResourceType::MAIN_FRAME, NULL, -1, -1); 82 ResourceType::MAIN_FRAME, NULL, -1, -1);
83 EXPECT_TRUE(attribute->IsFulfilled( 83 EXPECT_TRUE(attribute->IsFulfilled(
84 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST))); 84 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST)));
85 85
86 TestURLRequest url_request_fail( 86 TestURLRequest url_request_fail(
87 GURL("http://www.example.com"), NULL, &context); 87 GURL("http://www.example.com"), NULL, &context);
88 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, 88 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok,
89 ResourceType::SUB_FRAME, NULL, -1, -1); 89 ResourceType::SUB_FRAME, NULL, -1, -1);
90 EXPECT_FALSE(attribute->IsFulfilled( 90 EXPECT_FALSE(attribute->IsFulfilled(
91 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST))); 91 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST)));
92 } 92 }
93 93
94 TEST(WebRequestConditionAttributeTest, TestContentType) { 94 TEST(WebRequestConditionAttributeTest, ContentType) {
95 // Necessary for TestURLRequest. 95 // Necessary for TestURLRequest.
96 MessageLoop message_loop(MessageLoop::TYPE_IO); 96 MessageLoop message_loop(MessageLoop::TYPE_IO);
97 97
98 std::string error; 98 std::string error;
99 scoped_ptr<WebRequestConditionAttribute> result; 99 scoped_ptr<WebRequestConditionAttribute> result;
100 100
101 net::TestServer test_server( 101 net::TestServer test_server(
102 net::TestServer::TYPE_HTTP, 102 net::TestServer::TYPE_HTTP,
103 net::TestServer::kLocalhost, 103 net::TestServer::kLocalhost,
104 FilePath(FILE_PATH_LITERAL( 104 FilePath(FILE_PATH_LITERAL(
105 "chrome/test/data/extensions/api_test/webrequest/declarative"))); 105 "chrome/test/data/extensions/api_test/webrequest/declarative")));
106 ASSERT_TRUE(test_server.Start()); 106 ASSERT_TRUE(test_server.Start());
107 107
108 TestURLRequestContext context; 108 TestURLRequestContext context;
109 TestDelegate delegate; 109 TestDelegate delegate;
110 TestURLRequest url_request(test_server.GetURL("headers.html"), 110 TestURLRequest url_request(test_server.GetURL("headers.html"),
111 &delegate, &context); 111 &delegate, &context);
112 url_request.Start(); 112 url_request.Start();
113 MessageLoop::current()->Run(); 113 MessageLoop::current()->Run();
114 114
115 ListValue content_types; 115 ListValue content_types;
116 content_types.Append(Value::CreateStringValue("text/html")); 116 content_types.Append(Value::CreateStringValue("text/html"));
117 scoped_ptr<WebRequestConditionAttribute> attribute_ok = 117 scoped_ptr<WebRequestConditionAttribute> attribute_include =
118 WebRequestConditionAttribute::Create( 118 WebRequestConditionAttribute::Create(
119 keys::kContentTypeKey, &content_types, &error); 119 keys::kContentTypeKey, &content_types, &error);
120 EXPECT_EQ("", error); 120 EXPECT_EQ("", error);
121 ASSERT_TRUE(attribute_ok.get()); 121 ASSERT_TRUE(attribute_include.get());
122 122 EXPECT_FALSE(attribute_include->IsFulfilled(
123 EXPECT_FALSE(attribute_ok->IsFulfilled(
124 WebRequestRule::RequestData(&url_request, ON_BEFORE_REQUEST, 123 WebRequestRule::RequestData(&url_request, ON_BEFORE_REQUEST,
125 url_request.response_headers()))); 124 url_request.response_headers())));
126 EXPECT_TRUE(attribute_ok->IsFulfilled( 125 EXPECT_TRUE(attribute_include->IsFulfilled(
126 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED,
127 url_request.response_headers())));
128
129 scoped_ptr<WebRequestConditionAttribute> attribute_exclude =
130 WebRequestConditionAttribute::Create(
131 keys::kExcludeContentTypeKey, &content_types, &error);
132 EXPECT_EQ("", error);
133 ASSERT_TRUE(attribute_exclude.get());
134 EXPECT_FALSE(attribute_exclude->IsFulfilled(
127 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, 135 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED,
128 url_request.response_headers()))); 136 url_request.response_headers())));
129 137
130 content_types.Clear(); 138 content_types.Clear();
131 content_types.Append(Value::CreateStringValue("something/invalid")); 139 content_types.Append(Value::CreateStringValue("something/invalid"));
132 scoped_ptr<WebRequestConditionAttribute> attribute_fail = 140 scoped_ptr<WebRequestConditionAttribute> attribute_unincluded =
133 WebRequestConditionAttribute::Create( 141 WebRequestConditionAttribute::Create(
134 keys::kContentTypeKey, &content_types, &error); 142 keys::kContentTypeKey, &content_types, &error);
135 EXPECT_EQ("", error); 143 EXPECT_EQ("", error);
136 ASSERT_TRUE(attribute_fail.get()); 144 ASSERT_TRUE(attribute_unincluded.get());
145 EXPECT_FALSE(attribute_unincluded->IsFulfilled(
146 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED,
147 url_request.response_headers())));
137 148
138 EXPECT_FALSE(attribute_fail->IsFulfilled( 149 scoped_ptr<WebRequestConditionAttribute> attribute_unexcluded =
150 WebRequestConditionAttribute::Create(
151 keys::kExcludeContentTypeKey, &content_types, &error);
152 EXPECT_EQ("", error);
153 ASSERT_TRUE(attribute_unexcluded.get());
154 EXPECT_TRUE(attribute_unexcluded->IsFulfilled(
139 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, 155 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED,
140 url_request.response_headers()))); 156 url_request.response_headers())));
141 } 157 }
142 158
143 } // namespace extensions 159 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698