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 | 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 | }
|