-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Ensure task runner server closes websocket connection corr…
…ectly (#11633)
- Loading branch information
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
packages/cli/src/runners/__tests__/task-runner-server.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters