-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathviewerRequestSpec.js
More file actions
214 lines (187 loc) · 6.88 KB
/
viewerRequestSpec.js
File metadata and controls
214 lines (187 loc) · 6.88 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const handler = require("../handler");
const nock = require("nock");
const util = require("../lib/util");
const createUriShouldPrerender = (uri, querystring) =>
util.createUri(uri + (querystring ? `?${querystring}` : ""), true);
const createUriShouldNotPrerender = uri => util.createUri(uri, false);
describe("viewerRequest", function() {
beforeEach(function() {
handler.setPrerenderCloudOption(prerendercloud => null);
nock.cleanAll();
nock.disableNetConnect();
});
function runHandlerWithViewerRequestEvent() {
beforeEach(function(done) {
this.cb = jasmine.createSpy("originalCallback").and.callFake(done);
this.handler(this.event, this.context, this.cb);
});
}
function withUserAgentAndUri(userAgent, uri, querystring) {
beforeEach(function() {
this.event.Records[0].cf.request.uri = uri;
this.event.Records[0].cf.request.headers[
"user-agent"
][0].value = userAgent;
if (querystring) {
this.event.Records[0].cf.request.querystring = querystring;
}
});
}
function itPrerenders(userAgent, uri, querystring) {
it("modifies request object with base64 encoded JSON string that has path and user-agent", function() {
expect(this.cb).toHaveBeenCalledWith(null, {
headers: {
host: [{ value: "d123.cf.net", key: "Host" }],
"user-agent": [{ value: userAgent, key: "User-Agent" }],
[util.USER_AGENT_PLACEHOLDER]: [
{ value: userAgent, key: util.USER_AGENT_PLACEHOLDER }
]
},
clientIp: "2001:cdba::3257:9652",
uri: createUriShouldPrerender(uri, querystring),
querystring: querystring || "",
method: "GET"
});
});
}
function itDoesNotPrerender(userAgent, uri, querystring) {
it("modifies request object with base64 encoded JSON string that has path and user-agent", function() {
expect(this.cb).toHaveBeenCalledWith(null, {
headers: {
host: [{ value: "d123.cf.net", key: "Host" }],
"user-agent": [{ value: userAgent, key: "User-Agent" }]
},
clientIp: "2001:cdba::3257:9652",
uri: createUriShouldNotPrerender(uri), // the URI will not include query string when not pre-rendering
querystring: querystring || "",
method: "GET"
});
});
}
beforeEach(function() {
this.handler = handler.viewerRequest;
this.event = {
Records: [
{
cf: {
request: {
headers: {
host: [
{
value: "d123.cf.net",
key: "Host"
}
],
"user-agent": [
{
value: "test-agent",
key: "User-Agent"
}
]
},
clientIp: "2001:cdba::3257:9652",
uri: "/index.html",
method: "GET",
querystring: ""
}
}
}
]
};
this.context = {};
});
describe("with all user-agents enabled (default)", function() {
describe("curl user-agent", function() {
describe("html files", function() {
describe("html extension", function() {
withUserAgentAndUri("curl", "/index.html");
runHandlerWithViewerRequestEvent();
itPrerenders("curl", "/index.html");
});
describe("no extension or trailing slash", function() {
withUserAgentAndUri("curl", "/index");
runHandlerWithViewerRequestEvent();
itPrerenders("curl", "/index");
});
describe("trailing slash", function() {
withUserAgentAndUri("curl", "/index/");
runHandlerWithViewerRequestEvent();
itPrerenders("curl", "/index/");
});
describe("with query string", function() {
withUserAgentAndUri("curl", "/index.html", "a=b&c=d");
runHandlerWithViewerRequestEvent();
itPrerenders("curl", "/index.html", "a=b&c=d");
});
});
describe("non html files", function() {
withUserAgentAndUri("curl", "/app.js");
runHandlerWithViewerRequestEvent();
itDoesNotPrerender("curl", "/app.js");
});
});
// since shouldPrerender is false, it rewrites uri to /index.html for cache-key
describe("prerendercloud user-agent", function() {
describe("html files", function() {
describe("html extension", function() {
withUserAgentAndUri("prerendercloud random-suffix", "/index.html");
runHandlerWithViewerRequestEvent();
itDoesNotPrerender("prerendercloud random-suffix", "/index.html");
});
describe("no extension or trailing slash", function() {
withUserAgentAndUri("prerendercloud random-suffix", "/index");
runHandlerWithViewerRequestEvent();
itDoesNotPrerender("prerendercloud random-suffix", "/index.html");
});
describe("trailing slash", function() {
withUserAgentAndUri("prerendercloud random-suffix", "/index/");
runHandlerWithViewerRequestEvent();
itDoesNotPrerender("prerendercloud random-suffix", "/index.html");
});
describe("with query string", function() {
withUserAgentAndUri(
"prerendercloud random-suffix",
"/index/",
"a=b&c=d"
);
runHandlerWithViewerRequestEvent();
itDoesNotPrerender(
"prerendercloud random-suffix",
"/index.html",
"a=b&c=d"
);
});
});
// even though shouldPrerender is false, the uri is not HTML so it preserves uri for cache-key
describe("non html files", function() {
withUserAgentAndUri("prerendercloud random-suffix", "/app.js");
runHandlerWithViewerRequestEvent();
itDoesNotPrerender("prerendercloud random-suffix", "/app.js");
});
});
});
describe("with botsOnly user-agents", function() {
beforeEach(function() {
handler.setPrerenderCloudOption(prerendercloud =>
prerendercloud.set("botsOnly", true)
);
});
// since shouldPrerender is true, it preserves uri for cache-key
describe("twitterbot user-agent", function() {
withUserAgentAndUri("twitterbot", "/nested/path");
runHandlerWithViewerRequestEvent();
itPrerenders("twitterbot", "/nested/path");
});
// since shouldPrerender is false, it rewrites uri for cache-key
describe("curl user-agent", function() {
withUserAgentAndUri("curl", "/nested/path");
runHandlerWithViewerRequestEvent();
itDoesNotPrerender("curl", "/index.html");
});
describe("prerendercloud user-agent", function() {
withUserAgentAndUri("prerendercloud random-suffix", "/index.html");
runHandlerWithViewerRequestEvent();
itDoesNotPrerender("prerendercloud random-suffix", "/index.html");
});
});
});