| 1 | var exec = require('child_process').exec |
| 2 | , fs = require('fs') |
| 3 | , Connect = require('connect') |
| 4 | , dispatch = require('dispatch') |
| 5 | , mime = require('mime') |
| 6 | , DelayedStream = require('delayed-stream') |
| 7 | |
| 8 | , getMime = function(ext) { |
| 9 | return mime.lookup(ext == 'jsonp' ? 'js' : ext) |
| 10 | } |
| 11 | |
| 12 | var routes = { |
| 13 | '/': function (req, res) { |
| 14 | res.write(fs.readFileSync('./tests/tests.html', 'utf8')) |
| 15 | res.end() |
| 16 | }, |
| 17 | '/tests/timeout$': function (req, res) { |
| 18 | var delayed = DelayedStream.create(req) |
| 19 | setTimeout(function() { |
| 20 | res.writeHead(200, { |
| 21 | 'Expires': 0 |
| 22 | , 'Cache-Control': 'max-age=0, no-cache, no-store' |
| 23 | }) |
| 24 | req.query.callback && res.write(req.query.callback + '(') |
| 25 | res.write(JSON.stringify({ method: req.method, query: req.query, headers: req.headers })) |
| 26 | req.query.callback && res.write(');') |
| 27 | delayed.pipe(res) |
| 28 | }, 2000) |
| 29 | }, |
| 30 | '/tests/204': function(req, res) { |
| 31 | res.writeHead(204); |
| 32 | res.end(); |
| 33 | }, |
| 34 | '(([\\w\\-\\/\\.]+)\\.(css|js|json|jsonp|html|xml)$)': function (req, res, next, uri, file, ext) { |
| 35 | res.writeHead(200, { |
| 36 | 'Expires': 0 |
| 37 | , 'Cache-Control': 'max-age=0, no-cache, no-store' |
| 38 | , 'Content-Type': getMime(ext) |
| 39 | }) |
| 40 | if (req.query.echo !== undefined) { |
| 41 | ext == 'jsonp' && res.write((req.query.callback || req.query.testCallback || 'echoCallback') + '(') |
| 42 | res.write(JSON.stringify({ method: req.method, query: req.query, headers: req.headers })) |
| 43 | ext == 'jsonp' && res.write(');') |
| 44 | } else { |
| 45 | res.write(fs.readFileSync('./' + file + '.' + ext)) |
| 46 | } |
| 47 | res.end() |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Connect.createServer(Connect.query(), dispatch(routes)).listen(1234) |
| 52 | |
| 53 | var otherOriginRoutes = { |
| 54 | '/get-value': function (req, res) { |
| 55 | res.writeHead(200, { |
| 56 | 'Access-Control-Allow-Origin': req.headers.origin, |
| 57 | 'Content-Type': 'text/plain' |
| 58 | }) |
| 59 | res.end('hello') |
| 60 | }, |
| 61 | '/set-cookie': function (req, res) { |
| 62 | res.writeHead(200, { |
| 63 | 'Access-Control-Allow-Origin': req.headers.origin, |
| 64 | 'Access-Control-Allow-Credentials': 'true', |
| 65 | 'Content-Type': 'text/plain', |
| 66 | 'Set-Cookie': 'cookie=hello' |
| 67 | }) |
| 68 | res.end('Set a cookie!') |
| 69 | }, |
| 70 | '/get-cookie-value': function (req, res) { |
| 71 | var cookies = {} |
| 72 | , value |
| 73 | |
| 74 | req.headers.cookie && req.headers.cookie.split(';').forEach(function( cookie ) { |
| 75 | var parts = cookie.split('=') |
| 76 | cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim() |
| 77 | }) |
| 78 | value = cookies.cookie |
| 79 | |
| 80 | res.writeHead(200, { |
| 81 | 'Access-Control-Allow-Origin': req.headers.origin, |
| 82 | 'Access-Control-Allow-Credentials': 'true', |
| 83 | 'Content-Type': 'text/plain' |
| 84 | }) |
| 85 | res.end(value) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | Connect.createServer(Connect.query(), dispatch(otherOriginRoutes)).listen(5678) |