Skip to content

Commit c5c3348

Browse files
Improve url handling
1 parent 6d6dd8b commit c5c3348

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
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(/^\/+|\/+$/g, '')) : '/'}`,
102102
transport: thrift.TBufferedTransport,
103103
protocol: thrift.TBinaryProtocol,
104104
getRetryPolicy: () => this.getRetryPolicy(),

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
"@types/chai": "^4.3.14",
5151
"@types/http-proxy": "^1.17.14",
5252
"@types/lz4": "^0.6.4",
53-
"@types/mocha": "^10.0.6",
54-
"@types/node": "^18.11.9",
53+
"@types/mocha": "^10.0.10",
54+
"@types/node": "^18.19.84",
5555
"@types/node-fetch": "^2.6.4",
5656
"@types/node-int64": "^0.4.29",
5757
"@types/sinon": "^17.0.3",

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

Lines changed: 47 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,50 @@ 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: '//sql/v1//' },
155+
expected: 'https://xyz.com:443/sql/v1'
156+
},
157+
{
158+
input: { host: 'xyz.com', path: undefined },
159+
expected: 'https://xyz.com:443/'
160+
}
161+
];
162+
163+
for (const testCase of testCases) {
164+
const options: IConnectionOptions = {
165+
host: testCase.input.host,
166+
port: 443,
167+
path: testCase.input.path,
168+
https: true,
169+
};
170+
171+
const connection = new HttpConnection(options, new ClientContextStub());
172+
const thriftConnection = await connection.getThriftConnection();
173+
expect(thriftConnection.url).to.be.equal(testCase.expected);
174+
}
175+
});
130176
});

0 commit comments

Comments
 (0)