Skip to content

Commit

Permalink
fix(middleware): switch headers and mime types handling back after fi…
Browse files Browse the repository at this point in the history
…le check (#672)
  • Loading branch information
knagaitsev committed Jun 30, 2020
1 parent 7fa2c15 commit dd6d59e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
16 changes: 6 additions & 10 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ export default function wrapper(context) {
return;
}

// Content-Type and headers need to be set before checking if
// the file is in the outputFileSystem, as these things should be
// applied to all files that are being served
try {
content = context.outputFileSystem.readFileSync(filename);
} catch (_ignoreError) {
await goNext();
return;
}

if (!res.get('Content-Type')) {
// content-type name(like application/javascript; charset=utf-8) or false
Expand All @@ -68,13 +71,6 @@ export default function wrapper(context) {
}
}

try {
content = context.outputFileSystem.readFileSync(filename);
} catch (_ignoreError) {
await goNext();
return;
}

// Buffer
content = handleRangeHeaders(content, req, res);

Expand Down
18 changes: 9 additions & 9 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@ describe('middleware', () => {
});
});

describe('should set "Content-Type" header for route not from outputFileSystem', () => {
describe('should not set "Content-Type" header for route not from outputFileSystem', () => {
beforeAll((done) => {
const outputPath = path.resolve(__dirname, './outputs/basic');
const compiler = getCompiler({
Expand Down Expand Up @@ -2109,10 +2109,10 @@ describe('middleware', () => {

afterAll(close);

it('should return the "200" code for the "GET" request "file.jpg"', (done) => {
it('should return the "200" code for the "GET" request "file.jpg" with default content type', (done) => {
request(app)
.get('/file.jpg')
.expect('Content-Type', /application\/octet-stream/)
.expect('Content-Type', /text\/html/)
.expect(200, done);
});
});
Expand Down Expand Up @@ -2741,15 +2741,15 @@ describe('middleware', () => {
.expect(200, done);
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem and return headers', (done) => {
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
app.get('/file.jpg', (req, res) => {
res.send('welcome');
});
request(app)
.get('/file.jpg')
.expect('X-nonsense-1', 'yes')
.expect('X-nonsense-2', 'no')
.expect(200, done);

const res = await request(app).get('/file.jpg');
expect(res.statusCode).toEqual(200);
expect(res.headers['X-nonsense-1']).toBeUndefined();
expect(res.headers['X-nonsense-2']).toBeUndefined();
});
});

Expand Down

0 comments on commit dd6d59e

Please sign in to comment.