2022-11-09 06:25:00 -08:00
|
|
|
import { toHttpNodeParameters } from '@/CurlConverterHelper';
|
2022-09-29 14:28:02 -07:00
|
|
|
|
|
|
|
describe('CurlConverterHelper', () => {
|
|
|
|
test('Should parse form-urlencoded content type correctly', () => {
|
2022-11-09 06:25:00 -08:00
|
|
|
const curl =
|
|
|
|
'curl -X POST https://reqbin.com/echo/post/form -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1¶m2=value2"';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo/post/form');
|
|
|
|
expect(parameters.sendBody).toBe(true);
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].name).toBe('param1');
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].value).toBe('value1');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].name).toBe('param2');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].value).toBe('value2');
|
|
|
|
expect(parameters.contentType).toBe('form-urlencoded');
|
|
|
|
expect(parameters.sendHeaders).toBe(false);
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse JSON content type correctly', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl =
|
|
|
|
'curl -X POST https://reqbin.com/echo/post/json -H \'Content-Type: application/json\' -d \'{"login":"my_login","password":"my_password"}\'';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo/post/json');
|
|
|
|
expect(parameters.sendBody).toBe(true);
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].name).toBe('login');
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].value).toBe('my_login');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].name).toBe('password');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].value).toBe('my_password');
|
|
|
|
expect(parameters.contentType).toBe('json');
|
|
|
|
expect(parameters.sendHeaders).toBe(false);
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse multipart-form-data content type correctly', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl =
|
|
|
|
'curl -X POST https://reqbin.com/echo/post/json -v -F key1=value1 -F upload=@localfilename';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo/post/json');
|
|
|
|
expect(parameters.sendBody).toBe(true);
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].parameterType).toBe('formData');
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].name).toBe('key1');
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].value).toBe('value1');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].parameterType).toBe('formBinaryData');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].name).toBe('upload');
|
|
|
|
expect(parameters.contentType).toBe('multipart-form-data');
|
|
|
|
expect(parameters.sendHeaders).toBe(false);
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse binary request correctly', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl =
|
|
|
|
"curl --location --request POST 'https://www.website.com' --header 'Content-Type: image/png' --data-binary '@/Users/image.png";
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://www.website.com');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(true);
|
|
|
|
expect(parameters.contentType).toBe('binaryData');
|
|
|
|
expect(parameters.sendHeaders).toBe(false);
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse unknown content type correctly', () => {
|
|
|
|
const curl = `curl -X POST https://reqbin.com/echo/post/xml
|
|
|
|
-H "Content-Type: application/xml"
|
|
|
|
-H "Accept: application/xml"
|
|
|
|
-d "<Request><Login>my_login</Login><Password>my_password</Password></Request>"`;
|
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo/post/xml');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(true);
|
|
|
|
expect(parameters.contentType).toBe('raw');
|
|
|
|
expect(parameters.rawContentType).toBe('application/xml');
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('Accept');
|
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe('application/xml');
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse header properties and keep the original case', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl =
|
|
|
|
'curl -X POST https://reqbin.com/echo/post/json -v -F key1=value1 -F upload=@localfilename -H "ACCEPT: text/javascript" -H "content-type: multipart/form-data"';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo/post/json');
|
|
|
|
expect(parameters.sendBody).toBe(true);
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].parameterType).toBe('formData');
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].name).toBe('key1');
|
|
|
|
expect(parameters.bodyParameters?.parameters[0].value).toBe('value1');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].parameterType).toBe('formBinaryData');
|
|
|
|
expect(parameters.bodyParameters?.parameters[1].name).toBe('upload');
|
|
|
|
expect(parameters.contentType).toBe('multipart-form-data');
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('ACCEPT');
|
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe('text/javascript');
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse querystring properties', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = "curl -G -d 'q=kitties' -d 'count=20' https://google.com/search";
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://google.com/search');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendHeaders).toBe(false);
|
|
|
|
expect(parameters.sendQuery).toBe(true);
|
|
|
|
expect(parameters.queryParameters?.parameters[0].name).toBe('q');
|
|
|
|
expect(parameters.queryParameters?.parameters[0].value).toBe('kitties');
|
|
|
|
expect(parameters.queryParameters?.parameters[1].name).toBe('count');
|
|
|
|
expect(parameters.queryParameters?.parameters[1].value).toBe('20');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse basic authentication property and keep the original case', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password"';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse location flag with --location', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password" --location';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.options.redirect.redirect.followRedirects).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse location flag with --L', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password" -L';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.options.redirect.redirect.followRedirects).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse location and max redirects flags with --location and --max-redirs 10', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password" --location --max-redirs 10';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.options.redirect.redirect.followRedirects).toBe(true);
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.options.redirect.redirect.maxRedirects).toBe('10');
|
2022-09-29 14:28:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse proxy flag -x', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.options.proxy).toBe('https://google.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse proxy flag --proxy', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.options.proxy).toBe('https://google.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse include headers on output flag --include', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password" --include -x https://google.com';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.options.response.response.fullResponse).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse include headers on output flag -i', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com -i';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.contentType).toBeUndefined();
|
|
|
|
expect(parameters.sendQuery).toBe(false);
|
|
|
|
expect(parameters.sendHeaders).toBe(true);
|
|
|
|
expect(parameters.headerParameters?.parameters[0].name).toBe('authorization');
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(parameters.headerParameters?.parameters[0].value).toBe(
|
|
|
|
`Basic ${Buffer.from('login:password').toString('base64')}`,
|
|
|
|
);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.options.response.response.fullResponse).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse include request flag -X', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl -X POST https://reqbin.com/echo -u "login:password" -x https://google.com';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse include request flag --request', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl =
|
|
|
|
'curl --request POST https://reqbin.com/echo -u "login:password" -x https://google.com';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse include timeout flag --connect-timeout', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl =
|
|
|
|
'curl --request POST https://reqbin.com/echo -u "login:password" --connect-timeout 20';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.options.timeout).toBe(20000);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should parse download file flag -O', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -O';
|
2022-11-09 06:25:00 -08:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
2022-09-29 14:28:02 -07:00
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.options.response.response.responseFormat).toBe('file');
|
|
|
|
expect(parameters.options.response.response.outputPropertyName).toBe('data');
|
2022-11-09 06:25:00 -08:00
|
|
|
});
|
2022-09-29 14:28:02 -07:00
|
|
|
|
|
|
|
test('Should parse download file flag -o', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -o';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.options.response.response.responseFormat).toBe('file');
|
|
|
|
expect(parameters.options.response.response.outputPropertyName).toBe('data');
|
2022-11-09 06:25:00 -08:00
|
|
|
});
|
2022-09-29 14:28:02 -07:00
|
|
|
|
|
|
|
test('Should parse ignore SSL flag -k', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -k';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.options.allowUnauthorizedCerts).toBe(true);
|
2022-11-09 06:25:00 -08:00
|
|
|
});
|
2022-09-29 14:28:02 -07:00
|
|
|
|
|
|
|
test('Should parse ignore SSL flag --insecure', () => {
|
2023-05-02 01:37:19 -07:00
|
|
|
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" --insecure';
|
2022-09-29 14:28:02 -07:00
|
|
|
const parameters = toHttpNodeParameters(curl);
|
|
|
|
expect(parameters.url).toBe('https://reqbin.com/echo');
|
|
|
|
expect(parameters.method).toBe('POST');
|
|
|
|
expect(parameters.sendBody).toBe(false);
|
|
|
|
expect(parameters.options.allowUnauthorizedCerts).toBe(true);
|
2022-11-09 06:25:00 -08:00
|
|
|
});
|
2022-09-29 14:28:02 -07:00
|
|
|
});
|