UNPKG

1.19 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 return check(jsonConfiguration)
16 .then(configuration => {
17 configuration[$configurationEventEmitter] = eventEmitter
18 Object.assign(configuration.handlers, mockedHandlers)
19 configuration.listeners.forEach(listener => listener(eventEmitter))
20 eventEmitter.emit('server-created', {
21 configuration: configuration[$configurationInterface],
22 server: null
23 })
24 const dispatch = dispatcher.bind(eventEmitter, configuration)
25 eventEmitter.request = function () {
26 const request = new Request(...arguments)
27 const response = new Response()
28 const finished = response.waitForFinish()
29 return dispatch(request, response)
30 .then(() => finished)
31 }
32 return eventEmitter
33 })
34}