|
| 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