Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit b5927ce

Browse files
authored
issue: #33 - Should support undici timeout err.code (#34)
1 parent 5f0e9b9 commit b5927ce

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ module.exports = (opts) => {
8888
if (err.code === 'ECONNREFUSED' || err.code === 'ERR_HTTP2_STREAM_CANCEL') {
8989
res.statusCode = 503
9090
res.end('Service Unavailable')
91-
} else if (err.code === 'ECONNRESET') {
91+
} else if (err.code === 'ECONNRESET' || err.code === 'UND_ERR_REQUEST_TIMEOUT') {
9292
res.statusCode = 504
9393
res.end(err.message)
9494
} else {

test/5.undici.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
/* global describe, it */
22
'use strict'
3-
43
const request = require('supertest')
54
const bodyParser = require('body-parser')
5+
const { promisify } = require('util')
66
const { expect } = require('chai')
7+
8+
const sleep = promisify(setTimeout)
9+
710
let gateway, service, close, proxy, gHttpServer
811

912
describe('undici', () => {
1013
it('init', async () => {
1114
const fastProxy = require('../index')({
1215
base: 'http://127.0.0.1:3000',
1316
undici: {
14-
pipelining: 10
17+
pipelining: 10,
18+
requestTimeout: 100
1519
}
1620
})
1721
close = fastProxy.close
@@ -45,6 +49,10 @@ describe('undici', () => {
4549
res.setHeader('x-agent', 'fast-proxy')
4650
res.send()
4751
})
52+
service.get('/service/timeout', async (req, res) => {
53+
await sleep(200)
54+
res.send()
55+
})
4856

4957
service.start(3000).then(() => done())
5058
})
@@ -75,6 +83,12 @@ describe('undici', () => {
7583
})
7684
})
7785

86+
it('should 200 on GET /service/timeout', async () => {
87+
await request(gHttpServer)
88+
.get('/service/timeout')
89+
.expect(504)
90+
})
91+
7892
it('close all', async () => {
7993
close()
8094
await gateway.close()

0 commit comments

Comments
 (0)