File tree 5 files changed +98
-1
lines changed
5 files changed +98
-1
lines changed Original file line number Diff line number Diff line change @@ -27,4 +27,6 @@ export * from './http/delay.js';
27
27
export * from './http/cookies.js'
28
28
export * from './http/basic-auth.js' ;
29
29
export * from './http/json.js' ;
30
- export * from './http/trailers.js' ;
30
+ export * from './http/trailers.js' ;
31
+ export * from './http/error/close.js' ;
32
+ export * from './http/error/reset.js' ;
Original file line number Diff line number Diff line change
1
+ import { HttpHandler } from '../../http-index.js' ;
2
+ const matchPath = ( path : string ) => path === '/error/close' ;
3
+
4
+ const handle : HttpHandler = async ( req ) => {
5
+ req . socket . end ( ) ;
6
+ }
7
+
8
+ export const closeEndpoint = {
9
+ matchPath,
10
+ handle
11
+ } ;
Original file line number Diff line number Diff line change
1
+ import { HttpHandler } from '../../http-index.js' ;
2
+
3
+ const matchPath = ( path : string ) => path === '/error/reset' ;
4
+
5
+ const handle : HttpHandler = async ( req ) => {
6
+ req . socket . resetAndDestroy ( ) ;
7
+ }
8
+
9
+ export const resetEndpoint = {
10
+ matchPath,
11
+ handle
12
+ } ;
Original file line number Diff line number Diff line change
1
+ import * as net from 'net' ;
2
+ import * as http from 'http' ;
3
+
4
+ import { expect } from 'chai' ;
5
+ import { DestroyableServer , makeDestroyable } from 'destroyable-server' ;
6
+
7
+ import { createServer } from '../src/server.js' ;
8
+
9
+ describe ( "Close endpoint" , ( ) => {
10
+
11
+ let server : DestroyableServer ;
12
+ let serverPort : number ;
13
+
14
+ beforeEach ( async ( ) => {
15
+ server = makeDestroyable ( await createServer ( ) ) ;
16
+ await new Promise < void > ( ( resolve ) => server . listen ( resolve ) ) ;
17
+ serverPort = ( server . address ( ) as net . AddressInfo ) . port ;
18
+ } ) ;
19
+
20
+ afterEach ( async ( ) => {
21
+ await server . destroy ( ) ;
22
+ } ) ;
23
+
24
+ it ( "closes the connection" , async ( ) => {
25
+ const address = `http://localhost:${ serverPort } /error/close` ;
26
+ const request = http . request ( address ) . end ( ) ;
27
+ const result = await new Promise < any > ( ( resolve , reject ) => {
28
+ request . on ( 'error' , resolve ) ;
29
+ request . on ( 'response' , ( ) => reject ( new Error ( 'Expected an error' ) ) ) ;
30
+ } ) ;
31
+
32
+ expect ( result . code ) . to . equal ( 'ECONNRESET' ) ;
33
+ expect ( result . message ) . to . equal ( 'socket hang up' ) ;
34
+ } ) ;
35
+
36
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import * as net from 'net' ;
2
+ import * as http from 'http' ;
3
+
4
+ import { expect } from 'chai' ;
5
+ import { DestroyableServer , makeDestroyable } from 'destroyable-server' ;
6
+
7
+ import { createServer } from '../src/server.js' ;
8
+
9
+ describe ( "Reset endpoint" , ( ) => {
10
+
11
+ let server : DestroyableServer ;
12
+ let serverPort : number ;
13
+
14
+ beforeEach ( async ( ) => {
15
+ server = makeDestroyable ( await createServer ( ) ) ;
16
+ await new Promise < void > ( ( resolve ) => server . listen ( resolve ) ) ;
17
+ serverPort = ( server . address ( ) as net . AddressInfo ) . port ;
18
+ } ) ;
19
+
20
+ afterEach ( async ( ) => {
21
+ await server . destroy ( ) ;
22
+ } ) ;
23
+
24
+ it ( "resets the connection" , async ( ) => {
25
+ const address = `http://localhost:${ serverPort } /error/reset` ;
26
+ const request = http . request ( address ) . end ( ) ;
27
+ const result = await new Promise < any > ( ( resolve , reject ) => {
28
+ request . on ( 'error' , resolve ) ;
29
+ request . on ( 'response' , ( ) => reject ( new Error ( 'Expected an error' ) ) ) ;
30
+ } ) ;
31
+
32
+ expect ( result . code ) . to . equal ( 'ECONNRESET' ) ;
33
+ expect ( result . message ) . to . equal ( 'read ECONNRESET' ) ;
34
+ } ) ;
35
+
36
+ } ) ;
You can’t perform that action at this time.
0 commit comments