Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/server/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe('server', () => {
});
});

describe('POST /hook/*', () => {
it('should return 404 when no client is connected', async () => {
describe('HTTP /hook/*', () => {
it('should return 404 when no client is connected (POST)', async () => {
const response = await request(server)
.post('/hook/test-path')
.send({ test: 'data' })
Expand All @@ -48,6 +48,14 @@ describe('server', () => {
expect(response.text).toContain('No matching client found');
});

it('should return 404 when no client is connected (OPTIONS)', async () => {
const response = await request(server)
.options('/hook/test-path')
.expect(404);

expect(response.text).toContain('No matching client found');
});

it('should relay request to connected client', (done) => {
const ws = new WebSocket(`ws://localhost:${port}?clientId=test-client`);

Expand Down Expand Up @@ -94,6 +102,51 @@ describe('server', () => {
});
});

it('should relay OPTIONS request to connected client', (done) => {
const ws = new WebSocket(`ws://localhost:${port}?clientId=test-client`);

ws.on('message', (data) => {
const message = JSON.parse(data.toString());

if (message.kind === 'challenge') {
const challengeMessage = message as WebSocketChallengeMessage;
const hmac = createHmac('sha256', challengePassphrase)
.update(challengeMessage.nonce)
.digest('hex');

const response: WebSocketChallengeResponse = {
kind: 'challenge-response',
clientId: 'test-client',
hmac,
};
ws.send(JSON.stringify(response));
} else if (message.kind === 'challenge-result' && message.success) {
request(server)
.options('/hook/test-path')
.end(() => {});
} else if (message.kind === 'http') {
const httpMessage = message as WebSocketHTTPMessage;
expect(httpMessage.path).toBe('/test-path');
expect(httpMessage.method).toBe('OPTIONS');

const response: WebSocketHTTPResponse = {
kind: 'http',
clientId: 'test-client',
messageId: httpMessage.messageId,
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS' },
status: 200,
body: '',
};
ws.send(JSON.stringify(response));

setTimeout(() => {
ws.close();
done();
}, 100);
}
});
});

it('should handle wildcard client', (done) => {
const ws = new WebSocket(`ws://localhost:${port}?clientId=test-client`);

Expand Down
7 changes: 5 additions & 2 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export function createServer(logger: Logger, challengePassphrase: string): {
});
});

app.post('/hook/*', async (
const hookHandler = async (
req: Request,
res: Response,
) => {
Expand Down Expand Up @@ -315,7 +315,10 @@ export function createServer(logger: Logger, challengePassphrase: string): {
logger.error(err, 'Error while receiving data');
res.status(500).send('Error receiving data');
});
});
};

app.post('/hook/*', hookHandler);
app.options('/hook/*', hookHandler);

const CALLBACK_REGISTRATION_EXPIRATION_MS = 30 * 1000; // 30 seconds
app.post('/callback/oneshot/register', express.json(), async (req: Request, res: Response) => {
Expand Down