refactor(core): Move curl endpoint to its own controller (no-changelog) (#9605)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-06-05 10:43:09 +02:00
committed by GitHub
parent 375b347b0f
commit 1563bf571d
8 changed files with 283 additions and 235 deletions

View File

@@ -0,0 +1,51 @@
import type { Request } from 'express';
import { mock } from 'jest-mock-extended';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { CurlController } from '@/controllers/curl.controller';
import type { CurlService } from '@/services/curl.service';
describe('CurlController', () => {
const service = mock<CurlService>();
const controller = new CurlController(service);
beforeEach(() => jest.clearAllMocks());
describe('toJson', () => {
it('should throw BadRequestError when invalid cURL command is provided', () => {
const req = mock<Request>();
service.toHttpNodeParameters.mockImplementation(() => {
throw new Error();
});
expect(() => controller.toJson(req)).toThrow(BadRequestError);
});
it('should return flattened parameters when valid cURL command is provided', () => {
const curlCommand = 'curl -v -X GET https://test.n8n.berlin/users';
const req = mock<Request>();
req.body = { curlCommand };
service.toHttpNodeParameters.mockReturnValue({
url: 'https://test.n8n.berlin/users',
authentication: 'none',
method: 'GET',
sendHeaders: false,
sendQuery: false,
options: {
redirect: { redirect: {} },
response: { response: {} },
},
sendBody: false,
});
const result = controller.toJson(req);
expect(result).toEqual({
'parameters.method': 'GET',
'parameters.url': 'https://test.n8n.berlin/users',
'parameters.authentication': 'none',
'parameters.sendBody': false,
'parameters.sendHeaders': false,
'parameters.sendQuery': false,
});
});
});
});

View File

@@ -1,10 +1,12 @@
import { toHttpNodeParameters } from '@/CurlConverterHelper';
import { CurlService } from '@/services/curl.service';
describe('CurlService', () => {
const service = new CurlService();
describe('CurlConverterHelper', () => {
test('Should parse form-urlencoded content type correctly', () => {
const curl =
'curl -X POST https://reqbin.com/echo/post/form -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1&param2=value2"';
const parameters = toHttpNodeParameters(curl);
const parameters = service.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');
@@ -19,7 +21,7 @@ describe('CurlConverterHelper', () => {
test('Should parse JSON content type correctly', () => {
const curl =
'curl -X POST https://reqbin.com/echo/post/json -H \'Content-Type: application/json\' -d \'{"login":"my_login","password":"my_password"}\'';
const parameters = toHttpNodeParameters(curl);
const parameters = service.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');
@@ -34,7 +36,7 @@ describe('CurlConverterHelper', () => {
test('Should parse multipart-form-data content type correctly', () => {
const curl =
'curl -X POST https://reqbin.com/echo/post/json -v -F key1=value1 -F upload=@localfilename';
const parameters = toHttpNodeParameters(curl);
const parameters = service.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');
@@ -50,7 +52,7 @@ describe('CurlConverterHelper', () => {
test('Should parse binary request correctly', () => {
const curl =
"curl --location --request POST 'https://www.website.com' --header 'Content-Type: image/png' --data-binary '@/Users/image.png";
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://www.website.com');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(true);
@@ -64,7 +66,7 @@ describe('CurlConverterHelper', () => {
-H "Content-Type: application/xml"
-H "Accept: application/xml"
-d "<Request><Login>my_login</Login><Password>my_password</Password></Request>"`;
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo/post/xml');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(true);
@@ -79,7 +81,7 @@ describe('CurlConverterHelper', () => {
test('Should parse header properties and keep the original case', () => {
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"';
const parameters = toHttpNodeParameters(curl);
const parameters = service.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');
@@ -96,7 +98,7 @@ describe('CurlConverterHelper', () => {
test('Should parse querystring properties', () => {
const curl = "curl -G -d 'q=kitties' -d 'count=20' https://google.com/search";
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://google.com/search');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -110,7 +112,7 @@ describe('CurlConverterHelper', () => {
test('Should parse basic authentication property and keep the original case', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password"';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -124,7 +126,7 @@ describe('CurlConverterHelper', () => {
test('Should parse location flag with --location', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" --location';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -139,7 +141,7 @@ describe('CurlConverterHelper', () => {
test('Should parse location flag with --L', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -L';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -154,7 +156,7 @@ describe('CurlConverterHelper', () => {
test('Should parse location and max redirects flags with --location and --max-redirs 10', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" --location --max-redirs 10';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -170,7 +172,7 @@ describe('CurlConverterHelper', () => {
test('Should parse proxy flag -x', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -185,7 +187,7 @@ describe('CurlConverterHelper', () => {
test('Should parse proxy flag --proxy', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -200,7 +202,7 @@ describe('CurlConverterHelper', () => {
test('Should parse include headers on output flag --include', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" --include -x https://google.com';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -215,7 +217,7 @@ describe('CurlConverterHelper', () => {
test('Should parse include headers on output flag -i', () => {
const curl = 'curl https://reqbin.com/echo -u "login:password" -x https://google.com -i';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.sendBody).toBe(false);
expect(parameters.contentType).toBeUndefined();
@@ -230,7 +232,7 @@ describe('CurlConverterHelper', () => {
test('Should parse include request flag -X', () => {
const curl = 'curl -X POST https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
@@ -239,7 +241,7 @@ describe('CurlConverterHelper', () => {
test('Should parse include request flag --request', () => {
const curl =
'curl --request POST https://reqbin.com/echo -u "login:password" -x https://google.com';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
@@ -248,7 +250,7 @@ describe('CurlConverterHelper', () => {
test('Should parse include timeout flag --connect-timeout', () => {
const curl =
'curl --request POST https://reqbin.com/echo -u "login:password" --connect-timeout 20';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
@@ -257,7 +259,7 @@ describe('CurlConverterHelper', () => {
test('Should parse download file flag -O', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -O';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
@@ -267,7 +269,7 @@ describe('CurlConverterHelper', () => {
test('Should parse download file flag -o', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -o';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
@@ -277,7 +279,7 @@ describe('CurlConverterHelper', () => {
test('Should parse ignore SSL flag -k', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" -k';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);
@@ -286,7 +288,7 @@ describe('CurlConverterHelper', () => {
test('Should parse ignore SSL flag --insecure', () => {
const curl = 'curl --request POST https://reqbin.com/echo -u "login:password" --insecure';
const parameters = toHttpNodeParameters(curl);
const parameters = service.toHttpNodeParameters(curl);
expect(parameters.url).toBe('https://reqbin.com/echo');
expect(parameters.method).toBe('POST');
expect(parameters.sendBody).toBe(false);