1
1
import axios from "axios" ;
2
2
import * as fs from "fs/promises" ;
3
3
import https from "https" ;
4
- import * as path from "path" ;
5
4
import { afterAll , beforeAll , describe , expect , it , vi } from "vitest" ;
6
5
7
6
import { CertificateError , X509_ERR , X509_ERR_CODE } from "@/error" ;
8
7
import { type Logger } from "@/logging/logger" ;
9
8
9
+ import { getFixturePath } from "../utils/fixtures" ;
10
+
10
11
describe ( "Certificate errors" , ( ) => {
11
12
// Before each test we make a request to sanity check that we really get the
12
13
// error we are expecting, then we run it through CertificateError.
@@ -46,12 +47,8 @@ describe("Certificate errors", () => {
46
47
async function startServer ( certName : string ) : Promise < string > {
47
48
const server = https . createServer (
48
49
{
49
- key : await fs . readFile (
50
- path . join ( __dirname , `../fixtures/tls/${ certName } .key` ) ,
51
- ) ,
52
- cert : await fs . readFile (
53
- path . join ( __dirname , `../fixtures/tls/${ certName } .crt` ) ,
54
- ) ,
50
+ key : await fs . readFile ( getFixturePath ( "tls" , `${ certName } .key` ) ) ,
51
+ cert : await fs . readFile ( getFixturePath ( "tls" , `${ certName } .crt` ) ) ,
55
52
} ,
56
53
( req , res ) => {
57
54
if ( req . url ?. endsWith ( "/error" ) ) {
@@ -88,9 +85,7 @@ describe("Certificate errors", () => {
88
85
const address = await startServer ( "chain-leaf" ) ;
89
86
const request = axios . get ( address , {
90
87
httpsAgent : new https . Agent ( {
91
- ca : await fs . readFile (
92
- path . join ( __dirname , "../fixtures/tls/chain-leaf.crt" ) ,
93
- ) ,
88
+ ca : await fs . readFile ( getFixturePath ( "tls" , "chain-leaf.crt" ) ) ,
94
89
} ) ,
95
90
} ) ;
96
91
await expect ( request ) . rejects . toHaveProperty (
@@ -125,9 +120,7 @@ describe("Certificate errors", () => {
125
120
const address = await startServer ( "no-signing" ) ;
126
121
const request = axios . get ( address , {
127
122
httpsAgent : new https . Agent ( {
128
- ca : await fs . readFile (
129
- path . join ( __dirname , "../fixtures/tls/no-signing.crt" ) ,
130
- ) ,
123
+ ca : await fs . readFile ( getFixturePath ( "tls" , "no-signing.crt" ) ) ,
131
124
servername : "localhost" ,
132
125
} ) ,
133
126
} ) ;
@@ -190,9 +183,7 @@ describe("Certificate errors", () => {
190
183
const address = await startServer ( "self-signed" ) ;
191
184
const request = axios . get ( address , {
192
185
httpsAgent : new https . Agent ( {
193
- ca : await fs . readFile (
194
- path . join ( __dirname , "../fixtures/tls/self-signed.crt" ) ,
195
- ) ,
186
+ ca : await fs . readFile ( getFixturePath ( "tls" , "self-signed.crt" ) ) ,
196
187
servername : "localhost" ,
197
188
} ) ,
198
189
} ) ;
@@ -235,9 +226,7 @@ describe("Certificate errors", () => {
235
226
const address = await startServer ( "chain" ) ;
236
227
const request = axios . get ( address , {
237
228
httpsAgent : new https . Agent ( {
238
- ca : await fs . readFile (
239
- path . join ( __dirname , "../fixtures/tls/chain-root.crt" ) ,
240
- ) ,
229
+ ca : await fs . readFile ( getFixturePath ( "tls" , "chain-root.crt" ) ) ,
241
230
servername : "localhost" ,
242
231
} ) ,
243
232
} ) ;
@@ -258,9 +247,7 @@ describe("Certificate errors", () => {
258
247
const address = await startServer ( "chain" ) ;
259
248
const request = axios . get ( address + "/error" , {
260
249
httpsAgent : new https . Agent ( {
261
- ca : await fs . readFile (
262
- path . join ( __dirname , "../fixtures/tls/chain-root.crt" ) ,
263
- ) ,
250
+ ca : await fs . readFile ( getFixturePath ( "tls" , "chain-root.crt" ) ) ,
264
251
servername : "localhost" ,
265
252
} ) ,
266
253
} ) ;
0 commit comments