feat(edge): support connection tokens when starting an edge and add token encode/decode utilities
This commit is contained in:
49
test/test.ts
49
test/test.ts
@@ -9,4 +9,53 @@ tap.test('should export RemoteIngressEdge', async () => {
|
||||
expect(remoteingress.RemoteIngressEdge).toBeTypeOf('function');
|
||||
});
|
||||
|
||||
tap.test('should export encodeConnectionToken and decodeConnectionToken', async () => {
|
||||
expect(remoteingress.encodeConnectionToken).toBeTypeOf('function');
|
||||
expect(remoteingress.decodeConnectionToken).toBeTypeOf('function');
|
||||
});
|
||||
|
||||
tap.test('should roundtrip encode → decode a connection token', async () => {
|
||||
const data: remoteingress.IConnectionTokenData = {
|
||||
hubHost: 'hub.example.com',
|
||||
hubPort: 8443,
|
||||
edgeId: 'edge-001',
|
||||
secret: 'super-secret-key',
|
||||
};
|
||||
const token = remoteingress.encodeConnectionToken(data);
|
||||
const decoded = remoteingress.decodeConnectionToken(token);
|
||||
expect(decoded.hubHost).toEqual(data.hubHost);
|
||||
expect(decoded.hubPort).toEqual(data.hubPort);
|
||||
expect(decoded.edgeId).toEqual(data.edgeId);
|
||||
expect(decoded.secret).toEqual(data.secret);
|
||||
});
|
||||
|
||||
tap.test('should throw on malformed token', async () => {
|
||||
let error: Error | undefined;
|
||||
try {
|
||||
remoteingress.decodeConnectionToken('not-valid-json!!!');
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(error!.message).toInclude('Invalid connection token');
|
||||
});
|
||||
|
||||
tap.test('should throw on token with missing fields', async () => {
|
||||
// Encode a partial object (missing 'p' and 's')
|
||||
const partial = Buffer.from(JSON.stringify({ h: 'host', e: 'edge' }), 'utf-8')
|
||||
.toString('base64')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '');
|
||||
|
||||
let error: Error | undefined;
|
||||
try {
|
||||
remoteingress.decodeConnectionToken(partial);
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(error!.message).toInclude('missing required fields');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
|
||||
Reference in New Issue
Block a user