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 virtual ~SafeSearchUtilTest() {}
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 static void SetCookie(net::HttpRequestHeaders
* headers
,
34 const std::string
& value
) {
35 headers
->SetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie
),
36 base::StringPiece(value
));
39 static void CheckHeaders(net::URLRequest
* request
,
40 const std::string
& header_string_original
,
41 const std::string
& header_string_expected
) {
42 net::HttpRequestHeaders headers
;
43 SetCookie(&headers
, header_string_original
);
44 safe_search_util::ForceYouTubeSafetyMode(request
, &headers
);
46 net::HttpRequestHeaders headers_expected
;
47 SetCookie(&headers_expected
, header_string_expected
);
48 EXPECT_EQ(headers_expected
.ToString(), headers
.ToString());
51 base::MessageLoop message_loop_
;
52 net::TestURLRequestContext context_
;
55 // ForceGoogleSafeSearch is already tested quite extensively in
56 // ChromeNetworkDelegateSafeSearchTest (in chrome_network_delegate_unittest.cc),
57 // so we won't test it again here.
59 TEST_F(SafeSearchUtilTest
, CreateYoutubePrefCookie
) {
60 scoped_ptr
<net::URLRequest
> request
= CreateYoutubeRequest();
61 CheckHeaders(request
.get(),
63 "OtherCookie=value; PREF=f2=8000000");
66 TEST_F(SafeSearchUtilTest
, ModifyYoutubePrefCookie
) {
67 scoped_ptr
<net::URLRequest
> request
= CreateYoutubeRequest();
68 CheckHeaders(request
.get(),
69 "PREF=f1=123; OtherCookie=value",
70 "PREF=f1=123&f2=8000000; OtherCookie=value");
71 CheckHeaders(request
.get(),
74 CheckHeaders(request
.get(),
76 "PREF=\"f2=8000000\"");
77 CheckHeaders(request
.get(),
78 "PREF=f1=123&f2=4321&foo=bar",
79 "PREF=f1=123&f2=8004321&foo=bar");
80 CheckHeaders(request
.get(),
81 "PREF=\"f1=1&f2=4321\"; OtherCookie=value",
82 "PREF=\"f1=1&f2=8004321\"; OtherCookie=value");
85 TEST_F(SafeSearchUtilTest
, DoesntTouchNonYoutubeURL
) {
86 scoped_ptr
<net::URLRequest
> request
= CreateNonYoutubeRequest();
87 CheckHeaders(request
.get(),