11import './util' ;
2- import { describe , beforeAll , test } from "vitest" ;
2+ import { describe , beforeAll , test , afterAll , beforeEach } from "vitest" ;
33import assert from "assert" ;
44import { Client } from "../src" ;
55
6+ import { createServer , Server } from 'http' ;
7+ import { AddressInfo } from 'net' ;
8+ import { AbortError } from '../src/errors/Errors' ;
9+
10+ function handler ( req , res ) {
11+ setTimeout ( ( ) => {
12+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
13+ res . end ( '{invalid_json' ) ;
14+ } , 60 ) ;
15+ }
16+
17+ export async function startDummyLocalServer ( ) {
18+ return new Promise ( res => {
19+ let app = createServer ( handler ) . listen ( ) ;
20+ let close = app . close . bind ( app ) ;
21+ let { port } = app . address ( ) as AddressInfo ;
22+ return res ( { port, close } ) ;
23+ } ) ;
24+ }
25+
26+
627describe ( "HTTP" , function ( ) {
728 let client : Client ;
29+ let localServer : any ;
830
9- beforeAll ( ( ) => {
10- client = new Client ( "ws://localhost:4545" ) ;
31+ beforeAll ( async ( ) => {
32+ localServer = await startDummyLocalServer ( ) ;
33+ } ) ;
34+
35+ beforeEach ( ( ) => {
36+ client = new Client ( `http://localhost:${ localServer . port } ` ) ;
37+ } ) ;
38+
39+ afterAll ( ( ) => {
40+ localServer . close ( ) ;
1141 } ) ;
1242
1343 describe ( "errors" , ( ) => {
1444 test ( "should return 'offline' error when requesting offline service" , async ( ) => {
45+ client = new Client ( `http://localhost:9090` ) ;
1546 try {
1647 await client . http . post ( "/anything" ) ;
1748 } catch ( e ) {
@@ -20,4 +51,23 @@ describe("HTTP", function() {
2051 } ) ;
2152 } ) ;
2253
54+ describe ( "AbortController" , ( ) => {
55+ test ( "should abort request" , async ( ) => {
56+ let abortError : AbortError | undefined = undefined ;
57+
58+ const controller = new AbortController ( ) ;
59+ setTimeout ( ( ) => controller . abort ( ) , 5 ) ;
60+
61+ try {
62+ await client . http . get ( "/anything" , { signal : controller . signal } ) ;
63+
64+ } catch ( e : any ) {
65+ abortError = e ;
66+ }
67+
68+ assert . ok ( abortError ) ;
69+ assert . strictEqual ( abortError ! . name , 'AbortError' ) ;
70+ } ) ;
71+ } ) ;
72+
2373} ) ;
0 commit comments