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.
@@ -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
  });