mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 04:04:06 -08:00
fix(core): Ensure task runner server closes websocket connection correctly (#11633)
This commit is contained in:
parent
0db658c6d0
commit
b496bf3147
|
@ -0,0 +1,67 @@
|
|||
import type { GlobalConfig } from '@n8n/config';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { ServerResponse } from 'node:http';
|
||||
import type WebSocket from 'ws';
|
||||
|
||||
import type { TaskRunnerAuthController } from '@/runners/auth/task-runner-auth.controller';
|
||||
import { TaskRunnerServer } from '@/runners/task-runner-server';
|
||||
|
||||
import type { TaskRunnerServerInitRequest } from '../runner-types';
|
||||
|
||||
describe('TaskRunnerServer', () => {
|
||||
describe('handleUpgradeRequest', () => {
|
||||
it('should close WebSocket when response status code is > 200', () => {
|
||||
const ws = mock<WebSocket>();
|
||||
const request = mock<TaskRunnerServerInitRequest>({
|
||||
url: '/runners/_ws',
|
||||
ws,
|
||||
});
|
||||
|
||||
const server = new TaskRunnerServer(
|
||||
mock(),
|
||||
mock<GlobalConfig>({ taskRunners: { path: '/runners' } }),
|
||||
mock<TaskRunnerAuthController>(),
|
||||
mock(),
|
||||
);
|
||||
|
||||
// @ts-expect-error Private property
|
||||
server.handleUpgradeRequest(request, mock(), Buffer.from(''));
|
||||
|
||||
const response = new ServerResponse(request);
|
||||
response.writeHead = (statusCode) => {
|
||||
if (statusCode > 200) ws.close();
|
||||
return response;
|
||||
};
|
||||
|
||||
response.writeHead(401);
|
||||
expect(ws.close).toHaveBeenCalledWith(); // no args
|
||||
});
|
||||
|
||||
it('should not close WebSocket when response status code is 200', () => {
|
||||
const ws = mock<WebSocket>();
|
||||
const request = mock<TaskRunnerServerInitRequest>({
|
||||
url: '/runners/_ws',
|
||||
ws,
|
||||
});
|
||||
|
||||
const server = new TaskRunnerServer(
|
||||
mock(),
|
||||
mock<GlobalConfig>({ taskRunners: { path: '/runners' } }),
|
||||
mock<TaskRunnerAuthController>(),
|
||||
mock(),
|
||||
);
|
||||
|
||||
// @ts-expect-error Private property
|
||||
server.handleUpgradeRequest(request, mock(), Buffer.from(''));
|
||||
|
||||
const response = new ServerResponse(request);
|
||||
response.writeHead = (statusCode) => {
|
||||
if (statusCode > 200) ws.close();
|
||||
return response;
|
||||
};
|
||||
|
||||
response.writeHead(200);
|
||||
expect(ws.close).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
|
@ -181,7 +181,7 @@ export class TaskRunnerServer {
|
|||
|
||||
const response = new ServerResponse(request);
|
||||
response.writeHead = (statusCode) => {
|
||||
if (statusCode > 200) ws.close(100);
|
||||
if (statusCode > 200) ws.close();
|
||||
return response;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue