TestServer.js 2.9 KB

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