Skip to content

Commit a556304

Browse files
committed
fix: handle the case when statusCode is a string
Closes #114
1 parent 3991e44 commit a556304

4 files changed

Lines changed: 40 additions & 10 deletions

File tree

.github/labeler.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
"scope: cli":
55
- src/cli/*
6-
6+
77
"scope: auth":
88
- src/auth/*
9-
9+
10+
"scope: rules engine":
11+
- src/proxy/routes-engine/*
12+
1013
"scope: proxy":
1114
- src/proxy/*

src/proxy/routes-engine/rules/customRoutes.spec.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe("customRoutes()", () => {
132132
expect(res.statusCode).toBe(200);
133133
});
134134

135-
it("should set custom status code (418)", async () => {
135+
it("should set custom status code as number (statusCode=418)", async () => {
136136
userConfig = [
137137
{
138138
route: "/foo",
@@ -144,7 +144,31 @@ describe("customRoutes()", () => {
144144
expect(res.statusCode).toBe(418);
145145
});
146146

147-
it("should set custom status code (404)", async () => {
147+
it("should set custom status code as string (statusCode='418')", async () => {
148+
userConfig = [
149+
{
150+
route: "/foo",
151+
statusCode: "418",
152+
},
153+
];
154+
await customRoutes(req, res, userConfig, false);
155+
156+
expect(res.statusCode).toBe(418);
157+
});
158+
159+
it("should not set custom status code if invalid code (statusCode='NaN')", async () => {
160+
userConfig = [
161+
{
162+
route: "/foo",
163+
statusCode: "NaN",
164+
},
165+
];
166+
await customRoutes(req, res, userConfig, false);
167+
168+
expect(res.statusCode).toBe(200);
169+
});
170+
171+
it("should set custom status code (statusCode=404)", async () => {
148172
userConfig = [
149173
{
150174
route: "/foo",
@@ -234,7 +258,7 @@ describe("customRoutes()", () => {
234258
expect(req.url).toBe("/bar");
235259
});
236260

237-
it("should serve with rewrite (statusCode = undefined)", async () => {
261+
it("should serve with rewrite (statusCode=undefined)", async () => {
238262
userConfig = [
239263
{
240264
route: "/foo",

src/proxy/routes-engine/rules/customRoutes.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,27 @@ export const customRoutes = async (
9292

9393
// specific status code but no attached route
9494
if (userDefinedRoute.statusCode && !userDefinedRoute.serve) {
95-
res.statusCode = userDefinedRoute.statusCode;
95+
const code = Number(userDefinedRoute.statusCode);
96+
if (isNaN(code) === false) {
97+
res.statusCode = code;
98+
}
9699
}
97100

98101
// rewrite
99-
const isServeWrite = userDefinedRoute.serve && ![301, 302].includes(userDefinedRoute.statusCode!);
102+
const isServeWrite = userDefinedRoute.serve && ![301, 302].includes(Number(userDefinedRoute.statusCode));
100103
if (isServeWrite || userDefinedRoute.rewrite) {
101104
req.url = userDefinedRoute.serve || userDefinedRoute.rewrite;
102105
}
103106

104107
// redirect route
105-
const isServeRedirect = userDefinedRoute.serve && [301, 302].includes(userDefinedRoute.statusCode!);
108+
const isServeRedirect = userDefinedRoute.serve && [301, 302].includes(Number(userDefinedRoute.statusCode));
106109
if (isServeRedirect || userDefinedRoute.redirect) {
107110
let route = (userDefinedRoute.serve || userDefinedRoute.redirect) as string;
108111

109112
// redirects
110113
// note: adding checks to avoid ERR_TOO_MANY_REDIRECTS
111114
if (route !== req.url) {
112-
res.writeHead(userDefinedRoute.statusCode || 302, {
115+
res.writeHead(Number(userDefinedRoute.statusCode) || 302, {
113116
Location: route,
114117
});
115118
res.end();

src/swa.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ declare type ClientPrincipal = {
8282
declare type SWAConfigFileRoute = {
8383
route: string;
8484
allowedRoles?: string[];
85-
statusCode?: number;
85+
statusCode?: number | string;
8686
serve?: string;
8787
headers?: { [key: string]: string };
8888
methods?: string[];

0 commit comments

Comments
 (0)