2023-07-25 03:20:30 -04:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
2024-09-04 09:30:16 -04:00
|
|
|
const { EventEmitter } = require('events')
|
2024-05-15 05:17:40 -04:00
|
|
|
const http = require('http')
|
|
|
|
const https = require('https')
|
|
|
|
const { promisify } = require('util')
|
2023-07-25 03:20:30 -04:00
|
|
|
|
|
|
|
class TestServer {
|
2024-05-15 05:17:40 -04:00
|
|
|
constructor() {
|
2023-07-25 03:20:30 -04:00
|
|
|
this.app = express()
|
2024-09-04 09:30:16 -04:00
|
|
|
this.events = new EventEmitter()
|
2023-07-25 03:20:30 -04:00
|
|
|
|
|
|
|
this.app.use(bodyParser.json())
|
|
|
|
this.app.use((req, res, next) => {
|
2024-09-30 05:49:47 -04:00
|
|
|
this.events.emit('request-received')
|
2023-07-25 03:20:30 -04:00
|
|
|
this.lastReq = req
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
// Plain text endpoints
|
|
|
|
|
|
|
|
this.app.get('/hello', (req, res) => {
|
|
|
|
res.send('hello')
|
|
|
|
})
|
|
|
|
|
|
|
|
this.largePayload = 'x'.repeat(16 * 1024 * 1024)
|
|
|
|
this.app.get('/large', (req, res) => {
|
|
|
|
res.send(this.largePayload)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.get('/204', (req, res) => {
|
|
|
|
res.status(204).end()
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.get('/empty', (req, res) => {
|
|
|
|
res.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.get('/500', (req, res) => {
|
|
|
|
res.sendStatus(500)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.post('/sink', (req, res) => {
|
|
|
|
req.on('data', () => {})
|
|
|
|
req.on('end', () => {
|
|
|
|
res.status(204).end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// JSON endpoints
|
|
|
|
|
|
|
|
this.app.get('/json/hello', (req, res) => {
|
|
|
|
res.json({ msg: 'hello' })
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.post('/json/add', (req, res) => {
|
|
|
|
const { a, b } = req.body
|
|
|
|
res.json({ sum: a + b })
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.get('/json/500', (req, res) => {
|
|
|
|
res.status(500).json({ error: 'Internal server error' })
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.get('/json/basic-auth', (req, res) => {
|
2023-08-17 09:30:21 -04:00
|
|
|
const expectedAuth =
|
|
|
|
'Basic ' + Buffer.from('user:pass').toString('base64')
|
2023-07-25 03:20:30 -04:00
|
|
|
if (req.headers.authorization === expectedAuth) {
|
|
|
|
res.json({ key: 'verysecret' })
|
|
|
|
} else {
|
|
|
|
res.status(401).json({ error: 'unauthorized' })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.app.post('/json/ignore-request', (req, res) => {
|
|
|
|
res.json({ msg: 'hello' })
|
|
|
|
})
|
|
|
|
|
|
|
|
// Never returns
|
|
|
|
|
2024-09-04 09:30:16 -04:00
|
|
|
this.app.get('/hang', (req, res) => {})
|
2023-07-25 03:20:30 -04:00
|
|
|
this.app.post('/hang', (req, res) => {})
|
2023-11-28 03:12:06 -05:00
|
|
|
|
|
|
|
// Redirect
|
|
|
|
|
|
|
|
this.app.get('/redirect/1', (req, res) => {
|
|
|
|
res.redirect('/redirect/2')
|
|
|
|
})
|
|
|
|
this.app.get('/redirect/2', (req, res) => {
|
|
|
|
res.send('body after redirect')
|
|
|
|
})
|
|
|
|
this.app.get('/redirect/empty-location', (req, res) => {
|
|
|
|
res.sendStatus(302)
|
|
|
|
})
|
2023-07-25 03:20:30 -04:00
|
|
|
}
|
|
|
|
|
2024-05-15 05:17:40 -04:00
|
|
|
start(port, httpsPort, httpsOptions) {
|
|
|
|
const startHttp = new Promise((resolve, reject) => {
|
|
|
|
this.server = http.createServer(this.app).listen(port, err => {
|
2023-07-25 03:20:30 -04:00
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2024-05-15 05:17:40 -04:00
|
|
|
const startHttps = new Promise((resolve, reject) => {
|
|
|
|
this.https_server = https
|
|
|
|
.createServer(httpsOptions, this.app)
|
|
|
|
.listen(httpsPort, err => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return Promise.all([startHttp, startHttps])
|
2023-07-25 03:20:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2024-05-15 05:17:40 -04:00
|
|
|
const stopHttp = promisify(this.server.close).bind(this.server)
|
|
|
|
const stopHttps = promisify(this.https_server.close).bind(this.https_server)
|
2024-09-04 09:30:16 -04:00
|
|
|
this.server.closeAllConnections()
|
|
|
|
this.https_server.closeAllConnections()
|
2024-05-15 05:17:40 -04:00
|
|
|
return Promise.all([stopHttp(), stopHttps()])
|
2023-07-25 03:20:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { TestServer }
|