UNPKG

@infomaker/http-test-server

Version:

IMID HTTP Test Server

155 lines (129 loc) 5.03 kB
/*eslint-env node, jasmine */ 'use strict' 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 describe('HTTP test server', () => { it('should be able to be instantiated with a specified port', () => { httpTestServerUnderTest = new HTTPTestServer({ port: SUT_PORT }) expect({port: httpTestServerUnderTest.port}).toEqual({port: SUT_PORT}) }) it('getServerHost() should return a string', () => { const serverHost = httpTestServerUnderTest.getServerHost() expect(serverHost).toEqual(expect.any(String)) }) it('should be able to start', () => { return httpTestServerUnderTest.start() }) it('should be able to wait for a call', () => { httpTestServerUnderTest.waitForCall('/should-be-uncalled') return Promise.resolve() }) it('should have uncalled endpoints when stopped with uncalled endpoints', () => { return httpTestServerUnderTest.stop().catch(err => { expect({err}).toEqual({ err: { bufferedHttpCalls: [], uncalledEndpoints: [{ endpoint: '/should-be-uncalled', callbacksCount: 1 }] } }) }) }) it('should have buffered calls when stopped with buffered calls', async () => { httpTestServerUnderTest = new HTTPTestServer({ port: SUT_PORT }) expect.assertions(1) await httpTestServerUnderTest.start() try { await got.get('http://localhost:8080/should-be-buffered', { timeout: 20, retry: 0 }) } catch (err) { return httpTestServerUnderTest.stop().catch(err => { expect({err}).toEqual({ err: { bufferedHttpCalls: [{ url: '/should-be-buffered', data: expect.any(Object) // The req object }], uncalledEndpoints: [] } }) }) } }) it('Start httpTestServer', () => { httpTestServerUnderTest = new HTTPTestServer({ port: SUT_PORT }) return httpTestServerUnderTest.start() }) it('should be able to wait for a call and respond before being called', async () => { const expectedResponse = 'test-response' const endpoint = 'wait-before-call' const testServerResponse = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => { params.response.end(expectedResponse) }) // Wait a bit to make sure wait goes through before request await new Promise(x => setTimeout(x, 5)) const requestResponse = got.get(`${SUT_URL}/${endpoint}`, { retry: 0 }).then(res => { expect(res.body).toEqual(expectedResponse) }) return Promise.all([testServerResponse, requestResponse]) }) it('should be able to buffer calls to same endpoint and handle them after a little while', async () => { const expectedResponse = 'test-response' const endpoint = 'wait-after-call' const requestResponse1 = got.get(`${SUT_URL}/${endpoint}`, {retry: 0}).then(res => { expect(res.body).toEqual(expectedResponse) }) const requestResponse2 = got.get(`${SUT_URL}/${endpoint}`, {retry: 0}).then(res => { expect(res.body).toEqual(expectedResponse) }) // Wait a bit to make sure request gets to call buffer await new Promise(x => setTimeout(x, 5)) const testServerResponse1 = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => { params.response.end(expectedResponse) }) const testServerResponse2 = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => { params.response.end(expectedResponse) }) return Promise.all([testServerResponse1,testServerResponse2, requestResponse1,requestResponse2]) }) it('should be able to wait for two calls and respond after being called twice', async () => { const expectedResponse = 'test-response' const endpoint = 'wait-after-call-twice' const requestResponseOne = got.get(`${SUT_URL}/${endpoint}`, {retry: 0}).then(res => { expect(res.body).toEqual(expectedResponse) }) const requestResponseTwo = got.get(`${SUT_URL}/${endpoint}`, {retry: 0}).then(res => { expect(res.body).toEqual(expectedResponse) }) // Wait a bit to make sure requests get to call buffer await new Promise(x => setTimeout(x, 5)) const testServerResponseOne = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => { params.response.end(expectedResponse) }) const testServerResponseTwo = httpTestServerUnderTest.waitForCall(`/${endpoint}`).then((params) => { params.response.end(expectedResponse) }) return Promise.all([testServerResponseOne, testServerResponseTwo, requestResponseOne, requestResponseTwo]) }) it('should have no leftovers when stopped', () => { return httpTestServerUnderTest.stop().catch(err => { expect({err}).toEqual({err: null}) }) }) })