1 // Copyright 2013 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 "ios/web/public/referrer_util.h"
7 #include "ios/web/public/referrer.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 const char* test_urls
[] = {
15 "http://insecure.com/foo/bar.html",
16 "https://secure.net/trustworthy.html",
19 enum { Empty
= 0, Insecure
, Secure
};
21 TEST(ReferrerUtilTest
, Sanitization
) {
22 GURL
unsanitized("http://user:password@foo.com/bar/baz.html#fragment");
23 GURL sanitized
= web::ReferrerForHeader(unsanitized
);
24 EXPECT_EQ("http://foo.com/bar/baz.html", sanitized
.spec());
27 TEST(ReferrerUtilTest
, DefaultPolicy
) {
28 // Default: all but secure->insecure should have a full referrer.
29 for (unsigned int source
= Empty
; source
< arraysize(test_urls
); ++source
) {
30 for (unsigned int dest
= Insecure
; dest
< arraysize(test_urls
); ++dest
) {
31 web::Referrer
referrer(GURL(test_urls
[source
]),
32 web::ReferrerPolicyDefault
);
33 std::string value
= web::ReferrerHeaderValueForNavigation(
34 GURL(test_urls
[dest
]), referrer
);
37 else if (source
== Secure
&& dest
== Insecure
)
40 EXPECT_EQ(test_urls
[source
], value
);
42 net::URLRequest::ReferrerPolicy policy
=
43 web::PolicyForNavigation(GURL(test_urls
[dest
]), referrer
);
45 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
51 TEST(ReferrerUtilTest
, NeverPolicy
) {
52 // Never: always empty string.
53 for (unsigned int source
= Empty
; source
< arraysize(test_urls
); ++source
) {
54 for (unsigned int dest
= Insecure
; dest
< arraysize(test_urls
); ++dest
) {
55 web::Referrer
referrer(GURL(test_urls
[source
]),
56 web::ReferrerPolicyNever
);
57 std::string value
= web::ReferrerHeaderValueForNavigation(
58 GURL(test_urls
[dest
]), referrer
);
61 net::URLRequest::ReferrerPolicy policy
=
62 web::PolicyForNavigation(GURL(test_urls
[dest
]), referrer
);
64 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
70 TEST(ReferrerUtilTest
, AlwaysPolicy
) {
71 // Always: always the full referrer.
72 for (unsigned int source
= Empty
; source
< arraysize(test_urls
); ++source
) {
73 for (unsigned int dest
= Insecure
; dest
< arraysize(test_urls
); ++dest
) {
74 web::Referrer
referrer(GURL(test_urls
[source
]),
75 web::ReferrerPolicyAlways
);
76 std::string value
= web::ReferrerHeaderValueForNavigation(
77 GURL(test_urls
[dest
]), referrer
);
78 EXPECT_EQ(test_urls
[source
], value
);
80 net::URLRequest::ReferrerPolicy policy
=
81 web::PolicyForNavigation(GURL(test_urls
[dest
]), referrer
);
82 if (source
== Empty
) {
83 EXPECT_EQ(net::URLRequest::
84 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
87 EXPECT_EQ(net::URLRequest::NEVER_CLEAR_REFERRER
, policy
);
93 TEST(ReferrerUtilTest
, OriginPolicy
) {
94 // Always: always just the origin.
95 for (unsigned int source
= Empty
; source
< arraysize(test_urls
); ++source
) {
96 for (unsigned int dest
= Insecure
; dest
< arraysize(test_urls
); ++dest
) {
97 web::Referrer
referrer(GURL(test_urls
[source
]),
98 web::ReferrerPolicyOrigin
);
99 std::string value
= web::ReferrerHeaderValueForNavigation(
100 GURL(test_urls
[dest
]), referrer
);
101 EXPECT_EQ(GURL(test_urls
[source
]).GetOrigin().spec(), value
);
103 net::URLRequest::ReferrerPolicy policy
=
104 web::PolicyForNavigation(GURL(test_urls
[dest
]), referrer
);
105 if (source
== Empty
) {
106 EXPECT_EQ(net::URLRequest::
107 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
110 EXPECT_EQ(net::URLRequest::NEVER_CLEAR_REFERRER
, policy
);