Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add option "serveIndex" to enable/disable serveIndex middleware #1752

Merged
merged 12 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const options = {
type: 'boolean',
describe: 'Lazy',
},
serveIndex: {
type: 'boolean',
describe: 'Enables/Disables serveIndex middleware',
default: true,
},
inline: {
type: 'boolean',
default: true,
Expand Down
10 changes: 9 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class Server {
this.headers = options.headers;
this.progress = options.progress;

this.serveIndex = options.serveIndex;

this.clientOverlay = options.overlay;
this.clientLogLevel = options.clientLogLevel;

Expand Down Expand Up @@ -557,9 +559,15 @@ class Server {

defaultFeatures.push('magicHtml');

if (contentBase !== false) {
// checking if it's set to true or not set (Default : undefined => true)
this.serveIndex = this.serveIndex || this.serveIndex === undefined;

const shouldHandleServeIndex = contentBase && this.serveIndex;

if (shouldHandleServeIndex) {
defaultFeatures.push('contentBaseIndex');
}

// compress is placed last and uses unshift so that it will be the first middleware used
if (options.compress) {
defaultFeatures.unshift('compress');
Expand Down
3 changes: 3 additions & 0 deletions lib/options.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "object",
"properties": {
"serveIndex": {
"type": "boolean"
},
"hot": {
"type": "boolean"
},
Expand Down
82 changes: 82 additions & 0 deletions test/ContentBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,89 @@ describe('ContentBase', () => {
}, 1000);
});
});
describe('test listing files in folders without index.html using the option serveIndex:false', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
serveIndex: false,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
});

it("shouldn't list the files inside the assets folder (404)", (done) => {
req.get('/assets/').expect(404, done);
});

it('should show Heyo. because bar has index.html inside it (200)', (done) => {
req.get('/bar/').expect(200, /Heyo/, done);
});
});
describe('test listing files in folders without index.html using the option serveIndex:true', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
serveIndex: true,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
});

it('should list the files inside the assets folder (200)', (done) => {
req.get('/assets/').expect(200, done);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the following test here because it is confirmed in serveIndex:false.

    it('should show Heyo. because bar has index.html inside it (200)', (done) => {
      req.get('/bar/').expect(200, /Heyo/, done);
    });


it('should show Heyo. because bar has index.html inside it (200)', (done) => {
req.get('/bar/').expect(200, /Heyo/, done);
});
});
describe('test listing files in folders without index.html using the option serveIndex default (true)', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
});

it('should list the files inside the assets folder (200)', (done) => {
req.get('/assets/').expect(200, done);
});

it('should show Heyo. because bar has index.html inside it (200)', (done) => {
req.get('/bar/').expect(200, /Heyo/, done);
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

describe('to directories', () => {
beforeAll((done) => {
server = helper.start(
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/contentbase-config/public/bar/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Heyo