Skip to content

Commit

Permalink
Merge 26abc32 into a3bac4c
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Memering committed Apr 12, 2019
2 parents a3bac4c + 26abc32 commit 6678697
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions src/test/hosting/functionsProxy.spec.ts
Expand Up @@ -19,49 +19,15 @@ describe("functionsProxy", () => {

const fakeRewrite: FunctionProxyRewrite = { function: "bar" };

before(() => {
nock("http://localhost:7778")
.get("/project-foo/us-central1/bar/")
.reply(200, "local version");
afterEach(() => {
nock.cleanAll();
});

it("should resolve a function returns middleware that proxies to the live version", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/")
.reply(200, "live version");

nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/vary")
.reply(200, "live vary version", { vary: "Other, Authorization" });

nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/cached")
.reply(200, "cached page", { "cache-control": "custom", "set-cookie": "nom" });

nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/404.html")
.reply(404, "normal 404");

nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/404-cascade.html")
.reply(404, "normal 404 with cascade", { "x-cascade": "pass" });

nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/500")
.replyWithError({ message: "normal error" });

nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/timeout")
.replyWithError({ message: "ahh", code: "ETIMEDOUT" });

nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/sockettimeout")
.replyWithError({ message: "ahh", code: "ESOCKETTIMEDOUT" });
});

after(() => {
nock.cleanAll();
});

it("should resolve a function returns middleware that proxies to the live version", async () => {
const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand All @@ -75,6 +41,10 @@ describe("functionsProxy", () => {
});

it("should resolve a function that returns middleware that proxies to a local version", async () => {
nock("http://localhost:7778")
.get("/project-foo/us-central1/bar/")
.reply(200, "local version");

const options = cloneDeep(fakeOptions);
options.targets = ["functions"];
const mwGenerator = await functionsProxy(options);
Expand All @@ -90,6 +60,10 @@ describe("functionsProxy", () => {
});

it("should pass through normal 404 errors", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/404.html")
.reply(404, "normal 404");

const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand All @@ -103,6 +77,10 @@ describe("functionsProxy", () => {
});

it("should do nothing on 404 errors with x-cascade", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/404-cascade.html")
.reply(404, "normal 404 with cascade", { "x-cascade": "pass" });

const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand All @@ -123,6 +101,10 @@ describe("functionsProxy", () => {
});

it("should remove cookies on non-private cached responses", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/cached")
.reply(200, "cached page", { "cache-control": "custom", "set-cookie": "nom" });

const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand All @@ -137,6 +119,10 @@ describe("functionsProxy", () => {
});

it("should add required Vary headers to the response", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/vary")
.reply(200, "live vary version", { vary: "Other, Authorization" });

const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand All @@ -151,6 +137,10 @@ describe("functionsProxy", () => {
});

it("should respond with a 500 error if an error occurs calling the function", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/500")
.replyWithError({ message: "normal error" });

const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand All @@ -164,6 +154,10 @@ describe("functionsProxy", () => {
});

it("should respond with a 504 error if a timeout error occurs calling the function", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/timeout")
.replyWithError({ message: "ahh", code: "ETIMEDOUT" });

const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand All @@ -177,6 +171,10 @@ describe("functionsProxy", () => {
});

it("should respond with a 504 error if a sockettimeout error occurs calling the function", async () => {
nock("https://us-central1-project-foo.cloudfunctions.net")
.get("/bar/sockettimeout")
.replyWithError({ message: "ahh", code: "ESOCKETTIMEDOUT" });

const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
Expand Down

0 comments on commit 6678697

Please sign in to comment.