Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some beforeRequest edited options are discarded #1293

Merged
merged 3 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,8 @@ export default class Request extends Duplex implements RequestEvents<Request> {

async _makeRequest(): Promise<void> {
const {options} = this;
const {url, headers, request, agent, timeout} = options;
Giotino marked this conversation as resolved.
Show resolved Hide resolved

const {headers} = options;

for (const key in headers) {
if (is.undefined(headers[key])) {
Expand Down Expand Up @@ -1289,6 +1290,8 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}
}

const {agent, request, timeout, url} = options;

if (options.dnsCache && !('lookup' in options)) {
options.lookup = options.dnsCache.lookup;
}
Expand Down
55 changes: 54 additions & 1 deletion test/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {URL} from 'url';
import test from 'ava';
import {Agent as HttpAgent} from 'http';
import test, {Constructor} from 'ava';
import nock = require('nock');
import getStream from 'get-stream';
import sinon = require('sinon');
import delay = require('delay');
import {Handler} from 'express';
import Responselike = require('responselike');
Expand Down Expand Up @@ -36,6 +38,13 @@ const redirectEndpoint: Handler = (_request, response) => {
response.end();
};

const createAgentSpy = <T extends HttpAgent>(AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => {
const agent: T = new AgentClass({keepAlive: true});
// @ts-ignore This IS correct
const spy = sinon.spy(agent, 'addRequest');
return {agent, spy};
};

test('async hooks', withServer, async (t, server, got) => {
server.get('/', echoHeaders);

Expand Down Expand Up @@ -899,3 +908,47 @@ test('async afterResponse allows to retry with allowGetBody and json payload', w
});
t.is(statusCode, 200);
});

test('beforeRequest hook respect `agent` option', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});

const {agent, spy} = createAgentSpy(HttpAgent);

t.truthy((await got({
hooks: {
beforeRequest: [
options => {
options.agent = {
http: agent
};
}
]
}
})).body);
t.true(spy.calledOnce);

// Make sure to close all open sockets
agent.destroy();
});

test('beforeRequest hook respect `url` option', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ko');
});

server.get('/changed', (_request, response) => {
response.end('ok');
});

t.is((await got(server.sslHostname, {
hooks: {
beforeRequest: [
options => {
options.url = new URL(server.url + '/changed');
}
]
}
})).body, 'ok');
});