1 | 'use strict'
|
2 |
|
3 | const { check } = require('./configuration')
|
4 | const dispatcher = require('./dispatcher')
|
5 | const EventEmitter = require('events')
|
6 | const Request = require('./mock/Request')
|
7 | const Response = require('./mock/Response')
|
8 | const {
|
9 | $configurationEventEmitter,
|
10 | $configurationInterface
|
11 | } = require('./symbols')
|
12 |
|
13 | module.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 | }
|