From 4301bea587641e5e979a4887f04b4ec67a1a7bb0 Mon Sep 17 00:00:00 2001 From: Olivier Yiptong Date: Wed, 30 Sep 2015 14:37:38 -0400 Subject: [PATCH] feat(web-server): add support for custom headers in files served --- docs/config/01-configuration-file.md | 24 ++++++++++++++++++++++++ lib/middleware/common.js | 12 +++++++++++- lib/web-server.js | 4 ++-- test/e2e/headers.feature | 26 ++++++++++++++++++++++++++ test/e2e/support/headers/foo.js | 1 + test/e2e/support/headers/test.js | 14 ++++++++++++++ 6 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 test/e2e/headers.feature create mode 100644 test/e2e/support/headers/foo.js create mode 100644 test/e2e/support/headers/test.js diff --git a/docs/config/01-configuration-file.md b/docs/config/01-configuration-file.md index fef334b93..ecf5abf05 100644 --- a/docs/config/01-configuration-file.md +++ b/docs/config/01-configuration-file.md @@ -175,6 +175,30 @@ documentation to see how (and if) it uses this value. **Description:** Enable or disable colors in the output (reporters and logs). +## customHeaders +**Type:** Array + +**Default:** `undefined` + +**Description** Custom HTTP headers that will be set upon serving files by Karma's web server. +Custom headers are useful, especially with upcoming browser features like Service Workers. + +The `customHeaders` option allows you to set HTTP headers for files that match a regular expression. +`customHeaders` is an array of `Objects` with properties as follows: + +* match: Regular expression string to match files +* name: HTTP header name +* value: HTTP header value + +**Example:** +```javascript +customHeaders: [{ + match: '.*foo.html', + name: 'Service-Worker-Allowed', + value: '/' +}] +``` + ## exclude **Type:** Array diff --git a/lib/middleware/common.js b/lib/middleware/common.js index b098790f2..b13433e64 100644 --- a/lib/middleware/common.js +++ b/lib/middleware/common.js @@ -23,7 +23,7 @@ var serve404 = function (response, path) { return response.end('NOT FOUND') } -var createServeFile = function (fs, directory) { +var createServeFile = function (fs, directory, config) { var cache = Object.create(null) return function (filepath, response, transform, content, doNotCache) { @@ -37,6 +37,16 @@ var createServeFile = function (fs, directory) { content = cache[filepath] } + if (config && config.customHeaders && config.customHeaders.length > 0) { + config.customHeaders.forEach(function (header) { + var regex = new RegExp(header.match) + if (regex.test(filepath)) { + log.debug('setting header: ' + header.name + ' for: ' + filepath) + response.setHeader(header.name, header.value) + } + }) + } + // serve from cache if (content && !doNotCache) { response.setHeader('Content-Type', mime.lookup(filepath, 'text/plain')) diff --git a/lib/web-server.js b/lib/web-server.js index 139b5e3ea..244e48f59 100644 --- a/lib/web-server.js +++ b/lib/web-server.js @@ -31,8 +31,8 @@ createCustomHandler.$inject = ['customFileHandlers', 'config.basePath'] var createWebServer = function (injector, emitter, fileList) { var config = injector.get('config') - var serveStaticFile = common.createServeFile(fs, path.normalize(__dirname + '/../static')) - var serveFile = common.createServeFile(fs) + var serveStaticFile = common.createServeFile(fs, path.normalize(__dirname + '/../static'), config) + var serveFile = common.createServeFile(fs, null, config) var filesPromise = new common.PromiseContainer() // Set an empty list of files to avoid race issues with diff --git a/test/e2e/headers.feature b/test/e2e/headers.feature new file mode 100644 index 000000000..e343f1135 --- /dev/null +++ b/test/e2e/headers.feature @@ -0,0 +1,26 @@ +Feature: Custom Headers + In order to use Karma + As a person who wants to write great tests + I want to Karma to send custom headers on files sent. + + Scenario: Simple file with headers + Given a configuration with: + """ + files = ['headers/*.js']; + browsers = ['PhantomJS']; + plugins = [ + 'karma-jasmine', + 'karma-phantomjs-launcher' + ]; + customHeaders = [{ + match: 'foo.js', + name: 'Custom-Header-Awesomeness', + value: 'there.is.no.dana.only.zuul' + }]; + """ + When I start Karma + Then it passes with: + """ + . + PhantomJS + """ diff --git a/test/e2e/support/headers/foo.js b/test/e2e/support/headers/foo.js new file mode 100644 index 000000000..88e3023a4 --- /dev/null +++ b/test/e2e/support/headers/foo.js @@ -0,0 +1 @@ +'/base/headers/foo.js source' diff --git a/test/e2e/support/headers/test.js b/test/e2e/support/headers/test.js new file mode 100644 index 000000000..99e6ca581 --- /dev/null +++ b/test/e2e/support/headers/test.js @@ -0,0 +1,14 @@ +function httpGet (url) { + var xmlHttp = new XMLHttpRequest() + + xmlHttp.open('GET', url, false) + xmlHttp.send(null) + + return xmlHttp +} + +describe('setting custom headers', function () { + it('should get custom headers', function () { + expect(httpGet('/base/headers/foo.js').getResponseHeader('Custom-Header-Awesomeness')).toBe('there.is.no.dana.only.zuul') + }) +})