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

Mime config option support #1735 Fixed #1736

Merged
merged 2 commits into from
Jan 4, 2016
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
17 changes: 17 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@ Note: Just about all additional reporters in Karma (other than progress) require
If `true`, Karma will start and capture all configured browsers, run tests and then exit with an exit code of `0` or `1` depending
on whether all tests passed or any tests failed.

## mime
**Type:** Object

**Default:** `{}`

**Description:** Redefine default mapping from file extensions to MIME-type

Set property name to required MIME, provide Array of extensions (without dots) as it's value

**Example:**
```javascript
mime: {
'text/x-typescript': ['ts','tsx']
'text/plain' : ['mytxt']
}
```


## transports
**Type:** Array
Expand Down
13 changes: 13 additions & 0 deletions lib/middleware/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

var mime = require('mime')
var log = require('../logger').create('web-server')
var helper = require('../helper')
var _ = helper._

var PromiseContainer = function () {
var promise
Expand Down Expand Up @@ -92,9 +94,20 @@ var setHeavyCacheHeaders = function (response) {
response.setHeader('Cache-Control', 'public, max-age=31536000')
}

var initializeMimeTypes = function (config) {
if (config && config.mime) {
_.forEach(config.mime, function (value, key) {
var map = {}
map[key] = value
mime.define(map)
})
}
}

// PUBLIC API
exports.PromiseContainer = PromiseContainer
exports.createServeFile = createServeFile
exports.setNoCacheHeaders = setNoCacheHeaders
exports.setHeavyCacheHeaders = setHeavyCacheHeaders
exports.initializeMimeTypes = initializeMimeTypes
exports.serve404 = serve404
1 change: 1 addition & 0 deletions lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ createCustomHandler.$inject = ['customFileHandlers', 'config.basePath']

var createWebServer = function (injector, emitter, fileList) {
var config = injector.get('config')
common.initializeMimeTypes(config)
var serveStaticFile = common.createServeFile(fs, path.normalize(__dirname + '/../static'), config)
var serveFile = common.createServeFile(fs, null, config)
var filesPromise = new common.PromiseContainer()
Expand Down
2 changes: 1 addition & 1 deletion test/unit/middleware/source_files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('middleware.source_files', function () {
})

it('should send no-caching headers for js source files without timestamps', function () {
var ZERO_DATE = (new Date(0)).toString()
var ZERO_DATE = new RegExp(new Date(0).toString().substring(0, 33).replace(/\+/, '\\+'))

servedFiles([
new File('/src/some.js')
Expand Down
12 changes: 11 additions & 1 deletion test/unit/web-server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import request from 'supertest-as-promised'
import di from 'di'
import mocks from 'mocks'
import fs from 'fs'
import mime from 'mime'

describe('web-server', () => {
var server
Expand Down Expand Up @@ -45,7 +46,8 @@ describe('web-server', () => {
basePath: '/base/path',
urlRoot: '/',
middleware: ['custom'],
middlewareResponse: 'hello middleware!'
middlewareResponse: 'hello middleware!',
mime: {'custom/custom': ['custom']}
}

var injector = new di.Injector([{
Expand All @@ -71,6 +73,14 @@ describe('web-server', () => {
server = injector.invoke(m.createWebServer)
})

it('should setup mime', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Would be good to have a test to ensure that the default mimes are still working

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, will add

expect(mime.lookup('/my.custom')).to.equal('custom/custom')
})

it('should keep default mimes', () => {
expect(mime.lookup('/my.html')).to.equal('text/html')
})

it('should serve client.html', () => {
servedFiles(new Set())

Expand Down