Skip to content

Commit 2679cd9

Browse files
committed
Reorganize & clarify endpoint groups
1 parent 008917f commit 2679cd9

29 files changed

Lines changed: 142 additions & 75 deletions

src/docs-page.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { httpEndpoints, wsEndpoints, tlsEndpoints } from './endpoints/endpoint-index.js';
2-
import { EndpointMeta, EndpointGroup } from './endpoints/groups.js';
2+
import {
3+
EndpointMeta, EndpointGroup,
4+
httpGroupOrder, wsGroupOrder, tlsGroupOrder
5+
} from './endpoints/groups.js';
36

47
interface EndpointWithMeta {
58
meta?: EndpointMeta;
@@ -19,7 +22,10 @@ function sortEndpoints(endpoints: EndpointMeta[]): EndpointMeta[] {
1922
return endpoints.sort((a, b) => a.path.localeCompare(b.path));
2023
}
2124

22-
function groupEndpoints(endpoints: EndpointWithMeta[]): GroupedEndpoints[] {
25+
function groupEndpoints(
26+
endpoints: EndpointWithMeta[],
27+
groupOrder: EndpointGroup[]
28+
): GroupedEndpoints[] {
2329
const withMeta = endpoints.filter(e => e.meta).map(e => e.meta!);
2430
const groups = new Map<string, { group: EndpointGroup; endpoints: EndpointMeta[] }>();
2531

@@ -43,10 +49,14 @@ function groupEndpoints(endpoints: EndpointWithMeta[]): GroupedEndpoints[] {
4349
result.push(ungrouped);
4450
}
4551

46-
// Add grouped endpoints alphabetically by name, with sorted endpoints within
52+
// Add grouped endpoints in the order defined by the groupOrder array
53+
const orderIndex = new Map(groupOrder.map((g, i) => [g.id, i]));
4754
const sortedEntries = [...groups.entries()]
4855
.filter(([k]) => k !== '')
49-
.sort((a, b) => a[1].group.name.localeCompare(b[1].group.name));
56+
.sort((a, b) =>
57+
(orderIndex.get(a[1].group.id) ?? Infinity) -
58+
(orderIndex.get(b[1].group.id) ?? Infinity)
59+
);
5060

5161
for (const [, groupData] of sortedEntries) {
5262
groupData.endpoints = sortEndpoints(groupData.endpoints);
@@ -480,14 +490,14 @@ export function getDocsHtml(): string {
480490
id: 'http-endpoints',
481491
title: 'HTTP Endpoints',
482492
intro: [],
483-
groups: groupEndpoints(httpEndpoints),
493+
groups: groupEndpoints(httpEndpoints, httpGroupOrder),
484494
type: 'http'
485495
},
486496
{
487497
id: 'websocket-endpoints',
488498
title: 'WebSocket Endpoints',
489499
intro: ['Connect via <code>wss://{hostname}/ws/...</code>'],
490-
groups: groupEndpoints(wsEndpoints),
500+
groups: groupEndpoints(wsEndpoints, wsGroupOrder),
491501
type: 'ws'
492502
},
493503
{
@@ -498,7 +508,7 @@ export function getDocsHtml(): string {
498508
'Endpoints can be combined using double-dashes, e.g. <code>expired--revoked--http2--tls-v1-2.{domain}</code> will return an expired and revoked certificate, use TLSv1.2, and then negotiate HTTP/2 on the connection.',
499509
'These can also be combined with the HTTP or WebSocket handlers above, which can be independently specified in the request path.'
500510
],
501-
groups: groupEndpoints(tlsEndpoints),
511+
groups: groupEndpoints(tlsEndpoints, tlsGroupOrder),
502512
type: 'tls'
503513
}
504514
];

src/endpoints/groups.ts

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ export interface EndpointMeta {
1313
}
1414

1515
// HTTP endpoint groups
16-
export const httpContentExamples: EndpointGroup = {
17-
id: 'content-examples',
18-
name: 'Response Content Formats'
16+
export const httpRequestInspection: EndpointGroup = {
17+
id: 'request-inspection',
18+
name: 'Request Inspection'
19+
};
20+
21+
export const httpCustomResponses: EndpointGroup = {
22+
id: 'custom-responses',
23+
name: 'Custom Responses'
24+
};
25+
26+
export const httpRedirects: EndpointGroup = {
27+
id: 'redirects',
28+
name: 'Redirects'
29+
};
30+
31+
export const httpCaching: EndpointGroup = {
32+
id: 'caching',
33+
name: 'Caching'
1934
};
2035

2136
export const httpAuthentication: EndpointGroup = {
@@ -28,35 +43,44 @@ export const httpCookies: EndpointGroup = {
2843
name: 'Cookies'
2944
};
3045

31-
export const httpContentEncoding: EndpointGroup = {
32-
id: 'content-encoding',
33-
name: 'Response Content Encodings'
46+
export const httpResponseFormats: EndpointGroup = {
47+
id: 'response-formats',
48+
name: 'Response Formats'
3449
};
3550

36-
export const httpErrors: EndpointGroup = {
37-
id: 'errors',
38-
name: 'Errors'
51+
export const httpResponseEncoding: EndpointGroup = {
52+
id: 'response-encoding',
53+
name: 'Response Encoding'
3954
};
4055

41-
export const httpTlsMetadata: EndpointGroup = {
42-
id: 'tls-metadata',
43-
name: 'TLS Metadata'
56+
export const httpDynamicData: EndpointGroup = {
57+
id: 'dynamic-data',
58+
name: 'Dynamic Data'
4459
};
4560

46-
export const httpRedirects: EndpointGroup = {
47-
id: 'redirects',
48-
name: 'Redirects'
61+
export const httpTlsInspection: EndpointGroup = {
62+
id: 'tls-inspection',
63+
name: 'TLS Inspection'
4964
};
5065

51-
export const httpResponseInspection: EndpointGroup = {
52-
id: 'response-inspection',
53-
name: 'Response Inspection'
66+
export const httpErrors: EndpointGroup = {
67+
id: 'errors',
68+
name: 'Errors'
5469
};
5570

56-
export const httpDynamicData: EndpointGroup = {
57-
id: 'dynamic-data',
58-
name: 'Dynamic Data'
59-
};
71+
export const httpGroupOrder: EndpointGroup[] = [
72+
httpRequestInspection,
73+
httpCustomResponses,
74+
httpRedirects,
75+
httpCaching,
76+
httpAuthentication,
77+
httpCookies,
78+
httpResponseFormats,
79+
httpResponseEncoding,
80+
httpDynamicData,
81+
httpTlsInspection,
82+
httpErrors
83+
];
6084

6185
// WebSocket endpoint groups
6286
export const wsMessaging: EndpointGroup = {
@@ -79,6 +103,13 @@ export const wsErrors: EndpointGroup = {
79103
name: 'Errors'
80104
};
81105

106+
export const wsGroupOrder: EndpointGroup[] = [
107+
wsMessaging,
108+
wsConnection,
109+
wsTiming,
110+
wsErrors
111+
];
112+
82113
// TLS endpoint groups
83114
export const tlsCertificateModes: EndpointGroup = {
84115
id: 'certificate-modes',
@@ -95,4 +126,10 @@ export const tlsVersions: EndpointGroup = {
95126
id: 'versions',
96127
name: 'TLS Versions',
97128
description: 'Endpoints that only accept specific TLS versions. These can be used together, to simulate a server supporting any specific combination of versions.'
98-
};
129+
};
130+
131+
export const tlsGroupOrder: EndpointGroup[] = [
132+
tlsCertificateModes,
133+
tlsProtocolNegotiation,
134+
tlsVersions
135+
];

src/endpoints/http/anything.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { HttpEndpoint } from '../http-index.js';
2+
import { httpRequestInspection } from '../groups.js';
23
import { buildHttpBinAnythingEndpoint } from '../../httpbin-compat.js';
34

45
export const anything: HttpEndpoint = {
@@ -7,6 +8,7 @@ export const anything: HttpEndpoint = {
78
meta: {
89
path: '/anything',
910
description: 'Returns JSON containing the parsed details of the request, including method, url, headers, args, data, files, form, and origin.',
10-
examples: ['/anything', '/anything/subpath']
11+
examples: ['/anything', '/anything/subpath'],
12+
group: httpRequestInspection
1113
}
1214
};

src/endpoints/http/cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StatusError } from '@httptoolkit/util';
22
import { HttpEndpoint } from '../http-index.js';
3-
import { httpResponseInspection } from '../groups.js';
3+
import { httpCaching } from '../groups.js';
44
import { buildHttpBinAnythingData } from '../../httpbin-compat.js';
55
import { serializeJson } from '../../util.js';
66

@@ -22,7 +22,7 @@ export const cache: HttpEndpoint = {
2222
path: '/cache',
2323
description: 'Returns 304 Not Modified if an If-Modified-Since or If-None-Match header is present, otherwise returns the same response as /get.',
2424
examples: ['/cache'],
25-
group: httpResponseInspection
25+
group: httpCaching
2626
}
2727
};
2828

@@ -46,6 +46,6 @@ export const cacheWithAge: HttpEndpoint = {
4646
path: '/cache/{n}',
4747
description: 'Sets a Cache-Control header for n seconds, then returns the same response as /get.',
4848
examples: ['/cache/60'],
49-
group: httpResponseInspection
49+
group: httpCaching
5050
}
5151
};

src/endpoints/http/delay.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { delay, StatusError } from '@httptoolkit/util';
22
import { HttpEndpoint, HttpHandler } from '../http-index.js';
3+
import { httpDynamicData } from '../groups.js';
34
import { buildHttpBinAnythingEndpoint } from '../../httpbin-compat.js';
45

56
const getRemainingPath = (path: string): string | undefined => {
@@ -43,6 +44,7 @@ export const delayEndpoint: HttpEndpoint = {
4344
meta: {
4445
path: '/delay/{seconds}',
4546
description: 'Delays the response by the specified number of seconds (max 10). Can be chained with other endpoints.',
46-
examples: ['/delay/1', '/delay/5', '/delay/2/status/200']
47+
examples: ['/delay/1', '/delay/5', '/delay/2/status/200'],
48+
group: httpDynamicData
4749
}
4850
};

src/endpoints/http/deny.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpEndpoint } from '../http-index.js';
2-
import { httpContentExamples } from '../groups.js';
2+
import { httpResponseFormats } from '../groups.js';
33

44
const DENY_TEXT = `
55
.-''''''-.
@@ -24,6 +24,6 @@ export const deny: HttpEndpoint = {
2424
path: '/deny',
2525
description: 'Returns a page denied by robots.txt.',
2626
examples: ['/deny'],
27-
group: httpContentExamples
27+
group: httpResponseFormats
2828
}
2929
};

src/endpoints/http/echo.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
HttpRequest,
66
HttpResponse
77
} from '../http-index.js';
8+
import { httpRequestInspection } from '../groups.js';
89

910
const matchPath = ((path: string) => path === '/echo');
1011

@@ -166,6 +167,7 @@ export const echo: HttpEndpoint = {
166167
meta: {
167168
path: '/echo',
168169
description: 'Echoes back the raw HTTP request data. For HTTP/2, returns parsed frame data as JSON lines.',
169-
examples: ['/echo']
170+
examples: ['/echo'],
171+
group: httpRequestInspection
170172
}
171173
}

src/endpoints/http/encoding/brotli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as zlib from 'node:zlib';
22
import { serializeJson } from '../../../util.js';
33
import { HttpEndpoint, HttpHandler } from '../../http-index.js';
4-
import { httpContentEncoding } from '../../groups.js';
4+
import { httpResponseEncoding } from '../../groups.js';
55

66
// We pre-decode the data. Note that this differs from HTTPBin which returns
77
// dynamic data here - for this reason, we use a subdirectory. Dynamic encoding
@@ -30,6 +30,6 @@ export const brotli: HttpEndpoint = {
3030
path: '/encoding/brotli',
3131
description: 'Returns brotli-encoded JSON data.',
3232
examples: ['/encoding/brotli'],
33-
group: httpContentEncoding
33+
group: httpResponseEncoding
3434
}
3535
};

src/endpoints/http/encoding/deflate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as zlib from 'node:zlib';
22
import { serializeJson } from '../../../util.js';
33
import { HttpEndpoint, HttpHandler } from '../../http-index.js';
4-
import { httpContentEncoding } from '../../groups.js';
4+
import { httpResponseEncoding } from '../../groups.js';
55

66
// We pre-decode the data. Note that this differs from HTTPBin which returns
77
// dynamic data here - for this reason, we use a subdirectory. Dynamic encoding
@@ -30,6 +30,6 @@ export const deflate: HttpEndpoint = {
3030
path: '/encoding/deflate',
3131
description: 'Returns deflate-encoded JSON data.',
3232
examples: ['/encoding/deflate'],
33-
group: httpContentEncoding
33+
group: httpResponseEncoding
3434
}
3535
};

src/endpoints/http/encoding/gzip.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as zlib from 'node:zlib';
22
import { serializeJson } from '../../../util.js';
33
import { HttpEndpoint, HttpHandler } from '../../http-index.js';
4-
import { httpContentEncoding } from '../../groups.js';
4+
import { httpResponseEncoding } from '../../groups.js';
55

66
// We pre-decode the data. Note that this differs from HTTPBin which returns
77
// dynamic data here - for this reason, we use a subdirectory. Dynamic encoding
@@ -30,6 +30,6 @@ export const gzip: HttpEndpoint = {
3030
path: '/encoding/gzip',
3131
description: 'Returns gzip-encoded JSON data.',
3232
examples: ['/encoding/gzip'],
33-
group: httpContentEncoding
33+
group: httpResponseEncoding
3434
}
3535
};

0 commit comments

Comments
 (0)