-
Notifications
You must be signed in to change notification settings - Fork 48
Support configured charsets for static content #106
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -150,6 +150,76 @@ describe('Static Cache', function () { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should serve files with configured charset', function (done) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var app = new Koa() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.use(staticCache(path.join(__dirname, '..'), { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| charset: 'iso-8859-1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filter(file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file === 'README.md' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var server = app.listen() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request(server) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get('/README.md') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect('Content-Type', 'text/markdown; charset=iso-8859-1') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect(200, done) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should serve files with configured charset for unknown mime types', function (done) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var app = new Koa() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.use(staticCache(path.join(__dirname, '..'), { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| charset: 'iso-8859-1', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filter(file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file === 'Makefile' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var server = app.listen() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request(server) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get('/Makefile') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect('Content-Type', 'application/octet-stream; charset=iso-8859-1') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect(200, done) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should serve files with function configured charset', function (done) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var app = new Koa() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.use(staticCache(path.join(__dirname, '..'), { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| charset(file, type) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file === 'README.md' && type === 'text/markdown' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? 'windows-1252' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filter(file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file === 'README.md' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var server = app.listen() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request(server) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get('/README.md') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect('Content-Type', 'text/markdown; charset=windows-1252') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect(200, done) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+199
to
+203
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add coverage for the case where the The current test only covers when the charset function returns a string. Please also add a case where
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should not append charset when charset function returns undefined', function (done) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var app = new Koa() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.use(staticCache(path.join(__dirname, '..'), { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| charset() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filter(file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return file === 'Makefile' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var server = app.listen() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request(server) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get('/Makefile') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect('Content-Type', 'application/octet-stream') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .expect(200, done) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should serve recursive files', function (done) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request(server) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .get('/test/index.js') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add a test to cover unknown MIME types with a configured charset to ensure the fallback MIME lookup behavior stays intact.
Specifically, please add a test where the served file has an unknown extension (e.g.
foo.bin) andoptions.charsetis set, and assert that theContent-Typeis'application/octet-stream; charset=iso-8859-1'(or equivalent). This will protect the fallback MIME type and charset concatenation behavior from regressions.Suggested implementation:
For this test to pass, ensure there is a
foo.binfile in the directory being served (based on your existing tests, likely the project root:path.join(__dirname, '..')). The file can be empty or contain arbitrary data; it only needs to exist so that the middleware can serve it and trigger the fallback MIME type logic.