Skip to content

Commit 5c9eee5

Browse files
authoredJan 3, 2021
fix: skip content-type header on unknown types (#809)
1 parent 16928a0 commit 5c9eee5

File tree

2 files changed

+32
-67
lines changed

2 files changed

+32
-67
lines changed
 

‎src/middleware.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ export default function wrapper(context) {
6565
// content-type name(like application/javascript; charset=utf-8) or false
6666
const contentType = mime.contentType(path.extname(filename));
6767

68-
// Express API
69-
if (res.set && contentType) {
70-
res.set('Content-Type', contentType);
71-
}
72-
// Node.js API
73-
else {
74-
res.setHeader(
75-
'Content-Type',
76-
contentType || 'application/octet-stream'
77-
);
68+
// Only set content-type header if media type is known
69+
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
70+
if (contentType) {
71+
// Express API
72+
if (res.set) {
73+
res.set('Content-Type', contentType);
74+
}
75+
// Node.js API
76+
else {
77+
res.setHeader('Content-Type', contentType);
78+
}
7879
}
7980
}
8081

‎test/middleware.test.js

+21-57
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ describe.each([
290290
request(app)
291291
.get('/unknown')
292292
.expect('Content-Length', fileData.byteLength.toString())
293-
.expect('Content-Type', 'application/octet-stream')
294293
.expect(200, done);
295294
});
296295
});
@@ -584,7 +583,6 @@ describe.each([
584583
urls: [
585584
{
586585
value: 'filename-name-with-dots/mono-v6.x.x',
587-
contentType: 'application/octet-stream',
588586
code: 200,
589587
},
590588
],
@@ -595,7 +593,6 @@ describe.each([
595593
urls: [
596594
{
597595
value: 'noextension',
598-
contentType: 'application/octet-stream',
599596
code: 200,
600597
},
601598
],
@@ -812,12 +809,19 @@ describe.each([
812809
it(`should return the "${code}" code for the "GET" request for the "${value}" url`, (done) => {
813810
request(app)
814811
.get(`${publicPathForRequest}${value}`)
815-
.expect('Content-Type', contentType)
816812
.expect(
817813
'Content-Length',
818814
data ? String(data.length) : /\d+/
819815
)
820-
.expect(code, done);
816+
.expect(code)
817+
.then((res) => {
818+
if (contentType) {
819+
expect(res.headers['content-type']).toEqual(
820+
contentType
821+
);
822+
}
823+
})
824+
.then(done);
821825
});
822826
}
823827
}
@@ -835,11 +839,14 @@ describe.each([
835839
app.use((req, res, next) => {
836840
// Express API
837841
if (res.set) {
838-
res.set('Content-Type', 'application/octet-stream');
842+
res.set('Content-Type', 'application/vnd.test+octet-stream');
839843
}
840844
// Connect API
841845
else {
842-
res.setHeader('Content-Type', 'application/octet-stream');
846+
res.setHeader(
847+
'Content-Type',
848+
'application/vnd.test+octet-stream'
849+
);
843850
}
844851
next();
845852
});
@@ -850,10 +857,10 @@ describe.each([
850857

851858
afterAll(close);
852859

853-
it('should not guess a MIME type if the "Content-Type" header is found', (done) => {
860+
it('should not modify the "Content-Type" header', (done) => {
854861
request(app)
855862
.get('/bundle.js')
856-
.expect('Content-Type', 'application/octet-stream')
863+
.expect('Content-Type', 'application/vnd.test+octet-stream')
857864
.expect(200, done);
858865
});
859866
});
@@ -2019,43 +2026,6 @@ describe.each([
20192026
});
20202027
});
20212028

2022-
describe('should set the correct value for "Content-Type" header to unknown MIME type', () => {
2023-
beforeAll((done) => {
2024-
const outputPath = path.resolve(__dirname, './outputs/basic');
2025-
const compiler = getCompiler({
2026-
...webpackConfig,
2027-
output: {
2028-
filename: 'bundle.js',
2029-
path: outputPath,
2030-
},
2031-
});
2032-
2033-
instance = middleware(compiler);
2034-
2035-
app = framework();
2036-
app.use(instance);
2037-
2038-
listen = listenShorthand(done);
2039-
2040-
instance.context.outputFileSystem.mkdirSync(outputPath, {
2041-
recursive: true,
2042-
});
2043-
instance.context.outputFileSystem.writeFileSync(
2044-
path.resolve(outputPath, 'file.phtml'),
2045-
'welcome'
2046-
);
2047-
});
2048-
2049-
afterAll(close);
2050-
2051-
it('should return the "200" code for the "GET" request to "file.phtml"', (done) => {
2052-
request(app)
2053-
.get('/file.phtml')
2054-
.expect('Content-Type', 'application/octet-stream')
2055-
.expect(200, done);
2056-
});
2057-
});
2058-
20592029
describe('should set the correct value for "Content-Type" header to specified MIME type', () => {
20602030
beforeAll((done) => {
20612031
const outputPath = path.resolve(__dirname, './outputs/basic');
@@ -2110,7 +2080,7 @@ describe.each([
21102080

21112081
instance = middleware(compiler, {
21122082
mimeTypes: {
2113-
jpg: 'application/octet-stream',
2083+
jpg: 'image/vnd.test+jpeg',
21142084
},
21152085
});
21162086

@@ -2133,7 +2103,7 @@ describe.each([
21332103
it('should return the "200" code for the "GET" request "file.jpg"', (done) => {
21342104
request(app)
21352105
.get('/file.jpg')
2136-
.expect('Content-Type', /application\/octet-stream/)
2106+
.expect('Content-Type', 'image/vnd.test+jpeg')
21372107
.expect(200, done);
21382108
});
21392109
});
@@ -2151,7 +2121,7 @@ describe.each([
21512121

21522122
instance = middleware(compiler, {
21532123
mimeTypes: {
2154-
jpg: 'application/octet-stream',
2124+
jpg: 'image/vnd.test+jpeg',
21552125
},
21562126
});
21572127

@@ -3225,10 +3195,7 @@ describe.each([
32253195
afterAll(close);
32263196

32273197
it('should return the "200" code for the "GET" request to the public path', (done) => {
3228-
request(app)
3229-
.get('/')
3230-
.expect('Content-Type', 'application/octet-stream')
3231-
.expect(200, done);
3198+
request(app).get('/').expect(200, done);
32323199
});
32333200
});
32343201

@@ -3305,10 +3272,7 @@ describe.each([
33053272
afterAll(close);
33063273

33073274
it('should return the "200" code for the "GET" request to the public path', (done) => {
3308-
request(app)
3309-
.get('/')
3310-
.expect('Content-Type', 'application/octet-stream')
3311-
.expect(200, done);
3275+
request(app).get('/').expect(200, done);
33123276
});
33133277
});
33143278

0 commit comments

Comments
 (0)