UNPKG

1.24 kBJavaScriptView Raw
1'use strict'
2
3const { check } = require('./configuration')
4const dispatcher = require('./dispatcher')
5const EventEmitter = require('events')
6const Request = require('./mock/Request')
7const Response = require('./mock/Response')
8const {
9 $configurationEventEmitter,
10 $configurationInterface
11} = require('./symbols')
12
13module.exports = (jsonConfiguration, mockedHandlers = {}) => {
14 const eventEmitter = new EventEmitter()
15 eventEmitter.close = () => Promise.resolve()
16 return check(jsonConfiguration)
17 .then(configuration => {
18 configuration[$configurationEventEmitter] = eventEmitter
19 Object.assign(configuration.handlers, mockedHandlers)
20 configuration.listeners.forEach(listener => listener(eventEmitter))
21 eventEmitter.emit('server-created', {
22 configuration: configuration[$configurationInterface],
23 server: null
24 })
25 const dispatch = dispatcher.bind(eventEmitter, configuration)
26 eventEmitter.request = function () {
27 const request = new Request(...arguments)
28 const response = new Response()
29 const finished = response.waitForFinish()
30 return dispatch(request, response)
31 .then(() => finished)
32 }
33 return eventEmitter
34 })
35}