-
Notifications
You must be signed in to change notification settings - Fork 906
/
functionsProxy.spec.ts
189 lines (157 loc) · 5.69 KB
/
functionsProxy.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { cloneDeep } from "lodash";
import { expect } from "chai";
import * as express from "express";
import * as nock from "nock";
import * as sinon from "sinon";
import * as supertest from "supertest";
import functionsProxy, {
FunctionProxyRewrite,
FunctionsProxyOptions,
} from "../../hosting/functionsProxy";
describe("functionsProxy", () => {
const fakeOptions: FunctionsProxyOptions = {
port: 7777,
project: "project-foo",
targets: [],
};
const fakeRewrite: FunctionProxyRewrite = { function: "bar" };
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");
const mwGenerator = await functionsProxy(fakeOptions);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
return supertest(spyMw)
.get("/")
.expect(200, "live version")
.then(() => {
expect(spyMw.calledOnce).to.be.true;
});
});
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);
const mw = await mwGenerator(fakeRewrite);
const spyMw = sinon.spy(mw);
return supertest(spyMw)
.get("/")
.expect(200, "local version")
.then(() => {
expect(spyMw.calledOnce).to.be.true;
});
});
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);
return supertest(spyMw)
.get("/404.html")
.expect(404, "normal 404")
.then(() => {
expect(spyMw.calledOnce).to.be.true;
});
});
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);
const finalMw = sinon.stub().callsFake((_, res) => {
res.status(404).send("unknown response");
});
const app = express();
app.use(spyMw);
app.use(finalMw);
return supertest(app)
.get("/404-cascade.html")
.expect(404, "unknown response")
.then(() => {
expect(spyMw.calledOnce).to.be.true;
});
});
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);
return supertest(spyMw)
.get("/cached")
.expect(200, "cached page")
.then((res) => {
expect(spyMw.calledOnce).to.be.true;
expect(res.header["set-cookie"]).to.be.undefined;
});
});
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);
return supertest(spyMw)
.get("/vary")
.expect(200, "live vary version")
.then((res) => {
expect(spyMw.calledOnce).to.be.true;
expect(res.header.vary).to.equal("Other, Authorization, Accept-Encoding, Cookie");
});
});
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);
return supertest(spyMw)
.get("/500")
.expect(500)
.then(() => {
expect(spyMw.calledOnce).to.be.true;
});
});
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);
return supertest(spyMw)
.get("/timeout")
.expect(504)
.then(() => {
expect(spyMw.calledOnce).to.be.true;
});
});
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);
return supertest(spyMw)
.get("/sockettimeout")
.expect(504)
.then(() => {
expect(spyMw.calledOnce).to.be.true;
});
});
});