Skip to content

Commit

Permalink
Test socket hung up error regression
Browse files Browse the repository at this point in the history
Fixes #1171
  • Loading branch information
szmarczak committed Apr 29, 2020
1 parent d03b3e1 commit b50c548
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/agent.ts
Expand Up @@ -146,3 +146,51 @@ test('socket connect listener cleaned up after request', withServer, async (t, s
// Make sure to close all open sockets
agent.destroy();
});

test('no socket hung up regression', withServer, async (t, server, got) => {
const agent = new HttpAgent({keepAlive: true});
const token = 'helloworld';

server.get('/', (request, response) => {
if (request.headers.token !== token) {
response.statusCode = 401;
response.end();
return;
}

response.end('ok');
})

const {body} = await got({
prefixUrl: 'http://127.0.0.1:3000',
agent: {
http: agent,
},
hooks: {
afterResponse: [
async (response, retryWithMergedOptions) => {
// Force clean-up
response.socket.destroy();

// Unauthorized
if (response.statusCode === 401) {
return retryWithMergedOptions({
headers: {
token
}
});
}

// No changes otherwise
return response;
},
],
},
// Disable automatic retries, manual retries are allowed
retry: 0
});

t.is(body, 'ok');

agent.destroy();
});

0 comments on commit b50c548

Please sign in to comment.