TestServer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const { EventEmitter } = require('events')
  4. const http = require('http')
  5. const https = require('https')
  6. const { promisify } = require('util')
  7. class TestServer {
  8. constructor() {
  9. this.app = express()
  10. this.events = new EventEmitter()
  11. this.app.use(bodyParser.json())
  12. this.app.use((req, res, next) => {
  13. this.events.emit('request-received')
  14. this.lastReq = req
  15. next()
  16. })
  17. // Plain text endpoints
  18. this.app.get('/hello', (req, res) => {
  19. res.send('hello')
  20. })
  21. this.largePayload = 'x'.repeat(16 * 1024 * 1024)
  22. this.app.get('/large', (req, res) => {
  23. res.send(this.largePayload)
  24. })
  25. this.app.get('/204', (req, res) => {
  26. res.status(204).end()
  27. })
  28. this.app.get('/empty', (req, res) => {
  29. res.end()
  30. })
  31. this.app.get('/500', (req, res) => {
  32. res.sendStatus(500)
  33. })
  34. this.app.post('/sink', (req, res) => {
  35. req.on('data', () => {})
  36. req.on('end', () => {
  37. res.status(204).end()
  38. })
  39. })
  40. // JSON endpoints
  41. this.app.get('/json/hello', (req, res) => {
  42. res.json({ msg: 'hello' })
  43. })
  44. this.app.post('/json/add', (req, res) => {
  45. const { a, b } = req.body
  46. res.json({ sum: a + b })
  47. })
  48. this.app.get('/json/500', (req, res) => {
  49. res.status(500).json({ error: 'Internal server error' })
  50. })
  51. this.app.get('/json/basic-auth', (req, res) => {
  52. const expectedAuth =
  53. 'Basic ' + Buffer.from('user:pass').toString('base64')
  54. if (req.headers.authorization === expectedAuth) {
  55. res.json({ key: 'verysecret' })
  56. } else {
  57. res.status(401).json({ error: 'unauthorized' })
  58. }
  59. })
  60. this.app.post('/json/ignore-request', (req, res) => {
  61. res.json({ msg: 'hello' })
  62. })
  63. // Never returns
  64. this.app.get('/hang', (req, res) => {})
  65. this.app.post('/hang', (req, res) => {})
  66. // Redirect
  67. this.app.get('/redirect/1', (req, res) => {
  68. res.redirect('/redirect/2')
  69. })
  70. this.app.get('/redirect/2', (req, res) => {
  71. res.send('body after redirect')
  72. })
  73. this.app.get('/redirect/empty-location', (req, res) => {
  74. res.sendStatus(302)
  75. })
  76. }
  77. start(port, httpsPort, httpsOptions) {
  78. const startHttp = new Promise((resolve, reject) => {
  79. this.server = http.createServer(this.app).listen(port, err => {
  80. if (err) {
  81. reject(err)
  82. } else {
  83. resolve()
  84. }
  85. })
  86. })
  87. const startHttps = new Promise((resolve, reject) => {
  88. this.https_server = https
  89. .createServer(httpsOptions, this.app)
  90. .listen(httpsPort, err => {
  91. if (err) {
  92. reject(err)
  93. } else {
  94. resolve()
  95. }
  96. })
  97. })
  98. return Promise.all([startHttp, startHttps])
  99. }
  100. stop() {
  101. const stopHttp = promisify(this.server.close).bind(this.server)
  102. const stopHttps = promisify(this.https_server.close).bind(this.https_server)
  103. this.server.closeAllConnections()
  104. this.https_server.closeAllConnections()
  105. return Promise.all([stopHttp(), stopHttps()])
  106. }
  107. }
  108. module.exports = { TestServer }