|
1 |
| -import { App, CfnOutput, Fn, IPostProcessor, IResolvable, IResolveContext, Lazy, Stack, Token } from '../lib'; |
| 1 | +import { App, Aws, CfnOutput, Fn, IPostProcessor, IResolvable, IResolveContext, Lazy, Stack, Token } from '../lib'; |
2 | 2 | import { Intrinsic } from '../lib/private/intrinsic';
|
3 | 3 | import { evaluateCFN } from './evaluate-cfn';
|
4 | 4 |
|
@@ -196,6 +196,35 @@ describe('tokens returning CloudFormation intrinsics', () => {
|
196 | 196 | expect(evaluateCFN(resolved, context)).toEqual(expected);
|
197 | 197 | });
|
198 | 198 |
|
| 199 | + test('embedded string literals are escaped in Fn.sub (implicit references)', () => { |
| 200 | + // GIVEN |
| 201 | + const token = Fn.sub('I am in account "${AWS::AccountId}"'); |
| 202 | + |
| 203 | + // WHEN |
| 204 | + const resolved = stack.resolve(stack.toJsonString({ token })); |
| 205 | + |
| 206 | + // THEN |
| 207 | + const context = { 'AWS::AccountId': '1234' }; |
| 208 | + const expected = '{"token":"I am in account \\"1234\\""}'; |
| 209 | + expect(evaluateCFN(resolved, context)).toEqual(expected); |
| 210 | + }); |
| 211 | + |
| 212 | + test('embedded string literals are escaped in Fn.sub (explicit references)', () => { |
| 213 | + // GIVEN |
| 214 | + const token = Fn.sub('I am in account "${Acct}", also wanted to say: ${Also}', { |
| 215 | + Acct: Aws.ACCOUNT_ID, |
| 216 | + Also: '"hello world"', |
| 217 | + }); |
| 218 | + |
| 219 | + // WHEN |
| 220 | + const resolved = stack.resolve(stack.toJsonString({ token })); |
| 221 | + |
| 222 | + // THEN |
| 223 | + const context = { 'AWS::AccountId': '1234' }; |
| 224 | + const expected = '{"token":"I am in account \\"1234\\", also wanted to say: \\"hello world\\""}'; |
| 225 | + expect(evaluateCFN(resolved, context)).toEqual(expected); |
| 226 | + }); |
| 227 | + |
199 | 228 | test('Tokens in Tokens are handled correctly', () => {
|
200 | 229 | // GIVEN
|
201 | 230 | const bucketName = new Intrinsic({ Ref: 'MyBucket' });
|
@@ -344,6 +373,26 @@ describe('tokens returning CloudFormation intrinsics', () => {
|
344 | 373 | });
|
345 | 374 | });
|
346 | 375 |
|
| 376 | +test('JSON strings nested inside JSON strings have correct quoting', () => { |
| 377 | + // GIVEN |
| 378 | + const payload = stack.toJsonString({ |
| 379 | + message: Fn.sub('I am in account "${AWS::AccountId}"'), |
| 380 | + }); |
| 381 | + |
| 382 | + // WHEN |
| 383 | + const resolved = stack.resolve(stack.toJsonString({ payload })); |
| 384 | + |
| 385 | + // THEN |
| 386 | + const context = { 'AWS::AccountId': '1234' }; |
| 387 | + const expected = '{"payload":"{\\"message\\":\\"I am in account \\\\\\"1234\\\\\\"\\"}"}'; |
| 388 | + const evaluated = evaluateCFN(resolved, context); |
| 389 | + expect(evaluated).toEqual(expected); |
| 390 | + |
| 391 | + // Is this even correct? Let's ask JavaScript because I have trouble reading this many backslashes. |
| 392 | + expect(JSON.parse(JSON.parse(evaluated).payload).message).toEqual('I am in account "1234"'); |
| 393 | +}); |
| 394 | + |
| 395 | + |
347 | 396 | /**
|
348 | 397 | * Return two Tokens, one of which evaluates to a Token directly, one which evaluates to it lazily
|
349 | 398 | */
|
|
0 commit comments