Skip to content

Commit

Permalink
feat: http form format
Browse files Browse the repository at this point in the history
  • Loading branch information
AASHISH MALIK committed Dec 8, 2024
1 parent 313710c commit 944a402
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 2 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"parse-static-imports": "^1.1.0",
"prom-client": "^14.2.0",
"qs": "^6.11.1",
"querystring": "^0.2.1",
"rs-jsonpath": "^1.1.2",
"set-value": "^4.1.0",
"sha256": "^0.2.0",
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/v2/destinations/http/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ steps:
const payload = $.getCustomMappings(.message, .destination.Config.propertiesMapping);
$.context.payload = $.removeUndefinedAndNullValues($.excludeMappedFields(payload, .destination.Config.propertiesMapping))
$.context.format === "XML" && !$.isEmptyObject($.context.payload) ? $.context.payload = {payload: $.getXMLPayload($.context.payload)};
$.context.format === "FORM" && !$.isEmptyObject($.context.payload) ? $.context.payload = {payload: $.getFORMPayload($.context.payload)};
- name: buildResponseForProcessTransformation
template: |
const response = $.defaultRequestConfig();
$.context.format === "JSON" ? response.body.JSON = $.context.payload: response.body.XML = $.context.payload;
$.context.format === "JSON" ? response.body.JSON = $.context.payload : $.context.format === "XML" ? response.body.XML = $.context.payload : response.body.FORM = $.context.payload ;
response.endpoint = $.context.endpoint;
response.headers = $.context.headers;
response.method = $.context.method;
Expand Down
14 changes: 14 additions & 0 deletions src/cdk/v2/destinations/http/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { toXML } = require('jstoxml');
const { groupBy } = require('lodash');
const { createHash } = require('crypto');
const querystring = require('querystring');
const { ConfigurationError } = require('@rudderstack/integrations-lib');
const { BatchUtils } = require('@rudderstack/workflow-engine');
const {
Expand Down Expand Up @@ -146,11 +147,24 @@ const batchSuccessfulEvents = (events, batchSize) => {
return response;
};

/**
* Converts JSON payload to application/x-www-form-urlencoded format.
* @param {Object} payload - The JSON payload to be converted.
* @returns {string} - The payload in application/x-www-form-urlencoded format.
*/
const getFORMPayload = (payload) => {
if (!payload) {
throw new ConfigurationError('Invalid payload for FORM format');

Check warning on line 157 in src/cdk/v2/destinations/http/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/http/utils.js#L157

Added line #L157 was not covered by tests
}
return querystring.stringify(payload);
};

module.exports = {
getAuthHeaders,
getCustomMappings,
addPathParams,
excludeMappedFields,
getXMLPayload,
batchSuccessfulEvents,
getFORMPayload,
};
60 changes: 60 additions & 0 deletions test/integrations/destinations/http/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,66 @@ const destinations: Destination[] = [
Transformations: [],
WorkspaceID: 'test-workspace-id',
},
{
Config: {
apiUrl: 'http://abc.com/events',
auth: 'bearerTokenAuth',
bearerToken: 'test-token',
method: 'POST',
format: 'FORM',
headers: [
{
to: '$.h1',
from: "'val1'",
},
{
to: '$.h2',
from: '$.key1',
},
{
to: "$.'content-type'",
from: "'application/json'",
},
],
propertiesMapping: [
{
from: '$.event',
to: '$.event',
},
{
from: '$.properties.currency',
to: '$.currency',
},
{
from: '$.userId',
to: '$.userId',
},
{
from: '$.properties.products[*].product_id',
to: '$.properties.items[*].item_id',
},
{
from: '$.properties.products[*].name',
to: '$.properties.items[*].name',
},
{
from: '$.properties.products[*].price',
to: '$.properties.items[*].price',
},
],
},
DestinationDefinition: {
DisplayName: displayName,
ID: '123',
Name: destTypeInUpperCase,
Config: { cdkV2Enabled: true },
},
Enabled: true,
ID: '123',
Name: destTypeInUpperCase,
Transformations: [],
WorkspaceID: 'test-workspace-id',
},
];

const traits = {
Expand Down
54 changes: 53 additions & 1 deletion test/integrations/destinations/http/processor/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProcessorTestData } from '../../../testTypes';
import { generateMetadata, transformResultBuilder } from '../../../testUtils';
import { destType, destinations, properties, traits } from '../common';
import { destinations, destType, properties, traits } from '../common';

export const configuration: ProcessorTestData[] = [
{
Expand Down Expand Up @@ -203,4 +203,56 @@ export const configuration: ProcessorTestData[] = [
},
},
},
{
id: 'http-configuration-test-4',
name: destType,
description:
'Track call with bearer token, form format, post method, additional headers and properties mapping',
scenario: 'Business',
successCriteria:
'Response should be in form format with post method, headers and properties mapping',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
destination: destinations[7],
message: {
type: 'track',
userId: 'userId123',
event: 'Order Completed',
properties,
},
metadata: generateMetadata(1),
},
],
},
},
output: {
response: {
status: 200,
body: [
{
output: transformResultBuilder({
method: 'POST',
userId: '',
endpoint: destinations[7].Config.apiUrl,
headers: {
Authorization: 'Bearer test-token',
h1: 'val1',
'content-type': 'application/json',
},
FORM: {
payload: 'event=Order%20Completed&currency=USD&userId=userId123&properties=',
},
}),
statusCode: 200,
metadata: generateMetadata(1),
},
],
},
},
},
];

0 comments on commit 944a402

Please sign in to comment.