Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / net / safe_search_util_unittest.cc
blob14653a0b808a92057ca8dcb80b76ae912e8870e3
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net/safe_search_util.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string_piece.h"
9 #include "chrome/common/url_constants.h"
10 #include "net/http/http_request_headers.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.h"
15 class SafeSearchUtilTest : public ::testing::Test {
16 protected:
17 SafeSearchUtilTest() {}
18 ~SafeSearchUtilTest() override {}
20 scoped_ptr<net::URLRequest> CreateRequest(const std::string& url) {
21 return context_.CreateRequest(GURL(url), net::DEFAULT_PRIORITY, NULL);
24 scoped_ptr<net::URLRequest> CreateYoutubeRequest() {
25 return CreateRequest("http://www.youtube.com");
28 scoped_ptr<net::URLRequest> CreateNonYoutubeRequest() {
29 return CreateRequest("http://www.notyoutube.com");
32 // Does a request using the |url_string| URL and verifies that the expected
33 // string is equal to the query part (between ? and #) of the final url of
34 // that request.
35 void CheckAddedParameters(const std::string& url_string,
36 const std::string& expected_query_parameters) {
37 // Show the URL in the trace so we know where we failed.
38 SCOPED_TRACE(url_string);
40 scoped_ptr<net::URLRequest> request(CreateRequest(url_string));
41 GURL result(url_string);
42 safe_search_util::ForceGoogleSafeSearch(request.get(), &result);
44 EXPECT_EQ(expected_query_parameters, result.query());
47 base::MessageLoop message_loop_;
48 net::TestURLRequestContext context_;
51 TEST_F(SafeSearchUtilTest, AddGoogleSafeSearchParams) {
52 const std::string kSafeParameter = chrome::kSafeSearchSafeParameter;
53 const std::string kSsuiParameter = chrome::kSafeSearchSsuiParameter;
54 const std::string kBothParameters = kSafeParameter + "&" + kSsuiParameter;
56 // Test the home page.
57 CheckAddedParameters("http://google.com/", kBothParameters);
59 // Test the search home page.
60 CheckAddedParameters("http://google.com/webhp",
61 kBothParameters);
63 // Test different valid search pages with parameters.
64 CheckAddedParameters("http://google.com/search?q=google",
65 "q=google&" + kBothParameters);
67 CheckAddedParameters("http://google.com/?q=google",
68 "q=google&" + kBothParameters);
70 CheckAddedParameters("http://google.com/webhp?q=google",
71 "q=google&" + kBothParameters);
73 // Test the valid pages with safe set to off.
74 CheckAddedParameters("http://google.com/search?q=google&safe=off",
75 "q=google&" + kBothParameters);
77 CheckAddedParameters("http://google.com/?q=google&safe=off",
78 "q=google&" + kBothParameters);
80 CheckAddedParameters("http://google.com/webhp?q=google&safe=off",
81 "q=google&" + kBothParameters);
83 CheckAddedParameters("http://google.com/webhp?q=google&%73afe=off",
84 "q=google&%73afe=off&" + kBothParameters);
86 // Test the home page, different TLDs.
87 CheckAddedParameters("http://google.de/", kBothParameters);
88 CheckAddedParameters("http://google.ro/", kBothParameters);
89 CheckAddedParameters("http://google.nl/", kBothParameters);
91 // Test the search home page, different TLD.
92 CheckAddedParameters("http://google.de/webhp", kBothParameters);
94 // Test the search page with parameters, different TLD.
95 CheckAddedParameters("http://google.de/search?q=google",
96 "q=google&" + kBothParameters);
98 // Test the home page with parameters, different TLD.
99 CheckAddedParameters("http://google.de/?q=google",
100 "q=google&" + kBothParameters);
102 // Test the search page with the parameters set.
103 CheckAddedParameters("http://google.de/?q=google&" + kBothParameters,
104 "q=google&" + kBothParameters);
106 // Test some possibly tricky combinations.
107 CheckAddedParameters("http://google.com/?q=goog&" + kSafeParameter +
108 "&ssui=one",
109 "q=goog&" + kBothParameters);
111 CheckAddedParameters("http://google.de/?q=goog&unsafe=active&" +
112 kSsuiParameter,
113 "q=goog&unsafe=active&" + kBothParameters);
115 CheckAddedParameters("http://google.de/?q=goog&safe=off&ssui=off",
116 "q=goog&" + kBothParameters);
118 // Test various combinations where we should not add anything.
119 CheckAddedParameters("http://google.com/?q=goog&" + kSsuiParameter + "&" +
120 kSafeParameter,
121 "q=goog&" + kBothParameters);
123 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&q=goog&" +
124 kSafeParameter,
125 "q=goog&" + kBothParameters);
127 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&" +
128 kSafeParameter + "&q=goog",
129 "q=goog&" + kBothParameters);
131 // Test that another website is not affected, without parameters.
132 CheckAddedParameters("http://google.com/finance", std::string());
134 // Test that another website is not affected, with parameters.
135 CheckAddedParameters("http://google.com/finance?q=goog", "q=goog");
137 // Test with percent-encoded data (%26 is &)
138 CheckAddedParameters("http://google.com/?q=%26%26%26&" + kSsuiParameter +
139 "&" + kSafeParameter + "&param=%26%26%26",
140 "q=%26%26%26&param=%26%26%26&" + kBothParameters);
143 TEST_F(SafeSearchUtilTest, SetYoutubeHeader) {
144 scoped_ptr<net::URLRequest> request = CreateYoutubeRequest();
145 net::HttpRequestHeaders headers;
146 safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers);
147 std::string value;
148 EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value));
149 EXPECT_EQ("Active", value);
152 TEST_F(SafeSearchUtilTest, OverrideYoutubeHeader) {
153 scoped_ptr<net::URLRequest> request = CreateYoutubeRequest();
154 net::HttpRequestHeaders headers;
155 headers.SetHeader("Youtube-Safety-Mode", "Off");
156 safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers);
157 std::string value;
158 EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value));
159 EXPECT_EQ("Active", value);
162 TEST_F(SafeSearchUtilTest, DoesntTouchNonYoutubeURL) {
163 scoped_ptr<net::URLRequest> request = CreateNonYoutubeRequest();
164 net::HttpRequestHeaders headers;
165 headers.SetHeader("Youtube-Safety-Mode", "Off");
166 safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers);
167 std::string value;
168 EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value));
169 EXPECT_EQ("Off", value);