Skip to content

Commit 81aaee5

Browse files
authored
export function 'isRedirectStatusCode' (#2810)
1 parent c53b9fd commit 81aaee5

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import RequestHookEventProvider from './request-pipeline/request-hooks/events/ev
2828
import { RequestInfo, ResponseInfo } from './request-pipeline/request-hooks/events/info';
2929
import BaseRequestHookEventFactory from './request-pipeline/request-hooks/events/factory/base';
3030
import BaseRequestPipelineContext from './request-pipeline/context/base';
31-
31+
import isRedirectStatusCode from './utils/is-redirect-status-code';
3232

3333
export default {
3434
Proxy,
@@ -61,4 +61,5 @@ export default {
6161
acceptCrossOrigin,
6262
getAssetPath,
6363
ResponseInfo,
64+
isRedirectStatusCode,
6465
};

src/request-pipeline/context/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import RequestHookEventProvider from '../request-hooks/events/event-provider';
3535
import { PassThrough } from 'stream';
3636
import promisifyStream from '../../utils/promisify-stream';
3737
import { toReadableStream } from '../../utils/buffer';
38+
import isRedirectStatusCode from '../../utils/is-redirect-status-code';
3839

3940
export interface DestInfo {
4041
url: string;
@@ -93,7 +94,6 @@ interface Socket extends net.Socket {
9394

9495
export type DestinationResponse = IncomingMessage | FileStream | IncomingMessageLike | Http2Response;
9596

96-
const REDIRECT_STATUS_CODES = [301, 302, 303, 307, 308];
9797
const CANNOT_BE_USED_WITH_WEB_SOCKET_ERR_MSG = 'The function cannot be used with a WebSocket request.';
9898

9999
export default class RequestPipelineContext extends BaseRequestPipelineContext {
@@ -302,7 +302,7 @@ export default class RequestPipelineContext extends BaseRequestPipelineContext {
302302

303303
const isRedirect = this.destRes.headers[BUILTIN_HEADERS.location] &&
304304
this.destRes.statusCode &&
305-
REDIRECT_STATUS_CODES.includes(this.destRes.statusCode) ||
305+
isRedirectStatusCode(this.destRes.statusCode) ||
306306
false;
307307
const requireAssetsProcessing = (isCSS || isScript || isManifest) && this.destRes.statusCode !== 204;
308308
const isNotModified = this.req.method === 'GET' && this.destRes.statusCode === 304 &&

src/utils/is-redirect-status-code.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const REDIRECT_STATUS_CODES = [301, 302, 303, 307, 308];
2+
3+
export default function (code: number): boolean {
4+
return REDIRECT_STATUS_CODES.includes(code);
5+
}

ts-defs/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,9 @@ declare module 'testcafe-hammerhead' {
591591
/** Calculates the asset path depending on the run mode (production or development) **/
592592
function getAssetPath(originPath: string, developmentMode: boolean): string;
593593

594+
/** Return whether the HTTP status code is a redirect status code **/
595+
function isRedirectStatusCode (code: number): boolean;
596+
594597
/** **/
595598
export class IncomingMessageLike {
596599
/** The headers of the instance **/

0 commit comments

Comments
 (0)