Skip to content

Commit

Permalink
FormatQueryInterceptor 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
masonJS committed Nov 2, 2023
1 parent 04d4e34 commit c32b5a7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/libs/web-common/app/setNestApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import { HttpExceptionFilter } from '../../../exceptions/HttpExceptionFilter';
import { Reflector } from '@nestjs/core';
import { ValidationError } from 'class-validator';
import { CustomValidationError } from '../../../exceptions/CustomValidationError';
import { FormatQueryInterceptor } from '../interceptor/FormatQueryInterceptor';

export function setNestApp<T extends INestApplication>(app: T): void {
app.useGlobalFilters(
new NotFoundExceptionFilter(app.get(Logger)),
new BadParameterExceptionFilter(app.get(Logger)),
new HttpExceptionFilter(app.get(Logger)),
);
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
app.useGlobalInterceptors(
new ClassSerializerInterceptor(app.get(Reflector)),
new FormatQueryInterceptor(),
);

// 이렇게 해야 @Body()를 사용한 후 매개변수를 받으면 인스턴스화를 자동으로 진행한다.
// https://github.com/nestjs/nest/issues/552
Expand Down
34 changes: 34 additions & 0 deletions src/libs/web-common/interceptor/FormatQueryInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class FormatQueryInterceptor implements NestInterceptor {
intercept(
context: ExecutionContext,
next: CallHandler<any>,
): Observable<any> {
const request = context.switchToHttp().getRequest();

if (!request.query) {
return next.handle();
}

request.query = Object.entries(request.query).reduce(
(acc: Record<string, unknown>, [key, value]) => {
if (typeof value === 'string') {
acc[key] = value.replace(/\0/g, '');
}

return acc;
},
{},
);

return next.handle();
}
}
40 changes: 40 additions & 0 deletions test/libs/web-common/interceptor/FormatQueryInterceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FormatQueryInterceptor } from '../../../../src/libs/web-common/interceptor/FormatQueryInterceptor';
import { of } from 'rxjs';

describe('FormatQueryInterceptor', () => {
const interceptor = new FormatQueryInterceptor();

it('query 값에 null byte가 포함되어 있을 경우 공백으로 치환한다', (done) => {
// given
const context: any = {
switchToHttp: function () {
return this;
},
getRequest: function () {
return this.request;
},
request: {
query: {
key: 'value\0withNullByte',
},
},
};
const next = { handle: () => of('') };

// when
const result = interceptor.intercept(context, next);

// then
result.subscribe(() => {
try {
expect(context.switchToHttp().getRequest().query.key).toBe(
'valuewithNullByte',
);

done();
} catch (e) {
done(e);
}
});
});
});

0 comments on commit c32b5a7

Please sign in to comment.