-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_test.ts
More file actions
78 lines (71 loc) · 2.4 KB
/
utils_test.ts
File metadata and controls
78 lines (71 loc) · 2.4 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
import { describe, it } from '@std/testing/bdd'
import { assertEquals, assertThrows } from '@std/assert'
import { fromEnv, generateFilebaseRequestOptions, parseUrl, presignRequest, type RequestOptions } 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)
})
})
})
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, '')
const amzShortDate = amzDate.slice(0, 8)
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/${amzShortDate}/us-east-1/s3/aws4_request`,
'X-Amz-Date': amzDate,
'X-Amz-Expires': '3600',
'X-Amz-SignedHeaders': 'host',
})
assertEquals(signedRequest.headers, {})
})
})
describe('generateFilebaseRequestOptions', () => {
it('should throw if no token is provided', () => {
assertThrows(() => generateFilebaseRequestOptions('lol', {} as RequestOptions))
})
})
describe('fromEnv', () => {
it('should throw if no token is provided', () => {
assertThrows(() => fromEnv('lol')())
})
})