api-json-server 1.1.0 → 1.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/README.md +476 -126
- package/dist/behavior.js +12 -0
- package/dist/history/historyRecorder.js +66 -0
- package/dist/history/types.js +2 -0
- package/dist/index.js +29 -18
- package/dist/logger/customLogger.js +75 -0
- package/dist/logger/formatters.js +82 -0
- package/dist/logger/types.js +2 -0
- package/dist/registerEndpoints.js +20 -3
- package/dist/requestMatch.js +40 -1
- package/dist/server.js +62 -2
- package/dist/spec.js +38 -2
- package/package.json +6 -1
- package/src/behavior.ts +15 -1
- package/src/history/historyRecorder.ts +77 -0
- package/src/history/types.ts +25 -0
- package/src/index.ts +34 -21
- package/src/logger/customLogger.ts +85 -0
- package/src/logger/formatters.ts +74 -0
- package/src/logger/types.ts +30 -0
- package/src/registerEndpoints.ts +24 -6
- package/src/requestMatch.ts +43 -1
- package/src/server.ts +77 -4
- package/src/spec.ts +40 -2
- package/tests/cors.test.ts +128 -0
- package/tests/headers.test.ts +124 -0
- package/tests/helpers.ts +2 -2
- package/tests/history.test.ts +188 -0
- package/tests/matching.test.ts +109 -0
package/tests/matching.test.ts
CHANGED
|
@@ -133,4 +133,113 @@ describe("matching rules", () => {
|
|
|
133
133
|
|
|
134
134
|
await app.close();
|
|
135
135
|
});
|
|
136
|
+
|
|
137
|
+
it("matches request headers (case-insensitive)", async () => {
|
|
138
|
+
const app = buildTestServer({
|
|
139
|
+
version: 1,
|
|
140
|
+
settings: baseSettings,
|
|
141
|
+
endpoints: [
|
|
142
|
+
{
|
|
143
|
+
method: "GET",
|
|
144
|
+
path: "/api/data",
|
|
145
|
+
match: { headers: { Authorization: "Bearer token123" } },
|
|
146
|
+
response: { ok: true, data: "secure" }
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const ok = await app.inject({
|
|
152
|
+
method: "GET",
|
|
153
|
+
url: "/api/data",
|
|
154
|
+
headers: { authorization: "Bearer token123" }
|
|
155
|
+
});
|
|
156
|
+
expect(ok.statusCode).toBe(200);
|
|
157
|
+
expect(ok.json()).toEqual({ ok: true, data: "secure" });
|
|
158
|
+
|
|
159
|
+
const bad = await app.inject({
|
|
160
|
+
method: "GET",
|
|
161
|
+
url: "/api/data",
|
|
162
|
+
headers: { authorization: "Bearer wrong" }
|
|
163
|
+
});
|
|
164
|
+
expect(bad.statusCode).toBe(404);
|
|
165
|
+
|
|
166
|
+
await app.close();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("matches cookies", async () => {
|
|
170
|
+
const app = buildTestServer({
|
|
171
|
+
version: 1,
|
|
172
|
+
settings: baseSettings,
|
|
173
|
+
endpoints: [
|
|
174
|
+
{
|
|
175
|
+
method: "GET",
|
|
176
|
+
path: "/profile",
|
|
177
|
+
match: { cookies: { sessionId: "abc123" } },
|
|
178
|
+
response: { user: "john" }
|
|
179
|
+
}
|
|
180
|
+
]
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const ok = await app.inject({
|
|
184
|
+
method: "GET",
|
|
185
|
+
url: "/profile",
|
|
186
|
+
headers: { cookie: "sessionId=abc123" }
|
|
187
|
+
});
|
|
188
|
+
expect(ok.statusCode).toBe(200);
|
|
189
|
+
expect(ok.json()).toEqual({ user: "john" });
|
|
190
|
+
|
|
191
|
+
const bad = await app.inject({
|
|
192
|
+
method: "GET",
|
|
193
|
+
url: "/profile",
|
|
194
|
+
headers: { cookie: "sessionId=wrong" }
|
|
195
|
+
});
|
|
196
|
+
expect(bad.statusCode).toBe(404);
|
|
197
|
+
|
|
198
|
+
await app.close();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("combines query, body, headers, and cookies in match rules", async () => {
|
|
202
|
+
const app = buildTestServer({
|
|
203
|
+
version: 1,
|
|
204
|
+
settings: baseSettings,
|
|
205
|
+
endpoints: [
|
|
206
|
+
{
|
|
207
|
+
method: "POST",
|
|
208
|
+
path: "/api/action",
|
|
209
|
+
match: {
|
|
210
|
+
query: { type: "premium" },
|
|
211
|
+
body: { action: "delete" },
|
|
212
|
+
headers: { "X-API-Key": "secret" },
|
|
213
|
+
cookies: { session: "valid" }
|
|
214
|
+
},
|
|
215
|
+
response: { ok: true }
|
|
216
|
+
}
|
|
217
|
+
]
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const ok = await app.inject({
|
|
221
|
+
method: "POST",
|
|
222
|
+
url: "/api/action?type=premium",
|
|
223
|
+
headers: {
|
|
224
|
+
"x-api-key": "secret",
|
|
225
|
+
cookie: "session=valid"
|
|
226
|
+
},
|
|
227
|
+
payload: { action: "delete" }
|
|
228
|
+
});
|
|
229
|
+
expect(ok.statusCode).toBe(200);
|
|
230
|
+
|
|
231
|
+
// Missing one of the requirements
|
|
232
|
+
const bad = await app.inject({
|
|
233
|
+
method: "POST",
|
|
234
|
+
url: "/api/action?type=premium",
|
|
235
|
+
headers: {
|
|
236
|
+
"x-api-key": "secret",
|
|
237
|
+
cookie: "session=wrong"
|
|
238
|
+
},
|
|
239
|
+
payload: { action: "delete" }
|
|
240
|
+
});
|
|
241
|
+
expect(bad.statusCode).toBe(404);
|
|
242
|
+
|
|
243
|
+
await app.close();
|
|
244
|
+
});
|
|
136
245
|
});
|