Skip to content

Commit 662ed20

Browse files
committed
fix: config tweaks, tests
1 parent 57e8435 commit 662ed20

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function findOrderedMatches(patterns, path) {
5050

5151
function extractPathParams(pattern, path) {
5252
// create a RegExp with named groups from the string contained in '{{}}'
53-
const re = new RegExp(pattern.replace(/\{\{([^}]+)\}\}/g, '(?<$1>[^/]+)'));
53+
const re = new RegExp(pattern.replace(/\{\{([^}]+)\}\}/g, '(?<$1>[^/]+)').replace(/\*/g, '([^/]+)'));
5454
const match = path.match(re);
5555
return match ? match.groups : {};
5656
}

test/config.test.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2024 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import assert from 'node:assert';
14+
import { resolveConfig } from '../src/config.js';
15+
16+
/**
17+
* @param {string} path
18+
* @returns {Context}
19+
*/
20+
const TEST_CONTEXT = (path) => ({
21+
env: {},
22+
log: console,
23+
url: new URL(`https://www.example.com/tenant/content${path}`),
24+
info: {
25+
method: 'GET',
26+
headers: {},
27+
},
28+
});
29+
30+
describe('config tests', () => {
31+
it('should extract path params', () => {
32+
const tenantConfigs = {
33+
'test-tenant': {
34+
base: {
35+
apiKey: 'bad',
36+
},
37+
'/us/p/{{urlkey}}/{{sku}}': {
38+
pageType: 'product',
39+
apiKey: 'good',
40+
},
41+
},
42+
};
43+
const config = resolveConfig(TEST_CONTEXT('/us/p/my-url-key/some-sku'), 'test-tenant', undefined, tenantConfigs);
44+
assert.deepStrictEqual(config, {
45+
apiKey: 'good',
46+
params: { urlkey: 'my-url-key', sku: 'some-sku' },
47+
pageType: 'product',
48+
});
49+
});
50+
51+
it('should allow wildcard path segments', () => {
52+
const tenantConfigs = {
53+
'test-tenant': {
54+
base: {
55+
apiKey: 'bad',
56+
},
57+
'/us/p/*/{{sku}}': {
58+
pageType: 'product',
59+
apiKey: 'good',
60+
},
61+
},
62+
};
63+
const config = resolveConfig(TEST_CONTEXT('/us/p/something-here/some-sku'), 'test-tenant', undefined, tenantConfigs);
64+
assert.deepStrictEqual(config, {
65+
apiKey: 'good',
66+
params: { sku: 'some-sku' },
67+
pageType: 'product',
68+
});
69+
});
70+
71+
it('should allow overrides', () => {
72+
const tenantConfigs = {
73+
'test-tenant': {
74+
base: {
75+
apiKey: 'bad1',
76+
},
77+
'/us/p/{{sku}}': {
78+
pageType: 'product',
79+
apiKey: 'bad2',
80+
},
81+
},
82+
};
83+
const config = resolveConfig(TEST_CONTEXT('/us/p/some-sku'), 'test-tenant', { apiKey: 'good' }, tenantConfigs);
84+
assert.deepStrictEqual(config, {
85+
apiKey: 'good',
86+
params: { sku: 'some-sku' },
87+
pageType: 'product',
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)