Skip to content

Commit aaab327

Browse files
committed
Adding headers in response objects
1 parent 1a07a23 commit aaab327

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/controllers/guildRoleHandler.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,21 @@ export async function getGuildRolesHandler(request: IRequest, env: env) {
8686
{ error: response.ROLE_FETCH_FAILED },
8787
{
8888
status: 500,
89+
headers: {
90+
"content-type": "application/json;charset=UTF-8",
91+
},
8992
}
9093
);
9194
}
92-
return new JSONResponse(response.INTERNAL_SERVER_ERROR, { status: 500 });
95+
return new JSONResponse(
96+
{ error: response.INTERNAL_SERVER_ERROR },
97+
{
98+
status: 500,
99+
headers: {
100+
"content-type": "application/json;charset=UTF-8",
101+
},
102+
}
103+
);
93104
}
94105
}
95106

@@ -111,7 +122,12 @@ export async function getGuildRoleByRoleNameHandler(
111122
await verifyAuthToken(authHeader, env);
112123
const role = await getGuildRoleByName(roleName, env);
113124
if (!role) {
114-
return new JSONResponse(response.NOT_FOUND, { status: 404 });
125+
return new JSONResponse(response.NOT_FOUND, {
126+
status: 404,
127+
headers: {
128+
"content-type": "application/json;charset=UTF-8",
129+
},
130+
});
115131
}
116132
return new JSONResponse(role);
117133
} catch (err: any) {
@@ -120,9 +136,20 @@ export async function getGuildRoleByRoleNameHandler(
120136
{ error: response.ROLE_FETCH_FAILED },
121137
{
122138
status: 500,
139+
headers: {
140+
"content-type": "application/json;charset=UTF-8",
141+
},
123142
}
124143
);
125144
}
126-
return new JSONResponse(response.INTERNAL_SERVER_ERROR, { status: 500 });
145+
return new JSONResponse(
146+
{ error: response.INTERNAL_SERVER_ERROR },
147+
{
148+
status: 500,
149+
headers: {
150+
"content-type": "application/json;charset=UTF-8",
151+
},
152+
}
153+
);
127154
}
128155
}

tests/unit/handlers/guildRoleHandler.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ describe("get roles", () => {
6767
);
6868
const jsonResponse = await response.json();
6969
expect(response.status).toBe(500);
70-
expect(jsonResponse).toBe(responseConstants.INTERNAL_SERVER_ERROR);
70+
expect(jsonResponse).toEqual({
71+
error: responseConstants.INTERNAL_SERVER_ERROR,
72+
});
7173
});
7274

7375
it("should return empty array if there is no roles in guild", async () => {
@@ -230,7 +232,9 @@ describe("get role by role name", () => {
230232
);
231233
const jsonResponse: { roles: Array<GuildRole> } = await response.json();
232234
expect(response.status).toBe(500);
233-
expect(jsonResponse).toBe(responseConstants.INTERNAL_SERVER_ERROR);
235+
expect(jsonResponse).toEqual({
236+
error: responseConstants.INTERNAL_SERVER_ERROR,
237+
});
234238
});
235239

236240
it("should return object of id and name corresponding to the role name recieved", async () => {

0 commit comments

Comments
 (0)