Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch: improve util.inspect output for web specifications #2938

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved

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')
Dismissed Show dismissed Hide dismissed

const inspectedOutput = util.inspect(headers, { depth: 1 })

const expectedOutput = "Headers { 'Content-Type': 'application/json', Authorization: 'Bearer token' }"
assert.strictEqual(inspectedOutput, expectedOutput)
})