Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/__tests__/getMtlsFetch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2025 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import { describe, it, expect, vi, beforeEach } from 'vitest';
import getMtlsFetch from '../getMtlsFetch.js';
import { MTLS_BINDING } from '../constants.js';

describe('getMtlsFetch', () => {
let mockEnv;
let mockFetch;

beforeEach(() => {
mockFetch = vi.fn();
mockEnv = {};
});

it('should return the MTLS fetch function when the binding exists', () => {
// Arrange
mockEnv[MTLS_BINDING] = { fetch: mockFetch };

// Act
const result = getMtlsFetch(mockEnv);

// Assert
expect(result).toBe(mockFetch);
});

it('should return a function that throws an error when the binding does not exist', () => {
// Arrange
mockEnv = {}; // Empty env without MTLS binding

// Act
const result = getMtlsFetch(mockEnv);

// Assert
expect(() => result()).toThrowError(
'MTLS certificate not found on the worker'
);
});

it('should handle undefined env gracefully', () => {
// Act
const result = getMtlsFetch(undefined);

// Assert
expect(result).toBeInstanceOf(Function);
expect(() => result()).toThrowError(
'MTLS certificate not found on the worker'
);
});
});
15 changes: 9 additions & 6 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ describe('index', () => {
request: { header: {}, body: { xdm: {}, data: {} } }
};

const execute = index.initialize(containerInitFunction, {
fetch: globalFetch
});
const execute = index.initialize(containerInitFunction);

return execute(callData, {
headersForSubrequests: {}
}).then((result) => {
return execute(
callData,
{},
{
headersForSubrequests: {},
fetch: globalFetch
}
).then((result) => {
expect(result).toStrictEqual([
{
ruleId: 'RLbb1d94c79fee4733a510564a86ba3c59',
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ governing permissions and limitations under the License.

export const CORE = 'core';
export const PROMISE_TIMEOUT = 30000;
export const MTLS_BINDING = 'ADOBE_MTLS_CERTIFICATE';
14 changes: 11 additions & 3 deletions src/executeRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import logRuleStarting from './rules/logRuleStarting.js';
import logRuleEnding from './rules/logRuleEnding.js';
import returnRuleResult from './rules/returnRuleResult.js';
import createPromiseChain from './rules/createPromiseChain.js';
import getMtlsFetch from './getMtlsFetch.js';

export default (
moduleProvider,
container,
globalFetch,
requestData,
env,
{ headersForSubrequests } = {}
{ headersForSubrequests, fetch: globalFetch } = {}
) => {
const rulePromises = [];

Expand Down Expand Up @@ -53,11 +53,19 @@ export default (
logger
);

const mtlsFetch = getRuleFetchFn(
getMtlsFetch(env),
getHeaderOverrides(env),
headersForSubrequests,
logger
);

const utils = {
getRule: () => ({ id, name }),
getBuildInfo: () => buildInfo,
logger,
fetch
fetch,
mtlsFetch
};

const initialContext = {
Expand Down
22 changes: 22 additions & 0 deletions src/getMtlsFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2025 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import { MTLS_BINDING } from './constants.js';

export default (env) => {
if (env && env[MTLS_BINDING]) {
return env[MTLS_BINDING].fetch;
}

return () => {
throw new Error('MTLS certificate not found on the worker');
};
};
9 changes: 2 additions & 7 deletions src/getRuleFetchFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ governing permissions and limitations under the License.
const byteArrayToString = (buf) =>
new TextDecoder('utf-8').decode(new Uint8Array(buf));

export default (
globalFetch,
headerOverrides,
headersForSubrequests,
logger
) => {
export default (fetch, headerOverrides, headersForSubrequests, logger) => {
return (resource, init = {}) => {
// If resource is not a string then it must be a Request object and we
// need to read it's headers. Otherwise the Request headers will be
Expand Down Expand Up @@ -50,7 +45,7 @@ export default (
}
});

return globalFetch(resource, init).then(
return fetch(resource, init).then(
(r) => {
// Below we will read the body of the response. The body can be read only once.
// We are cloning the response and sending it down to the actions so in case
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const getDataElementValue = createGetDataElementValue(

const getDataElementValues = createGetDataElementValues(getDataElementValue);

const initialize = (containerInitFunction, { fetch }) => {
const initialize = (containerInitFunction) => {
const container = containerInitFunction(getDataElementValues);
if (container.dataElements) {
dataElements = container.dataElements;
}

moduleProvider.registerModules(container.modules, container.extensions);
return executeRules.bind(null, moduleProvider, container, fetch);
return executeRules.bind(null, moduleProvider, container);
};

export default {
Expand Down