Skip to content

Commit 6618bad

Browse files
committed
removed set header code before running interceptors. Added e2e test for testing header override.
1 parent 1521400 commit 6618bad

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

integration/hello-world/e2e/interceptors.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ export class StatusInterceptor {
4141
}
4242
}
4343

44+
@Injectable()
45+
export class HeaderInterceptor {
46+
47+
constructor(private authorization: string){}
48+
49+
intercept(context: ExecutionContext, next: CallHandler) {
50+
const ctx = context.switchToHttp();
51+
const res = ctx.getResponse();
52+
res.header('Authorization', this.authorization);
53+
return next.handle().pipe(map(data => ({ data })));
54+
}
55+
}
56+
4457
function createTestModule(interceptor) {
4558
return Test.createTestingModule({
4659
imports: [ApplicationModule],
@@ -111,6 +124,18 @@ describe('Interceptors', () => {
111124
.expect(400, { data: 'Hello world!' });
112125
});
113126

127+
it(`should modify Authorization header`, async () => {
128+
app = (await createTestModule(
129+
new HeaderInterceptor('jwt'),
130+
)).createNestApplication();
131+
132+
await app.init();
133+
return request(app.getHttpServer())
134+
.get('/hello')
135+
.expect(200)
136+
.expect('Authorization', 'jwt');
137+
});
138+
114139
afterEach(async () => {
115140
await app.close();
116141
});

packages/core/router/router-execution-context.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ export class RouterExecutionContext {
139139

140140
this.responseController.status(res, httpStatusCode);
141141

142+
const responseHeaders = this.reflectResponseHeaders(callback);
143+
const hasCustomHeaders = !isEmpty(responseHeaders);
144+
145+
hasCustomHeaders &&
146+
this.responseController.setHeaders(res, responseHeaders);
147+
142148
const result = await this.interceptorsConsumer.intercept(
143149
interceptors,
144150
[req, res],
@@ -347,19 +353,13 @@ export class RouterExecutionContext {
347353
httpStatusCode?: number
348354
) {
349355
const renderTemplate = this.reflectRenderTemplate(callback);
350-
const responseHeaders = this.reflectResponseHeaders(callback);
351-
const hasCustomHeaders = !isEmpty(responseHeaders);
352356

353357
if (renderTemplate) {
354358
return async <TResult, TResponse>(result: TResult, res: TResponse) => {
355-
hasCustomHeaders &&
356-
this.responseController.setHeaders(res, responseHeaders);
357359
await this.responseController.render(result, res, renderTemplate);
358360
};
359361
}
360362
return async <TResult, TResponse>(result: TResult, res: TResponse) => {
361-
hasCustomHeaders &&
362-
this.responseController.setHeaders(res, responseHeaders);
363363
result = await this.responseController.transformToResult(result);
364364
!isResponseHandled &&
365365
(await this.responseController.apply(result, res, httpStatusCode));

0 commit comments

Comments
 (0)