-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
209 lines (172 loc) · 7.63 KB
/
test.js
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
const { test } = require("tap");
const Fastify = require("fastify");
const fastifyStatic = require("fastify-static");
const fm = require("./index");
const fs = require("fs");
const path = require("path");
const { Readable } = require("stream");
const mainCSS = fs.readFileSync(path.join(__dirname, "testfiles", "public", "main.css")).toString();
const mainJS = fs.readFileSync(path.join(__dirname, "testfiles", "public", "main.js")).toString();
const mainHTML = fs.readFileSync(path.join(__dirname, "testfiles", "index.html")).toString();
test("decorators", t => {
t.plan(8);
const fastify = newFastify();
fastify.ready(async () => {
t.ok(fastify.minifyHTML);
t.ok(fastify.minifyJS);
t.ok(fastify.minifyCSS);
const html1 = "<a href = \"#\">\n AA </a>", html2 = "<a href=\"#\">AA</a>";
const js1 = "const ZZZZ = 9 ;;; console. log(ZZZZ)", js2 = "const ZZZZ=9;console.log(9);";
const css1 = ".a { color : red ;} .b { color : red ;}", css2 = ".a,.b{color:red}";
t.equal(await fastify.minifyHTML(html1), html2);
t.equal(await fastify.minifyJS(js1), js2);
t.equal(await fastify.minifyCSS(css1), css2);
t.rejects(fastify.minifyHTML("<a href = \"#\"> < / a>"));
t.rejects(fastify.minifyJS("const {{ let ("));
});
});
test("global", t => {
t.plan(10);
const fastify = newFastify({ global: true, validate: () => t.ok(1) });
fastify.register(fastifyStatic, { root: path.join(__dirname, "testfiles") });
fastify.get("/string", (req, rep) => rep.type("text/css").send(mainCSS));
fastify.get("/buffer", (req, rep) => rep.type("text/css").send(Buffer.from(mainCSS)));
fastify.get("/stream", (req, rep) => rep.type("text/css").send(Readable.from(mainCSS)));
fastify.get("/null", (req, rep) => rep.type("text/css").send(null));
fastify.inject().get("/string").end().then(res => t.ok(minifiedCSS(res.body)));
fastify.inject().get("/buffer").end().then(res => t.ok(minifiedCSS(res.body)));
fastify.inject().get("/stream").end().then(res => t.ok(minifiedCSS(res.body)));
fastify.inject().get("/null").end().then(res => t.notOk(res.body));
fastify.inject().get("/public/main.css").end().then(res => t.ok(minifiedCSS(res.body)));
const fastify2 = newFastify({ global: true, validate: () => false });
fastify2.get("/", (req, rep) => rep.type("text/css").send(mainCSS));
fastify2.inject().get("/").end().then(res => t.equal(res.body, mainCSS));
});
test("minInfix", t => {
t.plan(12);
t.rejects(newFastify({ minInfix: true }).ready());
const fastify = newFastify({ minInfix: (req, filePath) => filePath.endsWith(".css") || filePath.endsWith(".html") });
fastify.register(fastifyStatic, { root: path.join(__dirname, "testfiles") });
t.resolves(fastify.ready());
fastify.inject().get("/public/main.min.css").end().then(res => t.ok(minifiedCSS(res.body)));
fastify.inject().get("/public/main.css").end().then(res => t.equal(res.body, mainCSS));
fastify.inject().get("/public/main.min.js").end().then(res => t.equal(res.statusCode, 404));
fastify.inject().get("/index.min.html").end().then(res => t.equal(res.statusCode, 200) && t.ok(res.body.length < mainHTML.length));
const fastify2 = newFastify({ minInfix: req => t.ok(1) && req.url.includes("css") });
fastify2.register(fastifyStatic, { root: path.join(__dirname, "testfiles") });
fastify2.inject().get("/index.min.html").end().then(res => t.equal(res.statusCode, 404));
fastify2.inject().get("/public/main.min.css").end().then(res => t.ok(minifiedCSS(res.body)));
const fastify3 = newFastify();
fastify3.register(fastifyStatic, { root: path.join(__dirname, "testfiles") });
fastify3.inject().get("/public/main.min.css").end().then(res => t.equal(res.body, "minify"));
});
test("global & minInfix", t => {
t.plan(3);
const fastify = newFastify({ minInfix: true, global: true });
fastify.register(fastifyStatic, { root: path.join(__dirname, "testfiles") });
Promise.all([fastify.inject().get("/public/main.js").end(), fastify.inject().get("/public/main.min.js").end()])
.then(results => {
t.equal(results[0].statusCode, 200);
t.equal(results[0].body, results[1].body);
t.ok(results[0].body.length < mainJS.length);
});
});
test("transformers", t => {
t.plan(4);
const fastify = newFastify({
transformers: [
{
suffix: "txt",
contentType: "text/plain",
func: value => value.toUpperCase(),
decorate: "ttt"
},
{
suffix: "html",
func: null,
}
],
minInfix: true,
global: true
});
fastify.register(fastifyStatic, { root: path.join(__dirname, "testfiles") });
fastify.get("/", (req, rep) => rep.type("text/plain").send("bar"));
fastify.inject().get("/public/test.txt").end().then(res => t.equal(res.body, "JAVASCRIPT"));
fastify.inject().get("/").end().then(res => t.equal(res.body, "BAR"));
fastify.inject().get("/index.min.html").end().then(res => t.equal(res.statusCode, 404));
fastify.ready(async () => {
t.equal(await fastify.ttt("foo"), "FOO");
});
});
test("options", t => {
t.plan(2);
const fastify1 = newFastify();
const fastify2 = newFastify({
jsOptions: { mangle: false, compress: false },
htmlOptions: { minifyJS: false }
});
function jsHandler(req, rep) {
this.minifyJS(mainJS).then(r => rep.type("application/javascript").send(r));
}
function htmlHandler(req, rep) {
this.minifyHTML(mainHTML).then(r => rep.type("text/html").send(r));
}
fastify1.get("/j", jsHandler);
fastify1.get("/h", htmlHandler);
fastify2.get("/j", jsHandler);
fastify2.get("/h", htmlHandler);
Promise.all([fastify1.inject().get("/j").end(), fastify2.inject().get("/j").end()])
.then(arr => t.ok(arr[0].body.length < arr[1].body.length));
Promise.all([fastify1.inject().get("/h").end(), fastify2.inject().get("/h").end()])
.then(arr => t.ok(arr[0].body.length < arr[1].body.length));
});
test("cache", t => {
t.plan(9)
const fastify = newFastify({ cache: 100 });
fastify.get("/", (req, rep) => {
fastify.minifyCSS(mainCSS).then(r => rep.type("text/css").send(r))
});
let time1, time2, time3;
time1 = new Date().getTime();
fastify.inject({
method: "GET",
url: "/"
}, (err, res1) => {
t.error(err);
time2 = new Date().getTime();
fastify.inject({
method: "GET",
url: "/"
}, (err2, res2) => {
t.error(err2);
time3 = new Date().getTime();
t.ok(mainCSS.length > res1.body.length);
t.equal(res1.statusCode, 200);
t.equal(res1.body, res2.body);
t.ok(time3 - time2 < time2 - time1);
t.ok(time3 - time2 < 10);
});
});
let count = 0;
const fastify2 = newFastify({
cache: {
set: (k, v) => count++,
get: (k) => count === 0 ? null : Promise.resolve("party")
}
});
fastify2.get("/", (req, rep) => {
fastify2.minifyCSS(mainCSS).then(r => rep.type("text/css").send(r))
});
fastify2.inject().get("/").end()
.then(res => fastify2.inject().get("/").end()
.then(res => {
t.equal(res.body, "party");
t.equal(count, 1);
}));
});
function newFastify(opts = {}) {
return new Fastify().register(fm, opts);
}
function minifiedCSS(value) {
return value.includes("padding") && value.length < mainCSS.length;
}