This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for other response content-types
The new option `mimeType` lets you respond with any file type you want.
- Loading branch information
Showing
8 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a href="https://github.com">GitHub</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is just a plain old text file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const SetupEndpoint = require('./setup/'); | ||
|
||
module.exports = SetupEndpoint({ | ||
name: 'fileTypes', | ||
urls: [{ | ||
params: '/json', | ||
requests: [{ | ||
response: '/response-files/simpleExample.json' | ||
}] | ||
}, { | ||
params: '/text', | ||
requests: [{ | ||
response: '/response-files/example.txt', | ||
mimeType: 'text/plain' | ||
}] | ||
}, { | ||
params: '/html', | ||
requests: [{ | ||
response: '/response-files/example.html', | ||
statusCode: 201, | ||
mimeType: 'text/html' | ||
}] | ||
}] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict'; | ||
|
||
const Lab = require('lab'); | ||
const Code = require('code'); | ||
const Config = require('../../../config'); | ||
const Hapi = require('hapi'); | ||
const SetupEndpoint = require('../../../server/api/setup/'); | ||
|
||
const apiUrlPrefix = Config.get('/apiUrlPrefix'); | ||
|
||
const Endpoint = SetupEndpoint({ | ||
name: 'fileTypes', | ||
urls: [{ | ||
params: '/json', | ||
requests: [{ | ||
response: '/test/server/api/fixtures/response.json' | ||
}] | ||
}, { | ||
params: '/text', | ||
requests: [{ | ||
response: '/test/server/api/fixtures/response.txt', | ||
mimeType: 'text/plain' | ||
}] | ||
}, { | ||
params: '/html', | ||
requests: [{ | ||
response: '/test/server/api/fixtures/response.html', | ||
statusCode: 201, | ||
mimeType: 'text/html' | ||
}] | ||
}] | ||
}); | ||
|
||
const lab = exports.lab = Lab.script(); | ||
let request; | ||
let server; | ||
|
||
lab.beforeEach((done) => { | ||
|
||
const plugins = [Endpoint]; | ||
server = new Hapi.Server(); | ||
server.connection({ port: Config.get('/port/web') }); | ||
server.register(plugins, (err) => { | ||
|
||
if (err) { | ||
return done(err); | ||
} | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
|
||
lab.experiment('Different file types', () => { | ||
|
||
lab.beforeEach((done) => { | ||
|
||
|
||
done(); | ||
}); | ||
|
||
|
||
lab.test('content-type header defaults to `application/json`', (done) => { | ||
|
||
request = { | ||
method: 'GET', | ||
url: apiUrlPrefix + '/fileTypes/json' | ||
}; | ||
|
||
server.inject(request, (response) => { | ||
|
||
Code.expect(response.headers['content-type']).to.equal('application/json; charset=utf-8'); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
lab.test('return text files with the defined content-type header`', (done) => { | ||
|
||
request = { | ||
method: 'GET', | ||
url: apiUrlPrefix + '/fileTypes/text' | ||
}; | ||
|
||
server.inject(request, (response) => { | ||
|
||
Code.expect(response.headers['content-type']).to.equal('text/plain; charset=utf-8'); | ||
Code.expect(response.result).to.equal('This is just a plain old text file.\n'); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
lab.test('return with the defined content-type header and custom status code`', (done) => { | ||
|
||
request = { | ||
method: 'GET', | ||
url: apiUrlPrefix + '/fileTypes/html' | ||
}; | ||
|
||
server.inject(request, (response) => { | ||
|
||
Code.expect(response.headers['content-type']).to.equal('text/html; charset=utf-8'); | ||
Code.expect(response.result).to.equal('<a href="https://github.com">GitHub</a>\n'); | ||
Code.expect(response.statusCode).to.equal(201); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<a href="https://github.com">GitHub</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is just a plain old text file. |