Skip to content

Commit

Permalink
feat(config): mime config option support
Browse files Browse the repository at this point in the history
Allows to setup required mime-type to extensions mapping
(for ex. fix default *.ts to video/mp2t mapping)

Closes karma-runner#1735
  • Loading branch information
comdiv committed Dec 2, 2015
1 parent 94e4bf2 commit ce325be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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
8 changes: 7 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,10 @@ describe('web-server', () => {
server = injector.invoke(m.createWebServer)
})

it('should setup mime', () => {
expect(mime.lookup('/my.custom')).to.equal('custom/custom')
})

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

Expand Down

0 comments on commit ce325be

Please sign in to comment.