Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filterReq crashes send middleware #3

Merged
merged 4 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function proxy(

resolver(err, ctx, next);
} else {
next();
return next();
}
});
};
Expand Down
96 changes: 96 additions & 0 deletions test/filterReq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ function nextMethod(ctx: any, next: any) {
ctx.response.body = "Client Application";
}

async function nextMethodThatReliesOnPrivateStatesOfOak(
ctx: Oak.Context,
next: any,
) {
await ctx.send({ root: `${Deno.cwd()}/test`, index: "index.test.html" });
ctx.response.status = 201;
}

describe("filterReq", () => {
it("should continue route processing when the filter function returns false (i.e. don't filter)", async (
done,
Expand Down Expand Up @@ -106,4 +114,92 @@ describe("filterReq", () => {
done(err);
});
});

it("should continue route processing when the filter function returns false (i.e. don't filter and don't serve static files)", async (
done,
) => {
const proxyServer = proxyTarget({ handlers: proxyRouteFn });
const proxyPort = (proxyServer.listener.addr as Deno.NetAddr).port;

const app = new Application();
app.use(proxy(`http://localhost:${proxyPort}`, {
filterReq: () => false,
}));
app.use(nextMethodThatReliesOnPrivateStatesOfOak);

const request = await superoak(app);

request.get("/")
.expect(200)
.end((err) => {
proxyServer.close();
done(err);
});
});

it("should stop route processing when the filter function returns true (i.e. filter and serve static files)", async (
done,
) => {
const proxyServer = proxyTarget({ handlers: proxyRouteFn });
const proxyPort = (proxyServer.listener.addr as Deno.NetAddr).port;

const app = new Application();
app.use(proxy(`http://localhost:${proxyPort}`, {
filterReq: () => true,
}));
app.use(nextMethodThatReliesOnPrivateStatesOfOak);

const request = await superoak(app);

request.get("/")
.expect(201)
.end((err) => {
proxyServer.close();
done(err);
});
});

it("should continue route processing when the filter function returns Promise<false> (i.e. don't filter and don't serve static files)", async (
done,
) => {
const proxyServer = proxyTarget({ handlers: proxyRouteFn });
const proxyPort = (proxyServer.listener.addr as Deno.NetAddr).port;

const app = new Application();
app.use(proxy(`http://localhost:${proxyPort}`, {
filterReq: () => Promise.resolve(false),
}));
app.use(nextMethodThatReliesOnPrivateStatesOfOak);

const request = await superoak(app);

request.get("/")
.expect(200)
.end((err) => {
proxyServer.close();
done(err);
});
});

it("should stop route processing when the filter function returns Promise<true> (i.e. filter and serve static files)", async (
done,
) => {
const proxyServer = proxyTarget({ handlers: proxyRouteFn });
const proxyPort = (proxyServer.listener.addr as Deno.NetAddr).port;

const app = new Application();
app.use(proxy(`http://localhost:${proxyPort}`, {
filterReq: () => Promise.resolve(true),
}));
app.use(nextMethodThatReliesOnPrivateStatesOfOak);

const request = await superoak(app);

request.get("/")
.expect(201)
.end((err) => {
proxyServer.close();
done(err);
});
});
});
4 changes: 4 additions & 0 deletions test/index.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE html>
<html>
<body></body>
</html>