Merge pull request #68 from useblacksmith/stop-sending-form-data
src: start sending get request with query paramspull/1358/head
commit
c6b6f32adc
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,54 @@
|
||||
import * as reporter from '../reporter';
|
||||
import { getStickyDisk } from '../setup_builder';
|
||||
import FormData from 'form-data';
|
||||
|
||||
jest.mock('../reporter');
|
||||
|
||||
describe('getStickyDisk', () => {
|
||||
const mockGet = jest.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
process.env.GITHUB_REPO_NAME = 'test-repo';
|
||||
process.env.BLACKSMITH_REGION = 'test-region';
|
||||
process.env.BLACKSMITH_INSTALLATION_MODEL_ID = 'test-model';
|
||||
process.env.VM_ID = 'test-vm';
|
||||
|
||||
(reporter.createBlacksmithAgentClient as jest.Mock).mockResolvedValue({});
|
||||
(reporter.get as jest.Mock).mockImplementation(mockGet);
|
||||
mockGet.mockResolvedValue({
|
||||
data: {
|
||||
expose_id: 'test-expose-id',
|
||||
disk_identifier: 'test-device'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sets both FormData and query parameters correctly', async () => {
|
||||
const appendSpy = jest.spyOn(FormData.prototype, 'append');
|
||||
|
||||
await getStickyDisk();
|
||||
|
||||
expect(mockGet).toHaveBeenCalledTimes(1);
|
||||
const [, url, formData] = mockGet.mock.calls[0];
|
||||
|
||||
// Verify query parameters
|
||||
expect(url).toContain('stickyDiskKey=test-repo');
|
||||
expect(url).toContain('region=test-region');
|
||||
expect(url).toContain('installationModelID=test-model');
|
||||
expect(url).toContain('vmID=test-vm');
|
||||
|
||||
// Verify FormData is correct type
|
||||
expect(formData instanceof FormData).toBeTruthy();
|
||||
|
||||
// Verify the headers are set correctly
|
||||
const headers = formData.getHeaders();
|
||||
expect(headers['content-type']).toContain('multipart/form-data');
|
||||
|
||||
// Verify the correct fields were appended
|
||||
expect(appendSpy).toHaveBeenCalledWith('stickyDiskKey', 'test-repo');
|
||||
expect(appendSpy).toHaveBeenCalledWith('region', 'test-region');
|
||||
expect(appendSpy).toHaveBeenCalledWith('installationModelID', 'test-model');
|
||||
expect(appendSpy).toHaveBeenCalledWith('vmID', 'test-vm');
|
||||
});
|
||||
});
|
@ -1,59 +0,0 @@
|
||||
import axios, {AxiosError, AxiosInstance, AxiosResponse} from 'axios';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
export async function getBlacksmithAgentClient(): Promise<AxiosInstance> {
|
||||
const stickyDiskMgrUrl = 'http://192.168.127.1:5556';
|
||||
return axios.create({
|
||||
baseURL: stickyDiskMgrUrl
|
||||
});
|
||||
}
|
||||
|
||||
export async function postWithRetry(client: AxiosInstance, url: string, formData: FormData, retryCondition: (error: AxiosError) => boolean): Promise<AxiosResponse> {
|
||||
const maxRetries = 5;
|
||||
const retryDelay = 100;
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
return await client.post(url, formData, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.BLACKSMITH_STICKYDISK_TOKEN}`,
|
||||
'X-Github-Repo-Name': process.env.GITHUB_REPO_NAME || '',
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (attempt === maxRetries || !retryCondition(error as AxiosError)) {
|
||||
throw error;
|
||||
}
|
||||
core.warning(`Request failed, retrying (${attempt}/${maxRetries})...`);
|
||||
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
||||
}
|
||||
}
|
||||
throw new Error('Max retries reached');
|
||||
}
|
||||
|
||||
export async function postWithRetryToBlacksmithAPI(url: string, requestBody: unknown, retryCondition: (error: AxiosError) => boolean): Promise<AxiosResponse> {
|
||||
const maxRetries = 5;
|
||||
const retryDelay = 100;
|
||||
const apiUrl = process.env.BLACKSMITH_ENV?.includes('staging') ? 'https://stagingapi.blacksmith.sh' : 'https://api.blacksmith.sh';
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
const fullUrl = `${apiUrl}${url}`;
|
||||
return await axios.post(fullUrl, requestBody, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.BLACKSMITH_STICKYDISK_TOKEN}`,
|
||||
'X-Github-Repo-Name': process.env.GITHUB_REPO_NAME || '',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (attempt === maxRetries || !retryCondition(error as AxiosError)) {
|
||||
throw error;
|
||||
}
|
||||
core.warning(`Request failed, retrying (${attempt}/${maxRetries})...`);
|
||||
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
||||
}
|
||||
}
|
||||
throw new Error('Max retries reached');
|
||||
}
|
Loading…
Reference in New Issue