Skip to content

Commit

Permalink
make it work with redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Apr 18, 2024
1 parent a3b6dd8 commit 17e19cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
11 changes: 2 additions & 9 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ export async function parseRequestObject(requestObject: IRequestOptions) {
}

const host = getHostFromRequestObject(requestObject);
const agentOptions: AgentOptions = {};
const agentOptions: AgentOptions = { ...requestObject.agentOptions };
if (host) {
agentOptions.servername = host;
}
Expand All @@ -506,14 +506,7 @@ export async function parseRequestObject(requestObject: IRequestOptions) {
agentOptions.secureOptions = crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT;
}

if (requestObject.agentOptions) {
axiosConfig.httpsAgent = new Agent({
...agentOptions,
...requestObject.agentOptions,
});
} else {
axiosConfig.httpsAgent = new Agent(agentOptions);
}
axiosConfig.httpsAgent = new Agent(agentOptions);

axiosConfig.beforeRedirect = getBeforeRedirectFn(agentOptions, axiosConfig);

Expand Down
45 changes: 29 additions & 16 deletions packages/core/test/NodeExecuteFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SecureContextOptions } from 'tls';
import {
cleanupParameterData,
copyInputItems,
Expand Down Expand Up @@ -387,26 +388,38 @@ describe('NodeExecuteFunctions', () => {
expect((axiosOptions.httpsAgent as Agent).options.servername).toEqual('example.de');
});

test('should set ssl certificates', async () => {
const axiosOptions = await parseRequestObject({
describe('should set SSL certificates', () => {
const agentOptions: SecureContextOptions = {
ca: '-----BEGIN CERTIFICATE-----\nTEST\n-----END CERTIFICATE-----',
};
const requestObject: IRequestOptions = {
method: 'GET',
uri: 'https://example.de',
agentOptions: {
cert: undefined,
ca: '-----BEGIN CERTIFICATE-----\nTEST\n-----END CERTIFICATE-----',
key: undefined,
passphrase: undefined,
},
agentOptions,
};

test('on regular requests', async () => {
const axiosOptions = await parseRequestObject(requestObject);
expect((axiosOptions.httpsAgent as Agent).options).toEqual({
servername: 'example.de',
...agentOptions,
noDelay: true,
path: null,
});
});

expect((axiosOptions.httpsAgent as Agent).options).toEqual({
servername: 'example.de',
cert: undefined,
ca: '-----BEGIN CERTIFICATE-----\nTEST\n-----END CERTIFICATE-----',
key: undefined,
passphrase: undefined,
noDelay: true,
path: null,
test('on redirected requests', async () => {
const axiosOptions = await parseRequestObject(requestObject);
expect(axiosOptions.beforeRedirect).toBeDefined;
const redirectOptions: Record<string, any> = { agents: {}, hostname: 'example.de' };
axiosOptions.beforeRedirect!(redirectOptions, mock());
expect(redirectOptions.agent).toEqual(redirectOptions.agents.https);
expect((redirectOptions.agent as Agent).options).toEqual({
servername: 'example.de',
...agentOptions,
noDelay: true,
path: null,
});
});
});

Expand Down
5 changes: 3 additions & 2 deletions packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SecureContextOptions } from 'tls';
import type {
IDataObject,
INodeExecutionData,
Expand Down Expand Up @@ -202,13 +203,13 @@ export const setAgentOptions = (
sslCertificates: HttpSslAuthCredentials | undefined,
) => {
if (sslCertificates) {
const agentOptions = {
const agentOptions: SecureContextOptions = {
cert: sslCertificates.cert ? formatPrivateKey(sslCertificates.cert) : undefined,
ca: sslCertificates.ca ? formatPrivateKey(sslCertificates.ca) : undefined,
key: sslCertificates.key ? formatPrivateKey(sslCertificates.key) : undefined,
passphrase: sslCertificates.passphrase || undefined,
};

requestOptions.agentOptions = agentOptions;
requestOptions.agentOptions = { ...agentOptions };
}
};

0 comments on commit 17e19cb

Please sign in to comment.