Files
jose/test/jws/compact.sign.test.ts
Filip Skokan eebee55a01 refactor: restructure src/lib and src/runtime now that runtime is fixed
BREAKING CHANGE: createRemoteJWKSet now uses fetch, because of that its Node.js only options.agent property has been removed and new fetch-related options were added
2025-02-22 15:16:04 +01:00

44 lines
1.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'ava'
import { CompactSign } from '../../src/index.js'
test.before((t) => {
const encode = TextEncoder.prototype.encode.bind(new TextEncoder())
t.context.payload = encode('It’s a dangerous business, Frodo, going out your door.')
t.context.secret = new Uint8Array(32)
})
test('CompactSign', async (t) => {
const jws = await new CompactSign(t.context.payload)
.setProtectedHeader({ alg: 'HS256' })
.sign(t.context.secret)
t.is(
jws,
'eyJhbGciOiJIUzI1NiJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4.UKohvCM6JaKEJlDt7ApBPgcQMW4lmp-UGXfwPmCfUaA',
)
})
test('CompactSign.prototype.setProtectedHeader', (t) => {
t.throws(() => new CompactSign(t.context.payload).setProtectedHeader({}).setProtectedHeader({}), {
instanceOf: TypeError,
message: 'setProtectedHeader can only be called once',
})
})
test('CompactSign.prototype.sign must have a JOSE header', async (t) => {
await t.throwsAsync(new CompactSign(t.context.payload).sign(t.context.secret), {
code: 'ERR_JWS_INVALID',
message: 'either setProtectedHeader or setUnprotectedHeader must be called before #sign()',
})
})
test('CompactSign.prototype.sign JOSE header have an alg', async (t) => {
await t.throwsAsync(
new CompactSign(t.context.payload).setProtectedHeader({}).sign(t.context.secret),
{
code: 'ERR_JWS_INVALID',
message: 'JWS "alg" (Algorithm) Header Parameter missing or invalid',
},
)
})