Skip to content

Commit 3b99d45

Browse files
fix: fixed tests to be consistent with other sdks
1 parent af74827 commit 3b99d45

File tree

5 files changed

+151
-102
lines changed

5 files changed

+151
-102
lines changed

lib/utils/index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,17 @@ function process_layer(layer) {
285285
}
286286

287287
if (!isEmpty(text)) {
288-
let re = /\$\([a-zA-Z]\w*\)/g;
289-
let start = 0;
290-
let textSource = smart_escape(decodeURIComponent(text), /[,\/]/g);
291-
text = "";
292-
for (let res = re.exec(textSource); res; res = re.exec(textSource)) {
293-
text += smart_escape(textSource.slice(start, res.index));
294-
text += res[0];
295-
start = res.index + res[0].length;
296-
}
297-
text += encodeURIComponent(textSource.slice(start));
298-
components.push(text);
288+
const variablesRegex = new RegExp(/(\$\([a-zA-Z]\w+\))/g);
289+
const textDividedByVariables = text.split(variablesRegex).filter(x => x);
290+
const encodedParts = textDividedByVariables.map(subText => {
291+
const matches = variablesRegex[Symbol.match](subText);
292+
const isVariable = matches ? matches.length > 0 : false;
293+
if (isVariable) {
294+
return subText;
295+
}
296+
return encodeCurlyBraces(encodeURIComponent(smart_escape(subText, new RegExp(/([,\/])/g))));
297+
});
298+
components.push(encodedParts.join(''));
299299
}
300300
} else if (type === 'fetch') {
301301
const encodedUrl = base64EncodeURL(fetchUrl);
@@ -308,6 +308,10 @@ function process_layer(layer) {
308308
return components.join(':');
309309
}
310310

311+
function encodeCurlyBraces(input) {
312+
return input.replaceAll('(', '%28').replaceAll(')', '%29');
313+
}
314+
311315
/**
312316
* Parse radius options
313317
* @private
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const assert = require('assert');
2+
const cloudinary = require('../../../lib/cloudinary');
3+
4+
describe('URL utils with transformation parameters', () => {
5+
const CLOUD_NAME = 'test-cloud';
6+
7+
before(() => {
8+
cloudinary.config({
9+
cloud_name: CLOUD_NAME
10+
});
11+
});
12+
13+
it('should create a correct link with overlay string', () => {
14+
const url = cloudinary.utils.url('test', {
15+
overlay: {
16+
font_family: "arial",
17+
font_size: "30",
18+
text: "abc,αβγ/אבג"
19+
}
20+
});
21+
22+
assert.strictEqual(url, `http://res.cloudinary.com/${CLOUD_NAME}/image/upload/l_text:arial_30:abc%252C%CE%B1%CE%B2%CE%B3%252F%D7%90%D7%91%D7%92/test`);
23+
});
24+
25+
it('should create a correct link with underlay string', () => {
26+
const url = cloudinary.utils.url('test', {
27+
underlay: {
28+
font_family: "arial",
29+
font_size: "30",
30+
text: "abc,αβγ/אבג"
31+
}
32+
});
33+
34+
assert.strictEqual(url, `http://res.cloudinary.com/${CLOUD_NAME}/image/upload/u_text:arial_30:abc%252C%CE%B1%CE%B2%CE%B3%252F%D7%90%D7%91%D7%92/test`);
35+
});
36+
});

test/unit/url/sign_url_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe("URL for authenticated asset", () => {
7676
assert.strictEqual(signedUrl, 'http://res.cloudinary.com/test123/image/fetch/s--hH_YcbiS--/v1234/http://google.com/path/to/image.png');
7777
});
7878

79-
it.only('should have signature for authenticated asset with text overlay transformation including encoded emoji', () => {
79+
it('should have signature for authenticated asset with text overlay transformation including encoded emoji', () => {
8080
const signedUrl = cloudinary.utils.url(TEST_PUBLIC_ID, {
8181
type: 'authenticated',
8282
sign_url: true,
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const assert = require('assert');
2+
const cloudinary = require('../../../lib/cloudinary');
3+
4+
describe('User Define Variables', function () {
5+
const CLOUD_NAME = 'test';
6+
7+
before(() => {
8+
cloudinary.config({
9+
cloud_name: CLOUD_NAME
10+
});
11+
});
12+
13+
it('array should define a set of variables', function () {
14+
var options, t;
15+
options = {
16+
if: 'face_count > 2',
17+
variables: [['$z', 5], ['$foo', '$z * 2']],
18+
crop: 'scale',
19+
width: '$foo * 200'
20+
};
21+
t = cloudinary.utils.generate_transformation_string(options);
22+
expect(t).to.eql('if_fc_gt_2,$z_5,$foo_$z_mul_2,c_scale,w_$foo_mul_200');
23+
});
24+
25+
it('"$key" should define a variable', function () {
26+
var options, t;
27+
options = {
28+
transformation: [
29+
{
30+
$foo: 10
31+
},
32+
{
33+
if: 'face_count > 2'
34+
},
35+
{
36+
crop: 'scale',
37+
width: '$foo * 200 / face_count'
38+
},
39+
{
40+
if: 'end'
41+
}
42+
]
43+
};
44+
t = cloudinary.utils.generate_transformation_string(options);
45+
expect(t).to.eql('$foo_10/if_fc_gt_2/c_scale,w_$foo_mul_200_div_fc/if_end');
46+
});
47+
48+
it('should support power operator', function () {
49+
var options, t;
50+
options = {
51+
transformation: [
52+
{
53+
$small: 150,
54+
$big: '$small ^ 1.5'
55+
}
56+
]
57+
};
58+
t = cloudinary.utils.generate_transformation_string(options);
59+
expect(t).to.eql('$big_$small_pow_1.5,$small_150');
60+
});
61+
62+
it('should not change variable names even if they look like keywords', function () {
63+
var options, t;
64+
options = {
65+
transformation: [
66+
{
67+
$width: 10
68+
},
69+
{
70+
width: '$width + 10 + width'
71+
}
72+
]
73+
};
74+
t = cloudinary.utils.generate_transformation_string(options);
75+
expect(t).to.eql('$width_10/w_$width_add_10_add_w');
76+
});
77+
78+
it('should support text values', function () {
79+
const url = cloudinary.utils.url('sample', {
80+
effect: '$efname:100',
81+
$efname: '!blur!'
82+
});
83+
84+
assert.strictEqual(url, `http://res.cloudinary.com/${CLOUD_NAME}/image/upload/$efname_!blur!,e_$efname:100/sample`);
85+
});
86+
87+
it('should support string interpolation', function () {
88+
const url = cloudinary.utils.url('sample', {
89+
crop: 'scale',
90+
overlay: {
91+
text: '$(start)Hello $(name)$(ext), $(no ) $( no)$(end)',
92+
font_family: 'Arial',
93+
font_size: '18'
94+
}
95+
});
96+
97+
assert.strictEqual(url, `http://res.cloudinary.com/${CLOUD_NAME}/image/upload/c_scale,l_text:Arial_18:$(start)Hello%20$(name)$(ext)%252C%20%24%28no%20%29%20%24%28%20no%29$(end)/sample`);
98+
});
99+
});

test/utils/utils_spec.js

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,6 @@ describe("utils", function () {
717717
["string",
718718
"text:hello",
719719
"text:hello"],
720-
[
721-
"string",
722-
{
723-
"font_family": "arial",
724-
"font_size": "30",
725-
"text": "abc,αβγ/אבג"
726-
},
727-
"text:arial_30:abc%252C%CE%B1%CE%B2%CE%B3%252F%D7%90%D7%91%D7%92"
728-
],
729720
[
730721
"public_id",
731722
{
@@ -1066,87 +1057,6 @@ describe("utils", function () {
10661057
.to.contain('$xpos_ctx:!x_pos!_to_f,$ypos_ctx:!y_pos!_to_f,c_crop,x_$xpos_mul_w,y_$ypos_mul_h')
10671058
});
10681059
});
1069-
describe('User Define Variables', function () {
1070-
it("array should define a set of variables", function () {
1071-
var options, t;
1072-
options = {
1073-
if: "face_count > 2",
1074-
variables: [["$z", 5], ["$foo", "$z * 2"]],
1075-
crop: "scale",
1076-
width: "$foo * 200"
1077-
};
1078-
t = cloudinary.utils.generate_transformation_string(options);
1079-
expect(t).to.eql("if_fc_gt_2,$z_5,$foo_$z_mul_2,c_scale,w_$foo_mul_200");
1080-
});
1081-
it("'$key' should define a variable", function () {
1082-
var options, t;
1083-
options = {
1084-
transformation: [
1085-
{
1086-
$foo: 10
1087-
},
1088-
{
1089-
if: "face_count > 2"
1090-
},
1091-
{
1092-
crop: "scale",
1093-
width: "$foo * 200 / face_count"
1094-
},
1095-
{
1096-
if: "end"
1097-
}
1098-
]
1099-
};
1100-
t = cloudinary.utils.generate_transformation_string(options);
1101-
expect(t).to.eql("$foo_10/if_fc_gt_2/c_scale,w_$foo_mul_200_div_fc/if_end");
1102-
});
1103-
1104-
it("should not change variable names even if they look like keywords", function () {
1105-
var options, t;
1106-
options = {
1107-
transformation: [
1108-
{
1109-
$width: 10
1110-
},
1111-
{
1112-
width: "$width + 10 + width"
1113-
}
1114-
]
1115-
};
1116-
t = cloudinary.utils.generate_transformation_string(options);
1117-
expect(t).to.eql("$width_10/w_$width_add_10_add_w");
1118-
});
1119-
1120-
it("should support text values", function () {
1121-
test_cloudinary_url("sample", {
1122-
effect: "$efname:100",
1123-
$efname: "!blur!"
1124-
}, `http://res.cloudinary.com/${cloud_name}/image/upload/$efname_!blur!,e_$efname:100/sample`, {});
1125-
});
1126-
it("should support string interpolation", function () {
1127-
test_cloudinary_url("sample", {
1128-
crop: "scale",
1129-
overlay: {
1130-
text: "$(start)Hello $(name)$(ext), $(no ) $( no)$(end)",
1131-
font_family: "Arial",
1132-
font_size: "18"
1133-
}
1134-
}, `http://res.cloudinary.com/${cloud_name}/image/upload/c_scale,l_text:Arial_18:$(start)Hello%20$(name)$(ext)%252C%20%24%28no%20%29%20%24%28%20no%29$(end)/sample`, {});
1135-
});
1136-
it("should support power operator", function () {
1137-
var options, t;
1138-
options = {
1139-
transformation: [
1140-
{
1141-
$small: 150,
1142-
$big: "$small ^ 1.5"
1143-
}
1144-
]
1145-
};
1146-
t = cloudinary.utils.generate_transformation_string(options);
1147-
expect(t).to.eql("$big_$small_pow_1.5,$small_150");
1148-
});
1149-
});
11501060
describe("text", function () {
11511061
this.timeout(TIMEOUT.MEDIUM);
11521062
var text_encoded, text_layer;

0 commit comments

Comments
 (0)