pino/test/escaping.test.js
James Sumners 41d1706410
Convert tests to node:test (#2299)
* Convert tests to node:test

* broken-pipe.test.js

* browser-is-level-enabled.test.js

* complex-objects.test.js

* crlf.test.js

* custom-levels.test.js

* error.test.js

* error-key.test.js

* escaping.test.js

* exit.test.js

* formatters.test.js

* hooks.test.js

* http.test.js

* is-level-enabled.test.js

* levels.test.js

* metadata.test.js

* mixin.test.js

* mixin-merge-strategy.test.js

* multistream.test.js

* redact.test.js

* serializers.test.js

* stdout-protection.test.js

* syncfalse.test.js

* timestamp.test.js

* timestamp-nano.test.js

* transport-stream.test.js

* esm/*

* internals/version.test.js

* transport/big.test.js

* transport/bundlers-support.test.js

* transport/caller.test.js

* transport/core.test.js

* transport/core.transpiled.test.js

* transport/module-link.test.js

* transport/pipeline.test.js

* transport/repl.test.js

* transport/sync-false.test.js

* transport/sync-true.test.js

* transport/targets.test.js

* transport/uses-pino-config.test.js

* clean helper

* finalize

* restore transport/core.test.js

* address feedback

* skip broken-pipe in CITGM

* remove unused package

* remove duplicate test file
2025-10-03 06:12:54 -04:00

94 lines
2.4 KiB
JavaScript

'use strict'
const test = require('node:test')
const assert = require('node:assert')
const os = require('node:os')
const { sink, once } = require('./helper')
const pino = require('../')
const { pid } = process
const hostname = os.hostname()
function testEscape (ch, key) {
test('correctly escape ' + ch, async () => {
const stream = sink()
const instance = pino({
name: 'hello'
}, stream)
instance.fatal('this contains ' + key)
const result = await once(stream, 'data')
delete result.time
assert.deepEqual(result, {
pid,
hostname,
level: 60,
name: 'hello',
msg: 'this contains ' + key
})
})
}
testEscape('\\n', '\n')
testEscape('\\/', '/')
testEscape('\\\\', '\\')
testEscape('\\r', '\r')
testEscape('\\t', '\t')
testEscape('\\b', '\b')
const toEscape = [
'\u0000', // NUL Null character
'\u0001', // SOH Start of Heading
'\u0002', // STX Start of Text
'\u0003', // ETX End-of-text character
'\u0004', // EOT End-of-transmission character
'\u0005', // ENQ Enquiry character
'\u0006', // ACK Acknowledge character
'\u0007', // BEL Bell character
'\u0008', // BS Backspace
'\u0009', // HT Horizontal tab
'\u000A', // LF Line feed
'\u000B', // VT Vertical tab
'\u000C', // FF Form feed
'\u000D', // CR Carriage return
'\u000E', // SO Shift Out
'\u000F', // SI Shift In
'\u0010', // DLE Data Link Escape
'\u0011', // DC1 Device Control 1
'\u0012', // DC2 Device Control 2
'\u0013', // DC3 Device Control 3
'\u0014', // DC4 Device Control 4
'\u0015', // NAK Negative-acknowledge character
'\u0016', // SYN Synchronous Idle
'\u0017', // ETB End of Transmission Block
'\u0018', // CAN Cancel character
'\u0019', // EM End of Medium
'\u001A', // SUB Substitute character
'\u001B', // ESC Escape character
'\u001C', // FS File Separator
'\u001D', // GS Group Separator
'\u001E', // RS Record Separator
'\u001F' // US Unit Separator
]
toEscape.forEach((key) => {
testEscape(JSON.stringify(key), key)
})
test('correctly escape `hello \\u001F world \\n \\u0022`', async () => {
const stream = sink()
const instance = pino({
name: 'hello'
}, stream)
instance.fatal('hello \u001F world \n \u0022')
const result = await once(stream, 'data')
delete result.time
assert.deepEqual(result, {
pid,
hostname,
level: 60,
name: 'hello',
msg: 'hello \u001F world \n \u0022'
})
})