From e57e017102765e2d7d656587d00007786f603b0c Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Tue, 8 Nov 2022 19:15:23 +0200 Subject: [PATCH] [REGRESSION] Prepend slash to path if needed (#91) Prepend slash to path if needed Signed-off-by: Levko Kravets Signed-off-by: Levko Kravets --- lib/DBSQLClient.ts | 10 +++++++++- tests/unit/DBSQLClient.test.js | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index c9865dc8..65f064a0 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -21,6 +21,13 @@ import PlainHttpAuthentication from './connection/auth/PlainHttpAuthentication'; import IDBSQLLogger, { LogLevel } from './contracts/IDBSQLLogger'; import DBSQLLogger from './DBSQLLogger'; +function prependSlash(str: string): string { + if (str.length > 0 && str.charAt(0) !== '/') { + return `/${str}`; + } + return str; +} + function getInitialNamespaceOptions(catalogName?: string, schemaName?: string) { if (!catalogName && !schemaName) { return {}; @@ -61,11 +68,12 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient { } private getConnectionOptions(options: ConnectionOptions): IConnectionOptions { - const { host, port, token, clientId, ...otherOptions } = options; + const { host, port, path, token, clientId, ...otherOptions } = options; return { host, port: port || 443, options: { + path: prependSlash(path), https: true, ...otherOptions, }, diff --git a/tests/unit/DBSQLClient.test.js b/tests/unit/DBSQLClient.test.js index 3b7ab30a..6722405e 100644 --- a/tests/unit/DBSQLClient.test.js +++ b/tests/unit/DBSQLClient.test.js @@ -31,6 +31,42 @@ describe('DBSQLClient.connect', () => { token: 'dapi********************************', }; + it('should prepend "/" to path if it is missing', async () => { + const client = new DBSQLClient(); + client.thrift = { + createClient() {}, + }; + const connectionProvider = ConnectionProviderMock(); + + const path = 'example/path'; + + client.connectionProvider = connectionProvider; + await client.connect({ + ...options, + path, + }); + + expect(connectionProvider.options.options.path).to.equal(`/${path}`); + }); + + it('should not prepend "/" to path if it is already available', async () => { + const client = new DBSQLClient(); + client.thrift = { + createClient() {}, + }; + const connectionProvider = ConnectionProviderMock(); + + const path = '/example/path'; + + client.connectionProvider = connectionProvider; + await client.connect({ + ...options, + path, + }); + + expect(connectionProvider.options.options.path).to.equal(path); + }); + it('should set nosasl authenticator by default', async () => { const client = new DBSQLClient(); const connectionProvider = ConnectionProviderMock();