1 | var fs = require('fs') |
---|
2 | , http = require('http') |
---|
3 | , path = require('path') |
---|
4 | , https = require('https') |
---|
5 | , events = require('events') |
---|
6 | , stream = require('stream') |
---|
7 | , assert = require('assert') |
---|
8 | ; |
---|
9 | |
---|
10 | exports.createServer = function (port) { |
---|
11 | port = port || 6767 |
---|
12 | var s = http.createServer(function (req, resp) { |
---|
13 | s.emit(req.url, req, resp); |
---|
14 | }) |
---|
15 | s.port = port |
---|
16 | s.url = 'http://localhost:'+port |
---|
17 | return s; |
---|
18 | } |
---|
19 | |
---|
20 | exports.createSSLServer = function(port, opts) { |
---|
21 | port = port || 16767 |
---|
22 | |
---|
23 | var options = { 'key' : path.join(__dirname, 'ssl', 'test.key') |
---|
24 | , 'cert': path.join(__dirname, 'ssl', 'test.crt') |
---|
25 | } |
---|
26 | if (opts) { |
---|
27 | for (var i in opts) options[i] = opts[i] |
---|
28 | } |
---|
29 | |
---|
30 | for (var i in options) { |
---|
31 | options[i] = fs.readFileSync(options[i]) |
---|
32 | } |
---|
33 | |
---|
34 | var s = https.createServer(options, function (req, resp) { |
---|
35 | s.emit(req.url, req, resp); |
---|
36 | }) |
---|
37 | s.port = port |
---|
38 | s.url = 'https://localhost:'+port |
---|
39 | return s; |
---|
40 | } |
---|
41 | |
---|
42 | exports.createPostStream = function (text) { |
---|
43 | var postStream = new stream.Stream(); |
---|
44 | postStream.writeable = true; |
---|
45 | postStream.readable = true; |
---|
46 | setTimeout(function () {postStream.emit('data', new Buffer(text)); postStream.emit('end')}, 0); |
---|
47 | return postStream; |
---|
48 | } |
---|
49 | exports.createPostValidator = function (text, reqContentType) { |
---|
50 | var l = function (req, resp) { |
---|
51 | var r = ''; |
---|
52 | req.on('data', function (chunk) {r += chunk}) |
---|
53 | req.on('end', function () { |
---|
54 | if (req.headers['content-type'] && req.headers['content-type'].indexOf('boundary=') >= 0) { |
---|
55 | var boundary = req.headers['content-type'].split('boundary=')[1]; |
---|
56 | text = text.replace(/__BOUNDARY__/g, boundary); |
---|
57 | } |
---|
58 | if (r !== text) console.log(r, text); |
---|
59 | assert.equal(r, text) |
---|
60 | if (reqContentType) { |
---|
61 | assert.ok(req.headers['content-type']) |
---|
62 | assert.ok(~req.headers['content-type'].indexOf(reqContentType)) |
---|
63 | } |
---|
64 | resp.writeHead(200, {'content-type':'text/plain'}) |
---|
65 | resp.write('OK') |
---|
66 | resp.end() |
---|
67 | }) |
---|
68 | } |
---|
69 | return l; |
---|
70 | } |
---|
71 | exports.createGetResponse = function (text, contentType) { |
---|
72 | var l = function (req, resp) { |
---|
73 | contentType = contentType || 'text/plain' |
---|
74 | resp.writeHead(200, {'content-type':contentType}) |
---|
75 | resp.write(text) |
---|
76 | resp.end() |
---|
77 | } |
---|
78 | return l; |
---|
79 | } |
---|
80 | exports.createChunkResponse = function (chunks, contentType) { |
---|
81 | var l = function (req, resp) { |
---|
82 | contentType = contentType || 'text/plain' |
---|
83 | resp.writeHead(200, {'content-type':contentType}) |
---|
84 | chunks.forEach(function (chunk) { |
---|
85 | resp.write(chunk) |
---|
86 | }) |
---|
87 | resp.end() |
---|
88 | } |
---|
89 | return l; |
---|
90 | } |
---|