-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
338 additions
and
8 deletions.
There are no files selected for viewing
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
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,39 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import request from 'superwstest'; | ||
|
||
export const expectOkIdsWs = | ||
(path = '', event = 'hello', data = {}) => | ||
async (app: INestApplication) => | ||
request(await app.getUrl()) | ||
.ws(path) | ||
.sendJson({ | ||
event, | ||
data, | ||
}) | ||
.expectJson((body) => { | ||
const id = body.fromGuard ?? body.fromInterceptor; | ||
expect(body.fromInterceptor).toEqual(id); | ||
expect(body.fromInterceptorAfter).toEqual(id); | ||
expect(body.fromGateway).toEqual(id); | ||
expect(body.fromService).toEqual(id); | ||
expect(body.data).toEqual(data); | ||
}) | ||
.close(); | ||
|
||
export const expectErrorIdsWs = | ||
(path = '', event = 'error', data = {}) => | ||
(app: INestApplication) => | ||
request(app.getHttpServer()) | ||
.ws(path) | ||
.sendJson({ | ||
event, | ||
data, | ||
}) | ||
.expectJson((body) => { | ||
const id = body.fromGuard ?? body.fromInterceptor; | ||
expect(body.fromInterceptor).toEqual(id); | ||
expect(body.fromGateway).toEqual(id); | ||
expect(body.fromService).toEqual(id); | ||
expect(body.fromFilter).toEqual(id); | ||
}) | ||
.close(); |
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,18 @@ | ||
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; | ||
import { WebSocket } from 'ws'; | ||
import { ClsService } from '../../src'; | ||
import { TestException } from '../common/test.exception'; | ||
|
||
@Catch(TestException) | ||
export class TestWsExceptionFilter implements ExceptionFilter { | ||
constructor(private readonly cls: ClsService) {} | ||
|
||
catch(exception: TestException, host: ArgumentsHost) { | ||
const client = host.switchToWs().getClient<WebSocket>(); | ||
const response = { | ||
...exception.response, | ||
fromFilter: this.cls.getId(), | ||
}; | ||
client.send(JSON.stringify(response)); | ||
} | ||
} |
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,55 @@ | ||
import { | ||
Injectable, | ||
UseFilters, | ||
UseGuards, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { MessageBody, SubscribeMessage } from '@nestjs/websockets'; | ||
|
||
import { ClsService } from '../../src'; | ||
import { TestException } from '../common/test.exception'; | ||
import { TestGuard } from '../common/test.guard'; | ||
import { TestInterceptor } from '../common/test.interceptor'; | ||
import { TestWsExceptionFilter } from './test-ws.filter'; | ||
|
||
@Injectable() | ||
export class TestWebsocketService { | ||
constructor(private readonly cls: ClsService) {} | ||
|
||
async hello(data?: unknown) { | ||
return { | ||
fromGuard: this.cls.get('FROM_GUARD'), | ||
fromInterceptor: this.cls.get('FROM_INTERCEPTOR'), | ||
fromInterceptorAfter: this.cls.get('FROM_INTERCEPTOR'), | ||
fromGateway: this.cls.get('FROM_GATEWAY'), | ||
fromService: this.cls.getId(), | ||
data, | ||
}; | ||
} | ||
} | ||
|
||
@Injectable() | ||
@UseFilters(TestWsExceptionFilter) | ||
export class TestWebsocketGateway { | ||
constructor( | ||
private readonly service: TestWebsocketService, | ||
private readonly cls: ClsService, | ||
) {} | ||
|
||
@SubscribeMessage('hello') | ||
@UseGuards(TestGuard) | ||
@UseInterceptors(TestInterceptor) | ||
hello(@MessageBody() data: unknown) { | ||
this.cls.set('FROM_GATEWAY', this.cls.getId()); | ||
return this.service.hello(data); | ||
} | ||
|
||
@UseInterceptors(TestInterceptor) | ||
@UseGuards(TestGuard) | ||
@SubscribeMessage('error') | ||
async error(@MessageBody() data: unknown) { | ||
this.cls.set('FROM_GATEWAY', this.cls.getId()); | ||
const response = await this.service.hello(data); | ||
throw new TestException(response); | ||
} | ||
} |
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,76 @@ | ||
import { | ||
INestApplication, | ||
Module, | ||
UseGuards, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { WsAdapter } from '@nestjs/platform-ws'; | ||
import { Test } from '@nestjs/testing'; | ||
import { WebSocketGateway } from '@nestjs/websockets'; | ||
import { ClsGuard, ClsInterceptor, ClsModule } from '../../src'; | ||
import { expectErrorIdsWs, expectOkIdsWs } from './expect-ids-websockets'; | ||
import { TestWebsocketGateway, TestWebsocketService } from './websockets.app'; | ||
|
||
describe('Websockets - WS', () => { | ||
let app: INestApplication; | ||
|
||
@WebSocketGateway({ path: 'interceptor' }) | ||
@UseInterceptors(ClsInterceptor) | ||
class WebsocketGatewayWithClsInterceptor extends TestWebsocketGateway {} | ||
|
||
@WebSocketGateway({ path: 'guard' }) | ||
@UseGuards(ClsGuard) | ||
class WebsocketGatewayWithClsGuard extends TestWebsocketGateway {} | ||
|
||
@Module({ | ||
imports: [ | ||
ClsModule.forRoot({ | ||
interceptor: { mount: false, generateId: true }, | ||
guard: { mount: false, generateId: true }, | ||
}), | ||
], | ||
providers: [ | ||
TestWebsocketService, | ||
WebsocketGatewayWithClsInterceptor, | ||
WebsocketGatewayWithClsGuard, | ||
], | ||
}) | ||
class TestWebsocketModule {} | ||
|
||
beforeAll(async () => { | ||
const moduleFixture = await Test.createTestingModule({ | ||
imports: [TestWebsocketModule], | ||
}).compile(); | ||
app = moduleFixture.createNestApplication(); | ||
app.useWebSocketAdapter(new WsAdapter(app)); | ||
await app.listen(3125); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app?.close(); | ||
}); | ||
|
||
describe.each(['guard', 'interceptor'])( | ||
'when using an %s to initialize the context', | ||
(name) => { | ||
const path = '/' + name; | ||
|
||
it.each([ | ||
['ok', 'object', expectOkIdsWs(path, 'hello', { value: 12 })], | ||
['ok', 'primitive', expectOkIdsWs(path, 'hello', 'primitive')], | ||
[ | ||
'error', | ||
'object', | ||
expectErrorIdsWs(path, 'error', { value: 12 }), | ||
], | ||
[ | ||
'error', | ||
'primitive', | ||
expectErrorIdsWs(path, 'error', 'primitive'), | ||
], | ||
])('works with %s response and %s payload', async (_, __, func) => { | ||
await func(app); | ||
}); | ||
}, | ||
); | ||
}); |
Oops, something went wrong.