@infomaker/http-test-server
Version:
IMID HTTP Test Server
107 lines (88 loc) • 3.28 kB
JavaScript
/*eslint-env node, jasmine */
const HTTPTestServer = require('../src/http-test-server')
const got = require('got')
const SUT_PORT = 8080
const SUT_HOST = 'http://localhost'
const SUT_URL = `${SUT_HOST}:${SUT_PORT}`
let httpTestServerUnderTest = new HTTPTestServer({
port: SUT_PORT
})
const methodsThatSupportRequestBody = ['POST', 'PUT', 'PATCH']
describe('HTTP test server', () => {
it('start httpTestServer', () => {
return httpTestServerUnderTest.start()
})
methodsThatSupportRequestBody.map(method => {
it(`supports text/plain body for ${method} requests`, () => {
const expectedBody = 'testValue'
const endpoint = `text-body-${method}`
const testServerResponse = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => {
expect(params.request.body).toEqual(expectedBody)
params.response.end()
})
const requestResponse = got(`${SUT_URL}/${endpoint}`, {
body: expectedBody,
method: method,
headers: {
'content-type': 'text/plain'
}
}).then()
return Promise.all([testServerResponse, requestResponse])
})
it(`supports application/json body for ${method} requests`, () => {
const expectedBody = {
testKey: 'testValue'
}
const endpoint = `text-body-${method}`
const testServerResponse = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => {
expect(params.request.body).toEqual(expectedBody)
params.response.end()
})
const requestResponse = got(`${SUT_URL}/${endpoint}`, {
method: method,
json: expectedBody
})
return Promise.all([testServerResponse, requestResponse])
})
it(`supports application/x-www-form-urlencoded body for ${method} requests`, () => {
const expectedBody = {
testKey: 'testValue'
}
const endpoint = `text-body-${method}`
const testServerResponse = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => {
expect(params.request.body).toEqual(expectedBody)
params.response.end()
})
const requestResponse = got(`${SUT_URL}/${endpoint}`, {
method: method,
form: expectedBody
})
return Promise.all([testServerResponse, requestResponse])
})
it(`supports any body treated as application/json for ${method} requests`, () => {
const expectedBody = {
testKey: 'testValue'
}
const endpoint = `any-body-${method}`
const testServerResponse = httpTestServerUnderTest.waitForCall(`/${endpoint}`, {treatContentTypeAs: 'application/json'}).then((params) => {
expect(params.request.body).toEqual(expectedBody)
expect(params.request.headers['content-type']).toEqual('whatever')
params.response.end()
})
const requestResponse = got(`${SUT_URL}/${endpoint}`, {
method: method,
headers: {
'content-type': 'whatever'
},
body: JSON.stringify(expectedBody),
})
return Promise.all([testServerResponse, requestResponse])
})
})
it('stop httpTestServer', () => {
return httpTestServerUnderTest.stop().catch(err => {
expect(err).toEqual(null)
})
})
})