-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
79 additions
and
1 deletion.
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,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
40
test/libs/web-common/interceptor/FormatQueryInterceptor.spec.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,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); | ||
} | ||
}); | ||
}); | ||
}); |