Skip to content

Commit 056ed0b

Browse files
Improve url handling (#290)
* improve url handling --------- Signed-off-by: Vikrant Puppala <[email protected]>
1 parent b754b77 commit 056ed0b

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

Diff for: lib/connection/connections/HttpConnection.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ 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}${
102+
options.path ? (options.path.startsWith('/') ? '' : '/') + options.path.replace(/\/$/, '') : '/'
103+
}`,
102104
transport: thrift.TBufferedTransport,
103105
protocol: thrift.TBinaryProtocol,
104106
getRetryPolicy: () => this.getRetryPolicy(),

Diff for: tests/unit/connection/connections/HttpConnection.test.ts

+51-1
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,54 @@ describe('HttpConnection.connect', () => {
127127
...extraHeaders,
128128
});
129129
});
130+
131+
it('should handle trailing slashes in host 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: undefined },
151+
expected: 'https://xyz.com:443/',
152+
},
153+
{
154+
input: { host: 'xyz.com', path: 'sql/v1' },
155+
expected: 'https://xyz.com:443/sql/v1',
156+
},
157+
{
158+
input: { host: 'xyz.com/', path: 'sql/v1' },
159+
expected: 'https://xyz.com:443/sql/v1',
160+
},
161+
{
162+
input: { host: 'xyz.com', path: 'sql/v1/' },
163+
expected: 'https://xyz.com:443/sql/v1',
164+
},
165+
];
166+
167+
for (const testCase of testCases) {
168+
const options: IConnectionOptions = {
169+
host: testCase.input.host,
170+
port: 443,
171+
path: testCase.input.path,
172+
https: true,
173+
};
174+
175+
const connection = new HttpConnection(options, new ClientContextStub());
176+
const thriftConnection = await connection.getThriftConnection();
177+
expect(thriftConnection.url).to.be.equal(testCase.expected);
178+
}
179+
});
130180
});

0 commit comments

Comments
 (0)