UNPKG

1.09 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 { $configurationInterface } = require('./symbols')
9
10module.exports = (jsonConfiguration, mockedHandlers = {}) => {
11 const eventEmitter = new EventEmitter()
12 return check(jsonConfiguration)
13 .then(configuration => {
14 Object.assign(configuration.handlers, mockedHandlers)
15 configuration.listeners.forEach(listener => listener(eventEmitter))
16 eventEmitter.emit('server-created', {
17 configuration: configuration[$configurationInterface],
18 server: null
19 })
20 const dispatch = dispatcher.bind(eventEmitter, configuration)
21 eventEmitter.request = function () {
22 const request = new Request(...arguments)
23 const response = new Response()
24 const finished = response.waitForFinish()
25 return dispatch(request, response)
26 .then(() => finished)
27 }
28 return eventEmitter
29 })
30}