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 { $configurationInterface } = require('./symbols')
|
9 |
|
10 | module.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 | }
|