Bug 1423495 - Part2: Test case, r=baku
Test steps:
1. Create a XHR to get serverTiming.sjs.
2. Add Server-Timing headers in serverTiming.sjs.
3. Check if the value from PerformanceResourceTiming is correct.
MozReview-Commit-ID: KOQhoFQv4fy
--- a/dom/performance/tests/mochitest.ini
+++ b/dom/performance/tests/mochitest.ini
@@ -4,17 +4,21 @@ support-files =
test_performance_user_timing.js
test_worker_performance_now.js
worker_performance_user_timing.js
worker_performance_observer.js
sharedworker_performance_user_timing.js
test_worker_performance_entries.js
test_worker_performance_entries.sjs
empty.js
+ serverTiming.sjs
[test_performance_observer.html]
[test_performance_user_timing.html]
[test_worker_user_timing.html]
[test_worker_observer.html]
[test_sharedWorker_performance_user_timing.html]
[test_worker_performance_now.html]
[test_timeOrigin.html]
[test_worker_performance_entries.html]
+[test_performance_server_timing.html]
+scheme = https
+[test_performance_server_timing_plain_http.html]
new file mode 100644
--- /dev/null
+++ b/dom/performance/tests/serverTiming.sjs
@@ -0,0 +1,32 @@
+
+var responseServerTiming = [{metric:"metric1", duration:"123.4", description:"description1"},
+ {metric:"metric2", duration:"456.78", description:"description2"}];
+var trailerServerTiming = [{metric:"metric3", duration:"789.11", description:"description3"},
+ {metric:"metric4", duration:"1112.13", description:"description4"}];
+
+function createServerTimingHeader(headerData) {
+ var header = "";
+ for (var i = 0; i < headerData.length; i++) {
+ header += "Server-Timing:" + headerData[i].metric + ";" +
+ "dur=" + headerData[i].duration + ";" +
+ "desc=" + headerData[i].description + "\r\n";
+ }
+ return header;
+}
+
+function handleRequest(request, response)
+{
+ var body = "c\r\ndata reached\r\n3\r\nhej\r\n0\r\n";
+
+ response.seizePower();
+ response.write("HTTP/1.1 200 OK\r\n");
+ response.write("Content-Type: text/plain\r\n");
+ response.write(createServerTimingHeader(responseServerTiming));
+
+ response.write("Transfer-Encoding: chunked\r\n");
+ response.write("\r\n");
+ response.write(body);
+ response.write(createServerTimingHeader(trailerServerTiming));
+ response.write("\r\n");
+ response.finish();
+}
new file mode 100644
--- /dev/null
+++ b/dom/performance/tests/test_performance_server_timing.html
@@ -0,0 +1,58 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset=utf-8>
+<title>Test for PerformanceServerTiming</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+</head>
+<body>
+<div id="log"></div>
+<script>
+function makeXHR(aUrl) {
+ var xmlhttp = new XMLHttpRequest();
+ xmlhttp.open("get", aUrl, true);
+ xmlhttp.send();
+}
+
+// Note that |responseServerTiming| and |trailerServerTiming| SHOULD be synced with
+// the ones in serverTiming.sjs.
+var responseServerTiming = [{metric:"metric1", duration:"123.4", description:"description1"},
+ {metric:"metric2", duration:"456.78", description:"description2"}];
+var trailerServerTiming = [{metric:"metric3", duration:"789.11", description:"description3"},
+ {metric:"metric4", duration:"1112.13", description:"description4"}];
+
+function checkServerTimingContent(serverTiming) {
+ var expectedResult = responseServerTiming.concat(trailerServerTiming);
+ assert_equals(serverTiming.length, expectedResult.length);
+
+ for (var i = 0; i < expectedResult.length; i++) {
+ assert_equals(serverTiming[i].name, expectedResult[i].metric);
+ assert_equals(serverTiming[i].description, expectedResult[i].description);
+ assert_equals(serverTiming[i].duration, parseFloat(expectedResult[i].duration));
+ }
+}
+
+promise_test(t => {
+ var promise = new Promise(resolve => {
+ performance.clearResourceTimings();
+
+ var observer = new PerformanceObserver(list => resolve(list));
+ observer.observe({entryTypes: ['resource']});
+ t.add_cleanup(() => observer.disconnect());
+ });
+
+ makeXHR("serverTiming.sjs");
+
+ return promise.then(list => {
+ assert_equals(list.getEntries().length, 1);
+ checkServerTimingContent(list.getEntries()[0].serverTiming);
+ });
+}, "server-timing test");
+
+</script>
+</body>
new file mode 100644
--- /dev/null
+++ b/dom/performance/tests/test_performance_server_timing_plain_http.html
@@ -0,0 +1,40 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset=utf-8>
+<title>Plain HTTP Test for PerformanceServerTiming</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+</head>
+<body>
+<div id="log"></div>
+<script>
+function makeXHR(aUrl) {
+ var xmlhttp = new XMLHttpRequest();
+ xmlhttp.open("get", aUrl, true);
+ xmlhttp.send();
+}
+
+promise_test(t => {
+ var promise = new Promise(resolve => {
+ performance.clearResourceTimings();
+
+ var observer = new PerformanceObserver(list => resolve(list));
+ observer.observe({entryTypes: ['resource']});
+ t.add_cleanup(() => observer.disconnect());
+ });
+
+ makeXHR("serverTiming.sjs");
+
+ return promise.then(list => {
+ assert_equals(list.getEntries().length, 1);
+ assert_equals(list.getEntries()[0].serverTiming, undefined);
+ });
+}, "server-timing test");
+
+</script>
+</body>