Skip to content

Commit

Permalink
feat(web-server): add support for custom headers in files served
Browse files Browse the repository at this point in the history
  • Loading branch information
oyiptong committed Oct 1, 2015
1 parent 65080f5 commit 4301bea
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
24 changes: 24 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion lib/middleware/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'))
Expand Down
4 changes: 2 additions & 2 deletions lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/headers.feature
Original file line number Diff line number Diff line change
@@ -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
"""
1 change: 1 addition & 0 deletions test/e2e/support/headers/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'/base/headers/foo.js source'
14 changes: 14 additions & 0 deletions test/e2e/support/headers/test.js
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 4301bea

Please sign in to comment.