Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup custom format
Browse files Browse the repository at this point in the history
KhafraDev committed Mar 14, 2024
1 parent 633e5c8 commit 774a0a4
Showing 7 changed files with 30 additions and 18 deletions.
22 changes: 17 additions & 5 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
@@ -157,12 +157,24 @@ class FormData {
}

[nodeUtil.inspect.custom] (depth, options) {
let output = 'FormData:\n'
this[kState].forEach(entry => {
output += `${entry.name}: ${entry.value}\n`
})
const state = this[kState].reduce((a, b) => {
if (a[b.name]) {
if (Array.isArray(a[b.name])) {
a[b.name].push(b.value)
} else {
a[b.name] = [a[b.name], b.value]
}
} else {
a[b.name] = b.value
}

return a
}, { __proto__: null })

options.depth ??= depth
options.colors ??= true

return output
return `FormData ${nodeUtil.formatWithOptions(options, state)}`
}
}

10 changes: 2 additions & 8 deletions lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
@@ -572,16 +572,10 @@ class Headers {
return (this[kHeadersList][kHeadersSortedMap] = headers)
}

[Symbol.for('nodejs.util.inspect.custom')] () {
webidl.brandCheck(this, Headers)

return this[kHeadersList]
}

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

return `Headers ${inspected}`
return `Headers ${util.formatWithOptions(options, this[kHeadersList].entries)}`
}
}

4 changes: 3 additions & 1 deletion lib/web/fetch/request.js
Original file line number Diff line number Diff line change
@@ -778,6 +778,8 @@ class Request {
options.depth = 2
}

options.colors ??= true

const properties = {
method: this.method,
url: this.url,
@@ -796,7 +798,7 @@ class Request {
signal: this.signal
}

return nodeUtil.formatWithOptions(options, { ...properties })
return `Request ${nodeUtil.formatWithOptions(options, properties)}`
}
}

4 changes: 3 additions & 1 deletion lib/web/fetch/response.js
Original file line number Diff line number Diff line change
@@ -259,6 +259,8 @@ class Response {
options.depth = 2
}

options.colors ??= true

const properties = {
status: this.status,
statusText: this.statusText,
@@ -271,7 +273,7 @@ class Response {
url: this.url
}

return nodeUtil.formatWithOptions(options, `Response ${nodeUtil.inspect(properties)}`)
return `Response ${nodeUtil.formatWithOptions(options, properties)}`
}
}

4 changes: 3 additions & 1 deletion test/fetch/content-length.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ const { createServer } = require('node:http')
const { once } = require('node:events')
const { Blob } = require('node:buffer')
const { fetch, FormData } = require('../..')
const { closeServerAsPromise } = require('../utils/node-http')

// https://github.com/nodejs/undici/issues/1783
test('Content-Length is set when using a FormData body with fetch', async (t) => {
@@ -17,6 +18,7 @@ test('Content-Length is set when using a FormData body with fetch', async (t) =>
}).listen(0)

await once(server, 'listening')
t.after(closeServerAsPromise(server))

const fd = new FormData()
fd.set('file', new Blob(['hello world 👋'], { type: 'text/plain' }), 'readme.md')
@@ -25,5 +27,5 @@ test('Content-Length is set when using a FormData body with fetch', async (t) =>
await fetch(`http://localhost:${server.address().port}`, {
method: 'POST',
body: fd
}).finally(() => server.close())
})
})
2 changes: 1 addition & 1 deletion test/fetch/formdata-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ test('FormData class custom inspection', () => {
formData.append('username', 'john_doe')
formData.append('email', '[email protected]')

const expectedOutput = 'FormData:\nusername: john_doe\nemail: [email protected]\n'
const expectedOutput = "FormData { username: 'john_doe', email: '[email protected]' }"

assert.deepStrictEqual(inspect(formData), expectedOutput)

Check failure on line 15 in test/fetch/formdata-inspect-custom.js

GitHub Actions / Test with Node.js 18 on ubuntu-latest

FormData class custom inspection

[Error [ERR_TEST_FAILURE]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }"] { failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }" at TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/formdata-inspect-custom.js:15:10) at Test.runInAsyncScope (node:async_hooks:203:9) at Test.run (node:internal/test_runner/test:631:25) at Test.start (node:internal/test_runner/test:542:17) at startSubtest (node:internal/test_runner/harness:214:17) { generatedMessage: true, code: 'ERR_ASSERTION', actual: "FormData [Object: null prototype] {\n username: 'john_doe',\n email: '[email protected]'\n}", expected: "FormData { username: 'john_doe', email: '[email protected]' }", operator: 'deepStrictEqual' }, code: 'ERR_TEST_FAILURE' }

Check failure on line 15 in test/fetch/formdata-inspect-custom.js

GitHub Actions / Test with Node.js 18 on macos-latest

FormData class custom inspection

[Error [ERR_TEST_FAILURE]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }"] { failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }" at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/formdata-inspect-custom.js:15:10) at Test.runInAsyncScope (node:async_hooks:203:9) at Test.run (node:internal/test_runner/test:631:25) at Test.start (node:internal/test_runner/test:542:17) at startSubtest (node:internal/test_runner/harness:214:17) { generatedMessage: true, code: 'ERR_ASSERTION', actual: "FormData [Object: null prototype] {\n username: 'john_doe',\n email: '[email protected]'\n}", expected: "FormData { username: 'john_doe', email: '[email protected]' }", operator: 'deepStrictEqual' }, code: 'ERR_TEST_FAILURE' }

Check failure on line 15 in test/fetch/formdata-inspect-custom.js

GitHub Actions / Test with Node.js 20 on ubuntu-latest

FormData class custom inspection

[Error [ERR_TEST_FAILURE]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }"] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }" at TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/formdata-inspect-custom.js:15:10) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:631:25) at Test.start (node:internal/test_runner/test:542:17) at startSubtest (node:internal/test_runner/harness:216:17) { generatedMessage: true, code: 'ERR_ASSERTION', actual: "FormData [Object: null prototype] {\n username: 'john_doe',\n email: '[email protected]'\n}", expected: "FormData { username: 'john_doe', email: '[email protected]' }", operator: 'deepStrictEqual' } }

Check failure on line 15 in test/fetch/formdata-inspect-custom.js

GitHub Actions / Test with Node.js 20 on macos-latest

FormData class custom inspection

[Error [ERR_TEST_FAILURE]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }"] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }" at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/formdata-inspect-custom.js:15:10) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:631:25) at Test.start (node:internal/test_runner/test:542:17) at startSubtest (node:internal/test_runner/harness:216:17) { generatedMessage: true, code: 'ERR_ASSERTION', actual: "FormData [Object: null prototype] {\n username: 'john_doe',\n email: '[email protected]'\n}", expected: "FormData { username: 'john_doe', email: '[email protected]' }", operator: 'deepStrictEqual' } }

Check failure on line 15 in test/fetch/formdata-inspect-custom.js

GitHub Actions / Test with Node.js 21 on ubuntu-latest

FormData class custom inspection

[Error [ERR_TEST_FAILURE]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }"] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }" at TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/formdata-inspect-custom.js:15:10) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:639:25) at Test.start (node:internal/test_runner/test:550:17) at startSubtest (node:internal/test_runner/harness:218:17) { generatedMessage: true, code: 'ERR_ASSERTION', actual: "FormData [Object: null prototype] {\n username: 'john_doe',\n email: '[email protected]'\n}", expected: "FormData { username: 'john_doe', email: '[email protected]' }", operator: 'deepStrictEqual' } }

Check failure on line 15 in test/fetch/formdata-inspect-custom.js

GitHub Actions / Test with Node.js 21 on macos-latest

FormData class custom inspection

[Error [ERR_TEST_FAILURE]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }"] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected + 'FormData [Object: null prototype] {\n' + + " username: 'john_doe',\n" + + " email: '[email protected]'\n" + + '}' - "FormData { username: 'john_doe', email: '[email protected]' }" at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/formdata-inspect-custom.js:15:10) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:639:25) at Test.start (node:internal/test_runner/test:550:17) at startSubtest (node:internal/test_runner/harness:218:17) { generatedMessage: true, code: 'ERR_ASSERTION', actual: "FormData [Object: null prototype] {\n username: 'john_doe',\n email: '[email protected]'\n}", expected: "FormData { username: 'john_doe', email: '[email protected]' }", operator: 'deepStrictEqual' } }
})
2 changes: 1 addition & 1 deletion test/fetch/request-inspect-custom.js
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ describe('Request custom inspection', () => {

const inspectedOutput = util.inspect(request)

const expectedOutput = '{\n method: \'POST\',\n url: \'https://example.com/api\',\n headers: Headers { \'Content-Type\': \'application/json\' },\n destination: \'\',\n referrer: \'about:client\',\n referrerPolicy: \'\',\n mode: \'cors\',\n credentials: \'same-origin\',\n cache: \'default\',\n redirect: \'follow\',\n integrity: \'\',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}'
const expectedOutput = "Request {\n method: 'POST',\n url: 'https://example.com/api',\n headers: Headers { 'Content-Type': 'application/json' },\n destination: '',\n referrer: 'about:client',\n referrerPolicy: '',\n mode: 'cors',\n credentials: 'same-origin',\n cache: 'default',\n redirect: 'follow',\n integrity: '',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}"
assert.strictEqual(inspectedOutput, expectedOutput)
})
})

0 comments on commit 774a0a4

Please sign in to comment.