-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: update the unit tests for
fromTraffic
- Loading branch information
Showing
7 changed files
with
65 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { encodeBase64String } from '../encodeBase64String' | ||
import { decodeBase64String } from '../decodeBase64String' | ||
import { fromByteArray } from '../fromByteArray' | ||
|
||
describe('base64strings', () => { | ||
test('should be able to decode base64 string', () => { | ||
const base64String = 'aGVsbG8gd29ybGQ=' | ||
const decodedString = decodeBase64String(base64String) | ||
const asString = fromByteArray(decodedString) | ||
expect(asString).toEqual('hello world') | ||
}) | ||
|
||
test('should be able to encode string to base64', () => { | ||
const input = 'hello world' | ||
const base64String = 'aGVsbG8gd29ybGQ=' | ||
const encodedString = encodeBase64String(input) | ||
const asString = fromByteArray(encodedString) | ||
expect(asString).toEqual(base64String) | ||
}) | ||
|
||
}) |
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
export function decodeBase64String(data: string): Uint8Array { | ||
const binaryString = atob(data) | ||
Check failure on line 2 in src/fromTraffic/utils/decodeBase64String.ts GitHub Actions / buildtest/traffic/fromTraffic.test.ts > toResponseBody > returns a decoded text body given a base64-encoded response body
|
||
const bytes = new Uint8Array(binaryString.length) | ||
for (let i = 0; i < binaryString.length; i++) { | ||
bytes[i] = binaryString.charCodeAt(i) | ||
} | ||
const encoder = new TextEncoder() | ||
const bytes = encoder.encode(binaryString) | ||
return bytes | ||
|
||
} |
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,6 @@ | ||
export function encodeBase64String(data: string): Uint8Array { | ||
const binaryString = btoa(data) | ||
const encoder = new TextEncoder() | ||
const bytes = encoder.encode(binaryString) | ||
return bytes | ||
} |
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,4 @@ | ||
export function fromByteArray(bytes: Uint8Array) { | ||
const decoder = new TextDecoder().decode(bytes) | ||
return decoder.toString() | ||
} |
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,13 @@ | ||
export function isEqualBytes(bytes1: Uint8Array, bytes2: Uint8Array): boolean { | ||
if (bytes1.length !== bytes2.length) { | ||
return false | ||
} | ||
|
||
for (let i = 0; i < bytes1.length; i++) { | ||
if (bytes1[i] !== bytes2[i]) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
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