Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / loader / LinkHeaderTest.cpp
blobaec730ffd58c4aabbfd65d7666f9c8aece5fe147
1 // Copyright 2015 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 "config.h"
6 #include "core/loader/LinkHeader.h"
8 #include <base/macros.h>
9 #include <gtest/gtest.h>
11 namespace blink {
13 TEST(LinkHeaderTest, Empty)
15 String nullString;
16 LinkHeaderSet nullHeaderSet(nullString);
17 ASSERT_EQ(nullHeaderSet.size(), unsigned(0));
18 String emptyString("");
19 LinkHeaderSet emptyHeaderSet(emptyString);
20 ASSERT_EQ(emptyHeaderSet.size(), unsigned(0));
23 TEST(LinkHeaderTest, Single)
25 struct TestCase {
26 const char* headerValue;
27 const char* url;
28 const char* rel;
29 bool valid;
30 } cases[] = {
31 {"</images/cat.jpg>; rel=prefetch", "/images/cat.jpg", "prefetch", true},
32 {"</images/cat.jpg>;rel=prefetch", "/images/cat.jpg", "prefetch", true},
33 {"</images/cat.jpg> ;rel=prefetch", "/images/cat.jpg", "prefetch", true},
34 {"</images/cat.jpg> ; rel=prefetch", "/images/cat.jpg", "prefetch", true},
35 {"< /images/cat.jpg> ; rel=prefetch", "/images/cat.jpg", "prefetch", true},
36 {"</images/cat.jpg > ; rel=prefetch", "/images/cat.jpg", "prefetch", true},
37 {"</images/cat.jpg wutwut> ; rel=prefetch", "/images/cat.jpg", "prefetch", true},
38 {"</images/cat.jpg wutwut \t > ; rel=prefetch", "/images/cat.jpg", "prefetch", true},
39 {"</images/cat.jpg>; rel=prefetch ", "/images/cat.jpg", "prefetch", true},
40 {"</images/cat.jpg>; Rel=prefetch ", "/images/cat.jpg", "prefetch", true},
41 {"</images/cat.jpg>; Rel=PReFetCh ", "/images/cat.jpg", "prefetch", true},
42 {"</images/cat.jpg>; rel=prefetch; rel=somethingelse", "/images/cat.jpg", "prefetch", true},
43 {" </images/cat.jpg>; rel=prefetch ", "/images/cat.jpg", "prefetch", true},
44 {"\t </images/cat.jpg>; rel=prefetch ", "/images/cat.jpg", "prefetch", true},
45 {"</images/cat.jpg>\t\t ; \trel=prefetch \t ", "/images/cat.jpg", "prefetch", true},
46 {"\f</images/cat.jpg>\t\t ; \trel=prefetch \t ", "", "", false},
47 {"</images/cat.jpg>; rel= prefetch", "/images/cat.jpg", "prefetch", true},
48 {"</images/cat.jpg>; rel =prefetch", "/images/cat.jpg", "prefetch", true},
49 {"</images/cat.jpg>; rel pel=prefetch", "/images/cat.jpg", "", false},
50 {"< /images/cat.jpg>", "/images/cat.jpg", "", true},
51 {"</images/cat.jpg>; rel =", "/images/cat.jpg", "", false},
52 {"</images/cat.jpg>; wut=sup; rel =prefetch", "/images/cat.jpg", "prefetch", true},
53 {"</images/cat.jpg>; wut=sup ; rel =prefetch", "/images/cat.jpg", "prefetch", true},
54 {"</images/cat.jpg>; wut=sup ; rel =prefetch \t ;", "/images/cat.jpg", "prefetch", true},
55 {"</images/cat.jpg> wut=sup ; rel =prefetch \t ;", "/images/cat.jpg", "", false},
56 {"< /images/cat.jpg", "", "", false},
57 {"< http://wut.com/ sdfsdf ?sd>; rel=dns-prefetch", "http://wut.com/", "dns-prefetch", true},
58 {"< http://wut.com/%20%20%3dsdfsdf?sd>; rel=dns-prefetch", "http://wut.com/%20%20%3dsdfsdf?sd", "dns-prefetch", true},
59 {"< http://wut.com/dfsdf?sdf=ghj&wer=rty>; rel=prefetch", "http://wut.com/dfsdf?sdf=ghj&wer=rty", "prefetch", true},
60 {"< http://wut.com/dfsdf?sdf=ghj&wer=rty>;;;;; rel=prefetch", "http://wut.com/dfsdf?sdf=ghj&wer=rty", "prefetch", true},
61 {"</images/cat.jpg>; anchor=foo; rel=prefetch;", "/images/cat.jpg", "", false},
62 {"</images/cat.jpg>; rel=prefetch;anchor=foo ", "/images/cat.jpg", "prefetch", false},
63 {"</images/cat.jpg>; anchor='foo'; rel=prefetch;", "/images/cat.jpg", "", false},
64 {"</images/cat.jpg>; rel=prefetch;anchor='foo' ", "/images/cat.jpg", "prefetch", false},
65 {"</images/cat.jpg>; rel=prefetch;anchor='' ", "/images/cat.jpg", "prefetch", false},
66 {"</images/cat.jpg>; rel=prefetch;", "/images/cat.jpg", "prefetch", true},
67 {"</images/cat.jpg>; rel=prefetch ;", "/images/cat.jpg", "prefetch", true},
68 {"</images/ca,t.jpg>; rel=prefetch ;", "/images/ca,t.jpg", "prefetch", true},
69 {"<simple.css>; rel=stylesheet; title=\"title with a DQUOTE and backslash\"", "simple.css", "stylesheet", true},
70 {"<simple.css>; rel=stylesheet; title=\"title with a DQUOTE \\\" and backslash: \\\"", "simple.css", "stylesheet", false},
71 {"<simple.css>; title=\"title with a DQUOTE \\\" and backslash: \"; rel=stylesheet; ", "simple.css", "stylesheet", true},
72 {"<simple.css>; title=\'title with a DQUOTE \\\' and backslash: \'; rel=stylesheet; ", "simple.css", "stylesheet", true},
73 {"<simple.css>; title=\"title with a DQUOTE \\\" and ;backslash,: \"; rel=stylesheet; ", "simple.css", "stylesheet", true},
74 {"<simple.css>; title=\"title with a DQUOTE \' and ;backslash,: \"; rel=stylesheet; ", "simple.css", "stylesheet", true},
75 {"<simple.css>; title=\"\"; rel=stylesheet; ", "simple.css", "stylesheet", true},
76 {"<simple.css>; title=\"\"; rel=\"stylesheet\"; ", "simple.css", "stylesheet", true},
77 {"<simple.css>; rel=stylesheet; title=\"", "simple.css", "stylesheet", false},
78 {"<simple.css>; rel=stylesheet; title=\"\"", "simple.css", "stylesheet", true},
79 {"<simple.css>; rel=\"stylesheet\"; title=\"", "simple.css", "stylesheet", false},
80 {"<simple.css>; rel=\";style,sheet\"; title=\"", "simple.css", ";style,sheet", false},
81 {"<simple.css>; rel=\"bla'sdf\"; title=\"", "simple.css", "bla'sdf", false},
82 {"<simple.css>; rel=\"\"; title=\"\"", "simple.css", "", true},
83 {"<simple.css>; rel=''; title=\"\"", "simple.css", "", true},
84 {"<simple.css>; rel=''; title=", "simple.css", "", false},
85 {"<simple.css>; rel=''; title", "simple.css", "", false},
86 {"<simple.css>; rel=''; media", "simple.css", "", false},
87 {"<simple.css>; rel=''; hreflang", "simple.css", "", false},
88 {"<simple.css>; rel=''; type", "simple.css", "", false},
89 {"<simple.css>; rel=''; rev", "simple.css", "", false},
90 {"<simple.css>; rel=''; bla", "simple.css", "", true},
91 {"<simple.css>; rel='prefetch", "simple.css", "", false},
92 {"<simple.css>; rel=\"prefetch", "simple.css", "", false},
93 {"<simple.css>; rel=\"", "simple.css", "", false},
97 // Test the cases with a single header
98 for (auto& testCase : cases) {
99 LinkHeaderSet headerSet(testCase.headerValue);
100 LinkHeader& header = headerSet[0];
101 ASSERT_STREQ(testCase.url, header.url().ascii().data());
102 ASSERT_STREQ(testCase.rel, header.rel().ascii().data());
103 ASSERT_EQ(testCase.valid, header.valid());
107 TEST(LinkHeaderTest, Double)
109 struct DoubleTestCase {
110 const char* headerValue;
111 const char* url;
112 const char* rel;
113 bool valid;
114 const char* url2;
115 const char* rel2;
116 bool valid2;
117 } cases[] = {
118 {"<ybg.css>; rel=stylesheet, <simple.css>; rel=stylesheet", "ybg.css", "stylesheet", true, "simple.css", "stylesheet", true},
119 {"<ybg.css>; rel=stylesheet,<simple.css>; rel=stylesheet", "ybg.css", "stylesheet", true, "simple.css", "stylesheet", true},
120 {"<ybg.css>; rel=stylesheet;crossorigin,<simple.css>; rel=stylesheet", "ybg.css", "stylesheet", true, "simple.css", "stylesheet", true},
123 for (auto& testCase : cases) {
124 LinkHeaderSet headerSet(testCase.headerValue);
125 LinkHeader& header1 = headerSet[0];
126 LinkHeader& header2 = headerSet[1];
127 ASSERT_STREQ(testCase.url, header1.url().ascii().data());
128 ASSERT_STREQ(testCase.rel, header1.rel().ascii().data());
129 ASSERT_EQ(testCase.valid, header1.valid());
130 ASSERT_STREQ(testCase.url2, header2.url().ascii().data());
131 ASSERT_STREQ(testCase.rel2, header2.rel().ascii().data());
132 ASSERT_EQ(testCase.valid2, header2.valid());
136 TEST(LinkHeaderTest, CrossOrigin)
138 struct TestCase {
139 const char* headerValue;
140 const char* url;
141 const char* rel;
142 const CrossOriginAttributeValue crossorigin;
143 bool valid;
144 } cases[] = {
145 {"<http://whatever.com>; rel=preconnect", "http://whatever.com", "preconnect", CrossOriginAttributeNotSet, true},
146 {"<http://whatever.com>; rel=preconnect; crossorigin=", "http://whatever.com", "preconnect", CrossOriginAttributeAnonymous, true},
147 {"<http://whatever.com>; rel=preconnect; crossorigin", "http://whatever.com", "preconnect", CrossOriginAttributeAnonymous, true},
148 {"<http://whatever.com>; rel=preconnect; crossorigin;", "http://whatever.com", "preconnect", CrossOriginAttributeAnonymous, true},
149 {"<http://whatever.com>; rel=preconnect; crossorigin, <http://whatever2.com>; rel=preconnect", "http://whatever.com", "preconnect", CrossOriginAttributeAnonymous, true},
150 {"<http://whatever.com>; rel=preconnect; crossorigin,<http://whatever2.com>; rel=preconnect", "http://whatever.com", "preconnect", CrossOriginAttributeAnonymous, true},
151 {"<http://whatever.com>; rel=preconnect; crossorigin=anonymous", "http://whatever.com", "preconnect", CrossOriginAttributeAnonymous, true},
152 {"<http://whatever.com>; rel=preconnect; crossorigin=use-credentials", "http://whatever.com", "preconnect", CrossOriginAttributeUseCredentials, true},
153 {"<http://whatever.com>; rel=preconnect; crossorigin=whatever", "http://whatever.com", "preconnect", CrossOriginAttributeAnonymous, true},
154 {"<http://whatever.com>; rel=preconnect; crossorig|in=whatever", "http://whatever.com", "preconnect", CrossOriginAttributeNotSet, true},
155 {"<http://whatever.com>; rel=preconnect; crossorigin|=whatever", "http://whatever.com", "preconnect", CrossOriginAttributeNotSet, true},
156 {"<http://whatever.com>; rel=preconnect; valid!", "http://whatever.com", "preconnect", CrossOriginAttributeNotSet, true},
157 {"<http://whatever.com>; rel=preconnect; valid$", "http://whatever.com", "preconnect", CrossOriginAttributeNotSet, true},
158 {"<http://whatever.com>; rel=preconnect; invalid@", "http://whatever.com", "preconnect", CrossOriginAttributeNotSet, false},
159 {"<http://whatever.com>; rel=preconnect; invalid*", "http://whatever.com", "preconnect", CrossOriginAttributeNotSet, false},
163 // Test the cases with a single header
164 for (auto& testCase : cases) {
165 LinkHeaderSet headerSet(testCase.headerValue);
166 LinkHeader& header = headerSet[0];
167 ASSERT_STREQ(testCase.url, header.url().ascii().data());
168 ASSERT_STREQ(testCase.rel, header.rel().ascii().data());
169 ASSERT_EQ(testCase.crossorigin, header.crossOrigin());
170 ASSERT_EQ(testCase.valid, header.valid());
173 } // namespace blink