Skip to content

Commit

Permalink
Improved API Host Config Validation with Trailing Slash Removal
Browse files Browse the repository at this point in the history
### Added
- Print warning if trailing slashes in API host configuration are found, and automatically removes them, closes #92.
### Fixed
- Fixed deprecated ts-jest configuration under globals keyword, "Define `ts-jest` config under `globals` is deprecated.".
  • Loading branch information
Syndesi committed Oct 6, 2024
1 parent 05ad30c commit f47d54d
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Print warning if trailing slashes in API host configuration are found, and automatically removes them, closes #92.
### Fixed
- Fixed deprecated ts-jest configuration under globals keyword, "Define `ts-jest` config under `globals` is deprecated.".

## 0.0.62 - 2024-10-01

Expand Down
Empty file modified docs/type/assets/26e93147f10415a0ed4a.svg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified docs/type/assets/75c9471662e97ee24f29.svg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified docs/type/assets/db90e4df2373980c497d.svg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified docs/type/assets/hierarchy.css
100755 → 100644
Empty file.
Empty file modified docs/type/assets/hierarchy.js
100755 → 100644
Empty file.
Empty file modified docs/type/assets/style.css
100755 → 100644
Empty file.

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ const config: Config = {


globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json'
}
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
Expand Down Expand Up @@ -193,7 +190,11 @@ const config: Config = {
// testRunner: "jest-circus/runner",

// A map from regular expressions to paths to transformers
// transform: undefined,
transform: {
'^.+\\.ts$': ['ts-jest', {
tsconfig: 'tsconfig.test.json'
}]
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
Expand Down
9 changes: 8 additions & 1 deletion src/Service/WebSdkConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Service } from 'typedi';

import { Logger } from './Logger';
import { Token } from '../Type/Definition/index.js';

/**
Expand All @@ -13,7 +14,7 @@ class WebSdkConfiguration {
private collectionCacheMaxEntries: number;
private collectionPageSize: number;

constructor() {
constructor(private logger: Logger) {
this.token = null;
this.apiHost = '';
this.elementCacheMaxEntries = 100;
Expand All @@ -36,6 +37,12 @@ class WebSdkConfiguration {
return this.apiHost;
}
setApiHost(apiHost: string): WebSdkConfiguration {
if (apiHost.endsWith('/')) {
this.logger.warn(
'Removed trailing slash from API host configuration due to internal requirement. Please check if trailing slash can be directly removed.',
);
apiHost = apiHost.replace(/\/+$/, '');
}
this.apiHost = apiHost;
return this;
}
Expand Down
109 changes: 109 additions & 0 deletions test/Unit/Service/WebSdkConfiguration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { expect } from 'chai';
import { SinonSandbox, createSandbox } from 'sinon';
import { Container } from 'typedi';

import { Logger, WebSdkConfiguration } from '../../../src/Service';
import { validateTokenFromString } from '../../../src/Type/Definition';
import { TestLogger } from '../TestLogger';

describe('WebSdkConfiguration tests', () => {
let sandbox: SinonSandbox;
let testLogger: TestLogger;

beforeEach(() => {
sandbox = createSandbox();

testLogger = new TestLogger();
Container.set(Logger, testLogger);
});

afterEach(() => {
sandbox.restore();
Container.reset();
});

it('should return default values', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);

expect(webSdkConfiguration.getToken()).to.be.null;
expect(webSdkConfiguration.getApiHost()).to.be.equal('');
expect(webSdkConfiguration.getElementCacheMaxEntries()).to.be.equal(100);
expect(webSdkConfiguration.getCollectionCacheMaxEntries()).to.be.equal(50);
expect(webSdkConfiguration.getCollectionPageSize()).to.be.equal(25);
});

it('should correctly save the token', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);
const token = validateTokenFromString('secret-token:someToken');

expect(webSdkConfiguration.getToken()).to.be.null;
webSdkConfiguration.setToken(token);
expect(webSdkConfiguration.getToken()).to.be.equal(token);
});

it('should correctly save the api host', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);
const apiHost = 'https://localhost';

expect(webSdkConfiguration.getApiHost()).to.be.equal('');
webSdkConfiguration.setApiHost(apiHost);
expect(webSdkConfiguration.getApiHost()).to.be.equal(apiHost);
});

it('should warn if the api host has a trailing slash', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);
const apiHost = 'https://localhost/';

expect(webSdkConfiguration.getApiHost()).to.be.equal('');
webSdkConfiguration.setApiHost(apiHost);
expect(webSdkConfiguration.getApiHost()).to.not.be.equal(apiHost);
expect(webSdkConfiguration.getApiHost()).to.be.equal('https://localhost');
expect(
testLogger.assertWarnHappened(
'Removed trailing slash from API host configuration due to internal requirement. Please check if trailing slash can be directly removed.',
),
).to.be.true;
});

it('should warn if the api host has multiple trailing slashes', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);
const apiHost = 'https://localhost///';

expect(webSdkConfiguration.getApiHost()).to.be.equal('');
webSdkConfiguration.setApiHost(apiHost);
expect(webSdkConfiguration.getApiHost()).to.not.be.equal(apiHost);
expect(webSdkConfiguration.getApiHost()).to.be.equal('https://localhost');
expect(
testLogger.assertWarnHappened(
'Removed trailing slash from API host configuration due to internal requirement. Please check if trailing slash can be directly removed.',
),
).to.be.true;
});

it('should correctly save the element cache max entries', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);
const elementCacheMaxEntries = 999;

expect(webSdkConfiguration.getElementCacheMaxEntries()).to.be.equal(100);
webSdkConfiguration.setElementCacheMaxEntries(elementCacheMaxEntries);
expect(webSdkConfiguration.getElementCacheMaxEntries()).to.be.equal(elementCacheMaxEntries);
});

it('should correctly save the collection cache max entries', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);
const collectionCacheMaxEntries = 999;

expect(webSdkConfiguration.getCollectionCacheMaxEntries()).to.be.equal(50);
webSdkConfiguration.setCollectionCacheMaxEntries(collectionCacheMaxEntries);
expect(webSdkConfiguration.getCollectionCacheMaxEntries()).to.be.equal(collectionCacheMaxEntries);
});

it('should correctly save the collection page size', async () => {
const webSdkConfiguration = Container.get(WebSdkConfiguration);
const collectionPageSize = 999;

expect(webSdkConfiguration.getCollectionPageSize()).to.be.equal(25);
webSdkConfiguration.setCollectionPageSize(collectionPageSize);
expect(webSdkConfiguration.getCollectionPageSize()).to.be.equal(collectionPageSize);
});
});

0 comments on commit f47d54d

Please sign in to comment.