diff --git a/lib/preprocess.js b/lib/preprocess.js index e2527b32..7092bfb0 100644 --- a/lib/preprocess.js +++ b/lib/preprocess.js @@ -268,7 +268,14 @@ export const assertValidMeasurement = measurement => { assert(measurement.end_at >= measurement.first_byte_at, 'end_at must be greater than or equal to first_byte_at') assert(measurement.first_byte_at >= measurement.start_at, 'first_byte_at must be greater than or equal to start_at') - assert.strictEqual(typeof measurement.head_status_code, 'number', '`head_status_code` must be a number') + if (measurement.protocol === 'http') { + assert.strictEqual(typeof measurement.head_status_code, 'number', '`head_status_code` must be a number') + } else { + assert( + measurement.head_status_code === undefined || measurement.head_status_code === null, + '`head_status_code` must be undefined or null for non-HTTP retrievals' + ) + } } } diff --git a/test/preprocess.js b/test/preprocess.js index f13e7ad1..d15d94bf 100644 --- a/test/preprocess.js +++ b/test/preprocess.js @@ -314,6 +314,7 @@ describe('assertValidMeasurement', () => { assert.throws( () => assertValidMeasurement({ ...VALID_MEASUREMENT, + protocol: 'http', head_status_code: null }), /`head_status_code` must be a number/ @@ -321,12 +322,40 @@ describe('assertValidMeasurement', () => { assert.throws( () => assertValidMeasurement({ ...VALID_MEASUREMENT, + protocol: 'http', head_status_code: /** @type {any} */ ('200') }), /`head_status_code` must be a number/ ) }) + it('accepts Graphsync measurements with OK retrieval result and head_status_code is null', () => { + assertValidMeasurement({ + ...VALID_MEASUREMENT, + protocol: 'graphsync', + head_status_code: null + + }) + }) + + it('accepts Graphsync measurements with OK retrieval result and head_status_code is undefined', () => { + assertValidMeasurement({ + ...VALID_MEASUREMENT, + protocol: 'graphsync', + head_status_code: undefined + }) + }) + + it('rejects Graphsync measurements with head_status_code not null/undefined ', () => { + assert.throws( + () => assertValidMeasurement({ + ...VALID_MEASUREMENT, + head_status_code: /** @type {any} */ ('200') + }), + /`head_status_code` must be undefined/ + ) + }) + it('should throw an error for invalid start_at', () => { const measurement = { ...VALID_MEASUREMENT,