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 "net/http/http_request_headers.h"
10 #include "net/url_request/url_request_test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 class SafeSearchUtilTest
: public ::testing::Test
{
16 SafeSearchUtilTest() {}
17 ~SafeSearchUtilTest() override
{}
19 scoped_ptr
<net::URLRequest
> CreateYoutubeRequest() {
20 return context_
.CreateRequest(GURL("http://www.youtube.com"),
21 net::DEFAULT_PRIORITY
,
26 scoped_ptr
<net::URLRequest
> CreateNonYoutubeRequest() {
27 return context_
.CreateRequest(GURL("http://www.notyoutube.com"),
28 net::DEFAULT_PRIORITY
,
33 base::MessageLoop message_loop_
;
34 net::TestURLRequestContext context_
;
37 // ForceGoogleSafeSearch is already tested quite extensively in
38 // ChromeNetworkDelegateSafeSearchTest (in chrome_network_delegate_unittest.cc),
39 // so we won't test it again here.
41 TEST_F(SafeSearchUtilTest
, SetYoutubeHeader
) {
42 scoped_ptr
<net::URLRequest
> request
= CreateYoutubeRequest();
43 net::HttpRequestHeaders headers
;
44 safe_search_util::ForceYouTubeSafetyMode(request
.get(), &headers
);
46 EXPECT_TRUE(headers
.GetHeader("Youtube-Safety-Mode", &value
));
47 EXPECT_EQ("Active", value
);
50 TEST_F(SafeSearchUtilTest
, OverrideYoutubeHeader
) {
51 scoped_ptr
<net::URLRequest
> request
= CreateYoutubeRequest();
52 net::HttpRequestHeaders headers
;
53 headers
.SetHeader("Youtube-Safety-Mode", "Off");
54 safe_search_util::ForceYouTubeSafetyMode(request
.get(), &headers
);
56 EXPECT_TRUE(headers
.GetHeader("Youtube-Safety-Mode", &value
));
57 EXPECT_EQ("Active", value
);
60 TEST_F(SafeSearchUtilTest
, DoesntTouchNonYoutubeURL
) {
61 scoped_ptr
<net::URLRequest
> request
= CreateNonYoutubeRequest();
62 net::HttpRequestHeaders headers
;
63 headers
.SetHeader("Youtube-Safety-Mode", "Off");
64 safe_search_util::ForceYouTubeSafetyMode(request
.get(), &headers
);
66 EXPECT_TRUE(headers
.GetHeader("Youtube-Safety-Mode", &value
));
67 EXPECT_EQ("Off", value
);