2022-10-28 08:30:39 +03:00
|
|
|
var request = require('supertest');
|
|
|
|
request = request('http://127.0.0.1:8080');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2023-01-05 01:09:51 +03:00
|
|
|
const randomString = require('./lib/rand').randomString;
|
2022-10-28 08:30:39 +03:00
|
|
|
|
2023-01-01 02:10:31 +03:00
|
|
|
const publicPath = path.resolve(__dirname, '../../../public');
|
2023-01-05 01:09:51 +03:00
|
|
|
const filename = randomString() + '.txt';
|
|
|
|
const fileContent = randomString();
|
2022-10-28 08:30:39 +03:00
|
|
|
|
2023-01-01 02:10:31 +03:00
|
|
|
test('random public static file does not exist', async (done) => {
|
|
|
|
request.get('/public/' + filename).expect(404);
|
2022-10-28 08:30:39 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2023-01-01 02:10:31 +03:00
|
|
|
test('public directory is writable', async (done) => {
|
|
|
|
|
2022-10-28 08:30:39 +03:00
|
|
|
try {
|
2023-01-01 02:10:31 +03:00
|
|
|
writeFileToPublic();
|
|
|
|
} catch (err) {
|
|
|
|
if (err) {
|
|
|
|
if (err.code === "ENOENT") { // path does not exist
|
|
|
|
fs.mkdirSync(publicPath);
|
|
|
|
writeFileToPublic();
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2022-10-28 08:30:39 +03:00
|
|
|
|
2023-01-01 02:10:31 +03:00
|
|
|
test('public static file is accessible', async (done) => {
|
2022-10-28 08:30:39 +03:00
|
|
|
|
2023-01-01 02:10:31 +03:00
|
|
|
request.get('/public/' + filename).expect(200).then((res) => {
|
|
|
|
expect(res.text).toEqual(fileContent);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test('public static file is persistent and not locked', async (done) => {
|
|
|
|
|
|
|
|
fs.unlink(path.join(publicPath, filename), (err) => {
|
|
|
|
if (err) { throw err; }
|
|
|
|
});
|
2022-10-28 08:30:39 +03:00
|
|
|
done();
|
|
|
|
});
|
2023-01-01 02:10:31 +03:00
|
|
|
|
|
|
|
function writeFileToPublic() {
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(publicPath, filename),
|
|
|
|
fileContent
|
|
|
|
);
|
|
|
|
}
|