@jaredwray/mockhttp 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ MIT License & © Jared Wray
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ ![public/logo.svg](public/logo.svg)
2
+
3
+ [![tests](https://github.com/jaredwray/mockhttp/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/mockhttp/actions/workflows/tests.yaml)
4
+ [![GitHub license](https://img.shields.io/github/license/jaredwray/mockhttp)](https://github.com/jaredwray/mockhttp/blob/master/LICENSE)
5
+ [![codecov](https://codecov.io/gh/jaredwray/mockhttp/graph/badge.svg?token=eqtqoA3olU)](https://codecov.io/gh/jaredwray/mockhttp)
6
+ [![npm](https://img.shields.io/npm/dm/mockhttp)](https://npmjs.com/package/mockhttp)
7
+ [![npm](https://img.shields.io/npm/v/mockhttp)](https://npmjs.com/package/mockhttp)
8
+
9
+ A simple HTTP server that can be used to mock HTTP responses for testing purposes. Inspired by [httpbin](https://httpbin.org/) and built using `nodejs` and `fastify` with the idea of running it via https://mockhttp.org, via docker `jaredwray/mockhttp`, or nodejs `npm install jaredwray/mockhttp`.
10
+
11
+ # Deploy via Docker
12
+ ```bash
13
+ docker run -d -p 8080:8080 jaredwray/mockhttp
14
+ ```
15
+
16
+ # Deploy via NodeJS
17
+ ```bash
18
+ npm install @jaredwray/mockhttp --save
19
+ ```
20
+
21
+ then run `mockhttp` in your code.
22
+
23
+ ```javascript
24
+ import { mockhttp } from '@jaredwray/mockhttp';
25
+ mockhttp.start(); // start the server
26
+ const response = await fetch('http://localhost:8080/ip');
27
+ mockhttp.stop(); // stop the server
28
+ ```
29
+
30
+ # About mockhttp.org
31
+
32
+ [mockhttp.org](https://mockhttp.org) is a free service that runs this codebase and allows you to use it for testing purposes. It is a simple way to mock HTTP responses for testing purposes. It is globally available has some limitations on it to prevent abuse but many people will not see it. It is ran via [Cloudflare](https://cloudflare.com) and [Google Cloud Run](https://cloud.google.com/run/) across 7 regions globally.
33
+
34
+ # Contributing
35
+
36
+ Please read our [CODE OF CONDUCT](CODE_OF_CONDUCT.md) and [CONTRIBUTING](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
37
+
38
+ # License
39
+
40
+ [MIT License & © Jared Wray](LICENSE)
@@ -0,0 +1,12 @@
1
+ export const fastifyConfig = {
2
+ logger: {
3
+ transport: {
4
+ target: 'pino-pretty',
5
+ options: {
6
+ colorize: true,
7
+ translateTime: true,
8
+ ignore: 'pid,hostname',
9
+ },
10
+ },
11
+ },
12
+ };
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ import process from 'node:process';
2
+ import path from 'node:path';
3
+ import Fastify from 'fastify';
4
+ import fastifyStatic from '@fastify/static';
5
+ import { fastifySwagger } from '@fastify/swagger';
6
+ import { indexRoute } from './routes/index-route.js';
7
+ import { fastifyConfig } from './fastify-config.js';
8
+ import { fastifySwaggerConfig, registerSwaggerUi } from './swagger.js';
9
+ import { ipRoute, headersRoute, userAgentRoute } from './routes/request-inspection/index.js';
10
+ import { getRoute, postRoute, deleteRoute, putRoute, patchRoute, } from './routes/http-methods/index.js';
11
+ import { statusCodeRoute } from './routes/status-codes/index.js';
12
+ // eslint-disable-next-line new-cap
13
+ const fastify = Fastify(fastifyConfig);
14
+ await fastify.register(fastifyStatic, {
15
+ root: path.resolve('./public'),
16
+ });
17
+ // Set up Swagger for API documentation
18
+ await fastify.register(fastifySwagger, fastifySwaggerConfig);
19
+ // Register Swagger UI
20
+ await registerSwaggerUi(fastify);
21
+ // Register the index / home page route
22
+ await fastify.register(indexRoute);
23
+ // Register the HTTP method routes
24
+ await fastify.register(getRoute);
25
+ await fastify.register(postRoute);
26
+ await fastify.register(deleteRoute);
27
+ await fastify.register(putRoute);
28
+ await fastify.register(patchRoute);
29
+ // Register the request inspection routes
30
+ await fastify.register(ipRoute);
31
+ await fastify.register(headersRoute);
32
+ await fastify.register(userAgentRoute);
33
+ // Register the status code routes
34
+ await fastify.register(statusCodeRoute);
35
+ // Start the Fastify server
36
+ export const start = async () => {
37
+ try {
38
+ await fastify.listen({ port: 3000, host: '127.0.0.1' });
39
+ fastify.log.info('Server is running at http://localhost:3000');
40
+ }
41
+ catch (error) {
42
+ /* c8 ignore next 4 */
43
+ fastify.log.error(error);
44
+ // eslint-disable-next-line unicorn/no-process-exit
45
+ process.exit(1);
46
+ }
47
+ };
48
+ await start();
@@ -0,0 +1,27 @@
1
+ const deleteSchema = {
2
+ description: 'Handles a DELETE request and returns request information',
3
+ body: {
4
+ type: 'object',
5
+ additionalProperties: true,
6
+ description: 'The body of the DELETE request',
7
+ },
8
+ response: {
9
+ // eslint-disable-next-line @typescript-eslint/naming-convention
10
+ 200: {
11
+ type: 'object',
12
+ properties: {
13
+ method: { type: 'string' },
14
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
15
+ body: { type: 'object', additionalProperties: true },
16
+ },
17
+ required: ['method', 'headers', 'body'],
18
+ },
19
+ },
20
+ };
21
+ export const deleteRoute = (fastify) => {
22
+ fastify.delete('/delete', { schema: deleteSchema }, async (request) => ({
23
+ method: 'DELETE',
24
+ headers: request.headers,
25
+ body: request.body,
26
+ }));
27
+ };
@@ -0,0 +1,34 @@
1
+ export const getSchema = {
2
+ description: 'Returns request information for GET requests',
3
+ querystring: {
4
+ type: 'object',
5
+ additionalProperties: true,
6
+ properties: {},
7
+ description: 'Query parameters sent in the request',
8
+ },
9
+ headers: {
10
+ type: 'object',
11
+ additionalProperties: true,
12
+ properties: {},
13
+ description: 'Headers sent in the request',
14
+ },
15
+ response: {
16
+ // eslint-disable-next-line @typescript-eslint/naming-convention
17
+ 200: {
18
+ type: 'object',
19
+ properties: {
20
+ method: { type: 'string' },
21
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
22
+ queryParams: { type: 'object', additionalProperties: { type: 'string' } },
23
+ },
24
+ required: ['method', 'headers', 'queryParams'],
25
+ },
26
+ },
27
+ };
28
+ export const getRoute = (fastify) => {
29
+ fastify.get('/get', { schema: getSchema }, async (request, _reply) => ({
30
+ method: 'GET',
31
+ headers: request.headers,
32
+ queryParams: request.query,
33
+ }));
34
+ };
@@ -0,0 +1,28 @@
1
+ const deleteSchema = {
2
+ description: 'Handles a DELETE request and returns request information',
3
+ tags: ['HTTP Methods'],
4
+ body: {
5
+ type: 'object',
6
+ additionalProperties: true,
7
+ description: 'The body of the DELETE request',
8
+ },
9
+ response: {
10
+ // eslint-disable-next-line @typescript-eslint/naming-convention
11
+ 200: {
12
+ type: 'object',
13
+ properties: {
14
+ method: { type: 'string' },
15
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
16
+ body: { type: 'object', additionalProperties: true },
17
+ },
18
+ required: ['method', 'headers', 'body'],
19
+ },
20
+ },
21
+ };
22
+ export const deleteRoute = (fastify) => {
23
+ fastify.delete('/delete', { schema: deleteSchema }, async (request) => ({
24
+ method: 'DELETE',
25
+ headers: request.headers,
26
+ body: request.body,
27
+ }));
28
+ };
@@ -0,0 +1,35 @@
1
+ export const getSchema = {
2
+ description: 'Returns request information for GET requests',
3
+ tags: ['HTTP Methods'],
4
+ querystring: {
5
+ type: 'object',
6
+ additionalProperties: true,
7
+ properties: {},
8
+ description: 'Query parameters sent in the request',
9
+ },
10
+ headers: {
11
+ type: 'object',
12
+ additionalProperties: true,
13
+ properties: {},
14
+ description: 'Headers sent in the request',
15
+ },
16
+ response: {
17
+ // eslint-disable-next-line @typescript-eslint/naming-convention
18
+ 200: {
19
+ type: 'object',
20
+ properties: {
21
+ method: { type: 'string' },
22
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
23
+ queryParams: { type: 'object', additionalProperties: { type: 'string' } },
24
+ },
25
+ required: ['method', 'headers', 'queryParams'],
26
+ },
27
+ },
28
+ };
29
+ export const getRoute = (fastify) => {
30
+ fastify.get('/get', { schema: getSchema }, async (request, _reply) => ({
31
+ method: 'GET',
32
+ headers: request.headers,
33
+ queryParams: request.query,
34
+ }));
35
+ };
@@ -0,0 +1,5 @@
1
+ export { getRoute } from './get.js';
2
+ export { postRoute } from './post.js';
3
+ export { deleteRoute } from './delete.js';
4
+ export { putRoute } from './put.js';
5
+ export { patchRoute } from './patch.js';
@@ -0,0 +1,28 @@
1
+ const patchSchema = {
2
+ description: 'Handles a PATCH request and returns request information',
3
+ tags: ['HTTP Methods'],
4
+ body: {
5
+ type: 'object',
6
+ additionalProperties: true,
7
+ description: 'The body of the PATCH request',
8
+ },
9
+ response: {
10
+ // eslint-disable-next-line @typescript-eslint/naming-convention
11
+ 200: {
12
+ type: 'object',
13
+ properties: {
14
+ method: { type: 'string' },
15
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
16
+ body: { type: 'object', additionalProperties: true },
17
+ },
18
+ required: ['method', 'headers', 'body'],
19
+ },
20
+ },
21
+ };
22
+ export const patchRoute = (fastify) => {
23
+ fastify.patch('/patch', { schema: patchSchema }, async (request) => ({
24
+ method: 'PATCH',
25
+ headers: request.headers,
26
+ body: request.body,
27
+ }));
28
+ };
@@ -0,0 +1,28 @@
1
+ const postSchema = {
2
+ description: 'Handles a POST request and returns request information',
3
+ tags: ['HTTP Methods'],
4
+ body: {
5
+ type: 'object',
6
+ additionalProperties: true,
7
+ description: 'The body of the POST request',
8
+ },
9
+ response: {
10
+ // eslint-disable-next-line @typescript-eslint/naming-convention
11
+ 200: {
12
+ type: 'object',
13
+ properties: {
14
+ method: { type: 'string' },
15
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
16
+ body: { type: 'object', additionalProperties: true },
17
+ },
18
+ required: ['method', 'headers', 'body'],
19
+ },
20
+ },
21
+ };
22
+ export const postRoute = (fastify) => {
23
+ fastify.post('/post', { schema: postSchema }, async (request) => ({
24
+ method: 'POST',
25
+ headers: request.headers,
26
+ body: request.body,
27
+ }));
28
+ };
@@ -0,0 +1,28 @@
1
+ const putSchema = {
2
+ description: 'Handles a PUT request and returns request information',
3
+ tags: ['HTTP Methods'],
4
+ body: {
5
+ type: 'object',
6
+ additionalProperties: true,
7
+ description: 'The body of the PUT request',
8
+ },
9
+ response: {
10
+ // eslint-disable-next-line @typescript-eslint/naming-convention
11
+ 200: {
12
+ type: 'object',
13
+ properties: {
14
+ method: { type: 'string' },
15
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
16
+ body: { type: 'object', additionalProperties: true },
17
+ },
18
+ required: ['method', 'headers', 'body'],
19
+ },
20
+ },
21
+ };
22
+ export const putRoute = (fastify) => {
23
+ fastify.put('/put', { schema: putSchema }, async (request) => ({
24
+ method: 'PUT',
25
+ headers: request.headers,
26
+ body: request.body,
27
+ }));
28
+ };
@@ -0,0 +1,67 @@
1
+ export const indexRoute = (fastify) => {
2
+ fastify.get('/', { schema: { hide: true } }, async (_request, reply) => {
3
+ const redocHtml = `
4
+ <!doctype html>
5
+ <html>
6
+ <head>
7
+ <title>{ mockhttp } </title>
8
+ <meta charset="utf-8" />
9
+ <meta
10
+ name="viewport"
11
+ content="width=device-width, initial-scale=1" />
12
+ <style>
13
+ :root {
14
+ --scalar-custom-header-height: 70px;
15
+ }
16
+ .custom-header {
17
+ height: var(--scalar-custom-header-height);
18
+ background-color: var(--scalar-background-1);
19
+ box-shadow: inset 0 -1px 0 var(--scalar-border-color);
20
+ color: var(--scalar-color-1);
21
+ font-size: var(--scalar-font-size-2);
22
+ padding: 0 18px;
23
+ position: sticky;
24
+ justify-content: space-between;
25
+ top: 0;
26
+ z-index: 100;
27
+ }
28
+ .custom-header,
29
+ .custom-header nav {
30
+ display: flex;
31
+ align-items: center;
32
+ gap: 18px;
33
+ }
34
+
35
+ .custom-header img {
36
+ height: var(--scalar-custom-header-height);
37
+ object-fit: contain;
38
+ }
39
+
40
+ .custom-header a:hover {
41
+ color: var(--scalar-color-2);
42
+ }
43
+
44
+ .github-corner {
45
+ fill: var(--scalar-border-color) !important;
46
+ }
47
+
48
+ </style>
49
+ </head>
50
+ <body>
51
+ <header class="custom-header scalar-app">
52
+ <img src="/logo.svg" alt="{ mockhttp }" />
53
+ <a href="https://github.com/jaredwray/mockhttp" class="github-corner" aria-label="View source on GitHub">
54
+ <svg width="70" height="70" viewBox="0 0 250 250" style="color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
55
+ </header>
56
+
57
+ <script
58
+ id="api-reference"
59
+ data-url="/docs/json"></script>
60
+
61
+ <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
62
+ </body>
63
+ </html>
64
+ `;
65
+ await reply.type('text/html').send(redocHtml);
66
+ });
67
+ };
@@ -0,0 +1,25 @@
1
+ const ipSchema = {
2
+ description: 'Returns the IP address of the client',
3
+ response: {
4
+ // eslint-disable-next-line @typescript-eslint/naming-convention
5
+ 200: {
6
+ type: 'object',
7
+ properties: {
8
+ ip: { type: 'string', description: 'The IP address of the client' },
9
+ },
10
+ required: ['ip'],
11
+ },
12
+ },
13
+ };
14
+ export const ipRoute = (fastify) => {
15
+ fastify.get('/ip', { schema: ipSchema }, async (request) => {
16
+ const cloudflareIp = request.headers['cf-connecting-ip'];
17
+ const forwardedIp = request.headers['x-forwarded-for'];
18
+ const remoteIp = request.ip;
19
+ // Determine the client's IP address, considering Cloudflare and other proxies
20
+ const ip = cloudflareIp ?? (typeof forwardedIp === 'string' ? forwardedIp.split(',')[0].trim() : remoteIp);
21
+ return {
22
+ ip,
23
+ };
24
+ });
25
+ };
@@ -0,0 +1,27 @@
1
+ const postSchema = {
2
+ description: 'Handles a POST request and returns request information',
3
+ body: {
4
+ type: 'object',
5
+ additionalProperties: true,
6
+ description: 'The body of the POST request',
7
+ },
8
+ response: {
9
+ // eslint-disable-next-line @typescript-eslint/naming-convention
10
+ 200: {
11
+ type: 'object',
12
+ properties: {
13
+ method: { type: 'string' },
14
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
15
+ body: { type: 'object', additionalProperties: true },
16
+ },
17
+ required: ['method', 'headers', 'body'],
18
+ },
19
+ },
20
+ };
21
+ export const postRoute = (fastify) => {
22
+ fastify.post('/post', { schema: postSchema }, async (request) => ({
23
+ method: 'POST',
24
+ headers: request.headers,
25
+ body: request.body,
26
+ }));
27
+ };
@@ -0,0 +1,27 @@
1
+ const putSchema = {
2
+ description: 'Handles a PUT request and returns request information',
3
+ body: {
4
+ type: 'object',
5
+ additionalProperties: true,
6
+ description: 'The body of the PUT request',
7
+ },
8
+ response: {
9
+ // eslint-disable-next-line @typescript-eslint/naming-convention
10
+ 200: {
11
+ type: 'object',
12
+ properties: {
13
+ method: { type: 'string' },
14
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
15
+ body: { type: 'object', additionalProperties: true },
16
+ },
17
+ required: ['method', 'headers', 'body'],
18
+ },
19
+ },
20
+ };
21
+ export const putRoute = (fastify) => {
22
+ fastify.put('/put', { schema: putSchema }, async (request) => ({
23
+ method: 'PUT',
24
+ headers: request.headers,
25
+ body: request.body,
26
+ }));
27
+ };
@@ -0,0 +1,26 @@
1
+ const headersSchema = {
2
+ description: 'Returns all headers of the client request',
3
+ tags: ['Request Inspection'],
4
+ response: {
5
+ // eslint-disable-next-line @typescript-eslint/naming-convention
6
+ 200: {
7
+ type: 'object',
8
+ properties: {
9
+ headers: {
10
+ type: 'object',
11
+ additionalProperties: { type: 'string' },
12
+ description: 'All headers of the client request',
13
+ },
14
+ },
15
+ required: ['headers'],
16
+ },
17
+ },
18
+ };
19
+ export const headersRoute = (fastify) => {
20
+ fastify.get('/headers', { schema: headersSchema }, async (request) => {
21
+ const headers = request.headers;
22
+ return {
23
+ headers,
24
+ };
25
+ });
26
+ };
@@ -0,0 +1,3 @@
1
+ export { ipRoute } from './ip.js';
2
+ export { headersRoute } from './headers.js';
3
+ export { userAgentRoute } from './user-agent.js';
@@ -0,0 +1,26 @@
1
+ const ipSchema = {
2
+ description: 'Returns the IP address of the client',
3
+ tags: ['Request Inspection'],
4
+ response: {
5
+ // eslint-disable-next-line @typescript-eslint/naming-convention
6
+ 200: {
7
+ type: 'object',
8
+ properties: {
9
+ ip: { type: 'string', description: 'The IP address of the client' },
10
+ },
11
+ required: ['ip'],
12
+ },
13
+ },
14
+ };
15
+ export const ipRoute = (fastify) => {
16
+ fastify.get('/ip', { schema: ipSchema }, async (request) => {
17
+ const cloudflareIp = request.headers['cf-connecting-ip'];
18
+ const forwardedIp = request.headers['x-forwarded-for'];
19
+ const remoteIp = request.ip;
20
+ // Determine the client's IP address, considering Cloudflare and other proxies
21
+ const ip = cloudflareIp ?? (typeof forwardedIp === 'string' ? forwardedIp.split(',')[0].trim() : remoteIp);
22
+ return {
23
+ ip,
24
+ };
25
+ });
26
+ };
@@ -0,0 +1,25 @@
1
+ const userAgentSchema = {
2
+ description: 'Returns the User-Agent header of the client request',
3
+ tags: ['Request Inspection'],
4
+ response: {
5
+ // eslint-disable-next-line @typescript-eslint/naming-convention
6
+ 200: {
7
+ type: 'object',
8
+ properties: {
9
+ userAgent: {
10
+ type: 'string',
11
+ description: 'The User-Agent header of the client request',
12
+ },
13
+ },
14
+ required: ['userAgent'],
15
+ },
16
+ },
17
+ };
18
+ export const userAgentRoute = (fastify) => {
19
+ fastify.get('/user-agent', { schema: userAgentSchema }, async (request) => {
20
+ const userAgent = request.headers['user-agent'];
21
+ return {
22
+ userAgent,
23
+ };
24
+ });
25
+ };
@@ -0,0 +1,29 @@
1
+ const statusSchema = {
2
+ description: 'Returns a response with the provided status code',
3
+ tags: ['Status Codes'],
4
+ params: {
5
+ type: 'object',
6
+ properties: {
7
+ code: { type: 'string', description: 'HTTP status code to be returned' },
8
+ },
9
+ required: ['code'],
10
+ },
11
+ response: {
12
+ // eslint-disable-next-line @typescript-eslint/naming-convention
13
+ 200: {
14
+ type: 'object',
15
+ properties: {
16
+ status: { type: 'string' },
17
+ },
18
+ },
19
+ },
20
+ };
21
+ export const getStatusCodeRoute = (fastify) => {
22
+ fastify.get('/status/:code', { schema: statusSchema }, async (request, reply) => {
23
+ const { code } = request.params;
24
+ await reply.code(Number(code));
25
+ return {
26
+ status: `Response with status code ${code}`,
27
+ };
28
+ });
29
+ };
@@ -0,0 +1,102 @@
1
+ import { randomInt } from 'node:crypto';
2
+ const statusCodes = [
3
+ { code: 100, message: 'Continue' },
4
+ { code: 101, message: 'Switching Protocols' },
5
+ { code: 102, message: 'Processing' },
6
+ { code: 103, message: 'Early Hints' },
7
+ { code: 200, message: 'OK' },
8
+ { code: 201, message: 'Created' },
9
+ { code: 202, message: 'Accepted' },
10
+ { code: 203, message: 'Non-Authoritative Information' },
11
+ { code: 204, message: 'No Content' },
12
+ { code: 205, message: 'Reset Content' },
13
+ { code: 206, message: 'Partial Content' },
14
+ { code: 207, message: 'Multi-Status' },
15
+ { code: 208, message: 'Already Reported' },
16
+ { code: 226, message: 'IM Used' },
17
+ { code: 300, message: 'Multiple Choices' },
18
+ { code: 301, message: 'Moved Permanently' },
19
+ { code: 302, message: 'Found' },
20
+ { code: 303, message: 'See Other' },
21
+ { code: 304, message: 'Not Modified' },
22
+ { code: 305, message: 'Use Proxy' },
23
+ { code: 306, message: 'Switch Proxy' },
24
+ { code: 307, message: 'Temporary Redirect' },
25
+ { code: 308, message: 'Permanent Redirect' },
26
+ { code: 400, message: 'Bad Request' },
27
+ { code: 401, message: 'Unauthorized' },
28
+ { code: 402, message: 'Payment Required' },
29
+ { code: 403, message: 'Forbidden' },
30
+ { code: 404, message: 'Not Found' },
31
+ { code: 405, message: 'Method Not Allowed' },
32
+ { code: 406, message: 'Not Acceptable' },
33
+ { code: 407, message: 'Proxy Authentication Required' },
34
+ { code: 408, message: 'Request Timeout' },
35
+ { code: 409, message: 'Conflict' },
36
+ { code: 410, message: 'Gone' },
37
+ { code: 411, message: 'Length Required' },
38
+ { code: 412, message: 'Precondition Failed' },
39
+ { code: 413, message: 'Payload Too Large' },
40
+ { code: 414, message: 'URI Too Long' },
41
+ { code: 415, message: 'Unsupported Media Type' },
42
+ { code: 416, message: 'Range Not Satisfiable' },
43
+ { code: 417, message: 'Expectation Failed' },
44
+ { code: 418, message: 'I\'m a teapot' },
45
+ { code: 421, message: 'Misdirected Request' },
46
+ { code: 422, message: 'Unprocessable Entity' },
47
+ { code: 423, message: 'Locked' },
48
+ { code: 424, message: 'Failed Dependency' },
49
+ { code: 425, message: 'Too Early' },
50
+ { code: 426, message: 'Upgrade Required' },
51
+ { code: 428, message: 'Precondition Required' },
52
+ { code: 429, message: 'Too Many Requests' },
53
+ { code: 431, message: 'Request Header Fields Too Large' },
54
+ { code: 451, message: 'Unavailable For Legal Reasons' },
55
+ { code: 500, message: 'Internal Server Error' },
56
+ { code: 501, message: 'Not Implemented' },
57
+ { code: 502, message: 'Bad Gateway' },
58
+ { code: 503, message: 'Service Unavailable' },
59
+ { code: 504, message: 'Gateway Timeout' },
60
+ { code: 505, message: 'HTTP Version Not Supported' },
61
+ { code: 506, message: 'Variant Also Negotiates' },
62
+ { code: 507, message: 'Insufficient Storage' },
63
+ { code: 508, message: 'Loop Detected' },
64
+ { code: 510, message: 'Not Extended' },
65
+ { code: 511, message: 'Network Authentication Required' },
66
+ ];
67
+ const statusSchema = {
68
+ description: 'Return status code or random status code if more than one are given ',
69
+ tags: ['Status Codes'],
70
+ params: {
71
+ type: 'object',
72
+ properties: {
73
+ code: { type: 'string', description: 'HTTP status code to be returned' },
74
+ },
75
+ },
76
+ response: {
77
+ // eslint-disable-next-line @typescript-eslint/naming-convention
78
+ 200: {
79
+ type: 'object',
80
+ properties: {
81
+ status: { type: 'string' },
82
+ },
83
+ },
84
+ },
85
+ };
86
+ export const statusCodeRoute = (fastify) => {
87
+ const handler = async (request, reply) => {
88
+ let { code } = request.params;
89
+ // Check if the code is a valid HTTP status code, otherwise pick a random one
90
+ if (!code || !statusCodes.map(status => status.code).includes(Number(code))) {
91
+ code = statusCodes[randomInt(0, statusCodes.length)].code.toString();
92
+ }
93
+ await reply.code(Number(code)).send({
94
+ status: `Response with status code ${code}`,
95
+ });
96
+ };
97
+ fastify.get('/status/:code?', { schema: statusSchema }, handler);
98
+ fastify.post('/status/:code?', { schema: statusSchema }, handler);
99
+ fastify.put('/status/:code?', { schema: statusSchema }, handler);
100
+ fastify.patch('/status/:code?', { schema: statusSchema }, handler);
101
+ fastify.delete('/status/:code?', { schema: statusSchema }, handler);
102
+ };
@@ -0,0 +1,28 @@
1
+ const statusSchema = {
2
+ description: 'Returns a response with the provided status code',
3
+ params: {
4
+ type: 'object',
5
+ properties: {
6
+ code: { type: 'string', description: 'HTTP status code to be returned' },
7
+ },
8
+ required: ['code'],
9
+ },
10
+ response: {
11
+ // eslint-disable-next-line @typescript-eslint/naming-convention
12
+ 200: {
13
+ type: 'object',
14
+ properties: {
15
+ status: { type: 'string' },
16
+ },
17
+ },
18
+ },
19
+ };
20
+ export const statusRoute = (fastify) => {
21
+ fastify.get('/status/:code', { schema: statusSchema }, async (request, reply) => {
22
+ const { code } = request.params;
23
+ await reply.code(Number(code));
24
+ return {
25
+ status: `Response with status code ${code}`,
26
+ };
27
+ });
28
+ };
@@ -0,0 +1,34 @@
1
+ import { fastifySwaggerUi } from '@fastify/swagger-ui';
2
+ export const fastifySwaggerConfig = {
3
+ swagger: {
4
+ info: {
5
+ title: 'Mock HTTP API',
6
+ description: 'A replacement for httpbin built using Fastify and Typescript',
7
+ version: '1.0.0',
8
+ },
9
+ consumes: ['application/json'],
10
+ produces: ['application/json'],
11
+ },
12
+ };
13
+ export const registerSwaggerUi = async (fastify) => {
14
+ await fastify.register(fastifySwaggerUi, {
15
+ routePrefix: '/docs',
16
+ uiConfig: {
17
+ docExpansion: 'none',
18
+ deepLinking: false,
19
+ },
20
+ uiHooks: {
21
+ /* c8 ignore next 6 */
22
+ onRequest(_request, _reply, next) {
23
+ next();
24
+ },
25
+ preHandler(_request, _reply, next) {
26
+ next();
27
+ },
28
+ },
29
+ // eslint-disable-next-line @typescript-eslint/naming-convention
30
+ staticCSP: true,
31
+ transformSpecification: (swaggerObject, _request, _reply) => swaggerObject,
32
+ transformSpecificationClone: true,
33
+ });
34
+ };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@jaredwray/mockhttp",
3
+ "version": "0.0.1",
4
+ "description": "Mock Http - Easy way to mock http with httpbin replacement",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "directories": {
8
+ "test": "test"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/jaredwray/mockhttp.git"
13
+ },
14
+ "keywords": [
15
+ "mock",
16
+ "http",
17
+ "httpbin",
18
+ "test"
19
+ ],
20
+ "author": "Jared Wray <me@jaredwray.com>",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/jaredwray/mockhttp/issues"
24
+ },
25
+ "homepage": "https://github.com/jaredwray/mockhttp#readme",
26
+ "devDependencies": {
27
+ "@vitest/coverage-v8": "^2.1.5",
28
+ "tsx": "^4.19.2",
29
+ "typescript": "^5.7.2",
30
+ "vitest": "^2.1.5",
31
+ "xo": "^0.59.3"
32
+ },
33
+ "dependencies": {
34
+ "@fastify/static": "^8.0.3",
35
+ "@fastify/swagger": "^9.4.0",
36
+ "@fastify/swagger-ui": "^5.1.0",
37
+ "fastify": "^5.1.0",
38
+ "pino": "^9.5.0",
39
+ "pino-pretty": "^13.0.0"
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "public",
44
+ "LICENCE"
45
+ ],
46
+ "scripts": {
47
+ "test": "xo --fix && vitest run --coverage",
48
+ "dev": "xo --fix && npx tsx src/index.ts --watch",
49
+ "build": "tsc",
50
+ "start": "node dist/index.js"
51
+ }
52
+ }
Binary file
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 73.01 55.68">
3
+ <!-- Generator: Adobe Illustrator 29.1.0, SVG Export Plug-In . SVG Version: 2.1.0 Build 142) -->
4
+ <defs>
5
+ <style>
6
+ .st0 {
7
+ fill: #231f20;
8
+ }
9
+ </style>
10
+ </defs>
11
+ <path class="st0" d="M19.83,43.94c-2.09-.04-5.87-.9-5.87-5.18,0-2.59.22-4.64.22-6.91,0-1.94-.72-3.56-4.28-3.82v-1.22c3.56-.22,4.28-1.48,4.28-3.78s-.22-3.74-.22-6.05c0-4.28,3.64-5.26,5.87-5.36l.04,1.04c-2.81.5-3.31,2.09-3.31,3.89,0,2.3.25,3.64.25,5.87,0,2.59-1.84,4.46-4.07,4.93v.14c2.12.5,4.07,2.12,4.07,4.97,0,2.34-.25,4.25-.25,6.44,0,1.94.61,3.49,3.31,4l-.04,1.04Z"/>
12
+ <path class="st0" d="M42.4,37.5v-1.01c2.12-.22,2.38-.36,2.38-2.88v-6.01c0-2.56-.86-4.18-3.1-4.18-1.37,0-2.63.76-4,1.87.04.43.11.86.11,1.55v6.98c0,2.27.32,2.45,2.3,2.66v1.01h-7.7v-1.01c2.23-.22,2.56-.36,2.56-2.74v-6.19c0-2.66-.83-4.14-3.02-4.14-1.44,0-2.84.97-3.96,1.87v8.46c0,2.38.25,2.52,2.3,2.74v1.01h-7.74v-1.01c2.38-.22,2.59-.36,2.59-2.74v-7.45c0-2.3-.14-2.45-2.16-2.81v-.9c1.69-.25,3.38-.72,5-1.44v2.92c.76-.58,1.51-1.19,2.66-1.87.9-.58,1.69-.94,2.84-.94,1.73,0,3.2,1.08,3.92,2.95,1.01-.79,1.94-1.4,2.84-2.02.79-.5,1.8-.94,2.84-.94,2.81,0,4.54,2.02,4.54,5.54v6.95c0,2.41.22,2.52,2.27,2.74v1.01h-7.49Z"/>
13
+ <path class="st0" d="M52.48,11.62c2.12,0,5.9.9,5.9,5.15,0,2.63-.25,4.07-.25,6.34,0,1.91.76,3.6,4.32,3.78v1.22c-3.53.25-4.32,1.51-4.32,3.82s.25,4.32.25,6.62c0,4.28-3.71,5.36-5.9,5.4v-1.04c2.77-.5,3.31-2.05,3.31-3.92,0-2.3-.29-4.21-.29-6.44,0-2.63,1.87-4.46,4.07-4.97v-.14c-2.12-.47-4.07-2.34-4.07-4.93,0-2.09.29-3.67.29-5.87,0-1.94-.61-3.46-3.31-3.96v-1.04Z"/>
14
+ </svg>
@@ -0,0 +1,21 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 214.77 71.59">
3
+ <!-- Generator: Adobe Illustrator 29.1.0, SVG Export Plug-In . SVG Version: 2.1.0 Build 142) -->
4
+ <defs>
5
+ <style>
6
+ .st0 {
7
+ fill: #231f20;
8
+ }
9
+ </style>
10
+ </defs>
11
+ <path class="st0" d="M27.21,51.33c-2.09-.04-5.87-.9-5.87-5.18,0-2.59.22-4.64.22-6.91,0-1.94-.72-3.56-4.28-3.82v-1.22c3.56-.22,4.28-1.48,4.28-3.78s-.22-3.74-.22-6.05c0-4.28,3.64-5.26,5.87-5.36l.04,1.04c-2.81.5-3.31,2.09-3.31,3.89,0,2.3.25,3.64.25,5.87,0,2.59-1.84,4.46-4.07,4.93v.14c2.12.5,4.07,2.12,4.07,4.97,0,2.34-.25,4.25-.25,6.44,0,1.94.61,3.49,3.31,4l-.04,1.04Z"/>
12
+ <path class="st0" d="M58.17,44.89v-1.01c2.12-.22,2.38-.36,2.38-2.88v-6.01c0-2.56-.86-4.18-3.1-4.18-1.37,0-2.63.76-4,1.87.04.43.11.86.11,1.55v6.98c0,2.27.32,2.45,2.3,2.66v1.01h-7.7v-1.01c2.23-.22,2.56-.36,2.56-2.74v-6.19c0-2.66-.83-4.14-3.02-4.14-1.44,0-2.84.97-3.96,1.87v8.46c0,2.38.25,2.52,2.3,2.74v1.01h-7.74v-1.01c2.38-.22,2.59-.36,2.59-2.74v-7.45c0-2.3-.14-2.45-2.16-2.81v-.9c1.69-.25,3.38-.72,5-1.44v2.92c.76-.58,1.51-1.19,2.66-1.87.9-.58,1.69-.94,2.84-.94,1.73,0,3.2,1.08,3.92,2.95,1.01-.79,1.94-1.4,2.84-2.02.79-.5,1.8-.94,2.84-.94,2.81,0,4.54,2.02,4.54,5.54v6.95c0,2.41.22,2.52,2.27,2.74v1.01h-7.49Z"/>
13
+ <path class="st0" d="M75.85,28.65c4.5,0,7.85,3.42,7.85,8.06,0,5.83-4.32,8.6-7.85,8.6-4.97,0-7.99-4-7.99-8.03,0-5.87,4.57-8.64,7.99-8.64ZM75.42,29.88c-2.2,0-4.18,2.12-4.18,6.3,0,4.57,2.02,7.88,4.97,7.88,2.23,0,4.1-1.62,4.1-6.55,0-4.21-1.73-7.63-4.9-7.63Z"/>
14
+ <path class="st0" d="M100,41.65c-1.58,2.09-4,3.67-6.15,3.67-4.57,0-7.27-3.71-7.27-7.56,0-2.84,1.22-5.22,3.64-7.06,1.91-1.48,4.07-2.05,5.47-2.05,1.62,0,2.81.5,3.35.97.5.4.58.65.58,1.04,0,.86-.9,1.62-1.3,1.62-.18,0-.32-.07-.68-.36-1.12-1.01-2.34-1.51-3.64-1.51-2.38,0-4.46,2.02-4.46,5.76-.04,5,3.38,6.66,5.47,6.66,1.51,0,2.74-.4,4.39-1.98l.61.79Z"/>
15
+ <path class="st0" d="M119.3,44.89c-1.94,0-4.07.04-4.82.07-.22-.25-5.87-6.95-6.08-7.16-.5-.58-.72-.76-1.04-.76-.14,0-.43,0-.65.07v4c0,2.41.18,2.56,2.52,2.77v1.01h-7.96v-1.01c2.41-.22,2.59-.47,2.59-2.77v-17.21c0-2.34-.25-2.41-2.48-2.74v-.94c1.87-.18,4.03-.72,5.33-1.22v17.17c1.08-.25,1.76-.68,2.3-1.26.83-.83,2.16-2.45,2.77-3.35.76-1.04.47-1.33-1.26-1.48v-1.01l7.42-.32v.97c-2.52.4-3.24.68-4.79,2.3-.68.72-2.3,2.56-3.06,3.49.72,1.01,4.39,5.18,5.58,6.44,1.73,1.73,2.23,1.84,3.64,1.91v1.01Z"/>
16
+ <path class="st0" d="M129.34,44.89v-1.01c2.09-.22,2.38-.4,2.38-2.77v-5.9c0-2.99-1.15-4.5-3.56-4.46-1.37,0-2.81.68-3.82,1.87v8.5c0,2.34.29,2.56,2.45,2.77v1.01h-7.85v-1.01c2.09-.18,2.56-.36,2.56-2.77v-17.35c0-1.91-.11-2.38-2.52-2.59v-.94c1.91-.18,4.07-.72,5.36-1.22v12.27c1.3-1.3,3.28-2.63,5.22-2.63,2.95,0,5,1.84,5,6.16v6.33c0,2.38.29,2.56,2.38,2.74v1.01h-7.6Z"/>
17
+ <path class="st0" d="M145.79,44.85c-.61.32-1.19.47-1.55.47-2.3,0-3.53-1.4-3.53-4.28v-10.47h-2.48l-.14-.43.97-1.04h1.66v-3.02c.68-.72,1.73-1.66,2.41-2.38l.54.07c-.07,1.51-.11,3.6-.11,5.33h4.1c.32.36.22,1.15-.22,1.48h-3.89v9.21c0,2.92,1.15,3.46,2.09,3.46.86,0,1.69-.32,2.2-.58l.32.94-2.38,1.26Z"/>
18
+ <path class="st0" d="M156.34,44.85c-.61.32-1.19.47-1.55.47-2.3,0-3.53-1.4-3.53-4.28v-10.47h-2.48l-.14-.43.97-1.04h1.66v-3.02c.68-.72,1.73-1.66,2.41-2.38l.54.07c-.07,1.51-.11,3.6-.11,5.33h4.1c.32.36.22,1.15-.22,1.48h-3.89v9.21c0,2.92,1.15,3.46,2.09,3.46.86,0,1.69-.32,2.2-.58l.32.94-2.38,1.26Z"/>
19
+ <path class="st0" d="M159.62,29.84c1.69-.25,3.56-.86,5.22-1.55v2.88c.11-.07,2.99-1.73,3.13-1.8.86-.47,1.62-.72,2.09-.72,3.96,0,6.37,3.31,6.37,6.88,0,5.47-4.32,8.93-9.04,9.79-1.08-.04-2.05-.36-2.56-.65v4.86c0,2.66.29,2.88,3.06,3.13v1.01h-8.53v-1.01c2.34-.25,2.63-.43,2.63-2.84v-16.38c0-2.16-.07-2.3-2.38-2.63v-.97ZM164.84,42.15c.83.83,2.23,1.4,3.42,1.4,3.13,0,5.15-2.63,5.15-6.66s-2.27-6.08-4.82-6.08c-1.26,0-2.77.83-3.74,1.55v9.79Z"/>
20
+ <path class="st0" d="M187.91,19.01c2.12,0,5.9.9,5.9,5.15,0,2.63-.25,4.07-.25,6.33,0,1.91.76,3.6,4.32,3.78v1.22c-3.53.25-4.32,1.51-4.32,3.82s.25,4.32.25,6.62c0,4.28-3.71,5.36-5.9,5.4v-1.04c2.77-.5,3.31-2.05,3.31-3.92,0-2.3-.29-4.21-.29-6.44,0-2.63,1.87-4.46,4.07-4.97v-.14c-2.12-.47-4.07-2.34-4.07-4.93,0-2.09.29-3.67.29-5.87,0-1.94-.61-3.46-3.31-3.96v-1.04Z"/>
21
+ </svg>