You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.3 KiB

4 years ago
  1. var cookieParser = require('cookie-parser')
  2. var basicAuth = require('basic-auth')
  3. var express = require('express')
  4. var fs = require('fs')
  5. var http = require('http')
  6. var path = require('path')
  7. var url = require('url')
  8. var app = express()
  9. var server = http.createServer(app)
  10. // Otherwise, use 'application/octet-stream'
  11. var copiesMimeTypes = {
  12. '/basic.txt': 'text/plain'
  13. }
  14. var maxDelay = 5000 // ms
  15. // This should make sure bodies aren't cached
  16. // so the streaming tests always pass
  17. app.use(function (req, res, next) {
  18. res.setHeader('Cache-Control', 'no-store')
  19. next()
  20. })
  21. app.get('/testHeaders', function (req, res) {
  22. var parsed = url.parse(req.url, true)
  23. // Values in query parameters are sent as response headers
  24. Object.keys(parsed.query).forEach(function (key) {
  25. res.setHeader('Test-' + key, parsed.query[key])
  26. })
  27. res.setHeader('Content-Type', 'application/json')
  28. res.setHeader('Cache-Control', 'no-cache')
  29. // Request headers are sent in the body as json
  30. var reqHeaders = {}
  31. Object.keys(req.headers).forEach(function (key) {
  32. key = key.toLowerCase()
  33. if (key.indexOf('test-') === 0) {
  34. // different browsers format request headers with multiple values
  35. // slightly differently, so normalize
  36. reqHeaders[key] = req.headers[key].replace(', ', ',')
  37. }
  38. })
  39. var body = JSON.stringify(reqHeaders)
  40. res.setHeader('Content-Length', body.length)
  41. res.write(body)
  42. res.end()
  43. })
  44. app.get('/cookie', cookieParser(), function (req, res) {
  45. res.setHeader('Content-Type', 'text/plain')
  46. res.write('hello=' + req.cookies.hello)
  47. res.end()
  48. })
  49. app.get('/auth', function (req, res) {
  50. var user = basicAuth(req)
  51. if (!user || user.name !== 'TestUser' || user.pass !== 'trustno1') {
  52. res.setHeader('WWW-Authenticate', 'Basic realm="example"')
  53. res.end('Access denied')
  54. } else {
  55. res.setHeader('Content-Type', 'text/plain')
  56. res.write('You\'re in!')
  57. res.end()
  58. }
  59. })
  60. app.post('/echo', function (req, res) {
  61. res.setHeader('Content-Type', 'application/octet-stream')
  62. req.pipe(res)
  63. })
  64. app.use('/verifyEmpty', function (req, res) {
  65. var empty = true
  66. req.on('data', function (buf) {
  67. if (buf.length > 0) {
  68. empty = false
  69. }
  70. })
  71. req.on('end', function () {
  72. res.setHeader('Content-Type', 'text/plain')
  73. if (empty) {
  74. res.end('empty')
  75. } else {
  76. res.end('not empty')
  77. }
  78. })
  79. })
  80. app.use(function (req, res, next) {
  81. var parsed = url.parse(req.url, true)
  82. if ('copies' in parsed.query) {
  83. var totalCopies = parseInt(parsed.query.copies, 10)
  84. function fail () {
  85. res.statusCode = 500
  86. res.end()
  87. }
  88. fs.readFile(path.join(__dirname, 'static', parsed.pathname), function (err, data) {
  89. if (err)
  90. return fail()
  91. var mimeType = copiesMimeTypes[parsed.pathname] || 'application/octet-stream'
  92. res.setHeader('Content-Type', mimeType)
  93. res.setHeader('Content-Length', data.length * totalCopies)
  94. var pieceDelay = maxDelay / totalCopies
  95. if (pieceDelay > 100)
  96. pieceDelay = 100
  97. function write (copies) {
  98. if (copies === 0)
  99. return res.end()
  100. res.write(data, function (err) {
  101. if (err)
  102. return fail()
  103. setTimeout(write.bind(null, copies - 1), pieceDelay)
  104. })
  105. }
  106. write(totalCopies)
  107. })
  108. return
  109. }
  110. next()
  111. })
  112. app.use(express.static(path.join(__dirname, 'static')))
  113. var port = parseInt(process.env.AIRTAP_PORT) || 8199
  114. console.log('Test server listening on port', port)
  115. server.listen(port)