Skip to content

Commit

Permalink
test: update the unit tests for fromTraffic
Browse files Browse the repository at this point in the history
  • Loading branch information
weyert committed May 26, 2024
1 parent 83977b5 commit 97d1f80
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 16 deletions.
21 changes: 21 additions & 0 deletions src/fromTraffic/utils/__tests__/base64strings.test.ts
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)
})

})
7 changes: 3 additions & 4 deletions src/fromTraffic/utils/decodeBase64String.ts
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

View workflow job for this annotation

GitHub Actions / build

test/traffic/fromTraffic.test.ts > toResponseBody > returns a decoded text body given a base64-encoded response body

InvalidCharacterError: Invalid character ❯ atob node:buffer:1269:13 ❯ Module.decodeBase64String src/fromTraffic/utils/decodeBase64String.ts:2:24 ❯ test/traffic/fromTraffic.test.ts:117:26 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 5, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2, HIERARCHY_REQUEST_ERR: 3, WRONG_DOCUMENT_ERR: 4, INVALID_CHARACTER_ERR: 5, NO_DATA_ALLOWED_ERR: 6, NO_MODIFICATION_ALLOWED_ERR: 7, NOT_FOUND_ERR: 8, NOT_SUPPORTED_ERR: 9, INUSE_ATTRIBUTE_ERR: 10, INVALID_STATE_ERR: 11, SYNTAX_ERR: 12, INVALID_MODIFICATION_ERR: 13, NAMESPACE_ERR: 14, INVALID_ACCESS_ERR: 15, VALIDATION_ERR: 16, TYPE_MISMATCH_ERR: 17, SECURITY_ERR: 18, NETWORK_ERR: 19, ABORT_ERR: 20, URL_MISMATCH_ERR: 21, QUOTA_EXCEEDED_ERR: 22, TIMEOUT_ERR: 23, INVALID_NODE_TYPE_ERR: 24, DATA_CLONE_ERR: 25 }
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

}
6 changes: 6 additions & 0 deletions src/fromTraffic/utils/encodeBase64String.ts
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
}
4 changes: 4 additions & 0 deletions src/fromTraffic/utils/fromByteArray.ts
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()
}
22 changes: 13 additions & 9 deletions src/fromTraffic/utils/harUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { encodeBase64String } from './encodeBase64String'
import { isEqualBytes } from './isEqualBytes'
import { toHeaders, toResponse, toResponseBody } from './harUtils'

describe(toHeaders, () => {
Expand Down Expand Up @@ -61,15 +63,17 @@ describe(toResponseBody, () => {
})

it('decodes the base64-encoded response body', () => {
expect(
toResponseBody({
text: btoa('hello world'),
size: 11,
mimeType: 'text/plain',
encoding: 'base64',
}),
).toEqual(Uint8Array.from('hello world', (c) => c.charCodeAt(0)))
})
const bodyBytes = Uint8Array.from('hello world', (c) => c.charCodeAt(0))
const responseBody = toResponseBody({
text: btoa('hello world'),
size: 11,
mimeType: 'text/plain',
encoding: 'base64',
})

expect(responseBody).toBeDefined()
expect(isEqualBytes(bodyBytes, responseBody as Uint8Array)).toBe(true)
})

it.todo('handles a compressed response body')
})
Expand Down
13 changes: 13 additions & 0 deletions src/fromTraffic/utils/isEqualBytes.ts
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
}
8 changes: 5 additions & 3 deletions test/traffic/fromTraffic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('fromTraffic', () => {
)

expect(handlers).toHaveLength(1)
expect(handlers[0].info.header).toEqual('GET https://api.stripe.com')
expect(handlers[0].info.header).toEqual('get https://api.stripe.com')
})
})

Expand Down Expand Up @@ -123,7 +123,9 @@ describe('toResponseBody', () => {
).toEqual(expectedBody)
})

it('returns a plain text response body as-is', () => {
expect(toResponse(createResponse('hello world'))).toEqual('hello world')
it('returns a plain text response body as-is', async () => {
const bodyResponse = toResponse(createResponse('hello world'))
const textOfResponse = await bodyResponse.text()
expect(textOfResponse).toEqual('hello world')
})
})

0 comments on commit 97d1f80

Please sign in to comment.