From 8ff44ac030b1b45fa8ab3c85344a9696877e0b2f Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Tue, 6 Oct 2020 17:02:02 -0500 Subject: [PATCH 1/2] tests: add tests for networkRecordsToDevtoolsLog --- .../network-records-to-devtools-log-test.js | 63 +++++++++++++++++++ .../test/network-records-to-devtools-log.js | 30 +++++++-- 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 lighthouse-core/test/network-records-to-devtools-log-test.js diff --git a/lighthouse-core/test/network-records-to-devtools-log-test.js b/lighthouse-core/test/network-records-to-devtools-log-test.js new file mode 100644 index 000000000000..4270885f4d03 --- /dev/null +++ b/lighthouse-core/test/network-records-to-devtools-log-test.js @@ -0,0 +1,63 @@ +/** + * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +/* eslint-env jest */ + +const NetworkRecorder = require('../../lighthouse-core/lib/network-recorder.js'); +const networkRecordsToDevtoolsLog = require('./network-records-to-devtools-log.js'); +const lcpDevtoolsLog = require('./fixtures/traces/lcp-m78.devtools.log.json'); + +describe('networkRecordsToDevtoolsLog', () => { + it('should generate the four messages per request', () => { + const records = [{url: 'http://example.com'}]; + const log = networkRecordsToDevtoolsLog(records); + expect(log).toMatchObject([ + {method: 'Network.requestWillBeSent', params: {request: {url: 'http://example.com'}}}, + {method: 'Network.responseReceived', params: {response: {url: 'http://example.com'}}}, + {method: 'Network.dataReceived'}, + {method: 'Network.loadingFinished'}, + ]); + }); + + it('should set resource and transfer sizes', () => { + const records = [{url: 'http://example.com', resourceSize: 1024, transferSize: 2048}]; + const log = networkRecordsToDevtoolsLog(records); + expect(log).toMatchObject([ + {method: 'Network.requestWillBeSent', params: {request: {url: 'http://example.com'}}}, + {method: 'Network.responseReceived', params: {response: {url: 'http://example.com'}}}, + {method: 'Network.dataReceived', params: {dataLength: 1024, encodedDataLength: 2048}}, + {method: 'Network.loadingFinished', params: {encodedDataLength: 2048}}, + ]); + }); + + it('should handle redirects', () => { + const records = [ + {requestId: '0', url: 'http://example.com/'}, + {requestId: '0:redirect', url: 'http://www.example.com/'}, + ]; + + const log = networkRecordsToDevtoolsLog(records); + expect(log).toMatchObject([ + {method: 'Network.requestWillBeSent', params: {request: {url: 'http://example.com/'}}}, + {method: 'Network.requestWillBeSent', params: {request: {url: 'http://www.example.com/'}}}, + {method: 'Network.responseReceived', params: {response: {url: 'http://www.example.com/'}}}, + {method: 'Network.dataReceived'}, + {method: 'Network.loadingFinished'}, + ]); + }); + + it('should roundtrip a real devtools log properly', () => { + const records = NetworkRecorder.recordsFromLogs(lcpDevtoolsLog); + + // Skip verification in the method because we have circular references. + // We'll do our own stricter verification. + const roundTripLogs = networkRecordsToDevtoolsLog(records, {skipVerification: true}); + const roundTripRecords = NetworkRecorder.recordsFromLogs(roundTripLogs); + + expect(roundTripRecords).toEqual(records); + }); +}); diff --git a/lighthouse-core/test/network-records-to-devtools-log.js b/lighthouse-core/test/network-records-to-devtools-log.js index 6af4c24adc79..8b57ef7032de 100644 --- a/lighthouse-core/test/network-records-to-devtools-log.js +++ b/lighthouse-core/test/network-records-to-devtools-log.js @@ -63,7 +63,7 @@ function getRequestWillBeSentEvent(networkRecord, index) { initialPriority: networkRecord.priority || 'Low', isLinkPreload: networkRecord.isLinkPreload, }, - timestamp: networkRecord.startTime || 0, + timestamp: networkRecord.redirectResponseTimestamp || networkRecord.startTime || 0, wallTime: 0, initiator: initiator || {type: 'other'}, type: networkRecord.resourceType || 'Document', @@ -73,6 +73,19 @@ function getRequestWillBeSentEvent(networkRecord, index) { }; } +/** + * @param {Partial} networkRecord + * @return {LH.Protocol.RawEventMessage} + */ +function getRequestServedFromCacheEvent(networkRecord, index) { + return { + method: 'Network.requestServedFromCache', + params: { + requestId: getBaseRequestId(networkRecord) || `${idBase}.${index}`, + }, + }; +} + /** * @param {Partial} networkRecord * @return {LH.Protocol.RawEventMessage} @@ -97,11 +110,11 @@ function getResponseReceivedEvent(networkRecord, index) { url: networkRecord.url || exampleUrl, status: networkRecord.statusCode || 200, headers, - mimeType: networkRecord.mimeType || 'text/html', + mimeType: typeof networkRecord.mimeType === 'string' ? networkRecord.mimeType : 'text/html', connectionReused: networkRecord.connectionReused || false, connectionId: networkRecord.connectionId || 140, - fromDiskCache: networkRecord.fromDiskCache || undefined, - fromServiceWorker: networkRecord.fetchedViaServiceWorker || undefined, + fromDiskCache: networkRecord.fromDiskCache || false, + fromServiceWorker: networkRecord.fetchedViaServiceWorker || false, encodedDataLength: networkRecord.transferSize || 0, timing, protocol: networkRecord.protocol || 'http/1.1', @@ -177,7 +190,10 @@ function addRedirectResponseIfNeeded(networkRecords, record) { // populate `redirectResponse` with original's data, more or less. const originalResponse = getResponseReceivedEvent(originalRecord).params.response; originalResponse.status = originalRecord.statusCode || 302; - return Object.assign({}, record, {redirectResponse: originalResponse}); + return Object.assign({}, record, { + redirectResponseTimestamp: originalRecord.endTime, + redirectResponse: originalResponse, + }); } /** @@ -201,6 +217,10 @@ function networkRecordsToDevtoolsLog(networkRecords, options = {}) { return; } + if (networkRecord.fromMemoryCache) { + devtoolsLog.push(getRequestServedFromCacheEvent(networkRecord, index)); + } + devtoolsLog.push(getResponseReceivedEvent(networkRecord, index)); devtoolsLog.push(getDataReceivedEvent(networkRecord, index)); devtoolsLog.push(getLoadingFinishedEvent(networkRecord, index)); From 5990081870a1875867ac4590563d4595bdec5add Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Tue, 6 Oct 2020 17:48:20 -0500 Subject: [PATCH 2/2] spread --- lighthouse-core/test/network-records-to-devtools-log.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lighthouse-core/test/network-records-to-devtools-log.js b/lighthouse-core/test/network-records-to-devtools-log.js index 8b57ef7032de..4f5aff3bb95f 100644 --- a/lighthouse-core/test/network-records-to-devtools-log.js +++ b/lighthouse-core/test/network-records-to-devtools-log.js @@ -190,10 +190,11 @@ function addRedirectResponseIfNeeded(networkRecords, record) { // populate `redirectResponse` with original's data, more or less. const originalResponse = getResponseReceivedEvent(originalRecord).params.response; originalResponse.status = originalRecord.statusCode || 302; - return Object.assign({}, record, { + return { + ...record, redirectResponseTimestamp: originalRecord.endTime, redirectResponse: originalResponse, - }); + }; } /**