| 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 "extensions/browser/api/declarative_webrequest/webrequest_condition_att
ribute.h" | 5 #include "extensions/browser/api/declarative_webrequest/webrequest_condition_att
ribute.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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 | 91 |
| 92 scoped_refptr<const WebRequestConditionAttribute> attribute = | 92 scoped_refptr<const WebRequestConditionAttribute> attribute = |
| 93 WebRequestConditionAttribute::Create( | 93 WebRequestConditionAttribute::Create( |
| 94 keys::kResourceTypeKey, &resource_types, &error); | 94 keys::kResourceTypeKey, &resource_types, &error); |
| 95 EXPECT_EQ("", error); | 95 EXPECT_EQ("", error); |
| 96 ASSERT_TRUE(attribute.get()); | 96 ASSERT_TRUE(attribute.get()); |
| 97 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); | 97 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); |
| 98 | 98 |
| 99 net::TestURLRequestContext context; | 99 net::TestURLRequestContext context; |
| 100 scoped_ptr<net::URLRequest> url_request_ok(context.CreateRequest( | 100 scoped_ptr<net::URLRequest> url_request_ok(context.CreateRequest( |
| 101 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, NULL)); | 101 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL)); |
| 102 content::ResourceRequestInfo::AllocateForTesting( | 102 content::ResourceRequestInfo::AllocateForTesting( |
| 103 url_request_ok.get(), | 103 url_request_ok.get(), |
| 104 content::RESOURCE_TYPE_SUB_FRAME, | 104 content::RESOURCE_TYPE_SUB_FRAME, |
| 105 NULL, // context | 105 NULL, // context |
| 106 -1, // render_process_id | 106 -1, // render_process_id |
| 107 -1, // render_view_id | 107 -1, // render_view_id |
| 108 -1, // render_frame_id | 108 -1, // render_frame_id |
| 109 false, // is_main_frame | 109 false, // is_main_frame |
| 110 false, // parent_is_main_frame | 110 false, // parent_is_main_frame |
| 111 true, // allow_download | 111 true, // allow_download |
| 112 false); // is_async | 112 false); // is_async |
| 113 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(url_request_ok.get(), | 113 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(url_request_ok.get(), |
| 114 ON_BEFORE_REQUEST))); | 114 ON_BEFORE_REQUEST))); |
| 115 | 115 |
| 116 scoped_ptr<net::URLRequest> url_request_fail(context.CreateRequest( | 116 scoped_ptr<net::URLRequest> url_request_fail(context.CreateRequest( |
| 117 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, NULL)); | 117 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL)); |
| 118 content::ResourceRequestInfo::AllocateForTesting( | 118 content::ResourceRequestInfo::AllocateForTesting( |
| 119 url_request_fail.get(), | 119 url_request_fail.get(), |
| 120 content::RESOURCE_TYPE_MAIN_FRAME, | 120 content::RESOURCE_TYPE_MAIN_FRAME, |
| 121 NULL, // context | 121 NULL, // context |
| 122 -1, // render_process_id | 122 -1, // render_process_id |
| 123 -1, // render_view_id | 123 -1, // render_view_id |
| 124 -1, // render_frame_id | 124 -1, // render_frame_id |
| 125 true, // is_main_frame | 125 true, // is_main_frame |
| 126 false, // parent_is_main_frame | 126 false, // parent_is_main_frame |
| 127 true, // allow_download | 127 true, // allow_download |
| 128 false); // is_async | 128 false); // is_async |
| 129 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(url_request_fail.get(), | 129 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(url_request_fail.get(), |
| 130 ON_BEFORE_REQUEST))); | 130 ON_BEFORE_REQUEST))); |
| 131 } | 131 } |
| 132 | 132 |
| 133 TEST(WebRequestConditionAttributeTest, ContentType) { | 133 TEST(WebRequestConditionAttributeTest, ContentType) { |
| 134 // Necessary for TestURLRequest. | 134 // Necessary for TestURLRequest. |
| 135 base::MessageLoopForIO message_loop; | 135 base::MessageLoopForIO message_loop; |
| 136 | 136 |
| 137 std::string error; | 137 std::string error; |
| 138 scoped_refptr<const WebRequestConditionAttribute> result; | 138 scoped_refptr<const WebRequestConditionAttribute> result; |
| 139 | 139 |
| 140 net::test_server::EmbeddedTestServer test_server; | 140 net::test_server::EmbeddedTestServer test_server; |
| 141 test_server.ServeFilesFromDirectory(TestDataPath( | 141 test_server.ServeFilesFromDirectory(TestDataPath( |
| 142 "chrome/test/data/extensions/api_test/webrequest/declarative")); | 142 "chrome/test/data/extensions/api_test/webrequest/declarative")); |
| 143 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); | 143 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
| 144 | 144 |
| 145 net::TestURLRequestContext context; | 145 net::TestURLRequestContext context; |
| 146 net::TestDelegate delegate; | 146 net::TestDelegate delegate; |
| 147 scoped_ptr<net::URLRequest> url_request( | 147 scoped_ptr<net::URLRequest> url_request(context.CreateRequest( |
| 148 context.CreateRequest(test_server.GetURL("/headers.html"), | 148 test_server.GetURL("/headers.html"), net::DEFAULT_PRIORITY, &delegate)); |
| 149 net::DEFAULT_PRIORITY, | |
| 150 &delegate, | |
| 151 NULL)); | |
| 152 url_request->Start(); | 149 url_request->Start(); |
| 153 base::MessageLoop::current()->Run(); | 150 base::MessageLoop::current()->Run(); |
| 154 | 151 |
| 155 base::ListValue content_types; | 152 base::ListValue content_types; |
| 156 content_types.Append(new base::StringValue("text/plain")); | 153 content_types.Append(new base::StringValue("text/plain")); |
| 157 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 154 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
| 158 WebRequestConditionAttribute::Create( | 155 WebRequestConditionAttribute::Create( |
| 159 keys::kContentTypeKey, &content_types, &error); | 156 keys::kContentTypeKey, &content_types, &error); |
| 160 EXPECT_EQ("", error); | 157 EXPECT_EQ("", error); |
| 161 ASSERT_TRUE(attribute_include.get()); | 158 ASSERT_TRUE(attribute_include.get()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 ASSERT_TRUE(first_party_attribute.get()); | 222 ASSERT_TRUE(first_party_attribute.get()); |
| 226 EXPECT_EQ(std::string(keys::kThirdPartyKey), | 223 EXPECT_EQ(std::string(keys::kThirdPartyKey), |
| 227 first_party_attribute->GetName()); | 224 first_party_attribute->GetName()); |
| 228 | 225 |
| 229 const GURL url_empty; | 226 const GURL url_empty; |
| 230 const GURL url_a("http://a.com"); | 227 const GURL url_a("http://a.com"); |
| 231 const GURL url_b("http://b.com"); | 228 const GURL url_b("http://b.com"); |
| 232 net::TestURLRequestContext context; | 229 net::TestURLRequestContext context; |
| 233 net::TestDelegate delegate; | 230 net::TestDelegate delegate; |
| 234 scoped_ptr<net::URLRequest> url_request( | 231 scoped_ptr<net::URLRequest> url_request( |
| 235 context.CreateRequest(url_a, net::DEFAULT_PRIORITY, &delegate, NULL)); | 232 context.CreateRequest(url_a, net::DEFAULT_PRIORITY, &delegate)); |
| 236 | 233 |
| 237 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { | 234 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { |
| 238 if (!(kActiveStages & i)) | 235 if (!(kActiveStages & i)) |
| 239 continue; | 236 continue; |
| 240 const RequestStage stage = static_cast<RequestStage>(i); | 237 const RequestStage stage = static_cast<RequestStage>(i); |
| 241 url_request->set_first_party_for_cookies(url_empty); | 238 url_request->set_first_party_for_cookies(url_empty); |
| 242 EXPECT_FALSE(third_party_attribute->IsFulfilled( | 239 EXPECT_FALSE(third_party_attribute->IsFulfilled( |
| 243 WebRequestData(url_request.get(), stage))); | 240 WebRequestData(url_request.get(), stage))); |
| 244 EXPECT_TRUE(first_party_attribute->IsFulfilled( | 241 EXPECT_TRUE(first_party_attribute->IsFulfilled( |
| 245 WebRequestData(url_request.get(), stage))); | 242 WebRequestData(url_request.get(), stage))); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 &single_stage_list, | 313 &single_stage_list, |
| 317 &error)); | 314 &error)); |
| 318 EXPECT_EQ("", error); | 315 EXPECT_EQ("", error); |
| 319 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); | 316 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); |
| 320 } | 317 } |
| 321 | 318 |
| 322 const GURL url_empty; | 319 const GURL url_empty; |
| 323 net::TestURLRequestContext context; | 320 net::TestURLRequestContext context; |
| 324 net::TestDelegate delegate; | 321 net::TestDelegate delegate; |
| 325 scoped_ptr<net::URLRequest> url_request( | 322 scoped_ptr<net::URLRequest> url_request( |
| 326 context.CreateRequest(url_empty, net::DEFAULT_PRIORITY, &delegate, NULL)); | 323 context.CreateRequest(url_empty, net::DEFAULT_PRIORITY, &delegate)); |
| 327 | 324 |
| 328 for (size_t i = 0; i < arraysize(active_stages); ++i) { | 325 for (size_t i = 0; i < arraysize(active_stages); ++i) { |
| 329 EXPECT_FALSE(empty_attribute->IsFulfilled( | 326 EXPECT_FALSE(empty_attribute->IsFulfilled( |
| 330 WebRequestData(url_request.get(), active_stages[i].first))); | 327 WebRequestData(url_request.get(), active_stages[i].first))); |
| 331 | 328 |
| 332 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { | 329 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { |
| 333 EXPECT_EQ(i == j, | 330 EXPECT_EQ(i == j, |
| 334 one_stage_attributes[j]->IsFulfilled( | 331 one_stage_attributes[j]->IsFulfilled( |
| 335 WebRequestData(url_request.get(), active_stages[i].first))); | 332 WebRequestData(url_request.get(), active_stages[i].first))); |
| 336 } | 333 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 // "ResponseHeaders" (below), because the header-matching code is shared | 439 // "ResponseHeaders" (below), because the header-matching code is shared |
| 443 // by both types of condition attributes, so it is enough to test it once. | 440 // by both types of condition attributes, so it is enough to test it once. |
| 444 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 441 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
| 445 // Necessary for TestURLRequest. | 442 // Necessary for TestURLRequest. |
| 446 base::MessageLoopForIO message_loop; | 443 base::MessageLoopForIO message_loop; |
| 447 | 444 |
| 448 net::TestURLRequestContext context; | 445 net::TestURLRequestContext context; |
| 449 net::TestDelegate delegate; | 446 net::TestDelegate delegate; |
| 450 scoped_ptr<net::URLRequest> url_request( | 447 scoped_ptr<net::URLRequest> url_request( |
| 451 context.CreateRequest(GURL("http://example.com"), // Dummy URL. | 448 context.CreateRequest(GURL("http://example.com"), // Dummy URL. |
| 452 net::DEFAULT_PRIORITY, | 449 net::DEFAULT_PRIORITY, &delegate)); |
| 453 &delegate, | |
| 454 NULL)); | |
| 455 url_request->SetExtraRequestHeaderByName( | 450 url_request->SetExtraRequestHeaderByName( |
| 456 "Custom-header", "custom/value", true /* overwrite */); | 451 "Custom-header", "custom/value", true /* overwrite */); |
| 457 url_request->Start(); | 452 url_request->Start(); |
| 458 base::MessageLoop::current()->Run(); | 453 base::MessageLoop::current()->Run(); |
| 459 | 454 |
| 460 std::vector<std::vector<const std::string*> > tests; | 455 std::vector<std::vector<const std::string*> > tests; |
| 461 bool result = false; | 456 bool result = false; |
| 462 | 457 |
| 463 const RequestStage stage = ON_BEFORE_SEND_HEADERS; | 458 const RequestStage stage = ON_BEFORE_SEND_HEADERS; |
| 464 | 459 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 // Necessary for TestURLRequest. | 525 // Necessary for TestURLRequest. |
| 531 base::MessageLoopForIO message_loop; | 526 base::MessageLoopForIO message_loop; |
| 532 | 527 |
| 533 net::test_server::EmbeddedTestServer test_server; | 528 net::test_server::EmbeddedTestServer test_server; |
| 534 test_server.ServeFilesFromDirectory(TestDataPath( | 529 test_server.ServeFilesFromDirectory(TestDataPath( |
| 535 "chrome/test/data/extensions/api_test/webrequest/declarative")); | 530 "chrome/test/data/extensions/api_test/webrequest/declarative")); |
| 536 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); | 531 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
| 537 | 532 |
| 538 net::TestURLRequestContext context; | 533 net::TestURLRequestContext context; |
| 539 net::TestDelegate delegate; | 534 net::TestDelegate delegate; |
| 540 scoped_ptr<net::URLRequest> url_request( | 535 scoped_ptr<net::URLRequest> url_request(context.CreateRequest( |
| 541 context.CreateRequest(test_server.GetURL("/headers.html"), | 536 test_server.GetURL("/headers.html"), net::DEFAULT_PRIORITY, &delegate)); |
| 542 net::DEFAULT_PRIORITY, | |
| 543 &delegate, | |
| 544 NULL)); | |
| 545 url_request->Start(); | 537 url_request->Start(); |
| 546 base::MessageLoop::current()->Run(); | 538 base::MessageLoop::current()->Run(); |
| 547 | 539 |
| 548 // In all the tests below we assume that the server includes the headers | 540 // In all the tests below we assume that the server includes the headers |
| 549 // Custom-Header: custom/value | 541 // Custom-Header: custom/value |
| 550 // Custom-Header-B: valueA | 542 // Custom-Header-B: valueA |
| 551 // Custom-Header-B: valueB | 543 // Custom-Header-B: valueB |
| 552 // Custom-Header-C: valueC, valueD | 544 // Custom-Header-C: valueC, valueD |
| 553 // Custom-Header-D: | 545 // Custom-Header-D: |
| 554 // in the response, but does not include "Non-existing: void". | 546 // in the response, but does not include "Non-existing: void". |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 }; | 708 }; |
| 717 const size_t kExistingSize[] = { arraysize(kExisting) }; | 709 const size_t kExistingSize[] = { arraysize(kExisting) }; |
| 718 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 710 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
| 719 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, stage, | 711 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, stage, |
| 720 url_request.get(), &result); | 712 url_request.get(), &result); |
| 721 EXPECT_FALSE(result); | 713 EXPECT_FALSE(result); |
| 722 } | 714 } |
| 723 | 715 |
| 724 } // namespace | 716 } // namespace |
| 725 } // namespace extensions | 717 } // namespace extensions |
| OLD | NEW |