| 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/test/spawned_test_server/spawned_test_server.h" | 15 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 using base::DictionaryValue; | 19 using base::DictionaryValue; |
| 19 using base::FundamentalValue; | 20 using base::FundamentalValue; |
| 20 using base::ListValue; | 21 using base::ListValue; |
| 21 using base::StringValue; | 22 using base::StringValue; |
| 22 using base::Value; | 23 using base::Value; |
| 23 | 24 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 83 |
| 83 scoped_refptr<const WebRequestConditionAttribute> attribute = | 84 scoped_refptr<const WebRequestConditionAttribute> attribute = |
| 84 WebRequestConditionAttribute::Create( | 85 WebRequestConditionAttribute::Create( |
| 85 keys::kResourceTypeKey, &resource_types, &error); | 86 keys::kResourceTypeKey, &resource_types, &error); |
| 86 EXPECT_EQ("", error); | 87 EXPECT_EQ("", error); |
| 87 ASSERT_TRUE(attribute.get()); | 88 ASSERT_TRUE(attribute.get()); |
| 88 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); | 89 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); |
| 89 | 90 |
| 90 net::TestURLRequestContext context; | 91 net::TestURLRequestContext context; |
| 91 net::TestURLRequest url_request_ok( | 92 net::TestURLRequest url_request_ok( |
| 92 GURL("http://www.example.com"), NULL, &context, NULL); | 93 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, &context); |
| 93 content::ResourceRequestInfo::AllocateForTesting( | 94 content::ResourceRequestInfo::AllocateForTesting( |
| 94 &url_request_ok, ResourceType::SUB_FRAME, NULL, -1, -1, false); | 95 &url_request_ok, ResourceType::SUB_FRAME, NULL, -1, -1, false); |
| 95 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(&url_request_ok, | 96 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(&url_request_ok, |
| 96 ON_BEFORE_REQUEST))); | 97 ON_BEFORE_REQUEST))); |
| 97 | 98 |
| 98 net::TestURLRequest url_request_fail( | 99 net::TestURLRequest url_request_fail( |
| 99 GURL("http://www.example.com"), NULL, &context, NULL); | 100 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, &context); |
| 100 content::ResourceRequestInfo::AllocateForTesting( | 101 content::ResourceRequestInfo::AllocateForTesting( |
| 101 &url_request_fail, ResourceType::MAIN_FRAME, NULL, -1, -1, false); | 102 &url_request_fail, ResourceType::MAIN_FRAME, NULL, -1, -1, false); |
| 102 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, | 103 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, |
| 103 ON_BEFORE_REQUEST))); | 104 ON_BEFORE_REQUEST))); |
| 104 } | 105 } |
| 105 | 106 |
| 106 TEST(WebRequestConditionAttributeTest, ContentType) { | 107 TEST(WebRequestConditionAttributeTest, ContentType) { |
| 107 // Necessary for TestURLRequest. | 108 // Necessary for TestURLRequest. |
| 108 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 109 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 109 | 110 |
| 110 std::string error; | 111 std::string error; |
| 111 scoped_refptr<const WebRequestConditionAttribute> result; | 112 scoped_refptr<const WebRequestConditionAttribute> result; |
| 112 | 113 |
| 113 net::SpawnedTestServer test_server( | 114 net::SpawnedTestServer test_server( |
| 114 net::SpawnedTestServer::TYPE_HTTP, | 115 net::SpawnedTestServer::TYPE_HTTP, |
| 115 net::SpawnedTestServer::kLocalhost, | 116 net::SpawnedTestServer::kLocalhost, |
| 116 base::FilePath(FILE_PATH_LITERAL( | 117 base::FilePath(FILE_PATH_LITERAL( |
| 117 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 118 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
| 118 ASSERT_TRUE(test_server.Start()); | 119 ASSERT_TRUE(test_server.Start()); |
| 119 | 120 |
| 120 net::TestURLRequestContext context; | 121 net::TestURLRequestContext context; |
| 121 net::TestDelegate delegate; | 122 net::TestDelegate delegate; |
| 122 net::TestURLRequest url_request( | 123 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
| 123 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); | 124 net::DEFAULT_PRIORITY, |
| 125 &delegate, |
| 126 &context); |
| 124 url_request.Start(); | 127 url_request.Start(); |
| 125 base::MessageLoop::current()->Run(); | 128 base::MessageLoop::current()->Run(); |
| 126 | 129 |
| 127 ListValue content_types; | 130 ListValue content_types; |
| 128 content_types.Append(new base::StringValue("text/plain")); | 131 content_types.Append(new base::StringValue("text/plain")); |
| 129 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 132 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
| 130 WebRequestConditionAttribute::Create( | 133 WebRequestConditionAttribute::Create( |
| 131 keys::kContentTypeKey, &content_types, &error); | 134 keys::kContentTypeKey, &content_types, &error); |
| 132 EXPECT_EQ("", error); | 135 EXPECT_EQ("", error); |
| 133 ASSERT_TRUE(attribute_include.get()); | 136 ASSERT_TRUE(attribute_include.get()); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 ASSERT_EQ("", error); | 199 ASSERT_EQ("", error); |
| 197 ASSERT_TRUE(first_party_attribute.get()); | 200 ASSERT_TRUE(first_party_attribute.get()); |
| 198 EXPECT_EQ(std::string(keys::kThirdPartyKey), | 201 EXPECT_EQ(std::string(keys::kThirdPartyKey), |
| 199 first_party_attribute->GetName()); | 202 first_party_attribute->GetName()); |
| 200 | 203 |
| 201 const GURL url_empty; | 204 const GURL url_empty; |
| 202 const GURL url_a("http://a.com"); | 205 const GURL url_a("http://a.com"); |
| 203 const GURL url_b("http://b.com"); | 206 const GURL url_b("http://b.com"); |
| 204 net::TestURLRequestContext context; | 207 net::TestURLRequestContext context; |
| 205 net::TestDelegate delegate; | 208 net::TestDelegate delegate; |
| 206 net::TestURLRequest url_request(url_a, &delegate, &context, NULL); | 209 net::TestURLRequest url_request( |
| 210 url_a, net::DEFAULT_PRIORITY, &delegate, &context); |
| 207 | 211 |
| 208 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { | 212 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { |
| 209 if (!(kActiveStages & i)) | 213 if (!(kActiveStages & i)) |
| 210 continue; | 214 continue; |
| 211 const RequestStage stage = static_cast<RequestStage>(i); | 215 const RequestStage stage = static_cast<RequestStage>(i); |
| 212 url_request.set_first_party_for_cookies(url_empty); | 216 url_request.set_first_party_for_cookies(url_empty); |
| 213 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestData(&url_request, | 217 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestData(&url_request, |
| 214 stage))); | 218 stage))); |
| 215 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestData(&url_request, | 219 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestData(&url_request, |
| 216 stage))); | 220 stage))); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 WebRequestConditionAttribute::Create(keys::kStagesKey, | 290 WebRequestConditionAttribute::Create(keys::kStagesKey, |
| 287 &single_stage_list, | 291 &single_stage_list, |
| 288 &error)); | 292 &error)); |
| 289 EXPECT_EQ("", error); | 293 EXPECT_EQ("", error); |
| 290 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); | 294 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); |
| 291 } | 295 } |
| 292 | 296 |
| 293 const GURL url_empty; | 297 const GURL url_empty; |
| 294 net::TestURLRequestContext context; | 298 net::TestURLRequestContext context; |
| 295 net::TestDelegate delegate; | 299 net::TestDelegate delegate; |
| 296 net::TestURLRequest url_request(url_empty, &delegate, &context, NULL); | 300 net::TestURLRequest url_request( |
| 301 url_empty, net::DEFAULT_PRIORITY, &delegate, &context); |
| 297 | 302 |
| 298 for (size_t i = 0; i < arraysize(active_stages); ++i) { | 303 for (size_t i = 0; i < arraysize(active_stages); ++i) { |
| 299 EXPECT_FALSE(empty_attribute->IsFulfilled( | 304 EXPECT_FALSE(empty_attribute->IsFulfilled( |
| 300 WebRequestData(&url_request, active_stages[i].first))); | 305 WebRequestData(&url_request, active_stages[i].first))); |
| 301 | 306 |
| 302 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { | 307 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { |
| 303 EXPECT_EQ(i == j, | 308 EXPECT_EQ(i == j, |
| 304 one_stage_attributes[j]->IsFulfilled( | 309 one_stage_attributes[j]->IsFulfilled( |
| 305 WebRequestData(&url_request, active_stages[i].first))); | 310 WebRequestData(&url_request, active_stages[i].first))); |
| 306 } | 311 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 // correctly against request headers. This test is not as extensive as | 415 // correctly against request headers. This test is not as extensive as |
| 411 // "ResponseHeaders" (below), because the header-matching code is shared | 416 // "ResponseHeaders" (below), because the header-matching code is shared |
| 412 // by both types of condition attributes, so it is enough to test it once. | 417 // by both types of condition attributes, so it is enough to test it once. |
| 413 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 418 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
| 414 // Necessary for TestURLRequest. | 419 // Necessary for TestURLRequest. |
| 415 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 420 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 416 | 421 |
| 417 net::TestURLRequestContext context; | 422 net::TestURLRequestContext context; |
| 418 net::TestDelegate delegate; | 423 net::TestDelegate delegate; |
| 419 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. | 424 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. |
| 420 &delegate, &context, NULL); | 425 net::DEFAULT_PRIORITY, |
| 426 &delegate, |
| 427 &context); |
| 421 url_request.SetExtraRequestHeaderByName( | 428 url_request.SetExtraRequestHeaderByName( |
| 422 "Custom-header", "custom/value", true /* overwrite */); | 429 "Custom-header", "custom/value", true /* overwrite */); |
| 423 url_request.Start(); | 430 url_request.Start(); |
| 424 base::MessageLoop::current()->Run(); | 431 base::MessageLoop::current()->Run(); |
| 425 | 432 |
| 426 std::vector<std::vector<const std::string*> > tests; | 433 std::vector<std::vector<const std::string*> > tests; |
| 427 bool result = false; | 434 bool result = false; |
| 428 | 435 |
| 429 const RequestStage stage = ON_BEFORE_SEND_HEADERS; | 436 const RequestStage stage = ON_BEFORE_SEND_HEADERS; |
| 430 | 437 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 net::SpawnedTestServer test_server( | 502 net::SpawnedTestServer test_server( |
| 496 net::SpawnedTestServer::TYPE_HTTP, | 503 net::SpawnedTestServer::TYPE_HTTP, |
| 497 net::SpawnedTestServer::kLocalhost, | 504 net::SpawnedTestServer::kLocalhost, |
| 498 base::FilePath(FILE_PATH_LITERAL( | 505 base::FilePath(FILE_PATH_LITERAL( |
| 499 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 506 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
| 500 ASSERT_TRUE(test_server.Start()); | 507 ASSERT_TRUE(test_server.Start()); |
| 501 | 508 |
| 502 net::TestURLRequestContext context; | 509 net::TestURLRequestContext context; |
| 503 net::TestDelegate delegate; | 510 net::TestDelegate delegate; |
| 504 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 511 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
| 505 &delegate, &context, NULL); | 512 net::DEFAULT_PRIORITY, |
| 513 &delegate, |
| 514 &context); |
| 506 url_request.Start(); | 515 url_request.Start(); |
| 507 base::MessageLoop::current()->Run(); | 516 base::MessageLoop::current()->Run(); |
| 508 | 517 |
| 509 // In all the tests below we assume that the server includes the headers | 518 // In all the tests below we assume that the server includes the headers |
| 510 // Custom-Header: custom/value | 519 // Custom-Header: custom/value |
| 511 // Custom-Header-B: valueA | 520 // Custom-Header-B: valueA |
| 512 // Custom-Header-B: valueB | 521 // Custom-Header-B: valueB |
| 513 // Custom-Header-C: valueC, valueD | 522 // Custom-Header-C: valueC, valueD |
| 514 // Custom-Header-D: | 523 // Custom-Header-D: |
| 515 // in the response, but does not include "Non-existing: void". | 524 // in the response, but does not include "Non-existing: void". |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 keys::kValueEqualsKey, "valueB" | 673 keys::kValueEqualsKey, "valueB" |
| 665 }; | 674 }; |
| 666 const size_t kExistingSize[] = { arraysize(kExisting) }; | 675 const size_t kExistingSize[] = { arraysize(kExisting) }; |
| 667 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 676 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
| 668 MatchAndCheck( | 677 MatchAndCheck( |
| 669 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 678 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
| 670 EXPECT_FALSE(result); | 679 EXPECT_FALSE(result); |
| 671 } | 680 } |
| 672 | 681 |
| 673 } // namespace extensions | 682 } // namespace extensions |
| OLD | NEW |