Skip to content

Commit fc56e93

Browse files
fix(): fix middleware tests, fix socketio deprecations
1 parent 12aa576 commit fc56e93

14 files changed

Lines changed: 114 additions & 87 deletions

File tree

integration/websockets/e2e/error-gateway.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { INestApplication } from '@nestjs/common';
22
import { Test } from '@nestjs/testing';
33
import { expect } from 'chai';
4-
import * as io from 'socket.io-client';
4+
import { io } from 'socket.io-client';
55
import { ErrorGateway } from '../src/error.gateway';
66

77
describe('ErrorGateway', () => {
@@ -16,7 +16,7 @@ describe('ErrorGateway', () => {
1616
});
1717

1818
it(`should handle error`, async () => {
19-
const ws = io.connect('http://localhost:8080');
19+
const ws = io('http://localhost:8080');
2020
ws.emit('push', {
2121
test: 'test',
2222
});

integration/websockets/e2e/gateway-ack.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { INestApplication } from '@nestjs/common';
22
import { Test } from '@nestjs/testing';
33
import { expect } from 'chai';
4-
import * as io from 'socket.io-client';
4+
import { io } from 'socket.io-client';
55
import { AckGateway } from '../src/ack.gateway';
66

77
async function createNestApp(...gateways): Promise<INestApplication> {
88
const testingModule = await Test.createTestingModule({
99
providers: gateways,
1010
}).compile();
11-
const app = await testingModule.createNestApplication();
11+
const app = testingModule.createNestApplication();
1212
return app;
1313
}
1414

@@ -19,7 +19,7 @@ describe('WebSocketGateway (ack)', () => {
1919
app = await createNestApp(AckGateway);
2020
await app.listen(3000);
2121

22-
ws = io.connect('http://localhost:8080');
22+
ws = io('http://localhost:8080');
2323
await new Promise<void>(resolve =>
2424
ws.emit('push', { test: 'test' }, data => {
2525
expect(data).to.be.eql('pong');
@@ -32,7 +32,7 @@ describe('WebSocketGateway (ack)', () => {
3232
app = await createNestApp(AckGateway);
3333
await app.listen(3000);
3434

35-
ws = io.connect('http://localhost:8080');
35+
ws = io('http://localhost:8080');
3636
await new Promise<void>(resolve =>
3737
ws.emit('push', data => {
3838
expect(data).to.be.eql('pong');

integration/websockets/e2e/gateway.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { INestApplication } from '@nestjs/common';
22
import { Test } from '@nestjs/testing';
33
import { expect } from 'chai';
4-
import * as io from 'socket.io-client';
4+
import { io } from 'socket.io-client';
55
import { ApplicationGateway } from '../src/app.gateway';
66
import { NamespaceGateway } from '../src/namespace.gateway';
77
import { ServerGateway } from '../src/server.gateway';
@@ -10,7 +10,7 @@ async function createNestApp(...gateways): Promise<INestApplication> {
1010
const testingModule = await Test.createTestingModule({
1111
providers: gateways,
1212
}).compile();
13-
const app = await testingModule.createNestApplication();
13+
const app = testingModule.createNestApplication();
1414
return app;
1515
}
1616

@@ -21,7 +21,7 @@ describe('WebSocketGateway', () => {
2121
app = await createNestApp(ApplicationGateway);
2222
await app.listen(3000);
2323

24-
ws = io.connect('http://localhost:8080');
24+
ws = io('http://localhost:8080');
2525
ws.emit('push', {
2626
test: 'test',
2727
});
@@ -37,7 +37,7 @@ describe('WebSocketGateway', () => {
3737
app = await createNestApp(ServerGateway);
3838
await app.listen(3000);
3939

40-
ws = io.connect('http://localhost:3000');
40+
ws = io('http://localhost:3000');
4141
ws.emit('push', {
4242
test: 'test',
4343
});
@@ -53,8 +53,8 @@ describe('WebSocketGateway', () => {
5353
app = await createNestApp(ApplicationGateway, NamespaceGateway);
5454
await app.listen(3000);
5555

56-
ws = io.connect('http://localhost:8080');
57-
io.connect('http://localhost:8080/test').emit('push', {});
56+
ws = io('http://localhost:8080');
57+
io('http://localhost:8080/test').emit('push', {});
5858
ws.emit('push', {
5959
test: 'test',
6060
});

packages/core/helpers/router-method-factory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ export class RouterMethodFactory {
1818
return target.options;
1919
case RequestMethod.HEAD:
2020
return target.head;
21-
default: {
21+
case RequestMethod.GET:
2222
return target.get;
23+
default: {
24+
return target.use;
2325
}
2426
}
2527
}

packages/core/middleware/builder.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '@nestjs/common/interfaces/middleware';
1111
import { MiddlewareConfiguration } from '@nestjs/common/interfaces/middleware/middleware-configuration.interface';
1212
import { iterate } from 'iterare';
13-
import { NestContainer } from '../injector';
1413
import { RoutesMapper } from './routes-mapper';
1514
import { filterMiddleware } from './utils';
1615

@@ -20,7 +19,6 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
2019
constructor(
2120
private readonly routesMapper: RoutesMapper,
2221
private readonly httpAdapter: HttpServer,
23-
private readonly container: NestContainer,
2422
) {}
2523

2624
public apply(

packages/core/middleware/middleware-module.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ export class MiddlewareModule {
9191
const middlewareBuilder = new MiddlewareBuilder(
9292
this.routesMapper,
9393
this.httpAdapter,
94-
this.container,
9594
);
9695
await instance.configure(middlewareBuilder);
9796

packages/core/middleware/routes-mapper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestMethod } from '@nestjs/common';
21
import { MODULE_PATH, PATH_METADATA } from '@nestjs/common/constants';
32
import { RouteInfo, Type } from '@nestjs/common/interfaces';
43
import {
@@ -23,10 +22,11 @@ export class RoutesMapper {
2322
route: Type<any> | RouteInfo | string,
2423
): RouteInfo[] {
2524
if (isString(route)) {
25+
const defaultRequestMethod = -1;
2626
return [
2727
{
2828
path: addLeadingSlash(route),
29-
method: RequestMethod.ALL,
29+
method: defaultRequestMethod,
3030
},
3131
];
3232
}
@@ -83,7 +83,7 @@ export class RoutesMapper {
8383
private getHostModuleOfController(
8484
metatype: Type<unknown>,
8585
): Module | undefined {
86-
if (!metatype?.name) {
86+
if (!metatype) {
8787
return;
8888
}
8989
const modulesContainer = this.container.getModules();
@@ -95,7 +95,7 @@ export class RoutesMapper {
9595
const modules = Array.from(modulesContainer.values()).filter(moduleRef =>
9696
moduleRefsSet.has(moduleRef),
9797
);
98-
return modules.find(({ routes }) => routes.has(metatype.name));
98+
return modules.find(({ routes }) => routes.has(metatype));
9999
}
100100

101101
private getModulePath(

packages/core/middleware/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import { RequestMethod } from '@nestjs/common';
33
import { HttpServer, RouteInfo, Type } from '@nestjs/common/interfaces';
44
import { isFunction } from '@nestjs/common/utils/shared.utils';
5+
import { iterate } from 'iterare';
56
import * as pathToRegexp from 'path-to-regexp';
67
import { v4 as uuid } from 'uuid';
7-
import { iterate } from 'iterare';
88

99
type RouteInfoRegex = RouteInfo & { regex: RegExp };
1010

@@ -85,7 +85,11 @@ export function isRouteExcluded(
8585
: originalUrl;
8686

8787
const isExcluded = excludedRoutes.some(({ method, regex }) => {
88-
if (RequestMethod.ALL === method || RequestMethod[method] === reqMethod) {
88+
if (
89+
RequestMethod.ALL === method ||
90+
RequestMethod[method] === reqMethod ||
91+
method === -1
92+
) {
8993
return regex.exec(pathname);
9094
}
9195
return false;

packages/core/test/helpers/router-method-factory.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ describe('RouterMethodFactory', () => {
2929
expect(factory.get(target, RequestMethod.PATCH)).to.equal(target.patch);
3030
expect(factory.get(target, RequestMethod.OPTIONS)).to.equal(target.options);
3131
expect(factory.get(target, RequestMethod.HEAD)).to.equal(target.head);
32+
expect(factory.get(target, -1)).to.equal(target.use);
3233
});
3334
});

packages/core/test/middleware/builder.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestMethod } from '@nestjs/common';
21
import { expect } from 'chai';
32
import { Controller, Get } from '../../../common';
43
import { NestContainer } from '../../injector/container';
@@ -14,7 +13,6 @@ describe('MiddlewareBuilder', () => {
1413
builder = new MiddlewareBuilder(
1514
new RoutesMapper(container),
1615
new NoopHttpAdapter({}),
17-
container,
1816
);
1917
});
2018
describe('apply', () => {
@@ -65,7 +63,7 @@ describe('MiddlewareBuilder', () => {
6563
expect(proxy.getExcludedRoutes()).to.be.eql([
6664
{
6765
path,
68-
method: RequestMethod.ALL,
66+
method: -1,
6967
},
7068
]);
7169
});

0 commit comments

Comments
 (0)