From c8a079be0f60efdf257178f8b634da841d1dbc12 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Thu, 8 Aug 2019 23:39:11 -0500 Subject: [PATCH 1/3] tests(smokehouse): use mime-types for content-type. use proper encoding for response write --- lighthouse-cli/test/fixtures/static-server.js | 32 +++++++------------ .../test/smokehouse/perf/expectations.js | 4 +-- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/lighthouse-cli/test/fixtures/static-server.js b/lighthouse-cli/test/fixtures/static-server.js index 2a0cdf6eacee..7a6ba8ea2355 100644 --- a/lighthouse-cli/test/fixtures/static-server.js +++ b/lighthouse-cli/test/fixtures/static-server.js @@ -12,6 +12,7 @@ const http = require('http'); const zlib = require('zlib'); const path = require('path'); const fs = require('fs'); +const mime = require('mime-types'); const parseQueryString = require('querystring').parse; const parseURL = require('url').parse; const URLSearchParams = require('url').URLSearchParams; @@ -63,23 +64,13 @@ function requestHandler(request, response) { function sendResponse(statusCode, data) { const headers = {'Access-Control-Allow-Origin': '*'}; - if (filePath.endsWith('.js')) { - headers['Content-Type'] = 'text/javascript'; - } else if (filePath.endsWith('.css')) { - headers['Content-Type'] = 'text/css'; - } else if (filePath.endsWith('.svg')) { - headers['Content-Type'] = 'image/svg+xml'; - } else if (filePath.endsWith('.png')) { - headers['Content-Type'] = 'image/png'; - } else if (filePath.endsWith('.gif')) { - headers['Content-Type'] = 'image/gif'; - } else if (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg')) { - headers['Content-Type'] = 'image/jpeg'; - } else if (filePath.endsWith('.webp')) { - headers['Content-Type'] = 'image/webp'; - } else if (filePath.endsWith('.json')) { - headers['Content-Type'] = 'application/json'; - } + const contentType = mime.lookup(filePath); + const charset = mime.lookup(contentType); + // `mime.contentType` appends the correct charset too. + // Note: it seems to miss just one case, svg. Doesn't matter much, we'll just allow + // svgs to be sent without the proper encoding. + // see https://github.com/jshttp/mime-types/issues/66 + if (contentType) headers['Content-Type'] = mime.contentType(contentType); let delay = 0; let useGzip = false; @@ -127,7 +118,8 @@ function requestHandler(request, response) { return setTimeout(finishResponse, delay, data); } - finishResponse(data); + const encoding = charset === 'UTF-8' ? 'utf-8' : 'binary'; + finishResponse(data, encoding); } function sendRedirect(url) { @@ -138,8 +130,8 @@ function requestHandler(request, response) { response.end(); } - function finishResponse(data) { - response.write(data, 'binary'); + function finishResponse(data, encoding) { + response.write(data, encoding); response.end(); } } diff --git a/lighthouse-cli/test/smokehouse/perf/expectations.js b/lighthouse-cli/test/smokehouse/perf/expectations.js index 45b1e239905f..4ea41b4d5432 100644 --- a/lighthouse-cli/test/smokehouse/perf/expectations.js +++ b/lighthouse-cli/test/smokehouse/perf/expectations.js @@ -79,8 +79,8 @@ module.exports = [ {resourceType: 'font', requestCount: 2, size: '80000±1000'}, {resourceType: 'script', requestCount: 3, size: '55000±1000'}, {resourceType: 'image', requestCount: 2, size: '28000±1000'}, - {resourceType: 'document', requestCount: 1, size: '2100±100'}, - {resourceType: 'other', requestCount: 2, size: '1250±50'}, + {resourceType: 'document', requestCount: 1, size: '2200±100'}, + {resourceType: 'other', requestCount: 2, size: '1300±50'}, {resourceType: 'stylesheet', requestCount: 1, size: '450±100'}, {resourceType: 'media', requestCount: 0, size: 0}, {resourceType: 'third-party', requestCount: 0, size: 0}, From 45b2a92ca125c59311f9f3aeef9c54744cb5c924 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 9 Aug 2019 00:56:29 -0500 Subject: [PATCH 2/3] bytes --- .../test/smokehouse/byte-efficiency/expectations.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js b/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js index 9a00707a1ba3..722090bc0c1f 100644 --- a/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js +++ b/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js @@ -132,12 +132,12 @@ module.exports = [ }, { url: 'http://localhost:10200/byte-efficiency/script.js?gzip=1', - transferSize: 1136, + transferSize: 1158, resourceSize: 52997, }, { url: 'http://localhost:10200/byte-efficiency/script.js', - transferSize: 53181, + transferSize: 53203, resourceSize: 52997, }, { From 72095158ad8f717133832329d906cbf6da963674 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 9 Aug 2019 14:02:33 -0500 Subject: [PATCH 3/3] Update lighthouse-cli/test/fixtures/static-server.js Co-Authored-By: Patrick Hulce --- lighthouse-cli/test/fixtures/static-server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-cli/test/fixtures/static-server.js b/lighthouse-cli/test/fixtures/static-server.js index 7a6ba8ea2355..83d60fef781a 100644 --- a/lighthouse-cli/test/fixtures/static-server.js +++ b/lighthouse-cli/test/fixtures/static-server.js @@ -68,7 +68,7 @@ function requestHandler(request, response) { const charset = mime.lookup(contentType); // `mime.contentType` appends the correct charset too. // Note: it seems to miss just one case, svg. Doesn't matter much, we'll just allow - // svgs to be sent without the proper encoding. + // svgs to fallback to binary encoding. `Content-Type: image/svg+xml` is sufficient for our use case. // see https://github.com/jshttp/mime-types/issues/66 if (contentType) headers['Content-Type'] = mime.contentType(contentType);