Skip to content

Commit

Permalink
fix parsing to match previous tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-willis committed Jan 16, 2025
1 parent b326878 commit a3c8e2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('parseGrpcUrl', () => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: expected,
enableTls: false,
path: '',
});
});

Expand All @@ -22,6 +23,7 @@ describe('parseGrpcUrl', () => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: expected,
enableTls: true,
path: '',
});
});

Expand All @@ -33,13 +35,15 @@ describe('parseGrpcUrl', () => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: expected,
enableTls: false,
path: '',
});
});

it.each([null, undefined, ''])('can handle falsey urls', input => {
expect(parseGrpcUrl(input)).toStrictEqual({
url: '',
enableTls: false,
path: '',
});
});
});
17 changes: 14 additions & 3 deletions packages/insomnia/src/network/grpc/parse-grpc-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ export const parseGrpcUrl = (grpcUrl: string): { url: string; enableTls: boolean
if (!grpcUrl) {
return { url: '', enableTls: false, path: '' };
}
const url = new URL(grpcUrl);
const lcUrl = grpcUrl.toLowerCase();
let url = {
protocol: 'grpc:',
hostname: lcUrl,
pathname: '',
port: undefined,
} as unknown as URL;
if (lcUrl.includes('://')) {
try {
url = new URL(grpcUrl.toLowerCase());
} catch (e) { }
}
const result = {
url: url.host,
url: `${url.hostname}` + (url.port ? `:${url.port}` : ''),
enableTls: false,
path: url.pathname,
};
if (url.protocol === 'grpcs:') {
if (url.protocol.toLowerCase() === 'grpcs:') {
result.enableTls = true;
}
if (result.path === '/') {
Expand Down

0 comments on commit a3c8e2d

Please sign in to comment.