Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for buffer.ts #3004

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/utils/buffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import { SHA256 as sha256CryptoJS } from 'crypto-js'
import { bufferToFormData, bufferToString, timingSafeEqual } from './buffer'
import { bufferToFormData, bufferToString, equal, timingSafeEqual } from './buffer'
import { expect } from 'vitest'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary? I think the expect function is already imported globally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I've deleted this import.


describe('equal', () => {
it('should return true for identical ArrayBuffers', () => {
const buffer1 = new ArrayBuffer(1)
const buffer2 = buffer1
expect(equal(buffer1, buffer2)).toBe(true)
})

it('should return false for ArrayBuffers of different lengths', () => {
const buffer1 = new ArrayBuffer(1)
const buffer2 = new ArrayBuffer(2)
expect(equal(buffer1, buffer2)).toBe(false)
})

it('should return false for ArrayBuffers with different content', () => {
const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer
const buffer2 = new Uint8Array([2, 2, 3, 4]).buffer
expect(equal(buffer1, buffer2)).toBe(false)
})

it('should return true for ArrayBuffers with identical content', () => {
const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer
const buffer2 = new Uint8Array([1, 2, 3, 4]).buffer
expect(equal(buffer1, buffer2)).toBe(true)
})
})

describe('buffer', () => {
it('positive', async () => {
Expand Down Expand Up @@ -43,6 +70,7 @@ describe('buffer', () => {
expect(await timingSafeEqual({ a: 1 }, { a: 2 })).toBe(false)
expect(await timingSafeEqual([1, 2], [1, 2])).toBe(false)
expect(await timingSafeEqual([1, 2], [1, 2, 3])).toBe(false)
expect(await timingSafeEqual('a', 'b', () => undefined)).toBe(false)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test covering the first if statement branch

hono/src/utils/buffer.ts

Lines 41 to 43 in 7ee829b

if (!sa || !sb) {
return false
}

})
})

Expand All @@ -52,6 +80,10 @@ describe('bufferToString', () => {
const buffer = Uint8Array.from(bytes).buffer
expect(bufferToString(buffer)).toBe('あいうえお')
})
it('should return the passed arguments as is ', () => {
const notBuffer = 'あいうえお' as unknown as ArrayBuffer
expect(bufferToString(notBuffer)).toBe('あいうえお')
})
})

describe('bufferToFormData', () => {
Expand Down