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