Hide given file extensions
This commit is contained in:
+9
-1
@@ -17,7 +17,6 @@ module.exports = (config) => {
|
|||||||
|
|
||||||
const articles = [];
|
const articles = [];
|
||||||
|
|
||||||
|
|
||||||
const reload = (callback) => {
|
const reload = (callback) => {
|
||||||
fw.fetchArticles((err, list) => {
|
fw.fetchArticles((err, list) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -32,6 +31,8 @@ module.exports = (config) => {
|
|||||||
callback(true);
|
callback(true);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
if (config['test'])
|
||||||
|
app.reload = reload;
|
||||||
|
|
||||||
const render = (res, path, data, code = 200) => {
|
const render = (res, path, data, code = 200) => {
|
||||||
res.render(path, data, (err, html) => {
|
res.render(path, data, (err, html) => {
|
||||||
@@ -63,6 +64,13 @@ module.exports = (config) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('*', (req, res, next) => {
|
||||||
|
if (config['home']['hidden'].includes(path.extname(req.path)))
|
||||||
|
showError(req.path, 404, res);
|
||||||
|
else
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
app.get('*', express.static(config['data_dir']));
|
app.get('*', express.static(config['data_dir']));
|
||||||
|
|
||||||
app.get('*', (req, res) => {
|
app.get('*', (req, res) => {
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
},
|
},
|
||||||
"home": {
|
"home": {
|
||||||
"index": "index.ejs",
|
"index": "index.ejs",
|
||||||
"error": "error.ejs"
|
"error": "error.ejs",
|
||||||
|
"hidden": [".ejs"]
|
||||||
},
|
},
|
||||||
"article": {
|
"article": {
|
||||||
"index": "index.md",
|
"index": "index.md",
|
||||||
|
|||||||
+39
-6
@@ -14,8 +14,15 @@ const config = {
|
|||||||
'view_engine': 'ejs',
|
'view_engine': 'ejs',
|
||||||
'home': {
|
'home': {
|
||||||
'index': testIndex,
|
'index': testIndex,
|
||||||
'error': testError
|
'error': testError,
|
||||||
}
|
'hidden': ['.ejs','.test']
|
||||||
|
},
|
||||||
|
'article': {
|
||||||
|
'index': 'index.md',
|
||||||
|
'thumbnail_tag': 'thumbnail',
|
||||||
|
'default_title': 'Untitled',
|
||||||
|
'default_thumbnail': null
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const app = require('../src/app')(config);
|
const app = require('../src/app')(config);
|
||||||
@@ -46,15 +53,34 @@ describe('Test root path', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test('200 index page', (done) =>{
|
test('200 no articles', (done) =>{
|
||||||
fs.writeFileSync(path.join(dataDir,testIndex), 'hello there');
|
fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>');
|
||||||
request(app).get('/').then((response) =>{
|
request(app).get('/').then((response) =>{
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.text).toBe('hello there');
|
expect(response.text).toBe('articles 0');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
//TODO test articles list
|
test('200 2 articles', (done) =>{
|
||||||
|
utils.createEmptyDirs([
|
||||||
|
path.join(dataDir, '2019', '05', '05'),
|
||||||
|
path.join(dataDir, '2018', '05', '05')
|
||||||
|
]);
|
||||||
|
utils.createEmptyFiles([
|
||||||
|
path.join(dataDir, '2019', '05', '05','index.md'),
|
||||||
|
path.join(dataDir, '2018', '05', '05','index.md')
|
||||||
|
]);
|
||||||
|
fs.writeFileSync(path.join(dataDir,testIndex), 'articles <%= articles.length %>');
|
||||||
|
app.reload((res) => {
|
||||||
|
expect(res).toBe(true);
|
||||||
|
request(app).get('/').then((response) =>{
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.text).toBe('articles 2');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Test static files', () => {
|
describe('Test static files', () => {
|
||||||
@@ -72,6 +98,13 @@ describe('Test static files', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('404 hidden file', (done) =>{
|
||||||
|
fs.writeFileSync(path.join(dataDir,'somefile.test'), '');
|
||||||
|
request(app).get('/somefile.test').then((response) =>{
|
||||||
|
expect(response.statusCode).toBe(404);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
test('200 valid file', (done) =>{
|
test('200 valid file', (done) =>{
|
||||||
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
|
fs.writeFileSync(`${dataDir}/somefile.txt`, 'filecontent');
|
||||||
request(app).get('/somefile.txt').then((response) =>{
|
request(app).get('/somefile.txt').then((response) =>{
|
||||||
|
|||||||
Reference in New Issue
Block a user