test(HTTP Request Node): Unit tests (no-changelog)

This commit is contained in:
Michael Kret 2023-02-09 12:25:26 +02:00 committed by GitHub
parent b5948cbe13
commit 2150ea0e79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 601 additions and 10 deletions

View file

@ -0,0 +1,130 @@
import {
setup,
equalityTest,
workflowToTests,
getWorkflowFilenames,
} from '../../../../test/nodes/Helpers';
import nock from 'nock';
describe('Test HTTP Request Node', () => {
const workflows = getWorkflowFilenames(__dirname);
const tests = workflowToTests(workflows);
const baseUrl = 'https://dummyjson.com';
beforeAll(() => {
nock.disableNetConnect();
//GET
nock(baseUrl).get('/todos/1').reply(200, {
id: 1,
todo: 'Do something nice for someone I care about',
completed: true,
userId: 26,
});
nock(baseUrl).matchHeader('Authorization', 'Bearer 12345').get('/todos/3').reply(200, {
id: 3,
todo: 'Watch a classic movie',
completed: false,
userId: 4,
});
nock(baseUrl)
.get('/todos?limit=2&skip=10')
.reply(200, {
todos: [
{
id: 11,
todo: "Text a friend I haven't talked to in a long time",
completed: false,
userId: 39,
},
{
id: 12,
todo: 'Organize pantry',
completed: true,
userId: 39,
},
],
total: 150,
skip: 10,
limit: 2,
});
//POST
nock(baseUrl)
.post('/todos/add', {
todo: 'Use DummyJSON in the project',
completed: false,
userId: '5',
})
.reply(200, {
id: 151,
todo: 'Use DummyJSON in the project',
completed: false,
userId: '5',
});
nock(baseUrl)
.post('/todos/add2', {
todo: 'Use DummyJSON in the project',
completed: false,
userId: 15,
})
.reply(200, {
id: 151,
todo: 'Use DummyJSON in the project',
completed: false,
userId: 15,
});
//PUT
nock(baseUrl).put('/todos/10', { userId: '42' }).reply(200, {
id: 10,
todo: 'Have a football scrimmage with some friends',
completed: false,
userId: '42',
});
//PATCH
nock(baseUrl)
.patch('/products/1', '{"title":"iPhone 12"}')
.reply(200, {
id: 1,
title: 'iPhone 12',
price: 549,
stock: 94,
rating: 4.69,
images: [
'https://i.dummyjson.com/data/products/1/1.jpg',
'https://i.dummyjson.com/data/products/1/2.jpg',
'https://i.dummyjson.com/data/products/1/3.jpg',
'https://i.dummyjson.com/data/products/1/4.jpg',
'https://i.dummyjson.com/data/products/1/thumbnail.jpg',
],
thumbnail: 'https://i.dummyjson.com/data/products/1/thumbnail.jpg',
description: 'An apple mobile which is nothing like apple',
brand: 'Apple',
category: 'smartphones',
});
//DELETE
nock(baseUrl).delete('/todos/1').reply(200, {
id: 1,
todo: 'Do something nice for someone I care about',
completed: true,
userId: 26,
isDeleted: true,
deletedOn: '2023-02-09T05:37:31.720Z',
});
});
afterAll(() => {
nock.restore();
});
const nodeTypes = setup(tests);
for (const testData of tests) {
test(testData.description, async () => equalityTest(testData, nodeTypes));
}
});

View file

@ -0,0 +1,60 @@
{
"name": "http request test",
"nodes": [
{
"parameters": {},
"id": "12433cfb-74d9-4bf1-9afd-0ab9afc9ef19",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [820, 360]
},
{
"parameters": {
"method": "DELETE",
"url": "https://dummyjson.com/todos/1",
"options": {}
},
"id": "312e64ca-00bf-40e6-b21d-1f73930ef98c",
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [1100, 360]
}
],
"pinData": {
"HTTP Request": [
{
"json": {
"id": 1,
"todo": "Do something nice for someone I care about",
"completed": true,
"userId": 26,
"isDeleted": true,
"deletedOn": "2023-02-09T05:37:31.720Z"
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {},
"versionId": "b1c4f6ef-0d15-49f3-b46d-447671b1583e",
"id": "108",
"meta": {
"instanceId": "36203ea1ce3cef713fa25999bd9874ae26b9e4c2c3a90a365f2882a154d031d0"
},
"tags": []
}

View file

@ -0,0 +1,144 @@
{
"name": "http request test",
"nodes": [
{
"parameters": {},
"id": "12433cfb-74d9-4bf1-9afd-0ab9afc9ef19",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [820, 360]
},
{
"parameters": {
"url": "https://dummyjson.com/todos/1",
"options": {}
},
"id": "07670093-862f-403f-96a5-ddf7fdb0d225",
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [1120, 100]
},
{
"parameters": {
"url": "https://dummyjson.com/todos/3",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "Bearer 12345"
}
]
},
"options": {}
},
"id": "25cc4f31-9363-4247-a49d-7ac49f174d16",
"name": "HTTP Request fake header",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [1120, 440]
},
{
"parameters": {
"url": "https://dummyjson.com/todos",
"sendQuery": true,
"queryParameters": {
"parameters": [
{
"name": "limit",
"value": "2"
},
{
"name": "skip",
"value": "10"
}
]
},
"options": {}
},
"id": "33c80933-b113-4eff-beb7-4a5b0bc30bcf",
"name": "HTTP Request with query",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [1120, 620]
}
],
"pinData": {
"HTTP Request": [
{
"json": {
"id": 1,
"todo": "Do something nice for someone I care about",
"completed": true,
"userId": 26
}
}
],
"HTTP Request with query": [
{
"json": {
"todos": [
{
"id": 11,
"todo": "Text a friend I haven't talked to in a long time",
"completed": false,
"userId": 39
},
{
"id": 12,
"todo": "Organize pantry",
"completed": true,
"userId": 39
}
],
"total": 150,
"skip": 10,
"limit": 2
}
}
],
"HTTP Request fake header": [
{
"json": {
"id": 3,
"todo": "Watch a classic movie",
"completed": false,
"userId": 4
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
},
{
"node": "HTTP Request with query",
"type": "main",
"index": 0
},
{
"node": "HTTP Request fake header",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {},
"versionId": "0fb64565-22b3-4ff3-8ba4-354b2bcaf8a6",
"id": "108",
"meta": {
"instanceId": "36203ea1ce3cef713fa25999bd9874ae26b9e4c2c3a90a365f2882a154d031d0"
},
"tags": []
}

View file

@ -0,0 +1,73 @@
{
"name": "http request test",
"nodes": [
{
"parameters": {},
"id": "12433cfb-74d9-4bf1-9afd-0ab9afc9ef19",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [820, 360]
},
{
"parameters": {
"method": "PATCH",
"url": "https://dummyjson.com/products/1",
"sendBody": true,
"specifyBody": "json",
"jsonBody": "{\"title\":\"iPhone 12\"}",
"options": {}
},
"id": "312e64ca-00bf-40e6-b21d-1f73930ef98c",
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [1100, 360]
}
],
"pinData": {
"HTTP Request": [
{
"json": {
"id": 1,
"title": "iPhone 12",
"price": 549,
"stock": 94,
"rating": 4.69,
"images": [
"https://i.dummyjson.com/data/products/1/1.jpg",
"https://i.dummyjson.com/data/products/1/2.jpg",
"https://i.dummyjson.com/data/products/1/3.jpg",
"https://i.dummyjson.com/data/products/1/4.jpg",
"https://i.dummyjson.com/data/products/1/thumbnail.jpg"
],
"thumbnail": "https://i.dummyjson.com/data/products/1/thumbnail.jpg",
"description": "An apple mobile which is nothing like apple",
"brand": "Apple",
"category": "smartphones"
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {},
"versionId": "a49ffcc8-e61f-4fcd-93c0-c1c422d14b6c",
"id": "108",
"meta": {
"instanceId": "36203ea1ce3cef713fa25999bd9874ae26b9e4c2c3a90a365f2882a154d031d0"
},
"tags": []
}

View file

@ -0,0 +1,105 @@
{
"name": "http request test",
"nodes": [
{
"parameters": {},
"id": "12433cfb-74d9-4bf1-9afd-0ab9afc9ef19",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [820, 360]
},
{
"parameters": {
"method": "POST",
"url": "https://dummyjson.com/todos/add",
"sendBody": true,
"bodyParameters": {
"parameters": [
{
"name": "todo",
"value": "Use DummyJSON in the project"
},
{
"name": "completed",
"value": "={{ false }}"
},
{
"name": "userId",
"value": "5"
}
]
},
"options": {}
},
"id": "07670093-862f-403f-96a5-ddf7fdb0d225",
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [1140, 200]
},
{
"parameters": {
"method": "POST",
"url": "https://dummyjson.com/todos/add2",
"sendBody": true,
"specifyBody": "json",
"jsonBody": "{\"todo\":\"Use DummyJSON in the project\",\"completed\":false,\"userId\":15}",
"options": {}
},
"id": "db088210-2204-422c-823a-101afa464384",
"name": "HTTP Request1",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [1140, 440]
}
],
"pinData": {
"HTTP Request": [
{
"json": {
"id": 151,
"todo": "Use DummyJSON in the project",
"completed": false,
"userId": "5"
}
}
],
"HTTP Request1": [
{
"json": {
"id": 151,
"todo": "Use DummyJSON in the project",
"completed": false,
"userId": 15
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
},
{
"node": "HTTP Request1",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {},
"versionId": "c5d9075a-6d1e-49d8-b16b-7df985ebda69",
"id": "108",
"meta": {
"instanceId": "36203ea1ce3cef713fa25999bd9874ae26b9e4c2c3a90a365f2882a154d031d0"
},
"tags": []
}

View file

@ -0,0 +1,73 @@
{
"name": "http request test",
"nodes": [
{
"parameters": {},
"id": "12433cfb-74d9-4bf1-9afd-0ab9afc9ef19",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
820,
360
]
},
{
"parameters": {
"method": "PUT",
"url": "https://dummyjson.com/todos/10",
"sendBody": true,
"bodyParameters": {
"parameters": [
{
"name": "userId",
"value": "42"
}
]
},
"options": {}
},
"id": "07670093-862f-403f-96a5-ddf7fdb0d225",
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 3,
"position": [
1100,
360
]
}
],
"pinData": {
"HTTP Request": [
{
"json": {
"id": 10,
"todo": "Have a football scrimmage with some friends",
"completed": false,
"userId": "42"
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {},
"versionId": "209dd43e-fa03-4da7-94fb-cecf1974c5fe",
"id": "108",
"meta": {
"instanceId": "36203ea1ce3cef713fa25999bd9874ae26b9e4c2c3a90a365f2882a154d031d0"
},
"tags": []
}

View file

@ -248,6 +248,20 @@ export const equalityTest = async (testData: WorkflowTestData, types: INodeTypes
expect(result.finished).toEqual(true);
};
const preparePinData = (pinData: IDataObject) => {
const returnData = Object.keys(pinData).reduce(
(acc, key) => {
const data = pinData[key] as IDataObject[];
acc[key] = [data as IDataObject[]];
return acc;
},
{} as {
[key: string]: IDataObject[][];
},
);
return returnData;
};
export const workflowToTests = (workflowFiles: string[]) => {
const testCases: WorkflowTestData[] = [];
for (const filePath of workflowFiles) {
@ -256,16 +270,8 @@ export const workflowToTests = (workflowFiles: string[]) => {
if (workflowData.pinData === undefined) {
throw new Error('Workflow data does not contain pinData');
}
const nodeData = Object.keys(workflowData.pinData).reduce(
(acc, key) => {
const data = workflowData.pinData[key] as IDataObject[];
acc[key] = [data as IDataObject[]];
return acc;
},
{} as {
[key: string]: IDataObject[][];
},
);
const nodeData = preparePinData(workflowData.pinData);
delete workflowData.pinData;