| 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/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 std::string error; | 77 std::string error; |
| 78 ListValue resource_types; | 78 ListValue resource_types; |
| 79 resource_types.Append(Value::CreateStringValue("main_frame")); | 79 resource_types.Append(Value::CreateStringValue("main_frame")); |
| 80 | 80 |
| 81 scoped_ptr<WebRequestConditionAttribute> attribute = | 81 scoped_ptr<WebRequestConditionAttribute> attribute = |
| 82 WebRequestConditionAttribute::Create( | 82 WebRequestConditionAttribute::Create( |
| 83 keys::kResourceTypeKey, &resource_types, &error); | 83 keys::kResourceTypeKey, &resource_types, &error); |
| 84 EXPECT_EQ("", error); | 84 EXPECT_EQ("", error); |
| 85 ASSERT_TRUE(attribute.get()); | 85 ASSERT_TRUE(attribute.get()); |
| 86 | 86 |
| 87 net::TestURLRequestContext context; | 87 TestURLRequestContext context; |
| 88 net::TestURLRequest url_request_ok( | 88 TestURLRequest url_request_ok(GURL("http://www.example.com"), NULL, &context); |
| 89 GURL("http://www.example.com"), NULL, &context); | |
| 90 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, | 89 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, |
| 91 ResourceType::MAIN_FRAME, NULL, -1, -1); | 90 ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 92 EXPECT_TRUE(attribute->IsFulfilled( | 91 EXPECT_TRUE(attribute->IsFulfilled( |
| 93 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST))); | 92 WebRequestRule::RequestData(&url_request_ok, ON_BEFORE_REQUEST))); |
| 94 | 93 |
| 95 net::TestURLRequest url_request_fail( | 94 TestURLRequest url_request_fail( |
| 96 GURL("http://www.example.com"), NULL, &context); | 95 GURL("http://www.example.com"), NULL, &context); |
| 97 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, | 96 content::ResourceRequestInfo::AllocateForTesting(&url_request_ok, |
| 98 ResourceType::SUB_FRAME, NULL, -1, -1); | 97 ResourceType::SUB_FRAME, NULL, -1, -1); |
| 99 EXPECT_FALSE(attribute->IsFulfilled( | 98 EXPECT_FALSE(attribute->IsFulfilled( |
| 100 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST))); | 99 WebRequestRule::RequestData(&url_request_fail, ON_BEFORE_REQUEST))); |
| 101 } | 100 } |
| 102 | 101 |
| 103 TEST(WebRequestConditionAttributeTest, ContentType) { | 102 TEST(WebRequestConditionAttributeTest, ContentType) { |
| 104 // Necessary for TestURLRequest. | 103 // Necessary for TestURLRequest. |
| 105 MessageLoop message_loop(MessageLoop::TYPE_IO); | 104 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 106 | 105 |
| 107 std::string error; | 106 std::string error; |
| 108 scoped_ptr<WebRequestConditionAttribute> result; | 107 scoped_ptr<WebRequestConditionAttribute> result; |
| 109 | 108 |
| 110 net::TestServer test_server( | 109 net::TestServer test_server( |
| 111 net::TestServer::TYPE_HTTP, | 110 net::TestServer::TYPE_HTTP, |
| 112 net::TestServer::kLocalhost, | 111 net::TestServer::kLocalhost, |
| 113 FilePath(FILE_PATH_LITERAL( | 112 FilePath(FILE_PATH_LITERAL( |
| 114 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 113 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
| 115 ASSERT_TRUE(test_server.Start()); | 114 ASSERT_TRUE(test_server.Start()); |
| 116 | 115 |
| 117 net::TestURLRequestContext context; | 116 TestURLRequestContext context; |
| 118 net::TestDelegate delegate; | 117 TestDelegate delegate; |
| 119 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 118 TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
| 120 &delegate, &context); | 119 &delegate, &context); |
| 121 url_request.Start(); | 120 url_request.Start(); |
| 122 MessageLoop::current()->Run(); | 121 MessageLoop::current()->Run(); |
| 123 | 122 |
| 124 ListValue content_types; | 123 ListValue content_types; |
| 125 content_types.Append(Value::CreateStringValue("text/plain")); | 124 content_types.Append(Value::CreateStringValue("text/plain")); |
| 126 scoped_ptr<WebRequestConditionAttribute> attribute_include = | 125 scoped_ptr<WebRequestConditionAttribute> attribute_include = |
| 127 WebRequestConditionAttribute::Create( | 126 WebRequestConditionAttribute::Create( |
| 128 keys::kContentTypeKey, &content_types, &error); | 127 keys::kContentTypeKey, &content_types, &error); |
| 129 EXPECT_EQ("", error); | 128 EXPECT_EQ("", error); |
| 130 ASSERT_TRUE(attribute_include.get()); | 129 ASSERT_TRUE(attribute_include.get()); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 scoped_ptr<WebRequestConditionAttribute> first_party_attribute = | 183 scoped_ptr<WebRequestConditionAttribute> first_party_attribute = |
| 185 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, | 184 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, |
| 186 &value_false, | 185 &value_false, |
| 187 &error); | 186 &error); |
| 188 ASSERT_EQ("", error); | 187 ASSERT_EQ("", error); |
| 189 ASSERT_TRUE(first_party_attribute.get()); | 188 ASSERT_TRUE(first_party_attribute.get()); |
| 190 | 189 |
| 191 const GURL url_empty; | 190 const GURL url_empty; |
| 192 const GURL url_a("http://a.com"); | 191 const GURL url_a("http://a.com"); |
| 193 const GURL url_b("http://b.com"); | 192 const GURL url_b("http://b.com"); |
| 194 net::TestURLRequestContext context; | 193 TestURLRequestContext context; |
| 195 net::TestDelegate delegate; | 194 TestDelegate delegate; |
| 196 net::TestURLRequest url_request(url_a, &delegate, &context); | 195 TestURLRequest url_request(url_a, &delegate, &context); |
| 197 | 196 |
| 198 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { | 197 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { |
| 199 if (!(kActiveStages & i)) | 198 if (!(kActiveStages & i)) |
| 200 continue; | 199 continue; |
| 201 const RequestStage stage = static_cast<RequestStage>(i); | 200 const RequestStage stage = static_cast<RequestStage>(i); |
| 202 url_request.set_first_party_for_cookies(url_empty); | 201 url_request.set_first_party_for_cookies(url_empty); |
| 203 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( | 202 EXPECT_FALSE(third_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 204 &url_request, stage))); | 203 &url_request, stage))); |
| 205 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( | 204 EXPECT_TRUE(first_party_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 206 &url_request, stage))); | 205 &url_request, stage))); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 single_stage_list.AppendString(active_stages[i].second); | 270 single_stage_list.AppendString(active_stages[i].second); |
| 272 one_stage_attributes.push_back( | 271 one_stage_attributes.push_back( |
| 273 WebRequestConditionAttribute::Create(keys::kStagesKey, | 272 WebRequestConditionAttribute::Create(keys::kStagesKey, |
| 274 &single_stage_list, | 273 &single_stage_list, |
| 275 &error).release()); | 274 &error).release()); |
| 276 EXPECT_EQ("", error); | 275 EXPECT_EQ("", error); |
| 277 ASSERT_TRUE(one_stage_attributes.back() != NULL); | 276 ASSERT_TRUE(one_stage_attributes.back() != NULL); |
| 278 } | 277 } |
| 279 | 278 |
| 280 const GURL url_empty; | 279 const GURL url_empty; |
| 281 net::TestURLRequestContext context; | 280 TestURLRequestContext context; |
| 282 net::TestDelegate delegate; | 281 TestDelegate delegate; |
| 283 net::TestURLRequest url_request(url_empty, &delegate, &context); | 282 TestURLRequest url_request(url_empty, &delegate, &context); |
| 284 | 283 |
| 285 for (size_t i = 0; i < arraysize(active_stages); ++i) { | 284 for (size_t i = 0; i < arraysize(active_stages); ++i) { |
| 286 EXPECT_FALSE(empty_attribute->IsFulfilled(WebRequestRule::RequestData( | 285 EXPECT_FALSE(empty_attribute->IsFulfilled(WebRequestRule::RequestData( |
| 287 &url_request, active_stages[i].first))); | 286 &url_request, active_stages[i].first))); |
| 288 | 287 |
| 289 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { | 288 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { |
| 290 EXPECT_EQ( | 289 EXPECT_EQ( |
| 291 i == j, | 290 i == j, |
| 292 one_stage_attributes[j]->IsFulfilled(WebRequestRule::RequestData( | 291 one_stage_attributes[j]->IsFulfilled(WebRequestRule::RequestData( |
| 293 &url_request, active_stages[i].first))); | 292 &url_request, active_stages[i].first))); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 } // namespace | 392 } // namespace |
| 394 | 393 |
| 395 // Here we test WebRequestConditionAttributeRequestHeaders for matching | 394 // Here we test WebRequestConditionAttributeRequestHeaders for matching |
| 396 // correctly against request headers. This test is not as extensive as | 395 // correctly against request headers. This test is not as extensive as |
| 397 // "ResponseHeaders" (below), because the header-matching code is shared | 396 // "ResponseHeaders" (below), because the header-matching code is shared |
| 398 // by both types of condition attributes, so it is enough to test it once. | 397 // by both types of condition attributes, so it is enough to test it once. |
| 399 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 398 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
| 400 // Necessary for TestURLRequest. | 399 // Necessary for TestURLRequest. |
| 401 MessageLoop message_loop(MessageLoop::TYPE_IO); | 400 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 402 | 401 |
| 403 net::TestURLRequestContext context; | 402 TestURLRequestContext context; |
| 404 net::TestDelegate delegate; | 403 TestDelegate delegate; |
| 405 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. | 404 TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. |
| 406 &delegate, &context); | 405 &delegate, &context); |
| 407 url_request.SetExtraRequestHeaderByName( | 406 url_request.SetExtraRequestHeaderByName( |
| 408 "Custom-header", "custom/value", true /* overwrite */); | 407 "Custom-header", "custom/value", true /* overwrite */); |
| 409 url_request.Start(); | 408 url_request.Start(); |
| 410 MessageLoop::current()->Run(); | 409 MessageLoop::current()->Run(); |
| 411 | 410 |
| 412 std::vector<std::vector<const std::string*> > tests; | 411 std::vector<std::vector<const std::string*> > tests; |
| 413 bool result = false; | 412 bool result = false; |
| 414 | 413 |
| 415 const RequestStage stage = ON_BEFORE_SEND_HEADERS; | 414 const RequestStage stage = ON_BEFORE_SEND_HEADERS; |
| 416 | 415 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 // Necessary for TestURLRequest. | 477 // Necessary for TestURLRequest. |
| 479 MessageLoop message_loop(MessageLoop::TYPE_IO); | 478 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 480 | 479 |
| 481 net::TestServer test_server( | 480 net::TestServer test_server( |
| 482 net::TestServer::TYPE_HTTP, | 481 net::TestServer::TYPE_HTTP, |
| 483 net::TestServer::kLocalhost, | 482 net::TestServer::kLocalhost, |
| 484 FilePath(FILE_PATH_LITERAL( | 483 FilePath(FILE_PATH_LITERAL( |
| 485 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 484 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
| 486 ASSERT_TRUE(test_server.Start()); | 485 ASSERT_TRUE(test_server.Start()); |
| 487 | 486 |
| 488 net::TestURLRequestContext context; | 487 TestURLRequestContext context; |
| 489 net::TestDelegate delegate; | 488 TestDelegate delegate; |
| 490 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 489 TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
| 491 &delegate, &context); | 490 &delegate, &context); |
| 492 url_request.Start(); | 491 url_request.Start(); |
| 493 MessageLoop::current()->Run(); | 492 MessageLoop::current()->Run(); |
| 494 | 493 |
| 495 // In all the tests below we assume that the server includes the headers | 494 // In all the tests below we assume that the server includes the headers |
| 496 // Custom-Header: custom/value | 495 // Custom-Header: custom/value |
| 497 // Custom-Header-B: valueA | 496 // Custom-Header-B: valueA |
| 498 // Custom-Header-B: valueB | 497 // Custom-Header-B: valueB |
| 499 // Custom-Header-C: valueC, valueD | 498 // Custom-Header-C: valueC, valueD |
| 500 // Custom-Header-D: | 499 // Custom-Header-D: |
| 501 // in the response, but does not include "Non-existing: void". | 500 // in the response, but does not include "Non-existing: void". |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 keys::kValueEqualsKey, "valueB" | 649 keys::kValueEqualsKey, "valueB" |
| 651 }; | 650 }; |
| 652 const size_t kExistingSize[] = { arraysize(kExisting) }; | 651 const size_t kExistingSize[] = { arraysize(kExisting) }; |
| 653 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 652 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
| 654 MatchAndCheck( | 653 MatchAndCheck( |
| 655 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 654 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
| 656 EXPECT_FALSE(result); | 655 EXPECT_FALSE(result); |
| 657 } | 656 } |
| 658 | 657 |
| 659 } // namespace extensions | 658 } // namespace extensions |
| OLD | NEW |