Skip to content

Commit

Permalink
test: update swagger module test
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Lenzotti <[email protected]>
  • Loading branch information
Romakita committed Feb 29, 2024
1 parent 50637a9 commit b353122
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/specs/swagger/src/SwaggerModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("SwaggerModule", () => {
mod.$onRoutesInit();
mod.$onRoutesInit();

expect(mod.app.get).toHaveBeenCalledWith("/doc", expect.any(Function));
expect(mod.app.use).toHaveBeenCalledWith("/doc", expect.any(Function));
expect(mod.app.use).toHaveBeenCalledWith("/doc", expect.any(PlatformRouter));
expect(PlatformRouter.prototype.get).toHaveBeenCalledWith("/swagger.json", expect.any(Function));
expect(PlatformRouter.prototype.get).toHaveBeenCalledWith("/main.css", expect.any(Function));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ describe("redirectMiddleware and redirect", () => {
afterEach(PlatformTest.reset);
it("should create a middleware", () => {
const ctx = PlatformTest.createRequestContext();
jest.spyOn(ctx.response.raw, "redirect").mockReturnValue(undefined);
jest.spyOn(ctx.response, "redirect").mockReturnValue(undefined);

ctx.request.raw.url = "/path";
ctx.request.raw.originalUrl = "/path";
ctx.request.raw.method = "GET";

redirectMiddleware("/path")(ctx);

expect(ctx.response.raw.redirect).toHaveBeenCalledWith(302, "/path/");
expect(ctx.response.redirect).toHaveBeenCalledWith(302, "/path/");
});
it("should create a middleware and call next", () => {
const ctx = PlatformTest.createRequestContext();
jest.spyOn(ctx.response.raw, "redirect");
jest.spyOn(ctx.response, "redirect");
ctx.request.raw.url = "/path/";
ctx.request.raw.method = "GET";
ctx.request.raw.originalUrl = "/path/";

redirectMiddleware("/path")(ctx);

expect(ctx.response.raw.redirect).not.toHaveBeenCalled();
expect(ctx.response.redirect).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {PlatformContext} from "@tsed/common";
*/
export function redirectMiddleware(path: string) {
return (ctx: PlatformContext) => {
if (ctx.request.method === "GET" && ctx.request.url === path && !ctx.request.url.match(/\/$/)) {
if (ctx.request.method?.toUpperCase() === "GET" && ctx.request.url === path && !ctx.request.url.match(/\/$/)) {
ctx.response.redirect(302, `${path}/`);
}
};
Expand Down

0 comments on commit b353122

Please sign in to comment.