From 628e3139496728e25dead3760f47e011a9351629 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sat, 9 Mar 2024 12:41:51 +0300 Subject: [PATCH 1/3] fetch: improve util.inspect output for web specifications --- lib/web/fetch/headers.js | 14 ++++++++++++++ test/fetch/headers-inspect-custom.js | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/fetch/headers-inspect-custom.js diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index b3ec5a70711..cd1e02901ab 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -12,6 +12,7 @@ const { } = require('./util') const { webidl } = require('./webidl') const assert = require('node:assert') +const util = require('util') const kHeadersMap = Symbol('headers map') const kHeadersSortedMap = Symbol('headers map sorted') @@ -576,8 +577,21 @@ class Headers { return this[kHeadersList] } + + [util.inspect.custom] (depth, options) { + const headerString = Array.from(this[kHeadersList]) + .map(([name, value]) => + `${name.replace(/\b\w/g, (char) => + char.toUpperCase())}: ${value}`) + .join('\n') + return 'Headers:\n' + headerString + } } +Object.defineProperty(Headers.prototype, util.inspect.custom, { + enumerable: false +}) + iteratorMixin('Headers', Headers, kHeadersSortedMap, 0, 1) Object.defineProperties(Headers.prototype, { diff --git a/test/fetch/headers-inspect-custom.js b/test/fetch/headers-inspect-custom.js new file mode 100644 index 00000000000..7c1a0fe9f63 --- /dev/null +++ b/test/fetch/headers-inspect-custom.js @@ -0,0 +1,18 @@ +'use strict' + +const { Headers } = require('../../lib/web/fetch/headers') +const { test } = require('node:test') +const assert = require('node:assert') +const util = require('util') + +test('Headers class custom inspection', () => { + const headers = new Headers() + headers.set('content-Type', 'application/json') + headers.set('authorization', 'Bearer token') + + const inspectedOutput = util.inspect(headers, { depth: 1 }) + + const expectedOutput = + 'Headers:\nContent-Type: application/json\nAuthorization: Bearer token' + assert.strictEqual(inspectedOutput, expectedOutput) +}) From 1ff0166cf16dc21df71787b7d96a5259d6859830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20Can=20Alt=C4=B1n?= Date: Sat, 9 Mar 2024 22:31:04 +0300 Subject: [PATCH 2/3] Update headers.js Co-authored-by: Khafra --- lib/web/fetch/headers.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index cd1e02901ab..c8a9f416933 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -579,12 +579,9 @@ class Headers { } [util.inspect.custom] (depth, options) { - const headerString = Array.from(this[kHeadersList]) - .map(([name, value]) => - `${name.replace(/\b\w/g, (char) => - char.toUpperCase())}: ${value}`) - .join('\n') - return 'Headers:\n' + headerString + const inspected = util.inspect(this[kHeadersList].entries) + + return `Headers ${inspected}` } } From 10f46e8737913f0785b86b21540abf3a816990f3 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Sun, 10 Mar 2024 12:41:29 +0300 Subject: [PATCH 3/3] fix: test repair --- lib/web/fetch/headers.js | 2 +- test/fetch/headers-inspect-custom.js | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index c8a9f416933..b2580e90e04 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -580,7 +580,7 @@ class Headers { [util.inspect.custom] (depth, options) { const inspected = util.inspect(this[kHeadersList].entries) - + return `Headers ${inspected}` } } diff --git a/test/fetch/headers-inspect-custom.js b/test/fetch/headers-inspect-custom.js index 7c1a0fe9f63..1aa3326e98c 100644 --- a/test/fetch/headers-inspect-custom.js +++ b/test/fetch/headers-inspect-custom.js @@ -7,12 +7,11 @@ const util = require('util') test('Headers class custom inspection', () => { const headers = new Headers() - headers.set('content-Type', 'application/json') - headers.set('authorization', 'Bearer token') + headers.set('Content-Type', 'application/json') + headers.set('Authorization', 'Bearer token') const inspectedOutput = util.inspect(headers, { depth: 1 }) - const expectedOutput = - 'Headers:\nContent-Type: application/json\nAuthorization: Bearer token' + const expectedOutput = "Headers { 'Content-Type': 'application/json', Authorization: 'Bearer token' }" assert.strictEqual(inspectedOutput, expectedOutput) })