-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.ts
80 lines (73 loc) · 2.42 KB
/
utils_test.ts
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
import { describe, it } from '@std/testing/bdd'
import { assertEquals, assertThrows } from '@std/assert'
import { parseUrl, presignRequest, toUint8Array } from './utils.ts'
import type { HttpRequest } from '@smithy/types'
import { assertObjectMatch } from '@std/assert/object-match'
describe('parseUrl', () => {
describe('should return the correct parsed URL', () => {
it('with protocol', () => {
const url = 'https://example.com/path'
const parsedUrl = parseUrl(url)
assertEquals(parsedUrl.protocol, 'https:')
assertEquals(parsedUrl.hostname, 'example.com')
})
it('with port', () => {
const url = 'http://example.com:8080/path'
const parsedUrl = parseUrl(url)
assertEquals(parsedUrl.port, 8080)
})
})
it('should throw error for invalid URL', () => {
const url = 'not a valid url'
assertThrows(() => {
parseUrl(url)
})
})
})
const te = new TextEncoder()
describe('toUint8Array', () => {
it('should convert a UTF-8 string to Uint8Array', () => {
const input = 'Hello, World!'
const uint8Array = toUint8Array(input)
assertEquals(uint8Array, te.encode(input))
})
it('should convert an ArrayBuffer to Uint8Array', () => {
const buffer = new ArrayBuffer(8)
const uint8Array = toUint8Array(buffer)
assertEquals(uint8Array, new Uint8Array(buffer))
})
})
describe('presignRequest', () => {
it('should return a signed request', () => {
const request: HttpRequest = {
method: 'GET',
hostname: 'example.com',
path: '/path',
query: {},
headers: {},
username: '',
password: '',
fragment: '',
protocol: 'https:',
}
const signedRequest = presignRequest(request, {
accessKeyId: 'AKIAIOSFODNN7EXAMPLE',
secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
region: 'us-east-1',
expiresIn: 3600,
})
const now = new Date()
const amzDate = now.toISOString().replace(/[:-]|\.\d{3}/g, '')
assertEquals(signedRequest.method, 'GET')
assertEquals(signedRequest.hostname, 'example.com')
assertEquals(signedRequest.path, '/path')
assertObjectMatch(signedRequest.query!, {
'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
'X-Amz-Credential': 'AKIAIOSFODNN7EXAMPLE/20250419/us-east-1/s3/aws4_request',
'X-Amz-Date': amzDate,
'X-Amz-Expires': '3600',
'X-Amz-SignedHeaders': 'host',
})
assertEquals(signedRequest.headers, {})
})
})