-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnock-sample.coffee
46 lines (40 loc) · 1.36 KB
/
nock-sample.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# let nock intercept the request we made
# this allows us to use the full power of nock to respond the way we want
# see https://github.com/flatiron/nock for documentation
nock = require "nock"
fs = require "fs"
url = require "url"
scopes = {}
for verb in ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
scopes[verb] = nock("http://localhost")
.defaultReplyHeaders(
"Content-Type": "text/plain"
"Access-Control-Allow-Origin": "*"
"Access-Control-Allow-Methods": "POST, GET, PUT, DELETE, OPTIONS"
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
"X-Powered-By": "Nock"
)
.persist()
.filteringPath((path) ->
"/matchall"
)
.intercept("/matchall", verb)
module.exports = (req, res) ->
if req.method is "OPTIONS"
return scopes[req.method].reply 200, "200 OK"
urlPieces = url.parse req.url, true
filePatterns = [
"#{__dirname}#{urlPieces.pathname}/#{req.method}#{urlPieces.search}.json"
"#{__dirname}#{urlPieces.pathname}/#{req.method}.json"
]
for file in filePatterns
console.log "Looking for file #{file}"
if fs.existsSync file
returnFile = file
break
if returnFile?
console.log "Serving file #{returnFile}"
scopes[req.method].replyWithFile 200, returnFile,
"Content-Type": "application/json"
else
scopes[req.method].reply 404, "404 Not Found"