UNPKG

node-static-auth

Version:

Node.js static server with Basic auth and access file logging, HTTPS support and custom error pages

97 lines (80 loc) 2.15 kB
import assert from 'better-assert'; import fs from 'fs'; import request from 'superagent'; /** * Features set as disabled tests * */ let config, inst; // eslint-disable-next-line no-sync const cert = fs.readFileSync(`${__dirname}/../example/server/localhost-test-cert.pem`); config = { nodeStatic: { // all available node-static options https://www.npmjs.com/package/node-static: `new static.Server(root, options)` // use path relative to project root, i.e. process.cwd() root: 'example/public', customPages: { forbidden: 'forbidden.html', notFound: 'not-found.html', error: 'error.html' } }, // our web server options server: { port: 4012, http2: false, ssl: { enabled: true, httpListener: 4010, // enter path relative to project root key: 'example/server/localhost-test-privkey.pem', cert: 'example/server/localhost-test-cert.pem' } }, // basic auth credentials auth: { enabled: false }, // logger file options logger: { use: false, filename: 'disabled-access.log', folder: 'example/server/dlogs', type: 'combined', options: {} } } before(function(done) { // runs before all tests in this block const NodeStaticAuth3 = require('../lib'); // eslint-disable-next-line no-unused-vars let disabled = new NodeStaticAuth3(config, (svr) => { inst = svr; done(); }); }); describe('static-auth server with logger and auth disabled', function() { it('should get home page', function(done) { request .get(`http://localhost:${config.server.ssl.httpListener}/?redirect-2-secured`) .ca(cert) .end(function(err, res) { assert(res.ok); done(); }); }); it('should get custom 404 page', function(done) { request .get(`${config.server.ssl.enabled ? 'https://' : 'http://'}localhost:${config.server.port}/no-page-here`) .ca(cert) .end(function(err, res) { assert(res.status === 404); assert(res.text.includes(`<h1>404</h1>`)); done(); }); }); }); after(function(done) { inst.close(); done(); })