Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(652)

Side by Side Diff: content/browser/browsing_data/clear_site_data_throttle_unittest.cc

Issue 2368923003: Support the Clear-Site-Data header on resource requests (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/browsing_data/clear_site_data_throttle.h" 5 #include "content/browser/browsing_data/clear_site_data_throttle.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/test/scoped_command_line.h"
12 #include "content/common/net/url_request_service_worker_data.h"
10 #include "content/public/common/content_switches.h" 13 #include "content/public/common/content_switches.h"
14 #include "net/base/load_flags.h"
15 #include "net/url_request/url_request_test_util.h"
11 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
13 18
14 namespace content { 19 namespace content {
15 20
16 class ClearSiteDataThrottleTest : public testing::Test { 21 class ClearSiteDataThrottleTest : public testing::Test {
17 public: 22 public:
18 void SetUp() override { 23 void SetUp() override { throttle_.reset(new ClearSiteDataThrottle(nullptr)); }
19 base::CommandLine::ForCurrentProcess()->AppendSwitch(
20 switches::kEnableExperimentalWebPlatformFeatures);
21 throttle_ = ClearSiteDataThrottle::CreateThrottleForNavigation(nullptr);
22 }
23 24
24 ClearSiteDataThrottle* GetThrottle() { 25 ClearSiteDataThrottle* GetThrottle() {
25 return static_cast<ClearSiteDataThrottle*>(throttle_.get()); 26 return static_cast<ClearSiteDataThrottle*>(throttle_.get());
26 } 27 }
27 28
28 private: 29 private:
29 std::unique_ptr<NavigationThrottle> throttle_; 30 base::MessageLoop message_loop_;
31 std::unique_ptr<ResourceThrottle> throttle_;
30 }; 32 };
31 33
34 TEST_F(ClearSiteDataThrottleTest, CreateThrottleForRequest) {
35 // Enable experimental features.
36 std::unique_ptr<base::test::ScopedCommandLine> command_line(
37 new base::test::ScopedCommandLine());
38 command_line->GetProcessCommandLine()->AppendSwitch(
39 switches::kEnableExperimentalWebPlatformFeatures);
40
41 // Create a URL request.
42 GURL url("https://www.example.com");
43 net::TestURLRequestContext context;
44 std::unique_ptr<net::URLRequest> request(
45 context.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr));
46
47 // We will not create the throttle for an empty ResourceRequestInfo.
48 EXPECT_FALSE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get()));
49
50 // We can create the throttle for a valid ResourceRequestInfo.
51 ResourceRequestInfo::AllocateForTesting(request.get(), RESOURCE_TYPE_IMAGE,
52 nullptr, 0, 0, 0, false, true, true,
53 true, false);
54 EXPECT_TRUE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get()));
55
56 // We won't create the throttle for service worker requests.
57 request->SetUserData(URLRequestServiceWorkerData::kUserDataKey,
58 new URLRequestServiceWorkerData());
59 EXPECT_FALSE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get()));
60
61 // Nor will we for LOAD_DO_NOT_SAVE_COOKIES requests.
62 request->RemoveUserData(URLRequestServiceWorkerData::kUserDataKey);
63 request->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
64 EXPECT_FALSE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get()));
65
66 // Nor if experimental web features are disabled.
67 request->SetLoadFlags(net::LOAD_NORMAL);
68 command_line.reset();
69 EXPECT_FALSE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get()));
70 }
71
32 TEST_F(ClearSiteDataThrottleTest, ParseHeader) { 72 TEST_F(ClearSiteDataThrottleTest, ParseHeader) {
33 struct TestCase { 73 struct TestCase {
34 const char* header; 74 const char* header;
35 bool cookies; 75 bool cookies;
36 bool storage; 76 bool storage;
37 bool cache; 77 bool cache;
38 } test_cases[] = { 78 } test_cases[] = {
39 // One data type. 79 // One data type.
40 {"{ \"types\": [\"cookies\"] }", true, false, false}, 80 {"{ \"types\": [\"cookies\"] }", true, false, false},
41 {"{ \"types\": [\"storage\"] }", false, true, false}, 81 {"{ \"types\": [\"storage\"] }", false, true, false},
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 for (const auto& message : messages) { 164 for (const auto& message : messages) {
125 EXPECT_EQ(CONSOLE_MESSAGE_LEVEL_ERROR, message.level); 165 EXPECT_EQ(CONSOLE_MESSAGE_LEVEL_ERROR, message.level);
126 multiline_message += message.text + "\n"; 166 multiline_message += message.text + "\n";
127 } 167 }
128 168
129 EXPECT_EQ(test_case.console_message, multiline_message); 169 EXPECT_EQ(test_case.console_message, multiline_message);
130 } 170 }
131 } 171 }
132 172
133 } // namespace content 173 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698