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/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" |
12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
14 #include "net/base/request_priority.h" | 14 #include "net/base/request_priority.h" |
15 #include "net/test/spawned_test_server/spawned_test_server.h" | 15 #include "net/test/embedded_test_server/embedded_test_server.h" |
16 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using base::DictionaryValue; | 19 using base::DictionaryValue; |
20 using base::FundamentalValue; | 20 using base::FundamentalValue; |
21 using base::ListValue; | 21 using base::ListValue; |
22 using base::StringValue; | 22 using base::StringValue; |
23 using base::Value; | 23 using base::Value; |
24 | 24 |
25 namespace { | |
26 const char kUnknownConditionName[] = "unknownType"; | |
27 } // namespace | |
28 | |
29 namespace extensions { | 25 namespace extensions { |
30 | 26 |
31 namespace keys = declarative_webrequest_constants; | 27 namespace keys = declarative_webrequest_constants; |
32 | 28 |
29 namespace { | |
30 const char kUnknownConditionName[] = "unknownType"; | |
31 | |
32 base::FilePath TestDataPath(base::StringPiece relative_to_src) { | |
33 base::FilePath src_dir; | |
34 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); | |
35 return src_dir.AppendASCII(relative_to_src); | |
36 } | |
37 | |
33 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { | 38 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { |
34 // Necessary for TestURLRequest. | 39 // Necessary for TestURLRequest. |
35 base::MessageLoopForIO message_loop; | 40 base::MessageLoopForIO message_loop; |
36 | 41 |
37 std::string error; | 42 std::string error; |
38 scoped_refptr<const WebRequestConditionAttribute> result; | 43 scoped_refptr<const WebRequestConditionAttribute> result; |
39 base::StringValue string_value("main_frame"); | 44 base::StringValue string_value("main_frame"); |
40 base::ListValue resource_types; | 45 base::ListValue resource_types; |
41 resource_types.Append(new base::StringValue("main_frame")); | 46 resource_types.Append(new base::StringValue("main_frame")); |
42 | 47 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 ON_BEFORE_REQUEST))); | 109 ON_BEFORE_REQUEST))); |
105 } | 110 } |
106 | 111 |
107 TEST(WebRequestConditionAttributeTest, ContentType) { | 112 TEST(WebRequestConditionAttributeTest, ContentType) { |
108 // Necessary for TestURLRequest. | 113 // Necessary for TestURLRequest. |
109 base::MessageLoopForIO message_loop; | 114 base::MessageLoopForIO message_loop; |
110 | 115 |
111 std::string error; | 116 std::string error; |
112 scoped_refptr<const WebRequestConditionAttribute> result; | 117 scoped_refptr<const WebRequestConditionAttribute> result; |
113 | 118 |
114 net::SpawnedTestServer test_server( | 119 net::test_server::EmbeddedTestServer test_server; |
115 net::SpawnedTestServer::TYPE_HTTP, | 120 test_server.ServeFilesFromDirectory(TestDataPath( |
116 net::SpawnedTestServer::kLocalhost, | 121 "chrome/test/data/extensions/api_test/webrequest/declarative")); |
117 base::FilePath(FILE_PATH_LITERAL( | 122 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
118 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | |
119 ASSERT_TRUE(test_server.Start()); | |
120 | 123 |
121 net::TestURLRequestContext context; | 124 net::TestURLRequestContext context; |
122 net::TestDelegate delegate; | 125 net::TestDelegate delegate; |
123 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 126 net::TestURLRequest url_request(test_server.GetURL("/headers.html"), |
124 net::DEFAULT_PRIORITY, | 127 net::DEFAULT_PRIORITY, |
125 &delegate, | 128 &delegate, |
126 &context); | 129 &context); |
127 url_request.Start(); | 130 url_request.Start(); |
128 base::MessageLoop::current()->Run(); | 131 base::MessageLoop::current()->Run(); |
129 | 132 |
130 base::ListValue content_types; | 133 base::ListValue content_types; |
131 content_types.Append(new base::StringValue("text/plain")); | 134 content_types.Append(new base::StringValue("text/plain")); |
132 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 135 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
133 WebRequestConditionAttribute::Create( | 136 WebRequestConditionAttribute::Create( |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
493 } | 496 } |
494 | 497 |
495 // Here we test WebRequestConditionAttributeResponseHeaders for: | 498 // Here we test WebRequestConditionAttributeResponseHeaders for: |
496 // 1. Correct implementation of prefix/suffix/contains/equals matching. | 499 // 1. Correct implementation of prefix/suffix/contains/equals matching. |
497 // 2. Performing logical disjunction (||) between multiple specifications. | 500 // 2. Performing logical disjunction (||) between multiple specifications. |
498 // 3. Negating the match in case of 'doesNotContainHeaders'. | 501 // 3. Negating the match in case of 'doesNotContainHeaders'. |
499 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { | 502 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { |
500 // Necessary for TestURLRequest. | 503 // Necessary for TestURLRequest. |
501 base::MessageLoopForIO message_loop; | 504 base::MessageLoopForIO message_loop; |
502 | 505 |
503 net::SpawnedTestServer test_server( | 506 net::test_server::EmbeddedTestServer test_server; |
504 net::SpawnedTestServer::TYPE_HTTP, | 507 test_server.ServeFilesFromDirectory(TestDataPath( |
505 net::SpawnedTestServer::kLocalhost, | 508 "chrome/test/data/extensions/api_test/webrequest/declarative")); |
506 base::FilePath(FILE_PATH_LITERAL( | 509 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
507 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | |
508 ASSERT_TRUE(test_server.Start()); | |
509 | 510 |
510 net::TestURLRequestContext context; | 511 net::TestURLRequestContext context; |
511 net::TestDelegate delegate; | 512 net::TestDelegate delegate; |
512 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 513 net::TestURLRequest url_request(test_server.GetURL("/headers.html"), |
513 net::DEFAULT_PRIORITY, | 514 net::DEFAULT_PRIORITY, |
514 &delegate, | 515 &delegate, |
515 &context); | 516 &context); |
516 url_request.Start(); | 517 url_request.Start(); |
517 base::MessageLoop::current()->Run(); | 518 base::MessageLoop::current()->Run(); |
518 | 519 |
519 // In all the tests below we assume that the server includes the headers | 520 // In all the tests below we assume that the server includes the headers |
520 // Custom-Header: custom/value | 521 // Custom-Header: custom/value |
521 // Custom-Header-B: valueA | 522 // Custom-Header-B: valueA |
522 // Custom-Header-B: valueB | 523 // Custom-Header-B: valueB |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
673 keys::kNameEqualsKey, "custom-header-b", | 674 keys::kNameEqualsKey, "custom-header-b", |
674 keys::kValueEqualsKey, "valueB" | 675 keys::kValueEqualsKey, "valueB" |
675 }; | 676 }; |
676 const size_t kExistingSize[] = { arraysize(kExisting) }; | 677 const size_t kExistingSize[] = { arraysize(kExisting) }; |
677 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 678 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
678 MatchAndCheck( | 679 MatchAndCheck( |
679 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 680 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
680 EXPECT_FALSE(result); | 681 EXPECT_FALSE(result); |
681 } | 682 } |
682 | 683 |
684 } // namespace | |
vabr (Chromium)
2014/02/04 08:19:12
Did you put the whole test into anonymous namespac
Jeffrey Yasskin
2014/02/04 08:27:17
Yeah. It's nice to do that because it ensures that
| |
683 } // namespace extensions | 685 } // namespace extensions |
OLD | NEW |