Chromium Code Reviews| 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 "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/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "base/values.h" | 9 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
| 10 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " |
| 11 #include "content/public/browser/resource_request_info.h" | 12 #include "content/public/browser/resource_request_info.h" |
| 12 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 14 #include "net/test/test_server.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 const char kUnknownConditionName[] = "unknownType"; | 18 const char kUnknownConditionName[] = "unknownType"; |
| 17 } // namespace | 19 } // namespace |
| 18 | 20 |
| 19 namespace extensions { | 21 namespace extensions { |
| 20 | 22 |
| 21 namespace keys = declarative_webrequest_constants; | 23 namespace keys = declarative_webrequest_constants; |
| 22 | 24 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 37 EXPECT_FALSE(error.empty()); | 39 EXPECT_FALSE(error.empty()); |
| 38 EXPECT_FALSE(result.get()); | 40 EXPECT_FALSE(result.get()); |
| 39 | 41 |
| 40 // Test wrong data type passed | 42 // Test wrong data type passed |
| 41 error.clear(); | 43 error.clear(); |
| 42 result = WebRequestConditionAttribute::Create( | 44 result = WebRequestConditionAttribute::Create( |
| 43 keys::kResourceTypeKey, &string_value, &error); | 45 keys::kResourceTypeKey, &string_value, &error); |
| 44 EXPECT_FALSE(error.empty()); | 46 EXPECT_FALSE(error.empty()); |
| 45 EXPECT_FALSE(result.get()); | 47 EXPECT_FALSE(result.get()); |
| 46 | 48 |
| 49 // TODO(yoz): another one for content type | |
|
battre
2012/08/03 11:37:04
want to add that to this CL? should not be a lot o
Yoyo Zhou
2012/08/03 11:48:44
Done; I think I meant to.
| |
| 50 | |
| 47 // Test success | 51 // Test success |
| 48 error.clear(); | 52 error.clear(); |
| 49 result = WebRequestConditionAttribute::Create( | 53 result = WebRequestConditionAttribute::Create( |
| 50 keys::kResourceTypeKey, &resource_types, &error); | 54 keys::kResourceTypeKey, &resource_types, &error); |
| 51 EXPECT_EQ("", error); | 55 EXPECT_EQ("", error); |
| 52 ASSERT_TRUE(result.get()); | 56 ASSERT_TRUE(result.get()); |
| 53 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, | 57 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, |
| 54 result->GetType()); | 58 result->GetType()); |
| 55 } | 59 } |
| 56 | 60 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 76 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST))); | 80 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST))); |
| 77 | 81 |
| 78 TestURLRequest url_request_fail( | 82 TestURLRequest url_request_fail( |
| 79 GURL("http://www.example.com"), NULL, &context); | 83 GURL("http://www.example.com"), NULL, &context); |
| 80 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, | 84 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, |
| 81 ResourceType::SUB_FRAME, NULL, -1, -1); | 85 ResourceType::SUB_FRAME, NULL, -1, -1); |
| 82 EXPECT_FALSE(attribute->IsFulfilled( | 86 EXPECT_FALSE(attribute->IsFulfilled( |
| 83 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST))); | 87 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST))); |
| 84 } | 88 } |
| 85 | 89 |
| 90 TEST(WebRequestConditionAttributeTest, TestContentType) { | |
| 91 // Necessary for TestURLRequest. | |
| 92 MessageLoop message_loop(MessageLoop::TYPE_IO); | |
| 93 | |
| 94 std::string error; | |
| 95 scoped_ptr<WebRequestConditionAttribute> result; | |
| 96 | |
| 97 net::TestServer test_server( | |
| 98 net::TestServer::TYPE_HTTP, | |
| 99 net::TestServer::kLocalhost, | |
| 100 FilePath(FILE_PATH_LITERAL( | |
| 101 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | |
| 102 ASSERT_TRUE(test_server.Start()); | |
| 103 | |
| 104 TestURLRequestContext context; | |
| 105 TestDelegate delegate; | |
| 106 TestURLRequest url_request(test_server.GetURL("headers.html"), | |
| 107 &delegate, &context); | |
| 108 url_request.Start(); | |
| 109 MessageLoop::current()->Run(); | |
| 110 | |
| 111 ListValue content_types; | |
| 112 content_types.Append(Value::CreateStringValue("text/html")); | |
| 113 scoped_ptr<WebRequestConditionAttribute> attribute_ok = | |
| 114 WebRequestConditionAttribute::Create( | |
| 115 keys::kContentTypeKey, &content_types, &error); | |
| 116 EXPECT_EQ("", error); | |
| 117 ASSERT_TRUE(attribute_ok.get()); | |
| 118 | |
| 119 EXPECT_FALSE(attribute_ok->IsFulfilled( | |
| 120 WebRequestRule::RequestData(&url_request, ON_BEFORE_REQUEST, | |
| 121 url_request.response_headers()))); | |
| 122 EXPECT_TRUE(attribute_ok->IsFulfilled( | |
| 123 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, | |
| 124 url_request.response_headers()))); | |
| 125 | |
| 126 content_types.Clear(); | |
| 127 content_types.Append(Value::CreateStringValue("something/invalid")); | |
| 128 scoped_ptr<WebRequestConditionAttribute> attribute_fail = | |
| 129 WebRequestConditionAttribute::Create( | |
| 130 keys::kContentTypeKey, &content_types, &error); | |
| 131 EXPECT_EQ("", error); | |
| 132 ASSERT_TRUE(attribute_fail.get()); | |
| 133 | |
| 134 EXPECT_FALSE(attribute_fail->IsFulfilled( | |
| 135 WebRequestRule::RequestData(&url_request, ON_HEADERS_RECEIVED, | |
| 136 url_request.response_headers()))); | |
| 137 } | |
| 138 | |
| 86 } // namespace extensions | 139 } // namespace extensions |
| OLD | NEW |