🌐 fix(actions): Correct URL Formation for Subdomains in createURL (#3149)

This commit is contained in:
Danny Avila 2024-06-21 11:07:45 -04:00 committed by GitHub
parent cec2e57ee9
commit b2b469bd3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 3 deletions

View file

@ -1,6 +1,7 @@
import axios from 'axios'; import axios from 'axios';
import { OpenAPIV3 } from 'openapi-types'; import { OpenAPIV3 } from 'openapi-types';
import { import {
createURL,
resolveRef, resolveRef,
ActionRequest, ActionRequest,
openapiToFunction, openapiToFunction,
@ -506,3 +507,45 @@ describe('validateAndParseOpenAPISpec', () => {
expect(requestBuilders).toHaveProperty('saveCitation'); expect(requestBuilders).toHaveProperty('saveCitation');
}); });
}); });
describe('createURL', () => {
it('correctly combines domain and path', () => {
expect(createURL('https://example.com', '/api/v1/users')).toBe(
'https://example.com/api/v1/users',
);
});
it('handles domain with trailing slash', () => {
expect(createURL('https://example.com/', '/api/v1/users')).toBe(
'https://example.com/api/v1/users',
);
});
it('handles path with leading slash', () => {
expect(createURL('https://example.com', 'api/v1/users')).toBe(
'https://example.com/api/v1/users',
);
});
it('handles domain with trailing slash and path with leading slash', () => {
expect(createURL('https://example.com/', '/api/v1/users')).toBe(
'https://example.com/api/v1/users',
);
});
it('handles domain without trailing slash and path without leading slash', () => {
expect(createURL('https://example.com', 'api/v1/users')).toBe(
'https://example.com/api/v1/users',
);
});
it('handles empty path', () => {
expect(createURL('https://example.com', '')).toBe('https://example.com/');
});
it('handles domain with subdirectory', () => {
expect(createURL('https://example.com/subdirectory', '/api/v1/users')).toBe(
'https://example.com/subdirectory/api/v1/users',
);
});
});

View file

@ -32,8 +32,10 @@ export function sha1(input: string) {
} }
export function createURL(domain: string, path: string) { export function createURL(domain: string, path: string) {
const myURL = new URL(path, domain); const cleanDomain = domain.replace(/\/$/, '');
return myURL.toString(); const cleanPath = path.replace(/^\//, '');
const fullURL = `${cleanDomain}/${cleanPath}`;
return new URL(fullURL).toString();
} }
export class FunctionSignature { export class FunctionSignature {
@ -92,7 +94,7 @@ export class ActionRequest {
private authHeaders: Record<string, string> = {}; private authHeaders: Record<string, string> = {};
private authToken?: string; private authToken?: string;
async setParams(params: object) { setParams(params: object) {
this.operationHash = sha1(JSON.stringify(params)); this.operationHash = sha1(JSON.stringify(params));
this.params = params; this.params = params;