summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resource-timing/content-type.html
blob: f6b1db7d9f8e7133de365d2f40bc05057be13070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>This test validates the content-type of resources.</title>
<link rel="help" href="https://www.w3.org/TR/resource-timing-2/#sec-performanceresourcetiming"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/entry-invariants.js"></script>
<script src="resources/resource-loaders.js"></script>
<script src="/common/get-host-info.sub.js"></script>
</head>
<body>
<script>
const {ORIGIN, REMOTE_ORIGIN} = get_host_info();
const SAME_ORIGIN = location.origin;


// Content-type for same origin resources is exposed.
const run_test = (loader, contentType) => {
  let path = `/resource-timing/resources/content-type.py?content_type=${contentType}`;
  const url = new URL(path, ORIGIN);
  attribute_test(
    loader, url,
    entry => {
        assert_equals(entry.contentType, contentType,
            `content-type for ${entry.name} should be ${contentType}`);
    });
}

// Content-type is empty string when a no-cors request is made for cross
// origin resource.
// Content-type is empty for cross origin iframes.
const run_test_cross_origin = (loader, contentType) => {
  let path = `/resource-timing/resources/content-type.py?content_type=${contentType}`;
  const url = new URL(path, REMOTE_ORIGIN);
  attribute_test(
    loader, url,
    entry => {
        assert_equals(entry.contentType, "",
            `content-type for ${entry.name} should be ""`);
    });
}

const resource_loaders_and_types = [
  [load.font, ["font/woff", "font/otf"]],
  [load.image, ["image/png", "image/jpg"]],
  [load.script, ["application/javascript", "text/javascript"]],
  [load.stylesheet, ["text/css"]],
  [load.xhr_async, ["application/x-gzip", "application/pdf"]],
  [load.iframe, ["text/html"]]
];

resource_loaders_and_types.forEach(resource => {
  let loader = resource[0];
  let content_types = resource[1];
  content_types.forEach(type => {
    run_test(loader, type);
    run_test_cross_origin(loader, type);
  })
});


// Content-type is exposed for cors request for cross-origin resources.
const run_test_cross_origin_allow_origin = (loader_with_attr,contentType) => {
  let path = `/resource-timing/resources/content-type.py?content_type=${contentType}&allow_origin=${ORIGIN}`;
  const url = new URL(path, REMOTE_ORIGIN);
  loader_with_crossOrigin_attr = async url => {
    return loader_with_attr(url, {"crossOrigin": "anonymous"});
  }
  attribute_test(
    loader_with_crossOrigin_attr, url,
    entry => {
        assert_equals(entry.contentType, contentType,
            `content-type for ${entry.name} should be ${contentType}`);
    });
}

const resource_loaders_with_attrs_and_types = [
  [load.image_with_attrs, ["image/gif", "image/jpeg"]],
  [load.script_with_attrs, ["application/javascript", "text/javascript"]],
  [load.stylesheet_with_attrs, ["text/css"]],
]

resource_loaders_with_attrs_and_types.forEach(resource => {
  let loader = resource[0];
  let content_types = resource[1];
  content_types.forEach(type => {
    run_test_cross_origin_allow_origin(loader, type);
  })
});

// Content-type for iframes is empty when cross origin redirects are present.
var destUrl = `${SAME_ORIGIN}/resource-timing/resources/multi_redirect.py?`;
destUrl += `page_origin=${SAME_ORIGIN}`;
destUrl += `&cross_origin=${REMOTE_ORIGIN}`;
destUrl += `&final_resource=/resource-timing/resources/content-type.py?content_type=text/html`;
attribute_test(
    load.iframe, new URL(destUrl),
    entry => {
        assert_equals(entry.contentType, "",
            `content-type should be empty for iframes having cross origin redirects`);
});


// Content-type for iframes is exposed for same origin redirects.
var destUrl = `${SAME_ORIGIN}/resource-timing/resources/redirect-cors.py`;
destUrl += `?location=${SAME_ORIGIN}/resource-timing/resources/content-type.py?content_type=text/html`;
attribute_test(
    load.iframe, new URL(destUrl),
    entry => {
        assert_equals(entry.contentType, "text/html",
            `content-type should be exposed for iframes having only same origin redirects`);
});

</script>
</body>
</html>