Skip to content

Commit 6bd8531

Browse files
authored
Add tests. Fix regex edge-case. Add debug statements. (#4)
1 parent 9c1a145 commit 6bd8531

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

index.js

+29-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ const isRegExp = require('101/is-regexp')
99
const keypather = require('keypather')()
1010
const noop = require('101/noop')
1111
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')
1319

1420
const PRIORITY_LIMIT = 100
1521
const PRIORITY_DEFAULT = 10
@@ -46,12 +52,11 @@ module.exports = class MockAPI {
4652
}
4753
this.server.listen(this.port, (err) => {
4854
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}`)
5156
done(err)
5257
return
5358
}
54-
debug(`Server listening (port: ${this.port})`)
59+
debugInit(`Server listening (port: ${this.port})`)
5560
done()
5661
})
5762
}
@@ -65,7 +70,7 @@ module.exports = class MockAPI {
6570
done = noop
6671
}
6772
this.server.close((err) => {
68-
debug(`Server stopped (port: ${this.port})`)
73+
debugInit(`Server stopped (port: ${this.port})`)
6974
done(err)
7075
})
7176
}
@@ -83,7 +88,7 @@ module.exports = class MockAPI {
8388
}
8489
const result = routeStub(request, response)
8590

86-
debug(`Request: ${method} ${path}`)
91+
debugRespone(`Request: ${method} ${path}`)
8792

8893
let status = 200
8994
let body = 'response'
@@ -105,7 +110,7 @@ module.exports = class MockAPI {
105110
}
106111
}
107112

108-
debug(`Response: ${status} ${body} (${contentType})`)
113+
debugRespone(`Response: ${status} ${body} (${contentType})`)
109114

110115
response.writeHead(status, { 'Content-Type': contentType })
111116
response.end(body)
@@ -121,13 +126,15 @@ module.exports = class MockAPI {
121126
const key = `${method} ${path}`
122127
let textStub = keypather.get(this.routeStubs.text, key)
123128
if (textStub) {
129+
debugRespone(`Found string match: ${method} ${path}`)
124130
return textStub
125131
}
126132
// Check all regular expressions
127133
for (var i = PRIORITY_LIMIT; i >= 0; i -= 1) {
128134
if (Array.isArray(this.routeStubs.regex[i])) {
129135
for (let entry of this.routeStubs.regex[i]) {
130136
if (path.match(entry.regex)) {
137+
debugRespone(`Found regular expression: ${method} ${path} ${entry.regex}`)
131138
return entry.stub
132139
}
133140
}
@@ -155,6 +162,7 @@ module.exports = class MockAPI {
155162
this.routeStubs.text[key] = sinon.stub()
156163
// throw new Error('Stub already declared')
157164
}
165+
debugSetup(`String stub added: ${path}`)
158166
return this.routeStubs.text[key]
159167
}
160168
if (isRegExp(path)) {
@@ -171,12 +179,25 @@ module.exports = class MockAPI {
171179
if (!this.routeStubs.regex[priority]) {
172180
this.routeStubs.regex[priority] = []
173181
}
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+
}
174193
this.routeStubs.regex[priority].push({
175194
regex: path,
176195
stub: newStub
177196
})
197+
debugSetup(`Regex stub added: ${path}`)
178198
return newStub
179199
}
200+
debugSetupError('Only strings and regexs allowed')
180201
throw new Error('Only regular expressions and strings allowed')
181202
}
182203

@@ -188,6 +209,7 @@ module.exports = class MockAPI {
188209
let status = 500
189210
let body = 'The requested route has not been declared.'
190211
let contentType = 'text/plain'
212+
debugResponeError('No Stub Found')
191213
response.writeHead(status, { 'Content-Type': contentType })
192214
response.end(body)
193215
}

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
"mock",
2222
"http"
2323
],
24+
"standard": {
25+
"globals": [
26+
"describe",
27+
"it",
28+
"before",
29+
"after",
30+
"beforeEach",
31+
"afterEach"
32+
]
33+
},
2434
"author": "Ryan Sandor Richards <ryan@runnable.com> (http://www.runnable.com/)",
2535
"license": "MIT",
2636
"bugs": {

test/index.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('MockAPI', () => {
4343
afterEach(() => api.restore())
4444

4545
it('should stub basic routes', () => {
46-
api.stub('GET', '/bent');
46+
api.stub('GET', '/bent')
4747
return request.getAsync(`http://localhost:${PORT}/bent`)
4848
.then(() => sinon.assert.calledOnce(api.getStub('GET', '/bent')))
4949
})
@@ -162,6 +162,37 @@ describe('MockAPI', () => {
162162
})
163163
})
164164
})
165+
166+
describe('Overwriting Stub', function () {
167+
it('should overwrite a string stub if redefined', () => {
168+
api.stub('GET', '/bent').returns({
169+
status: 200,
170+
body: 'hello'
171+
})
172+
api.stub('GET', '/bent').returns({
173+
status: 200,
174+
body: 'goodbye'
175+
})
176+
return request.getAsync(`http://localhost:${PORT}/bent`)
177+
.then(res => {
178+
assert.equal(res.body, 'goodbye')
179+
})
180+
})
181+
it('should not overwrite a regex stub if redefined', () => {
182+
api.stub('GET', /bent/).returns({
183+
status: 200,
184+
body: 'hello'
185+
})
186+
api.stub('GET', /bent/).returns({
187+
status: 200,
188+
body: 'goodbye'
189+
})
190+
return request.getAsync(`http://localhost:${PORT}/bent`)
191+
.then(res => {
192+
assert.equal(res.body, 'goodbye')
193+
})
194+
})
195+
})
165196
}) // end 'stub'
166197

167198
describe('restore', () => {

0 commit comments

Comments
 (0)