Skip to content

Commit 8755571

Browse files
Merge pull request nestjs#3590 from tonyhallett/status-code-optional
fix(core): correct redirect decorator metadata interface
2 parents e51128f + 449144b commit 8755571

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

packages/core/router/router-response-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface CustomHeader {
88

99
export interface RedirectResponse {
1010
url: string;
11-
statusCode: number;
11+
statusCode?: number;
1212
}
1313
export class RouterResponseController {
1414
constructor(private readonly applicationRef: HttpServer) {}

packages/core/test/router/router-response-controller.spec.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isNil, isObject } from '@nestjs/common/utils/shared.utils';
22
import { expect } from 'chai';
33
import { of } from 'rxjs';
44
import * as sinon from 'sinon';
5-
import { RequestMethod } from '../../../common';
5+
import { RequestMethod, HttpStatus } from '../../../common';
66
import { RouterResponseController } from '../../router/router-response-controller';
77
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
88

@@ -170,4 +170,81 @@ describe('RouterResponseController', () => {
170170
expect(statusStub.calledWith(response, statusCode)).to.be.true;
171171
});
172172
});
173+
174+
describe('redirect should HttpServer.redirect', () => {
175+
it('should transformToResult', async () => {
176+
const transformToResultSpy = sinon
177+
.stub(routerResponseController, 'transformToResult')
178+
.returns(Promise.resolve({ statusCode: 123, url: 'redirect url' }));
179+
const result = {};
180+
await routerResponseController.redirect(result, null, null);
181+
expect(transformToResultSpy.firstCall.args[0]).to.be.equal(result);
182+
});
183+
it('should pass the response to redirect', async () => {
184+
sinon
185+
.stub(routerResponseController, 'transformToResult')
186+
.returns(Promise.resolve({ statusCode: 123, url: 'redirect url' }));
187+
const redirectSpy = sinon.spy(adapter, 'redirect');
188+
const response = {};
189+
await routerResponseController.redirect(null, response, null);
190+
expect(redirectSpy.firstCall.args[0]).to.be.equal(response);
191+
});
192+
describe('status code', () => {
193+
it('should come from the transformed result if present', async () => {
194+
sinon
195+
.stub(routerResponseController, 'transformToResult')
196+
.returns(Promise.resolve({ statusCode: 123, url: 'redirect url' }));
197+
const redirectSpy = sinon.spy(adapter, 'redirect');
198+
await routerResponseController.redirect(null, null, {
199+
statusCode: 999,
200+
url: 'not form here',
201+
});
202+
expect(redirectSpy.firstCall.args[1]).to.be.eql(123);
203+
});
204+
it('should come from the redirectResponse if not on the transformed result', async () => {
205+
sinon
206+
.stub(routerResponseController, 'transformToResult')
207+
.returns(Promise.resolve({}));
208+
const redirectSpy = sinon.spy(adapter, 'redirect');
209+
await routerResponseController.redirect(null, null, {
210+
statusCode: 123,
211+
url: 'redirect url',
212+
});
213+
expect(redirectSpy.firstCall.args[1]).to.be.eql(123);
214+
});
215+
it('should default to HttpStatus.FOUND', async () => {
216+
sinon
217+
.stub(routerResponseController, 'transformToResult')
218+
.returns(Promise.resolve({}));
219+
const redirectSpy = sinon.spy(adapter, 'redirect');
220+
await routerResponseController.redirect(null, null, {
221+
url: 'redirect url',
222+
});
223+
expect(redirectSpy.firstCall.args[1]).to.be.eql(HttpStatus.FOUND);
224+
});
225+
});
226+
describe('url', () => {
227+
it('should come from the transformed result if present', async () => {
228+
sinon
229+
.stub(routerResponseController, 'transformToResult')
230+
.returns(Promise.resolve({ statusCode: 123, url: 'redirect url' }));
231+
const redirectSpy = sinon.spy(adapter, 'redirect');
232+
await routerResponseController.redirect(null, null, {
233+
url: 'not from here',
234+
});
235+
expect(redirectSpy.firstCall.args[2]).to.be.eql('redirect url');
236+
});
237+
it('should come from the redirectResponse if not on the transformed result', async () => {
238+
sinon
239+
.stub(routerResponseController, 'transformToResult')
240+
.returns(Promise.resolve({}));
241+
const redirectSpy = sinon.spy(adapter, 'redirect');
242+
await routerResponseController.redirect(null, null, {
243+
statusCode: 123,
244+
url: 'redirect url',
245+
});
246+
expect(redirectSpy.firstCall.args[2]).to.be.eql('redirect url');
247+
});
248+
});
249+
});
173250
});

0 commit comments

Comments
 (0)