@jaredwray/mockhttp 0.1.0 → 0.2.0
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/dist/index.js +183 -2
- package/package.json +3 -1
package/dist/index.js
CHANGED
|
@@ -102,7 +102,7 @@ import { fastifySwaggerUi } from "@fastify/swagger-ui";
|
|
|
102
102
|
// package.json
|
|
103
103
|
var package_default = {
|
|
104
104
|
name: "@jaredwray/mockhttp",
|
|
105
|
-
version: "0.
|
|
105
|
+
version: "0.2.0",
|
|
106
106
|
description: "Mock Http - Easy way to mock http with httpbin replacement",
|
|
107
107
|
type: "module",
|
|
108
108
|
main: "dist/index.js",
|
|
@@ -138,6 +138,7 @@ var package_default = {
|
|
|
138
138
|
homepage: "https://github.com/jaredwray/mockhttp#readme",
|
|
139
139
|
devDependencies: {
|
|
140
140
|
"@swc/core": "^1.10.1",
|
|
141
|
+
"@types/html-escaper": "^3.0.2",
|
|
141
142
|
"@vitest/coverage-v8": "^2.1.8",
|
|
142
143
|
rimraf: "^6.0.1",
|
|
143
144
|
tsup: "^8.3.5",
|
|
@@ -153,6 +154,7 @@ var package_default = {
|
|
|
153
154
|
"@fastify/swagger-ui": "^5.2.0",
|
|
154
155
|
"@scalar/api-reference": "^1.25.80",
|
|
155
156
|
fastify: "^5.1.0",
|
|
157
|
+
"html-escaper": "^3.0.3",
|
|
156
158
|
pino: "^9.5.0",
|
|
157
159
|
"pino-pretty": "^13.0.0"
|
|
158
160
|
},
|
|
@@ -865,6 +867,182 @@ var statusCodeRoute = /* @__PURE__ */ __name((fastify2) => {
|
|
|
865
867
|
}, handler);
|
|
866
868
|
}, "statusCodeRoute");
|
|
867
869
|
|
|
870
|
+
// src/routes/response-inspection/cache.ts
|
|
871
|
+
var cacheGetSchema = {
|
|
872
|
+
description: "Handles cache validation and retrieval.",
|
|
873
|
+
tags: [
|
|
874
|
+
"Response Inspection"
|
|
875
|
+
],
|
|
876
|
+
response: {
|
|
877
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
878
|
+
200: {
|
|
879
|
+
type: "string",
|
|
880
|
+
description: "The cached content or resource."
|
|
881
|
+
},
|
|
882
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
883
|
+
304: {
|
|
884
|
+
type: "null",
|
|
885
|
+
description: "Indicates the resource has not been modified."
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
var cacheSetSchema = {
|
|
890
|
+
description: "Sets a Cache-Control header for n seconds.",
|
|
891
|
+
tags: [
|
|
892
|
+
"Response Inspection"
|
|
893
|
+
],
|
|
894
|
+
params: {
|
|
895
|
+
type: "object",
|
|
896
|
+
properties: {
|
|
897
|
+
value: {
|
|
898
|
+
type: "integer",
|
|
899
|
+
description: "Number of seconds to cache the resource."
|
|
900
|
+
}
|
|
901
|
+
},
|
|
902
|
+
required: [
|
|
903
|
+
"value"
|
|
904
|
+
]
|
|
905
|
+
},
|
|
906
|
+
response: {
|
|
907
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
908
|
+
200: {
|
|
909
|
+
type: "string",
|
|
910
|
+
description: "Confirmation message for setting cache."
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
var cacheRoutes = /* @__PURE__ */ __name((fastify2) => {
|
|
915
|
+
fastify2.get("/cache", {
|
|
916
|
+
schema: cacheGetSchema
|
|
917
|
+
}, async (request, reply) => {
|
|
918
|
+
const ifModifiedSince = request.headers["if-modified-since"];
|
|
919
|
+
const ifNoneMatch = request.headers["if-none-match"];
|
|
920
|
+
if (ifModifiedSince ?? ifNoneMatch) {
|
|
921
|
+
await reply.status(304).send();
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
await reply.send("Cache content or resource response here.");
|
|
925
|
+
});
|
|
926
|
+
fastify2.get("/cache/:value", {
|
|
927
|
+
schema: cacheSetSchema
|
|
928
|
+
}, async (request, reply) => {
|
|
929
|
+
const { value } = request.params;
|
|
930
|
+
await reply.header("Cache-Control", `max-age=${value}`).send(`Cache-Control set for ${value} seconds.`);
|
|
931
|
+
});
|
|
932
|
+
}, "cacheRoutes");
|
|
933
|
+
|
|
934
|
+
// src/routes/response-inspection/etag.ts
|
|
935
|
+
var etagSchema = {
|
|
936
|
+
description: "Handles ETag-based conditional requests.",
|
|
937
|
+
tags: [
|
|
938
|
+
"Response Inspection"
|
|
939
|
+
],
|
|
940
|
+
params: {
|
|
941
|
+
type: "object",
|
|
942
|
+
properties: {
|
|
943
|
+
etag: {
|
|
944
|
+
type: "string",
|
|
945
|
+
description: "The ETag value for the resource."
|
|
946
|
+
}
|
|
947
|
+
},
|
|
948
|
+
required: [
|
|
949
|
+
"etag"
|
|
950
|
+
]
|
|
951
|
+
},
|
|
952
|
+
response: {
|
|
953
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
954
|
+
200: {
|
|
955
|
+
type: "string",
|
|
956
|
+
description: "Resource content for matching ETag."
|
|
957
|
+
},
|
|
958
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
959
|
+
304: {
|
|
960
|
+
type: "null",
|
|
961
|
+
description: "Indicates the resource has not been modified."
|
|
962
|
+
},
|
|
963
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
964
|
+
412: {
|
|
965
|
+
type: "null",
|
|
966
|
+
description: "Indicates a precondition failed due to mismatched ETag."
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
var etagRoutes = /* @__PURE__ */ __name((fastify2) => {
|
|
971
|
+
fastify2.get("/etag/:etag", {
|
|
972
|
+
schema: etagSchema
|
|
973
|
+
}, async (request, reply) => {
|
|
974
|
+
const { etag } = request.params;
|
|
975
|
+
const ifNoneMatch = request.headers["if-none-match"];
|
|
976
|
+
const ifMatch = request.headers["if-match"];
|
|
977
|
+
if (ifNoneMatch === etag) {
|
|
978
|
+
await reply.status(304).send();
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
if (ifMatch && ifMatch !== etag) {
|
|
982
|
+
await reply.status(412).send();
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
await reply.header("ETag", etag).send("Resource content for matching ETag.");
|
|
986
|
+
});
|
|
987
|
+
}, "etagRoutes");
|
|
988
|
+
|
|
989
|
+
// src/routes/response-inspection/response-headers.ts
|
|
990
|
+
import { escape } from "html-escaper";
|
|
991
|
+
var responseHeadersSchema = {
|
|
992
|
+
description: "Returns a set of response headers based on the query string.",
|
|
993
|
+
tags: [
|
|
994
|
+
"Response Inspection"
|
|
995
|
+
],
|
|
996
|
+
querystring: {
|
|
997
|
+
type: "object",
|
|
998
|
+
additionalProperties: true,
|
|
999
|
+
description: "Freeform query string parameters to be added as response headers."
|
|
1000
|
+
},
|
|
1001
|
+
response: {
|
|
1002
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1003
|
+
200: {
|
|
1004
|
+
type: "object",
|
|
1005
|
+
additionalProperties: true,
|
|
1006
|
+
description: "Response headers as per query string parameters."
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
1010
|
+
var responseHeadersRoutes = /* @__PURE__ */ __name((fastify2) => {
|
|
1011
|
+
fastify2.get("/response-headers", {
|
|
1012
|
+
schema: responseHeadersSchema
|
|
1013
|
+
}, async (request, reply) => {
|
|
1014
|
+
const queryParameters = request.query;
|
|
1015
|
+
for (const [key, value] of Object.entries(queryParameters)) {
|
|
1016
|
+
reply.header(key, escape(value));
|
|
1017
|
+
}
|
|
1018
|
+
const cleanedResponse = Object.fromEntries(Object.entries(queryParameters).map(([key, value]) => [
|
|
1019
|
+
key,
|
|
1020
|
+
escape(value)
|
|
1021
|
+
]));
|
|
1022
|
+
await reply.send(cleanedResponse);
|
|
1023
|
+
});
|
|
1024
|
+
fastify2.post("/response-headers", {
|
|
1025
|
+
schema: responseHeadersSchema
|
|
1026
|
+
}, async (request, reply) => {
|
|
1027
|
+
const queryParameters = request.query ?? {};
|
|
1028
|
+
const body = request.body ?? {};
|
|
1029
|
+
for (const [key, value] of Object.entries(queryParameters)) {
|
|
1030
|
+
reply.header(key, escape(value));
|
|
1031
|
+
}
|
|
1032
|
+
for (const [key, value] of Object.entries(body)) {
|
|
1033
|
+
reply.header(key, escape(value));
|
|
1034
|
+
}
|
|
1035
|
+
const cleanedResponse = Object.fromEntries(Object.entries({
|
|
1036
|
+
...queryParameters,
|
|
1037
|
+
...body
|
|
1038
|
+
}).map(([key, value]) => [
|
|
1039
|
+
key,
|
|
1040
|
+
escape(value)
|
|
1041
|
+
]));
|
|
1042
|
+
await reply.send(cleanedResponse);
|
|
1043
|
+
});
|
|
1044
|
+
}, "responseHeadersRoutes");
|
|
1045
|
+
|
|
868
1046
|
// src/index.ts
|
|
869
1047
|
var fastify = Fastify(fastifyConfig);
|
|
870
1048
|
await fastify.register(fastifyStatic, {
|
|
@@ -884,10 +1062,13 @@ await fastify.register(postRoute);
|
|
|
884
1062
|
await fastify.register(deleteRoute);
|
|
885
1063
|
await fastify.register(putRoute);
|
|
886
1064
|
await fastify.register(patchRoute);
|
|
1065
|
+
await fastify.register(statusCodeRoute);
|
|
887
1066
|
await fastify.register(ipRoute);
|
|
888
1067
|
await fastify.register(headersRoute);
|
|
889
1068
|
await fastify.register(userAgentRoute);
|
|
890
|
-
await fastify.register(
|
|
1069
|
+
await fastify.register(cacheRoutes);
|
|
1070
|
+
await fastify.register(etagRoutes);
|
|
1071
|
+
await fastify.register(responseHeadersRoutes);
|
|
891
1072
|
var start = /* @__PURE__ */ __name(async () => {
|
|
892
1073
|
try {
|
|
893
1074
|
let port = 3e3;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jaredwray/mockhttp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Mock Http - Easy way to mock http with httpbin replacement",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"homepage": "https://github.com/jaredwray/mockhttp#readme",
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@swc/core": "^1.10.1",
|
|
39
|
+
"@types/html-escaper": "^3.0.2",
|
|
39
40
|
"@vitest/coverage-v8": "^2.1.8",
|
|
40
41
|
"rimraf": "^6.0.1",
|
|
41
42
|
"tsup": "^8.3.5",
|
|
@@ -51,6 +52,7 @@
|
|
|
51
52
|
"@fastify/swagger-ui": "^5.2.0",
|
|
52
53
|
"@scalar/api-reference": "^1.25.80",
|
|
53
54
|
"fastify": "^5.1.0",
|
|
55
|
+
"html-escaper": "^3.0.3",
|
|
54
56
|
"pino": "^9.5.0",
|
|
55
57
|
"pino-pretty": "^13.0.0"
|
|
56
58
|
},
|