/
server.js
144 lines (137 loc) Β· 3.78 KB
/
server.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict'
var fs = require('fs')
, http = require('http')
, path = require('path')
, https = require('https')
, stream = require('stream')
, assert = require('assert')
exports.createServer = function () {
var s = http.createServer(function (req, resp) {
s.emit(req.url.replace(/(\?.*)/, ''), req, resp)
})
s.on('listening', function () {
s.port = this.address().port
s.url = 'http://localhost:' + s.port
})
s.port = 0
s.protocol = 'http'
return s
}
exports.createEchoServer = function () {
var s = http.createServer(function (req, resp) {
var b = ''
req.on('data', function (chunk) {b += chunk})
req.on('end', function () {
resp.writeHead(200, {'content-type':'application/json'})
resp.write(JSON.stringify({
url: req.url,
method: req.method,
headers: req.headers,
body: b
}))
resp.end()
})
})
s.on('listening', function () {
s.port = this.address().port
s.url = 'http://localhost:' + s.port
})
s.port = 0
s.protocol = 'http'
return s
}
exports.createSSLServer = function(opts) {
var i
, options = { 'key' : path.join(__dirname, 'ssl', 'test.key')
, 'cert': path.join(__dirname, 'ssl', 'test.crt')
}
if (opts) {
for (i in opts) {
options[i] = opts[i]
}
}
for (i in options) {
if (i !== 'requestCert' && i !== 'rejectUnauthorized' && i !== 'ciphers') {
options[i] = fs.readFileSync(options[i])
}
}
var s = https.createServer(options, function (req, resp) {
s.emit(req.url, req, resp)
})
s.on('listening', function () {
s.port = this.address().port
s.url = 'https://localhost:' + s.port
})
s.port = 0
s.protocol = 'https'
return s
}
exports.createPostStream = function (text) {
var postStream = new stream.Stream()
postStream.writeable = true
postStream.readable = true
setTimeout(function() {
postStream.emit('data', new Buffer(text))
postStream.emit('end')
}, 0)
return postStream
}
exports.createPostValidator = function (text, reqContentType) {
var l = function (req, resp) {
var r = ''
req.on('data', function (chunk) {r += chunk})
req.on('end', function () {
if (req.headers['content-type'] && req.headers['content-type'].indexOf('boundary=') >= 0) {
var boundary = req.headers['content-type'].split('boundary=')[1]
text = text.replace(/__BOUNDARY__/g, boundary)
}
assert.equal(r, text)
if (reqContentType) {
assert.ok(req.headers['content-type'])
assert.ok(~req.headers['content-type'].indexOf(reqContentType))
}
resp.writeHead(200, {'content-type':'text/plain'})
resp.write(r)
resp.end()
})
}
return l
}
exports.createPostJSONValidator = function (value, reqContentType) {
var l = function (req, resp) {
var r = ''
req.on('data', function (chunk) {r += chunk})
req.on('end', function () {
var parsedValue = JSON.parse(r)
assert.deepEqual(parsedValue, value)
if (reqContentType) {
assert.ok(req.headers['content-type'])
assert.ok(~req.headers['content-type'].indexOf(reqContentType))
}
resp.writeHead(200, {'content-type':'application/json'})
resp.write(r)
resp.end()
})
}
return l
}
exports.createGetResponse = function (text, contentType) {
var l = function (req, resp) {
contentType = contentType || 'text/plain'
resp.writeHead(200, {'content-type':contentType})
resp.write(text)
resp.end()
}
return l
}
exports.createChunkResponse = function (chunks, contentType) {
var l = function (req, resp) {
contentType = contentType || 'text/plain'
resp.writeHead(200, {'content-type':contentType})
chunks.forEach(function (chunk) {
resp.write(chunk)
})
resp.end()
}
return l
}