-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.ts
More file actions
173 lines (164 loc) · 4.66 KB
/
Copy pathauthentication.ts
File metadata and controls
173 lines (164 loc) · 4.66 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import fetch from '@root/index'
import { handleError } from './utils'
/** HTTPBin.org base URL for testing authentication */
const httpbinUrl: string = 'https://httpbin.org'
/** HTTPBin.org base URL for testing authentication */
const httpBaseAuth: string = 'f3tch1mpl3ment4t1on'
/**
* Tests basic authentication functionality.
* @description Demonstrates username/password authentication using httpbin basic auth endpoint.
* @returns Promise that resolves when test completes
*/
async function authBasic(): Promise<void> {
try {
console.log(
'-> Basic Authentication',
await fetch.get(`${httpbinUrl}/basic-auth/${httpBaseAuth}/${httpBaseAuth}`, {
auth: {
type: 'basic',
username: httpBaseAuth,
password: httpBaseAuth
}
})
)
} catch (error: unknown) {
handleError(error, 'Basic Authentication')
}
}
/**
* Tests bearer token authentication functionality.
* @description Demonstrates JWT/OAuth token authentication using httpbin bearer endpoint.
* @returns Promise that resolves when test completes
*/
async function authBearer(): Promise<void> {
try {
console.log(
'-> Bearer Token Authentication',
await fetch.get(`${httpbinUrl}/bearer`, {
auth: {
type: 'bearer',
token: 'test-bearer-token-123'
}
})
)
} catch (error: unknown) {
handleError(error, 'Bearer Token Authentication')
}
}
/**
* Tests API key authentication functionality.
* @description Demonstrates custom header API key authentication using httpbin headers endpoint.
* @returns Promise that resolves when test completes
*/
async function authApiKey(): Promise<void> {
try {
console.log(
'-> API Key Authentication',
await fetch.get(`${httpbinUrl}/headers`, {
auth: {
type: 'apikey',
key: 'X-API-Key',
value: 'test-api-key-456'
}
})
)
} catch (error: unknown) {
handleError(error, 'API Key Authentication')
}
}
/**
* Tests custom API key header authentication functionality.
* @description Demonstrates custom API key header authentication with different header names.
* @returns Promise that resolves when test completes
*/
async function authApiKeyCustom(): Promise<void> {
try {
console.log(
'-> Custom API Key Authentication',
await fetch.get(`${httpbinUrl}/headers`, {
auth: {
type: 'apikey',
key: 'X-Custom-Auth',
value: 'custom-key-789'
}
})
)
} catch (error: unknown) {
handleError(error, 'Custom API Key Authentication')
}
}
/**
* Tests authentication with POST request functionality.
* @description Demonstrates authentication combined with POST requests and body data.
* @returns Promise that resolves when test completes
*/
async function authWithPost(): Promise<void> {
try {
console.log(
'-> Authentication with POST',
await fetch.post(
`${httpbinUrl}/post`,
{
name: 'John Doe',
email: 'john@example.com'
},
{
auth: {
type: 'bearer',
token: 'post-test-token'
}
}
)
)
} catch (error: unknown) {
handleError(error, 'Authentication with POST')
}
}
/**
* Tests authentication error handling functionality.
* @description Demonstrates how authentication errors are handled with invalid credentials.
* @returns Promise that resolves when test completes
*/
async function authErrorHandling(): Promise<void> {
try {
console.log(
'-> Authentication Error Handling',
await fetch.get(`${httpbinUrl}/basic-auth/testuser/testpass`, {
auth: {
type: 'basic',
username: httpBaseAuth,
password: httpBaseAuth
}
})
)
} catch (error: unknown) {
handleError(error, 'Authentication Error Handling')
}
}
/**
* Executes all authentication test functions in sequence.
* @description Runs all test functions with progress logging and error handling.
* @returns Promise that resolves when all tests complete
*/
async function runAllTests(): Promise<void> {
const listTests: Array<() => Promise<void>> = [
authBasic,
authBearer,
authApiKey,
authApiKeyCustom,
authWithPost,
authErrorHandling
]
for (const [index, testTask] of listTests.entries()) {
console.log(`[${index + 1}] Working on "${testTask.name}" Function...`)
await testTask()
console.log('-\n-')
}
}
/**
* Main function to execute all tests when run directly.
* @description Checks if the script is being run directly and executes all tests.
*/
if (import.meta.url === `file://${process.argv[1]}`) {
runAllTests().catch(console.error)
}