@@ -9,7 +9,13 @@ const isRegExp = require('101/is-regexp')
9
9
const keypather = require ( 'keypather' ) ( )
10
10
const noop = require ( '101/noop' )
11
11
const sinon = require ( 'sinon' )
12
- const debug = require ( 'debug' ) ( 'mehpi' )
12
+ const debug = require ( 'debug' )
13
+ const debugInit = debug ( 'mehpi:init' )
14
+ const debugInitError = debug ( 'mehpi:init:error' )
15
+ const debugSetup = debug ( 'mehpi:setup' )
16
+ const debugSetupError = debug ( 'mehpi:setup:error' )
17
+ const debugRespone = debug ( 'mehpi:response' )
18
+ const debugResponeError = debug ( 'mehpi:response:error' )
13
19
14
20
const PRIORITY_LIMIT = 100
15
21
const PRIORITY_DEFAULT = 10
@@ -46,12 +52,11 @@ module.exports = class MockAPI {
46
52
}
47
53
this . server . listen ( this . port , ( err ) => {
48
54
if ( err ) {
49
- debug ( `Failed to start server (port: ${ this . port } )` )
50
- debug ( err . message )
55
+ debugInitError ( `Failed to start server (port: ${ this . port } ) / ${ err . message } ` )
51
56
done ( err )
52
57
return
53
58
}
54
- debug ( `Server listening (port: ${ this . port } )` )
59
+ debugInit ( `Server listening (port: ${ this . port } )` )
55
60
done ( )
56
61
} )
57
62
}
@@ -65,7 +70,7 @@ module.exports = class MockAPI {
65
70
done = noop
66
71
}
67
72
this . server . close ( ( err ) => {
68
- debug ( `Server stopped (port: ${ this . port } )` )
73
+ debugInit ( `Server stopped (port: ${ this . port } )` )
69
74
done ( err )
70
75
} )
71
76
}
@@ -83,7 +88,7 @@ module.exports = class MockAPI {
83
88
}
84
89
const result = routeStub ( request , response )
85
90
86
- debug ( `Request: ${ method } ${ path } ` )
91
+ debugRespone ( `Request: ${ method } ${ path } ` )
87
92
88
93
let status = 200
89
94
let body = 'response'
@@ -105,7 +110,7 @@ module.exports = class MockAPI {
105
110
}
106
111
}
107
112
108
- debug ( `Response: ${ status } ${ body } (${ contentType } )` )
113
+ debugRespone ( `Response: ${ status } ${ body } (${ contentType } )` )
109
114
110
115
response . writeHead ( status , { 'Content-Type' : contentType } )
111
116
response . end ( body )
@@ -121,13 +126,15 @@ module.exports = class MockAPI {
121
126
const key = `${ method } ${ path } `
122
127
let textStub = keypather . get ( this . routeStubs . text , key )
123
128
if ( textStub ) {
129
+ debugRespone ( `Found string match: ${ method } ${ path } ` )
124
130
return textStub
125
131
}
126
132
// Check all regular expressions
127
133
for ( var i = PRIORITY_LIMIT ; i >= 0 ; i -= 1 ) {
128
134
if ( Array . isArray ( this . routeStubs . regex [ i ] ) ) {
129
135
for ( let entry of this . routeStubs . regex [ i ] ) {
130
136
if ( path . match ( entry . regex ) ) {
137
+ debugRespone ( `Found regular expression: ${ method } ${ path } ${ entry . regex } ` )
131
138
return entry . stub
132
139
}
133
140
}
@@ -155,6 +162,7 @@ module.exports = class MockAPI {
155
162
this . routeStubs . text [ key ] = sinon . stub ( )
156
163
// throw new Error('Stub already declared')
157
164
}
165
+ debugSetup ( `String stub added: ${ path } ` )
158
166
return this . routeStubs . text [ key ]
159
167
}
160
168
if ( isRegExp ( path ) ) {
@@ -171,12 +179,25 @@ module.exports = class MockAPI {
171
179
if ( ! this . routeStubs . regex [ priority ] ) {
172
180
this . routeStubs . regex [ priority ] = [ ]
173
181
}
182
+ // Replace same regex if found
183
+ let regexKeys = this . routeStubs . regex [ priority ] . map ( x => x . regex . toString ( ) )
184
+ if ( regexKeys . indexOf ( path . toString ( ) ) !== - 1 ) {
185
+ let i = regexKeys . indexOf ( path . toString ( ) )
186
+ this . routeStubs . regex [ priority ] [ i ] = {
187
+ regex : path ,
188
+ stub : newStub
189
+ }
190
+ debugSetup ( `Regex stub replaced: ${ path } ` )
191
+ return newStub
192
+ }
174
193
this . routeStubs . regex [ priority ] . push ( {
175
194
regex : path ,
176
195
stub : newStub
177
196
} )
197
+ debugSetup ( `Regex stub added: ${ path } ` )
178
198
return newStub
179
199
}
200
+ debugSetupError ( 'Only strings and regexs allowed' )
180
201
throw new Error ( 'Only regular expressions and strings allowed' )
181
202
}
182
203
@@ -188,6 +209,7 @@ module.exports = class MockAPI {
188
209
let status = 500
189
210
let body = 'The requested route has not been declared.'
190
211
let contentType = 'text/plain'
212
+ debugResponeError ( 'No Stub Found' )
191
213
response . writeHead ( status , { 'Content-Type' : contentType } )
192
214
response . end ( body )
193
215
}
0 commit comments