koa-classic-server 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -120
- package/index.cjs +236 -21
- package/package.json +10 -5
- package/.github/workflows/npm-publish.yml +0 -98
- package/CLAUDE.md +0 -101
- package/__tests__/benchmark-results-baseline-v1.2.0.txt +0 -354
- package/__tests__/benchmark-results-optimized-v2.0.0.txt +0 -354
- package/__tests__/benchmark-results-v3.0.0.txt +0 -372
- package/__tests__/benchmark.js +0 -239
- package/__tests__/caching-headers.test.js +0 -556
- package/__tests__/compression-fixtures/data.json +0 -1
- package/__tests__/compression-fixtures/large.txt +0 -1
- package/__tests__/compression-fixtures/small.txt +0 -1
- package/__tests__/compression.test.js +0 -284
- package/__tests__/customTest/README.md +0 -6
- package/__tests__/customTest/loadConfig.util.js +0 -41
- package/__tests__/customTest/serversToLoad.util.js +0 -93
- package/__tests__/demo-regex-index.js +0 -140
- package/__tests__/deprecation-warnings.test.js +0 -105
- package/__tests__/directory-sorting-links.test.js +0 -135
- package/__tests__/dt-unknown.test.js +0 -635
- package/__tests__/ejs.test.js +0 -299
- package/__tests__/hidden-fixtures/.dot-dir/inside.txt +0 -1
- package/__tests__/hidden-fixtures/.env +0 -2
- package/__tests__/hidden-fixtures/.well-known/acme-challenge.txt +0 -1
- package/__tests__/hidden-fixtures/config/secrets/password.txt +0 -1
- package/__tests__/hidden-fixtures/data.key +0 -1
- package/__tests__/hidden-fixtures/file.secret +0 -1
- package/__tests__/hidden-fixtures/index.html +0 -1
- package/__tests__/hidden-fixtures/normal.txt +0 -1
- package/__tests__/hidden-fixtures/subdir/.env +0 -1
- package/__tests__/hidden-fixtures/subdir/regular.txt +0 -1
- package/__tests__/hidden-option.test.js +0 -407
- package/__tests__/hideExtension.test.js +0 -607
- package/__tests__/index-option.test.js +0 -449
- package/__tests__/index.test.js +0 -345
- package/__tests__/listing.test.js +0 -437
- package/__tests__/logger.test.js +0 -232
- package/__tests__/performance.test.js +0 -370
- package/__tests__/publicWwwTest/cartella/sottocartella/ciao.html +0 -11
- package/__tests__/publicWwwTest/cartella/sottocartella/provaEjs/testEjs.ejs +0 -11
- package/__tests__/publicWwwTest/cartella vuota con spazi nel nome/file con spazio nel nome .txt +0 -1
- package/__tests__/publicWwwTest/cartella vuota con spazi nel nome /file con spazio nel nome .txt +0 -1
- package/__tests__/publicWwwTest/ejs-templates/complex.ejs +0 -56
- package/__tests__/publicWwwTest/ejs-templates/index.ejs +0 -30
- package/__tests__/publicWwwTest/ejs-templates/simple.ejs +0 -13
- package/__tests__/publicWwwTest/ejs-templates/with-conditional.ejs +0 -28
- package/__tests__/publicWwwTest/ejs-templates/with-escaping.ejs +0 -26
- package/__tests__/publicWwwTest/ejs-templates/with-loop.ejs +0 -16
- package/__tests__/publicWwwTest/hideext-test/about/index.html +0 -1
- package/__tests__/publicWwwTest/hideext-test/about.EJS +0 -1
- package/__tests__/publicWwwTest/hideext-test/about.ejs +0 -2
- package/__tests__/publicWwwTest/hideext-test/blog/articolo.ejs +0 -2
- package/__tests__/publicWwwTest/hideext-test/conflict-test/pagina +0 -1
- package/__tests__/publicWwwTest/hideext-test/conflict-test/pagina.ejs +0 -1
- package/__tests__/publicWwwTest/hideext-test/file.ejs.bak +0 -1
- package/__tests__/publicWwwTest/hideext-test/index.ejs +0 -2
- package/__tests__/publicWwwTest/hideext-test/photo.txt +0 -1
- package/__tests__/publicWwwTest/hideext-test/sezione/index.ejs +0 -1
- package/__tests__/publicWwwTest/hideext-test/style.css +0 -1
- package/__tests__/publicWwwTest/ile_vuoto.txt +0 -0
- package/__tests__/publicWwwTest/index.html +0 -11
- package/__tests__/publicWwwTest/prova file .txt +0 -2
- package/__tests__/publicWwwTest/semplicetxt.txt +0 -1
- package/__tests__/publicWwwTest/test-page.html +0 -1
- package/__tests__/publicWwwTest/test.txt +0 -1
- package/__tests__/range-fixtures/sample.txt +0 -1
- package/__tests__/range.test.js +0 -223
- package/__tests__/security-headers.test.js +0 -165
- package/__tests__/security.test.js +0 -322
- package/__tests__/server-cache-fixtures/large.txt +0 -1
- package/__tests__/server-cache-fixtures/small.txt +0 -1
- package/__tests__/server-cache.test.js +0 -594
- package/__tests__/setup-benchmark.js +0 -178
- package/__tests__/symlink.test.js +0 -441
- package/__tests__/template-timeout.test.js +0 -321
- package/__tests__/test-regex-quick.js +0 -158
- package/__tests__/useOriginalUrl.test.js +0 -213
- package/docs/ACTION_PLAN.md +0 -293
- package/docs/BENCHMARKS.md +0 -317
- package/docs/CHANGELOG.md +0 -880
- package/docs/CODE_REVIEW.md +0 -300
- package/docs/DEBUG_REPORT.md +0 -593
- package/docs/DOCUMENTATION.md +0 -1864
- package/docs/EXAMPLES_INDEX_OPTION.md +0 -395
- package/docs/FLOW_DIAGRAM.md +0 -954
- package/docs/INDEX_OPTION_PRIORITY.md +0 -527
- package/docs/OPTIMIZATION_HTTP_CACHING.md +0 -689
- package/docs/OPTIMIZATION_ROADMAP_for_V3.md +0 -864
- package/docs/PERFORMANCE_ANALYSIS.md +0 -839
- package/docs/PERFORMANCE_COMPARISON.md +0 -388
- package/docs/noteExports.md +0 -148
- package/docs/security_improvement_for_V3.md +0 -421
- package/docs/template-engine/TEMPLATE_ENGINE_GUIDE.md +0 -1734
- package/docs/template-engine/esempi-incrementali.js +0 -192
- package/docs/template-engine/examples/esempio1-nessun-dato.ejs +0 -12
- package/docs/template-engine/examples/esempio2-una-variabile.ejs +0 -11
- package/docs/template-engine/examples/esempio3-piu-variabili.ejs +0 -15
- package/docs/template-engine/examples/esempio4-condizionale.ejs +0 -18
- package/docs/template-engine/examples/esempio5-loop.ejs +0 -18
- package/docs/template-engine/examples/index-esempi.html +0 -181
- package/docs/template-engine/examples/index.html +0 -40
- package/docs/template-engine/examples/test.ejs +0 -64
- package/eslint.config.mjs +0 -17
- package/jest.config.js +0 -18
package/__tests__/index.test.js
DELETED
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
2
|
-
//
|
|
3
|
-
// ATTENZIONE QUESTO TEST NON COPRE SE NEL VISUALIZARE IL CONTENUTO DELLE CARTELLE TUTTO VIENE MOSTRATO COME SI DEVE
|
|
4
|
-
// ATTENZIONE MANCA IL TEST PER TESTARE I MOTORI DI RENDERING COME .ejs
|
|
5
|
-
// ERRORI NOTI:
|
|
6
|
-
// 1)QUANDO UNA RISORSA NON VIENE TROVATA IN UNA CARTELLO LO STATO NON È SETTATO SU 404 MA SU 200
|
|
7
|
-
// 2)IL PERCORSO RISERVATO UNZIONA SOLO SE NON VI SONO SPAZI NEL NOME
|
|
8
|
-
// FURURES:
|
|
9
|
-
// A) IMPLEMENTARE UNA ARRAY DI FILE INDEX ,MAGAI DANDO LA POSSIBILITA DI NON DISTINGURE FRA MINUSCOLE EMAIUSCOLE
|
|
10
|
-
//
|
|
11
|
-
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const supertest = require('supertest');
|
|
15
|
-
const koaClassicServer = require('../index.cjs');
|
|
16
|
-
const Koa = require('koa');
|
|
17
|
-
const fs = require('fs');
|
|
18
|
-
const path = require('path');
|
|
19
|
-
const mime = require('mime-types'); //serve alla funzione getFilesRecursivelySync();
|
|
20
|
-
//const {configurations} = require('../customTest/serversToLoad.util'); --> inutile questo serve solo per testari manualmente
|
|
21
|
-
|
|
22
|
-
const rootDir = path.join(__dirname, 'publicWwwTest');
|
|
23
|
-
|
|
24
|
-
// Legge tutti i file ricorsivamente e li memorizza in un array
|
|
25
|
-
const filesAndDirArray = getFilesRecursivelySync(rootDir);
|
|
26
|
-
|
|
27
|
-
// Visualizza l'array risultante
|
|
28
|
-
//console.log(filesAndDirArray);
|
|
29
|
-
|
|
30
|
-
//START option0
|
|
31
|
-
|
|
32
|
-
// Configuriamo le opzioni per koa-classic-server
|
|
33
|
-
const options0 = {
|
|
34
|
-
urlPrefix: '/public', // Il prefisso URL che il middleware dovrà intercettare
|
|
35
|
-
method: ['GET'],// I metodi HTTP ammessi (default 'GET')
|
|
36
|
-
dirListing: { enabled: true },// Se mostrare il contenuto della directory in caso di richiesta ad una cartella
|
|
37
|
-
//index: 'index.html', // Nome del file index da cercare all'interno di una directory (se presente)
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
describe(` koaClassicServer options0: ${JSON.stringify(options0)}`, () => {
|
|
41
|
-
let app;
|
|
42
|
-
let server;
|
|
43
|
-
|
|
44
|
-
// Avvia il server prima di eseguire i test
|
|
45
|
-
beforeAll(() => {
|
|
46
|
-
app = new Koa();
|
|
47
|
-
app.use(koaClassicServer(rootDir, options0)); // Monta il middleware
|
|
48
|
-
server = app.listen();// Avvia il server in modo che Supertest possa inviare richieste HTTP
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('controllo che se chiamo un percorso che non esiste mi venga restituito l\'errore apropiato ', async () => {
|
|
52
|
-
// Effettua una richiesta GET sull'endpoint configurato (il prefisso)
|
|
53
|
-
const res = await supertest(server).get('/public/percorso_di_una_cartella_o_file_che_non_esiste_fbrojngbornbo/gbrtbbbrbr/tbrbr/rtbrtbrt');
|
|
54
|
-
expect(res.status).toBe(404); // FIX: Now returns proper 404 status
|
|
55
|
-
expect(res.type).toMatch(/text\/html/);// type sta per mimetype .... restituisce text/plain anche se dovrebbe essere text/html
|
|
56
|
-
expect(res.text.replace(/\s/g, '')).toBe(requestedUrlNotFound().replace(/\s/g, '')); //.replace(/\s/g, '') --> rimuoce gli spazi bianchi e le tabulazioni , il server agiunge degli spazi all'inizio facendo fallire il controllo
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test('controllo che se l\'indirizzo non ricade nel urlPrefix allora debba essere passato al midlware successivo e se non c\'e allora errore 404 not founf', async () => {
|
|
60
|
-
// Effettua una richiesta GET sull'endpoint configurato (il prefisso)
|
|
61
|
-
const res = await supertest(server).get('/percorso al di fuori di url prefix public/gbrtbbbrbr/tbrbr/rtbrtbrt');
|
|
62
|
-
expect(res.status).toBe(404);
|
|
63
|
-
expect(res.type).toMatch("text/plain");// type sta per mimetype .... restituisce text/plain anche se dovrebbe essere text/html
|
|
64
|
-
expect(res.text.replace(/\s/g, '')).toBe("Not Found".replace(/\s/g, '')); //.replace(/\s/g, '') --> rimuoce gli spazi bianchi e le tabulazioni , il server agiunge degli spazi all'inizio facendo fallire il controllo
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// queste rige generaranno test per ogni ile presente nella cartella di test
|
|
69
|
-
const testFnCallbacks = testAllPathByFileList(filesAndDirArray, () => server, options0);//Genera l'array di callback per i test
|
|
70
|
-
testFnCallbacks.forEach(cb => cb());// Esegui ogni callback per registrare il test nello scope del describe
|
|
71
|
-
|
|
72
|
-
afterAll(() => {// Chiude il server dopo aver completato tutti i test
|
|
73
|
-
server.close();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
//END option0
|
|
78
|
-
|
|
79
|
-
//START option1
|
|
80
|
-
const options1 = {
|
|
81
|
-
method: ['GET'],
|
|
82
|
-
dirListing: { enabled: true },
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
describe(` koaClassicServer options1: ${JSON.stringify(options1)}`, () => {
|
|
86
|
-
let app;
|
|
87
|
-
let server;
|
|
88
|
-
|
|
89
|
-
// Avvia il server prima di eseguire i test
|
|
90
|
-
beforeAll(() => {
|
|
91
|
-
app = new Koa();
|
|
92
|
-
app.use(koaClassicServer(rootDir, options1)); // Monta il middleware
|
|
93
|
-
server = app.listen();// Avvia il server in modo che Supertest possa inviare richieste HTTP
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('controllo che se chiamo un percorso che non esiste mi venga restituito l\'errore apropiato [ANCHE SENZA URL PREFIX] ', async () => {
|
|
97
|
-
// Effettua una richiesta GET sull'endpoint configurato (il prefisso)
|
|
98
|
-
const res = await supertest(server).get('/BTBg h gh /percorso_di_una_cartella_o_file_che_non_esiste_fbrojngbornbo/gbrtbbbrbr/tbrbr/rtbrtbrt');
|
|
99
|
-
expect(res.status).toBe(404); // FIX: Now returns proper 404 status
|
|
100
|
-
expect(res.type).toMatch(/text\/html/);// type sta per mimetype .... restituisce text/plain anche se dovrebbe essere text/html
|
|
101
|
-
expect(res.text.replace(/\s/g, '')).toBe(requestedUrlNotFound().replace(/\s/g, '')); //.replace(/\s/g, '') --> rimuoce gli spazi bianchi e le tabulazioni , il server agiunge degli spazi all'inizio facendo fallire il controllo
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
const testFnCallbacks = testAllPathByFileList(filesAndDirArray, () => server, options1);//Genera l'array di callback per i test
|
|
105
|
-
testFnCallbacks.forEach(cb => cb());// Esegui ogni callback per registrare il test nello scope del describe
|
|
106
|
-
|
|
107
|
-
afterAll(() => {// Chiude il server dopo aver completato tutti i test
|
|
108
|
-
server.close();
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
//END option1
|
|
113
|
-
|
|
114
|
-
//STASRT option2
|
|
115
|
-
const options2 = {
|
|
116
|
-
method: ['GET'],
|
|
117
|
-
dirListing: { enabled: false },
|
|
118
|
-
index: ['index.html'],
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
describe(` koaClassicServer options2: ${JSON.stringify(options2)}`, () => {
|
|
122
|
-
let app;
|
|
123
|
-
let server;
|
|
124
|
-
|
|
125
|
-
// Avvia il server prima di eseguire i test
|
|
126
|
-
beforeAll(() => {
|
|
127
|
-
app = new Koa();
|
|
128
|
-
app.use(koaClassicServer(rootDir, options2)); // Monta il middleware
|
|
129
|
-
server = app.listen();// Avvia il server in modo che Supertest possa inviare richieste HTTP
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const testFnCallbacks = testAllPathByFileList(filesAndDirArray, () => server, options2);//Genera l'array di callback per i test
|
|
133
|
-
testFnCallbacks.forEach(cb => cb());// Esegui ogni callback per registrare il test nello scope del describe
|
|
134
|
-
|
|
135
|
-
afterAll(() => {// Chiude il server dopo aver completato tutti i test
|
|
136
|
-
server.close();
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
//END option2
|
|
140
|
-
|
|
141
|
-
//STASRT option3
|
|
142
|
-
const options3 = {
|
|
143
|
-
method: ['GET'],
|
|
144
|
-
dirListing: { enabled: false },
|
|
145
|
-
urlsReserved : Array('/percorso_riservato', '/percorso riservato con spazi')
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
describe(` koaClassicServer options2: ${JSON.stringify(options2)}`, () => {
|
|
149
|
-
let app;
|
|
150
|
-
let server;
|
|
151
|
-
|
|
152
|
-
// Avvia il server prima di eseguire i test
|
|
153
|
-
beforeAll(() => {
|
|
154
|
-
app = new Koa();
|
|
155
|
-
app.use(koaClassicServer(rootDir, options3)); // Monta il middleware
|
|
156
|
-
server = app.listen();// Avvia il server in modo che Supertest possa inviare richieste HTTP
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
test('controllo che se l\'indirizzo ricate di un percorso riservato allora venga passato il tutto al midlware successivo e in questo caso errore not ound', async () => {
|
|
160
|
-
// Effettua una richiesta GET sull'endpoint configurato (il prefisso)
|
|
161
|
-
const res = await supertest(server).get('/percorso_riservato/ciao.txt');
|
|
162
|
-
expect(res.status).toBe(404);
|
|
163
|
-
expect(res.type).toMatch("text/plain");// type sta per mimetype .... restituisce text/plain anche se dovrebbe essere text/html
|
|
164
|
-
expect(res.text.replace(/\s/g, '')).toBe("Not Found".replace(/\s/g, '')); //.replace(/\s/g, '') --> rimuoce gli spazi bianchi e le tabulazioni , il server agiunge degli spazi all'inizio facendo fallire il controllo
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
/* test('lo stesso del precedente ma questa volta il percorso riservato con spazi', async () => {
|
|
168
|
-
// Effettua una richiesta GET sull'endpoint configurato (il prefisso)
|
|
169
|
-
const res = await supertest(server).get('/percorso riservato con spazi/ciao.txt');
|
|
170
|
-
expect(res.status).toBe(404);
|
|
171
|
-
expect(res.type).toMatch("text/plain");// type sta per mimetype .... restituisce text/plain anche se dovrebbe essere text/html
|
|
172
|
-
expect(res.text.replace(/\s/g, '')).toBe("Not Found".replace(/\s/g, '')); //.replace(/\s/g, '') --> rimuoce gli spazi bianchi e le tabulazioni , il server agiunge degli spazi all'inizio facendo fallire il controllo
|
|
173
|
-
}); */
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const testFnCallbacks = testAllPathByFileList(filesAndDirArray, () => server, options2);//Genera l'array di callback per i test
|
|
177
|
-
testFnCallbacks.forEach(cb => cb());// Esegui ogni callback per registrare il test nello scope del describe
|
|
178
|
-
|
|
179
|
-
afterAll(() => {// Chiude il server dopo aver completato tutti i test
|
|
180
|
-
server.close();
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
//END option3
|
|
184
|
-
|
|
185
|
-
/* const options0 = {
|
|
186
|
-
// Il prefisso URL che il middleware dovrà intercettare
|
|
187
|
-
urlPrefix: '/public',
|
|
188
|
-
// I metodi HTTP ammessi (default 'GET')
|
|
189
|
-
method: ['GET'],
|
|
190
|
-
// Se mostrare il contenuto della directory in caso di richiesta ad una cartella
|
|
191
|
-
dirListing: { enabled: true },
|
|
192
|
-
// Nome del file index da cercare all'interno di una directory (se presente)
|
|
193
|
-
//index: 'index.html',
|
|
194
|
-
}; */
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
// questa funzione serve per leggere ricorsivamente il contenuto di una cartella in modo da leggere il contenuto oferto via __tests__/publicWwwTest e confrontarlo con quello via http
|
|
200
|
-
//in linux anhe le directory sono file quindi mi salvo :)
|
|
201
|
-
function getFilesRecursivelySync(dir) {
|
|
202
|
-
let results = [];
|
|
203
|
-
// Leggiamo il contenuto della directory, ottenendo anche informazioni sui file e le cartelle
|
|
204
|
-
const list = fs.readdirSync(dir, { withFileTypes: true });
|
|
205
|
-
|
|
206
|
-
list.forEach((entry) => {
|
|
207
|
-
const fullPath = path.join(dir, entry.name);
|
|
208
|
-
entry.fullPath = fullPath;
|
|
209
|
-
if (entry.isDirectory()) {
|
|
210
|
-
// Se l'entry è una cartella, la elaboriamo ricorsivamente
|
|
211
|
-
entry.type = 'directory';
|
|
212
|
-
results.push(entry);// inserisco anche la directory stessa nell'elenco
|
|
213
|
-
results = results.concat(getFilesRecursivelySync(fullPath));
|
|
214
|
-
} else if (entry.isFile()) {
|
|
215
|
-
// Se l'entry è un file, lo aggiungiamo all'array dei risultati
|
|
216
|
-
const mimeType = mime.lookup(entry.name) || 'application/octet-stream'; // fallback for unknown MIME types
|
|
217
|
-
entry.type = 'file';
|
|
218
|
-
entry.mimeType = mimeType;
|
|
219
|
-
results.push(entry);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
return results;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
//ATTENZIONE questa funzione è esattamente identicaa a quella contenuta nel file index.cjs serve per confrontarla e vedere se il risultato combacia
|
|
227
|
-
function requestedUrlNotFound() {
|
|
228
|
-
return `
|
|
229
|
-
<!DOCTYPE html>
|
|
230
|
-
<html>
|
|
231
|
-
<head>
|
|
232
|
-
<meta charset="UTF-8">
|
|
233
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
234
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
235
|
-
<title>URL not found</title>
|
|
236
|
-
</head>
|
|
237
|
-
<body>
|
|
238
|
-
<h1>Not Found</h1>
|
|
239
|
-
<h3>The requested URL was not found on this server.</h3>
|
|
240
|
-
</body>
|
|
241
|
-
</html>
|
|
242
|
-
`;
|
|
243
|
-
} // function requestedUrlNotFound(){
|
|
244
|
-
|
|
245
|
-
//ATENZIONE per questa funzione guardare la descrizione sotto : per funzionare deve evvere chiamata nel seguente modo :
|
|
246
|
-
// const testFnCallbacks = testAllPathByFileList(filesAndDirArray, () => server, options0);//Genera l'array di callback per i test
|
|
247
|
-
// testFnCallbacks.forEach(cb => cb());// Esegui ogni callback per registrare il test nello scope del describe
|
|
248
|
-
|
|
249
|
-
function testAllPathByFileList(filesAndDirArray, getServer, options) {
|
|
250
|
-
return filesAndDirArray.map((entry, index) => {
|
|
251
|
-
const relativePath = path.relative(rootDir, entry.fullPath);
|
|
252
|
-
// Restituisci una funzione che, se chiamata, definisce un test.
|
|
253
|
-
return () => {
|
|
254
|
-
test(
|
|
255
|
-
`testo l'elemento ${index} (type: ${entry.type}, name: ${entry.name}) con percorso: ${relativePath}`,
|
|
256
|
-
async () => {
|
|
257
|
-
const server = getServer(); // Usa il getter per ottenere il server al momento dell'esecuzione
|
|
258
|
-
const url = path.join(options.urlPrefix || '/', relativePath);
|
|
259
|
-
const res = await supertest(server).get(url);
|
|
260
|
-
|
|
261
|
-
// Se l'entry è un file, controlla il contenuto e il MIME type
|
|
262
|
-
if (entry.type === 'file') {
|
|
263
|
-
expect(res.status).toBe(200);
|
|
264
|
-
const content = fs.readFileSync(entry.fullPath, 'utf8');
|
|
265
|
-
expect(res.type).toBe(entry.mimeType);
|
|
266
|
-
// For binary content-types (e.g. application/octet-stream), supertest populates
|
|
267
|
-
// res.body (Buffer) instead of res.text — normalise to string for comparison
|
|
268
|
-
const responseBody = res.text !== undefined ? res.text : res.body.toString('utf8');
|
|
269
|
-
expect(responseBody).toBe(content);
|
|
270
|
-
} else {//è una directory
|
|
271
|
-
if( options.dirListing && options.dirListing.enabled === false ){
|
|
272
|
-
// FIX: Quando directory listing è disabilitato, restituisce 404
|
|
273
|
-
expect(res.status).toBe(404);
|
|
274
|
-
expect(res.type).toBe('text/html');
|
|
275
|
-
expect(res.text.replace(/\s/g, '')).toBe(requestedUrlNotFound().replace(/\s/g, '')); //.replace(/\s/g, '') --> rimuoce gli spazi bianchi e le tabulazioni , il server agiunge degli spazi all'inizio facendo fallire il controllo
|
|
276
|
-
} else {
|
|
277
|
-
expect(res.status).toBe(200);
|
|
278
|
-
expect(res.type).toBe('text/html');
|
|
279
|
-
expect(res.text).toContain('<!DOCTYPE html>');
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
);
|
|
284
|
-
};
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/* ATTENZIONE QUESTA FUNZIONE [function testAllPathByFileList] È FATTA IN MANIERA STRANA PER AVER RISOLTO IL PROBLEMA DOPO LUNGA DISCUSSIONE CON CHATGPT
|
|
289
|
-
|
|
290
|
-
Spiegazione
|
|
291
|
-
|
|
292
|
-
Incapsulamento:
|
|
293
|
-
In testAllPathByFileList usiamo map per creare un array in cui ogni elemento è una funzione (callback) che, se invocata, definisce un test usando test().
|
|
294
|
-
Così facendo, non eseguiamo subito test(), ma ne restituiamo la definizione da chiamare in seguito.
|
|
295
|
-
|
|
296
|
-
Uso del Getter:
|
|
297
|
-
Passando () => server come secondo argomento, assicuriamo che al momento dell'esecuzione di ogni callback il server sia già inizializzato.
|
|
298
|
-
|
|
299
|
-
Registrazione dei Test:
|
|
300
|
-
Quando chiamiamo testFnCallbacks.forEach(cb => cb()); nello scope del blocco describe, ogni callback viene invocata e i test vengono definiti correttamente per Jest.
|
|
301
|
-
|
|
302
|
-
Idea di Base
|
|
303
|
-
|
|
304
|
-
Genera un array di callback:
|
|
305
|
-
Crea una funzione che, dato l'array dei file e directory (o dati dinamici), restituisca un array di funzioni. Ogni funzione (callback) quando invocata definirà un test tramite test() ed eseguirà le relative asserzioni con expect().
|
|
306
|
-
|
|
307
|
-
Esegui le callback nello scope del describe:
|
|
308
|
-
All'interno del blocco describe (dove Jest raccoglie i test) iteri sull'array di callback e invochi ciascuna, in modo che i test vengano registrati correttamente. In questo modo, l'errore "Your test suite must contain at least one test" non si verificherà perché i test saranno già stati definiti nello scope globale del describe
|
|
309
|
-
|
|
310
|
-
//END CONSIDERAZIONE IMPORTANTE
|
|
311
|
-
*/
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
/*
|
|
315
|
-
OLD//
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
function testAllPathByFileList(filesAndDirArray, getServer, options) {
|
|
319
|
-
return filesAndDirArray.map((entry, index) => {
|
|
320
|
-
const relativePath = path.relative(rootDir, entry.fullPath);
|
|
321
|
-
// Restituisci una funzione che, se chiamata, definisce un test.
|
|
322
|
-
return () => {
|
|
323
|
-
test(
|
|
324
|
-
`testo l'elemento ${index} (type: ${entry.type}, name: ${entry.name}) con percorso: ${relativePath}`,
|
|
325
|
-
async () => {
|
|
326
|
-
const server = getServer(); // Usa il getter per ottenere il server al momento dell'esecuzione
|
|
327
|
-
const url = path.join(options.urlPrefix || '/', relativePath);
|
|
328
|
-
const res = await supertest(server).get(url);
|
|
329
|
-
expect(res.status).toBe(200);
|
|
330
|
-
// Se l'entry è un file, controlla il contenuto e il MIME type
|
|
331
|
-
if (entry.type === 'file') {
|
|
332
|
-
const content = fs.readFileSync(entry.fullPath, 'utf8');
|
|
333
|
-
expect(res.type).toBe(entry.mimeType);
|
|
334
|
-
const responseBody = res.text !== undefined ? res.text : res.body.toString('utf8');
|
|
335
|
-
expect(responseBody).toBe(content);
|
|
336
|
-
} else {
|
|
337
|
-
expect(res.type).toBe('text/html');
|
|
338
|
-
expect(res.text).toContain('<!DOCTYPE html>');
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
);
|
|
342
|
-
};
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
*/
|