Skip to content

Commit 5cec293

Browse files
improve url handling
Signed-off-by: Vikrant Puppala <[email protected]>
1 parent 6d6dd8b commit 5cec293

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/connection/connections/HttpConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export default class HttpConnection implements IConnectionProvider {
9898

9999
this.connection = new ThriftHttpConnection(
100100
{
101-
url: `${options.https ? 'https' : 'http'}://${options.host}:${options.port}${options.path ?? '/'}`,
101+
url: `${options.https ? 'https' : 'http'}://${options.host.replace(/\/$/, '')}:${options.port}${options.path ? ('/' + options.path.replace(/\/$/, '')) : '/'}`,
102102
transport: thrift.TBufferedTransport,
103103
protocol: thrift.TBinaryProtocol,
104104
getRetryPolicy: () => this.getRetryPolicy(),

tests/unit/connection/connections/HttpConnection.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import http from 'http';
22
import { expect } from 'chai';
33
import HttpConnection from '../../../../lib/connection/connections/HttpConnection';
44
import ThriftHttpConnection from '../../../../lib/connection/connections/ThriftHttpConnection';
5-
5+
import IConnectionOptions from '../../../../lib/connection/contracts/IConnectionOptions';
66
import ClientContextStub from '../../.stubs/ClientContextStub';
77

88
describe('HttpConnection.connect', () => {
@@ -127,4 +127,46 @@ describe('HttpConnection.connect', () => {
127127
...extraHeaders,
128128
});
129129
});
130+
131+
it('should handle trailing slashes in host and path correctly', async () => {
132+
interface TestCase {
133+
input: {
134+
host: string;
135+
path?: string;
136+
};
137+
expected: string;
138+
}
139+
140+
const testCases: TestCase[] = [
141+
{
142+
input: { host: 'xyz.com/', path: '/sql/v1/' },
143+
expected: 'https://xyz.com:443/sql/v1'
144+
},
145+
{
146+
input: { host: 'xyz.com', path: 'sql/v1' },
147+
expected: 'https://xyz.com:443/sql/v1'
148+
},
149+
{
150+
input: { host: 'xyz.com/', path: 'sql/v1' },
151+
expected: 'https://xyz.com:443/sql/v1'
152+
},
153+
{
154+
input: { host: 'xyz.com', path: undefined },
155+
expected: 'https://xyz.com:443/'
156+
}
157+
];
158+
159+
for (const testCase of testCases) {
160+
const options: IConnectionOptions = {
161+
host: testCase.input.host,
162+
port: 443,
163+
path: testCase.input.path,
164+
https: true,
165+
};
166+
167+
const connection = new HttpConnection(options, new ClientContextStub());
168+
const thriftConnection = await connection.getThriftConnection();
169+
expect(thriftConnection.url).to.be.equal(testCase.expected);
170+
}
171+
});
130172
});

0 commit comments

Comments
 (0)