Skip to content

Commit

Permalink
fix: fb custom audience html response (#3402)
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishmalik authored May 27, 2024
1 parent 668d331 commit d1a2bd6
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 2 deletions.
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 htmlTags = /<(?!(!doctype\s*html|html))\b[^>]*>[\S\s]*?<\/[^>]*>/i;
return htmlTags.test(string);
};

module.exports = {
isHtmlFormat,
getContentType,
getContentCategory,
transformedPayloadData,
Expand Down
51 changes: 51 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,53 @@ describe('getContentType', () => {
expect(result).toBe(defaultValue);
});
});

describe('isHtmlFormat', () => {
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);
});

it('should return false for undefined', () => {
expect(isHtmlFormat(undefined)).toBe(false);
});

it('should return false for null', () => {
expect(isHtmlFormat(null)).toBe(false);
});

it('should return false for empty array', () => {
expect(isHtmlFormat([])).toBe(false);
});

it('should return true for html doctype', () => {
expect(
isHtmlFormat(
'<!DOCTYPE html><html lang="en"><body><div style="text-align: center; margin-top: 50px;"><h1>Sorry, something went wrong.</h1><p>We\'re working on it and we\'ll get it fixed as soon as we can.</p><p><a href="javascript:history.back()">Go Back</a></p><footer><p>Facebook &copy; 2024 &middot; <a href="https://www.facebook.com/help/">Help Center</a></p></footer></div></body></html>',
),
).toBe(true);
});

it('should return true for html', () => {
expect(
isHtmlFormat(
'<head> <title>Hello, World!</title><link rel="stylesheet" href="styles.css" /></head><body><h1 class="title">Hello World! </h1><p id="currentTime"></p><script src="script.js"></script></body></html>',
),
).toBe(true);
});

it('should return true for html', () => {
expect(
isHtmlFormat(
'<html><body><h1 class="title">Hello World! </h1><p id="currentTime"></p><script src="script.js"></script></body></html>',
),
).toBe(true);
});

it('should return false json type', () => {
expect(isHtmlFormat('{"<a>": 12, "b": "test, "arr": [1,2]}')).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),
},
],
},
},
},
},
},
];
60 changes: 60 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,64 @@ 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: [
[
'[email protected]',
'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,
},
},
];

0 comments on commit d1a2bd6

Please sign in to comment.