Tofu/tests/webdav/buildBasicAuthHeader.test.js

29 lines
1.2 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { buildBasicAuthHeader } from '#tofu/tools/webdav/buildBasicAuthHeader.js'
test('builds an HTTP Basic header from the credentials', () => {
assert.equal(buildBasicAuthHeader('tofu', 'secret'), 'Basic dG9mdTpzZWNyZXQ=')
})
test('encodes a non-ASCII password as UTF-8, not latin-1', () => {
const header = buildBasicAuthHeader('tofu', 'sécret')
assert.equal(header, 'Basic dG9mdTpzw6ljcmV0')
// What btoa() alone would have produced, one byte per code point: the é becomes
// a single 0xE9 byte and the server rejects the password.
assert.notEqual(header, `Basic ${btoa('tofu:sécret')}`)
})
test('encodes a non-ASCII user name as UTF-8 as well', () => {
const expected = Buffer.from('jeanné:secret', 'utf8').toString('base64')
assert.equal(buildBasicAuthHeader('jeanné', 'secret'), `Basic ${expected}`)
})
test('keeps the colons of a password, only the first one separates', () => {
const expected = Buffer.from('tofu:a:b:c', 'utf8').toString('base64')
assert.equal(buildBasicAuthHeader('tofu', 'a:b:c'), `Basic ${expected}`)
})
test('handles empty credentials without failing', () => {
assert.equal(buildBasicAuthHeader('', ''), 'Basic Og==')
})