Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve url handling #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 14, 2025
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
4 changes: 3 additions & 1 deletion lib/connection/connections/HttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export default class HttpConnection implements IConnectionProvider {

this.connection = new ThriftHttpConnection(
{
url: `${options.https ? 'https' : 'http'}://${options.host}:${options.port}${options.path ?? '/'}`,
url: `${options.https ? 'https' : 'http'}://${options.host.replace(/\/$/, '')}:${options.port}${
options.path ? (options.path.startsWith('/') ? '' : '/') + options.path.replace(/\/$/, '') : '/'
Copy link
Collaborator

@jackyhu-db jackyhu-db Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this (options.path.startsWith('/') ? '' : '/') is redundant

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my thought was that sql warehouses have paths starting with / like /sql/1.0/warehouses/xxxxxx whereas all purpose clusters have paths without a starting / like sql/protocolv1/o/xxxxx/yyyyyy, why do you say this is redundant?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I meant is you did these twice options.path ? (options.path.startsWith('/') ? '' : '/') + options.path.replace(/\/$/, '') : '/', nvm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(options.path.startsWith('/') ? '' : '/') this would ensure that it has a / at the start and options.path.replace(/\/$/, '') : '/' would ensure that it has a / at the end

}`,
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol,
getRetryPolicy: () => this.getRetryPolicy(),
Expand Down
52 changes: 51 additions & 1 deletion tests/unit/connection/connections/HttpConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import http from 'http';
import { expect } from 'chai';
import HttpConnection from '../../../../lib/connection/connections/HttpConnection';
import ThriftHttpConnection from '../../../../lib/connection/connections/ThriftHttpConnection';

import IConnectionOptions from '../../../../lib/connection/contracts/IConnectionOptions';
import ClientContextStub from '../../.stubs/ClientContextStub';

describe('HttpConnection.connect', () => {
Expand Down Expand Up @@ -127,4 +127,54 @@ describe('HttpConnection.connect', () => {
...extraHeaders,
});
});

it('should handle trailing slashes in host correctly', async () => {
interface TestCase {
input: {
host: string;
path?: string;
};
expected: string;
}

const testCases: TestCase[] = [
{
input: { host: 'xyz.com/', path: '/sql/v1' },
expected: 'https://xyz.com:443/sql/v1',
},
{
input: { host: 'xyz.com', path: '/sql/v1' },
expected: 'https://xyz.com:443/sql/v1',
},
{
input: { host: 'xyz.com/', path: undefined },
expected: 'https://xyz.com:443/',
},
{
input: { host: 'xyz.com', path: 'sql/v1' },
expected: 'https://xyz.com:443/sql/v1',
},
{
input: { host: 'xyz.com/', path: 'sql/v1' },
expected: 'https://xyz.com:443/sql/v1',
},
{
input: { host: 'xyz.com', path: 'sql/v1/' },
expected: 'https://xyz.com:443/sql/v1',
},
];

for (const testCase of testCases) {
const options: IConnectionOptions = {
host: testCase.input.host,
port: 443,
path: testCase.input.path,
https: true,
};

const connection = new HttpConnection(options, new ClientContextStub());
const thriftConnection = await connection.getThriftConnection();
expect(thriftConnection.url).to.be.equal(testCase.expected);
}
});
});
Loading