Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fb custom audience html response #3402

Merged
merged 7 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/v0/util/facebookUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,13 @@ const formingFinalResponse = (
throw new TransformationError('Payload could not be constructed');
};

const isHtmlFormat = (string) => {
const pattern = /<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/i;
return pattern.test(string);
};

module.exports = {
isHtmlFormat,
getContentType,
getContentCategory,
transformedPayloadData,
Expand Down
23 changes: 23 additions & 0 deletions src/v0/util/facebookUtils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
fetchUserData,
deduceFbcParam,
getContentType,
isHtmlFormat,
} = require('./index');
const sha256 = require('sha256');
const { MAPPING_CONFIG, CONFIG_CATEGORIES } = require('../../destinations/facebook_pixel/config');
Expand Down Expand Up @@ -639,3 +640,25 @@ describe('getContentType', () => {
expect(result).toBe(defaultValue);
});
});

describe('isHtmlFormat', () => {
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
it('should recognize simple valid HTML tags', () => {
expect(isHtmlFormat('<div></div>')).toBe(true);
});

it('should detect self-closing tags', () => {
expect(isHtmlFormat("<img src='image.jpg' />")).toBe(true);
});

it('should handle nested HTML correctly', () => {
expect(isHtmlFormat('<div><span></span></div>')).toBe(true);
});

it('should return false for Json', () => {
expect(isHtmlFormat('{"a": 1, "b":2}')).toBe(false);
});

it('should return false for empty Json', () => {
expect(isHtmlFormat('{}')).toBe(false);
});
});
13 changes: 13 additions & 0 deletions src/v0/util/facebookUtils/networkHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
} = require('../../../adapters/utils/networkUtils');
const { prepareProxyRequest, proxyRequest } = require('../../../adapters/network');
const { ErrorDetailsExtractorBuilder } = require('../../../util/error-extractor');
const { isHtmlFormat } = require('./index');

/**
* Only under below mentioned scenario(s), add the errorCodes, subCodes etc,. to this map
Expand Down Expand Up @@ -277,6 +278,18 @@ const errorResponseHandler = (destResponse) => {

const destResponseHandler = (responseParams) => {
const { destinationResponse } = responseParams;

// check If the response is in html format
if (isHtmlFormat(destinationResponse.response) || isHtmlFormat(destinationResponse)) {
throw new NetworkError(
'Invalid response format (HTML) during response transformation',
500,
{
[TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(destinationResponse.status),
},
destinationResponse,
);
}
errorResponseHandler(destinationResponse);
return {
destinationResponse: destinationResponse.response,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generateMetadata, generateProxyV1Payload } from '../../../testUtils';
import { ProxyV1TestData } from '../../../testTypes';
import { getEndPoint } from '../../../../../src/v0/destinations/fb_custom_audience/config';
import { ProxyV1TestData } from '../../../testTypes';
import { generateMetadata, generateProxyV1Payload } from '../../../testUtils';
import { statTags, testParams2 as testParams } from './business';

export const otherScenariosV1: ProxyV1TestData[] = [
Expand Down Expand Up @@ -50,4 +50,47 @@ export const otherScenariosV1: ProxyV1TestData[] = [
},
},
},
{
id: 'fbca_v1_other_scenario_2',
name: 'fb_custom_audience',
description: 'got invalid response format (not-json) from facebook',
successCriteria: 'should throw retyable error',
scenario: 'Framework',
feature: 'dataDelivery',
module: 'destination',
version: 'v1',
input: {
request: {
body: generateProxyV1Payload({
method: 'DELETE',
endpoint: getEndPoint('aud1'),
headers: {
'test-dest-response-key': 'htmlResponse',
},
params: testParams,
}),
method: 'POST',
},
},
output: {
response: {
status: 200,
body: {
output: {
status: 500,
message: 'Invalid response format (HTML) during response transformation',
statTags,
response: [
{
error:
'"<!DOCTYPE html><html> <body> <h1>My First Heading</h1><p>My first paragraph.</p> </body></html>"',
statusCode: 500,
metadata: generateMetadata(1),
},
],
},
},
},
},
},
];
118 changes: 118 additions & 0 deletions test/integrations/destinations/fb_custom_audience/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,122 @@ export const networkCallsData = [
status: 400,
},
},
{
httpReq: {
version: '1',
type: 'REST',
method: 'DELETE',
endpoint: getEndPoint('aud1'),
headers: {
'test-dest-response-key': 'htmlResponse',
},
params: {
access_token: 'ABC',
payload: {
is_raw: true,
data_source: {
sub_type: 'ANYTHING',
},
schema: [
'EMAIL',
'DOBM',
'DOBD',
'DOBY',
'PHONE',
'GEN',
'FI',
'MADID',
'ZIP',
'ST',
'COUNTRY',
],
data: [
[
'shrouti@abc.com',
'2',
'13',
'2013',
'@09432457768',
'f',
'Ms.',
'ABC',
'ZIP ',
'123abc ',
'IN',
],
],
},
},
userId: '',
body: {
JSON: {},
XML: {},
JSON_ARRAY: {},
FORM: {},
},
files: {},
},
httpRes: {
data: '<!DOCTYPE html><html> <body> <h1>My First Heading</h1><p>My first paragraph.</p> </body></html>',
status: 400,
},
},
{
httpReq: {
version: '1',
type: 'REST',
method: 'DELETE',
endpoint: getEndPoint('aud1'),
headers: {
'test-dest-response-key': 'htmlResponseType2',
},
params: {
access_token: 'ABC',
payload: {
is_raw: true,
data_source: {
sub_type: 'ANYTHING',
},
schema: [
'EMAIL',
'DOBM',
'DOBD',
'DOBY',
'PHONE',
'GEN',
'FI',
'MADID',
'ZIP',
'ST',
'COUNTRY',
],
data: [
[
'shrouti@abc.com',
'2',
'13',
'2013',
'@09432457768',
'f',
'Ms.',
'ABC',
'ZIP ',
'123abc ',
'IN',
],
],
},
},
userId: '',
body: {
JSON: {},
XML: {},
JSON_ARRAY: {},
FORM: {},
},
files: {},
},
httpRes:
'<!DOCTYPE html><html> <body> <h1>My First Heading</h1><p>My first paragraph.</p> </body></html>',
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
},
];
Loading