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 all commits
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 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', () => {
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
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,
},
},
];
Loading