|
| 1 | +/* global describe, it */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const http = require('http') |
| 5 | +const { expect } = require('chai') |
| 6 | +let gateway, service, close, proxy |
| 7 | +let clientTerminated = false |
| 8 | + |
| 9 | +describe('Client connection terminated', () => { |
| 10 | + it('init', async () => { |
| 11 | + const fastProxy = require('../index')({ |
| 12 | + base: 'http://127.0.0.1:3000' |
| 13 | + }) |
| 14 | + |
| 15 | + proxy = fastProxy.proxy |
| 16 | + close = fastProxy.close |
| 17 | + }) |
| 18 | + |
| 19 | + it('init & start gateway', async () => { |
| 20 | + // init gateway |
| 21 | + gateway = require('restana')() |
| 22 | + |
| 23 | + gateway.all('/service/*', function (req, res) { |
| 24 | + proxy(req, res, req.url, { |
| 25 | + onClientConnectionTerminated (res, _err, response) { |
| 26 | + clientTerminated = true |
| 27 | + } |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + await gateway.start(8080) |
| 32 | + }) |
| 33 | + |
| 34 | + it('init & start remote service', async () => { |
| 35 | + // init remote service |
| 36 | + service = require('restana')() |
| 37 | + |
| 38 | + service.get('/service/long', (req, res) => { |
| 39 | + setTimeout(() => res.end(), 500) |
| 40 | + }) |
| 41 | + |
| 42 | + await service.start(3000) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should invoke onClientConnectionTerminated callback', async () => { |
| 46 | + const options = { |
| 47 | + host: 'localhost', |
| 48 | + path: '/service/long', |
| 49 | + port: 8080, |
| 50 | + method: 'GET', |
| 51 | + headers: { |
| 52 | + 'Content-Length': 0 |
| 53 | + } |
| 54 | + } |
| 55 | + const req = http.request(options) |
| 56 | + req.on('error', () => {}) |
| 57 | + req.end() |
| 58 | + |
| 59 | + await sleep(100) |
| 60 | + req.destroy() |
| 61 | + |
| 62 | + await sleep(1000) |
| 63 | + expect(clientTerminated).to.equal(true) |
| 64 | + }) |
| 65 | + |
| 66 | + it('close all', async () => { |
| 67 | + close() |
| 68 | + await gateway.close() |
| 69 | + await service.close() |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +function sleep (ms) { |
| 74 | + return new Promise(resolve => setTimeout(resolve, ms)) |
| 75 | +} |
0 commit comments