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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ 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, request, timeout} = options;

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

const {agent, url} = options;

if (options.dnsCache && !('lookup' in options)) {
options.lookup = options.dnsCache.lookup;
}
Expand Down
56 changes: 55 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,48 @@ 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');
});