sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

Got doesn't respect response timeout #1493

Closed santanu33 closed 4 years ago

santanu33 commented 4 years ago

Describe the bug

Actual behavior

Setting up timeout with retry:0 doesn't get the terminate the long pending request.

Expected behavior

as per documentation.

timeout
Type: number | object

Milliseconds to wait for the server to end the response before aborting the request
with got.TimeoutError error (a.k.a. request property). By default, there's no timeout.

Code to reproduce

import nock from "nock"
import { Got } from "got"
import got from "got";
import needle = require("needle")

describe("HTTPClient", () => {
    test.only("should timeout", async () => {

        nock('http://foo')
            .get('/bar')
            .delay(200)
            .delayBody(200)
            .delayConnection(5000) // 2 seconds
            .reply(200, '<html></html>');

        let  instance: Got = got;
        instance.extend({
            retry: 0,
            timeout: 100
        });

        // no error here 😕, test completes after long delay
        try {
            let  response = await instance.get(`http://foo/bar`)
            console.log(response.body.toString());
        } catch (err) {
            console.log(err)
        }

        // needle works fine, with Needle request timed out. 👍🏼

        // try{
        //     const res = await needle("get",'http://foo/bar',{response_timeout: 100});
        //     console.log(res.body.toString())
        // } catch (err) {
        //     console.log(err)
        // }

    })
});

Checklist

szmarczak commented 4 years ago

You set instance to got and the extended instance is just hanging there.

https://github.com/sindresorhus/got#instances

        let  instance: Got = got.extend({
            retry: 0,
            timeout: 100
        });