-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdedupeRequestHash.test.ts
More file actions
143 lines (126 loc) · 4.21 KB
/
Copy pathdedupeRequestHash.test.ts
File metadata and controls
143 lines (126 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { describe, it, expect, afterEach } from 'vitest'
import {
dedupeRequestHash,
DedupeHashParams,
} from '../../src/dedupeRequestHash'
describe('dedupeRequestHash', () => {
const originalBuffer = (globalThis as { Buffer?: unknown }).Buffer
const originalBtoa = (globalThis as { btoa?: unknown }).btoa
afterEach(() => {
;(globalThis as { Buffer?: unknown }).Buffer = originalBuffer
;(globalThis as { btoa?: unknown }).btoa = originalBtoa
})
it('returns undefined for FormData body', () => {
const form = new FormData()
form.append('foo', 'bar')
const params: DedupeHashParams = {
method: 'POST',
url: 'https://example.com',
body: form,
}
expect(dedupeRequestHash(params)).toBeUndefined()
})
it('returns undefined for ReadableStream body', () => {
// Only run this test if ReadableStream is available
if (typeof ReadableStream !== 'undefined') {
const stream = new ReadableStream()
const params: DedupeHashParams = {
method: 'POST',
url: 'https://example.com',
body: stream,
}
expect(dedupeRequestHash(params)).toBeUndefined()
}
})
it('hashes GET with string body', () => {
const params: DedupeHashParams = {
method: 'GET',
url: 'https://example.com',
body: 'foo',
}
expect(dedupeRequestHash(params)).toBe('GET|https://example.com|foo')
})
it('hashes POST with URLSearchParams body', () => {
const params: DedupeHashParams = {
method: 'POST',
url: 'https://example.com',
body: new URLSearchParams({ a: '1', b: '2' }),
}
expect(dedupeRequestHash(params)).toBe('POST|https://example.com|a=1&b=2')
})
it('hashes PUT with ArrayBuffer body', () => {
const buf = new TextEncoder().encode('abc').buffer
const params: DedupeHashParams = {
method: 'PUT',
url: 'https://example.com',
body: buf,
}
expect(dedupeRequestHash(params)).toMatch(/^PUT\|https:\/\/example.com\|/)
})
it('hashes PATCH with Uint8Array body', () => {
const arr = new Uint8Array([1, 2, 3])
const params: DedupeHashParams = {
method: 'PATCH',
url: 'https://example.com',
body: arr,
}
expect(dedupeRequestHash(params)).toMatch(/^PATCH\|https:\/\/example.com\|/)
})
it('hashes POST with Blob body', () => {
const blob = new Blob(['hello'], { type: 'text/plain' })
const params: DedupeHashParams = {
method: 'POST',
url: 'https://example.com',
body: blob,
}
expect(dedupeRequestHash(params)).toBe(
`POST|https://example.com|[blob:text/plain:${blob.size}]`
)
})
it('hashes DELETE with null body', () => {
const params: DedupeHashParams = {
method: 'DELETE',
url: 'https://example.com',
body: null,
}
expect(dedupeRequestHash(params)).toBe('DELETE|https://example.com|')
})
it('dedupeRequestHash handles unserializable body (circular reference)', () => {
type Circular = { self?: Circular }
const circular: Circular = {}
circular.self = circular
const params = {
method: 'POST',
url: 'http://example.com',
body: circular as DedupeHashParams['body'],
}
const hash = dedupeRequestHash(params)
expect(hash).toContain('[unserializable-body]')
})
it('falls back to btoa when Buffer is unavailable', () => {
;(globalThis as { Buffer?: unknown }).Buffer = undefined
;(globalThis as { btoa?: (input: string) => string }).btoa = (input) => {
if (input === 'abc') return 'YWJj'
throw new Error('unexpected btoa input')
}
const buf = new Uint8Array([97, 98, 99]).buffer
const params: DedupeHashParams = {
method: 'PUT',
url: 'https://example.com',
body: buf,
}
expect(dedupeRequestHash(params)).toBe('PUT|https://example.com|YWJj')
})
it('throws when neither Buffer nor btoa are available', () => {
;(globalThis as { Buffer?: unknown }).Buffer = undefined
;(globalThis as { btoa?: unknown }).btoa = undefined
const params: DedupeHashParams = {
method: 'PATCH',
url: 'https://example.com',
body: new Uint8Array([1, 2, 3]),
}
expect(() => dedupeRequestHash(params)).toThrow(
'Base64 encoding is not available in this runtime'
)
})
})