-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* bugfix #5218: de/encode custom state using base16 * use aws-sdk hex encoding util unit tests for url safe encoding/decoding * avoid using TextEncoder to support IE11 Co-authored-by: Sam Martinez <samlmar@amazon.com>
- Loading branch information
1 parent
df83573
commit 8cc5587
Showing
5 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { urlSafeEncode, urlSafeDecode } from '../src/Util'; | ||
import { TextDecoder, TextEncoder } from 'util'; | ||
global.TextEncoder = TextEncoder; | ||
global.TextDecoder = TextDecoder; | ||
|
||
const complexCustomState = 'https://amplify-app:300/?empty=&list=1,a,%,@'; | ||
|
||
describe('StringUtils', () => { | ||
test('urlSafe encoding', () => { | ||
const urlSafeState = urlSafeEncode(complexCustomState); | ||
expect(encodeURIComponent(urlSafeState)).toEqual(urlSafeState); | ||
}); | ||
test('urlSafe decoding', () => { | ||
const urlSafeState = urlSafeEncode(complexCustomState); | ||
const encodedState = encodeURIComponent(urlSafeState); | ||
|
||
expect(decodeURIComponent(encodedState)).toEqual(encodedState); | ||
}); | ||
test('URI encode/decode with url safe state', () => { | ||
const urlSafeState = urlSafeEncode(complexCustomState); | ||
const encodedState = encodeURIComponent(urlSafeState); | ||
|
||
expect(urlSafeDecode(decodeURIComponent(encodedState))).toEqual( | ||
complexCustomState | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function urlSafeEncode(str: string) { | ||
return str | ||
.split('') | ||
.map(char => | ||
char | ||
.charCodeAt(0) | ||
.toString(16) | ||
.padStart(2, '0') | ||
) | ||
.join(''); | ||
} | ||
|
||
export function urlSafeDecode(hex: string) { | ||
return hex | ||
.match(/.{2}/g) | ||
.map(char => String.fromCharCode(parseInt(char, 16))) | ||
.join(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters