Skip to content

Commit

Permalink
fix(HTTP Request Node): Support form data when using pagination (#8497)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsmr authored and netroy committed Feb 2, 2024
1 parent 31e4cd8 commit af929ba
Show file tree
Hide file tree
Showing 3 changed files with 1,900 additions and 1,568 deletions.
23 changes: 20 additions & 3 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,23 @@ async function prepareBinaryData(
return await setBinaryDataBuffer(returnData, binaryData, workflowId, executionId);
}

function applyPaginationRequestData(
requestData: OptionsWithUri,
paginationRequestData: PaginationOptions['request'],
): OptionsWithUri {
const preparedPaginationData: Partial<OptionsWithUri> = { ...paginationRequestData };

if ('formData' in requestData) {
preparedPaginationData.formData = paginationRequestData.body;
delete preparedPaginationData.body;
} else if ('form' in requestData) {
preparedPaginationData.form = paginationRequestData.body;
delete preparedPaginationData.body;
}

return merge({}, requestData, preparedPaginationData);
}

/**
* Makes a request using OAuth data for authentication
*
Expand Down Expand Up @@ -2806,7 +2823,7 @@ const getRequestHelperFunctions = (

let tempResponseData: IN8nHttpFullResponse;
let makeAdditionalRequest: boolean;
let paginateRequestData: IHttpRequestOptions;
let paginateRequestData: PaginationOptions['request'];

const runIndex = 0;

Expand Down Expand Up @@ -2836,9 +2853,9 @@ const getRequestHelperFunctions = (
executeData,
additionalKeys,
false,
) as object as IHttpRequestOptions;
) as object as PaginationOptions['request'];

const tempRequestOptions = merge(requestOptions, paginateRequestData);
const tempRequestOptions = applyPaginationRequestData(requestOptions, paginateRequestData);

if (credentialsType) {
tempResponseData = await this.helpers.requestWithAuthentication.call(
Expand Down
84 changes: 54 additions & 30 deletions packages/nodes-base/nodes/HttpRequest/test/node/HttpRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ describe('Test HTTP Request Node', () => {
await initBinaryDataService();
nock.disableNetConnect();

function getPaginationReturnData(this: nock.ReplyFnContext, limit = 10, skip = 0) {
const nextUrl = `${baseUrl}/users?skip=${skip + limit}&limit=${limit}`;

const response = [];
for (let i = skip; i < skip + limit; i++) {
if (i > 14) {
break;
}
response.push({
id: i,
});
}

if (!response.length) {
return [
404,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
}

return [
200,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
}

//GET
nock(baseUrl).get('/todos/1').reply(200, {
id: 1,
Expand Down Expand Up @@ -119,46 +153,36 @@ describe('Test HTTP Request Node', () => {
deletedOn: '2023-02-09T05:37:31.720Z',
});

// Pagination - Data not identical to dummyjson.com
// Pagination - GET
nock(baseUrl)
.persist()
.get('/users')
.query(true)
.reply(function (uri) {
const data = parseUrl(uri, true);
const skip = parseInt((data.query.skip as string) || '0', 10);
const limit = parseInt((data.query.limit as string) || '10', 10);
const nextUrl = `${baseUrl}/users?skip=${skip + limit}&limit=${limit}`;

const response = [];
for (let i = skip; i < skip + limit; i++) {
if (i > 14) {
break;
}
response.push({
id: i,
});
}
const skip = parseInt((data.query.skip as string) || '0', 10);
return getPaginationReturnData.call(this, limit, skip);
});

if (!response.length) {
return [
404,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
// Pagination - POST
nock(baseUrl)
.persist()
.post('/users')
.reply(function (_uri, body) {
let skip = 0;
let limit = 10;

if (typeof body === 'string') {
// Form data
skip = parseInt(body.split('name="skip"')[1].split('---')[0] ?? '0', 10);
limit = parseInt(body.split('name="limit"')[1].split('---')[0] ?? '0', 10);
} else {
skip = parseInt(body.skip ?? '0', 10);
limit = parseInt(body.limit ?? '10', 10);
}

return [
200,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
return getPaginationReturnData.call(this, limit, skip);
});
});

Expand Down
Loading

0 comments on commit af929ba

Please sign in to comment.