Skip to content

Commit

Permalink
fetch: improve util.inspect output for web specifications (#2938)
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcanaltin authored Mar 10, 2024
1 parent 3d4cf43 commit 70f2871
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -576,8 +577,18 @@ class Headers {

return this[kHeadersList]
}

[util.inspect.custom] (depth, options) {
const inspected = util.inspect(this[kHeadersList].entries)

return `Headers ${inspected}`
}
}

Object.defineProperty(Headers.prototype, util.inspect.custom, {
enumerable: false
})

iteratorMixin('Headers', Headers, kHeadersSortedMap, 0, 1)

Object.defineProperties(Headers.prototype, {
Expand Down
17 changes: 17 additions & 0 deletions test/fetch/headers-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'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 { 'Content-Type': 'application/json', Authorization: 'Bearer token' }"
assert.strictEqual(inspectedOutput, expectedOutput)
})

0 comments on commit 70f2871

Please sign in to comment.