1+ import * as net from 'net' ;
2+ import { expect } from 'chai' ;
3+ import { DestroyableServer , makeDestroyable } from 'destroyable-server' ;
4+
5+ import { createServer } from '../src/server.js' ;
6+
7+ describe ( "Root page endpoint" , ( ) => {
8+
9+ let server : DestroyableServer ;
10+ let serverPort : number ;
11+
12+ beforeEach ( async ( ) => {
13+ server = makeDestroyable ( await createServer ( ) ) ;
14+ await new Promise < void > ( ( resolve ) => server . listen ( resolve ) ) ;
15+ serverPort = ( server . address ( ) as net . AddressInfo ) . port ;
16+ } ) ;
17+
18+ afterEach ( async ( ) => {
19+ await server . destroy ( ) ;
20+ } ) ;
21+
22+ it ( "redirects to the source for direct requests" , async ( ) => {
23+ const address = `http://localhost:${ serverPort } /` ;
24+ const response = await fetch ( address , {
25+ redirect : 'manual'
26+ } ) ;
27+
28+ expect ( response . status ) . to . equal ( 307 ) ;
29+ expect ( response . headers . get ( 'location' ) ) . to . equal ( 'https://github.com/httptoolkit/testserver/' ) ;
30+ } ) ;
31+
32+ it ( "just returns a 404 for any other hostnames" , async ( ) => {
33+ const address = `http://http1.localhost:${ serverPort } /` ;
34+ const response = await fetch ( address , {
35+ redirect : 'manual'
36+ } ) ;
37+
38+ expect ( response . status ) . to . equal ( 404 ) ;
39+ expect ( await response . text ( ) ) . to . equal ( 'Could not match endpoint for / (http1)' ) ;
40+ } ) ;
41+
42+ } ) ;
0 commit comments