2 , http = require('http')
3 , path = require('path')
4 , https = require('https')
5 , events = require('events')
6 , stream = require('stream')
7 , assert = require('assert')
10 exports.createServer = function (port) {
12 var s = http.createServer(function (req, resp) {
13 s.emit(req.url, req, resp);
16 s.url = 'http://localhost:'+port
20 exports.createSSLServer = function(port, opts) {
23 var options = { 'key' : path.join(__dirname, 'ssl', 'test.key')
24 , 'cert': path.join(__dirname, 'ssl', 'test.crt')
27 for (var i in opts) options[i] = opts[i]
30 for (var i in options) {
31 options[i] = fs.readFileSync(options[i])
34 var s = https.createServer(options, function (req, resp) {
35 s.emit(req.url, req, resp);
38 s.url = 'https://localhost:'+port
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);
49 exports.createPostValidator = function (text, reqContentType) {
50 var l = function (req, resp) {
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);
58 if (r !== text) console.log(r, text);
61 assert.ok(req.headers['content-type'])
62 assert.ok(~req.headers['content-type'].indexOf(reqContentType))
64 resp.writeHead(200, {'content-type':'text/plain'})
71 exports.createGetResponse = function (text, contentType) {
72 var l = function (req, resp) {
73 contentType = contentType || 'text/plain'
74 resp.writeHead(200, {'content-type':contentType})
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) {