@stratal/testing 0.0.2 → 0.0.5

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.
Files changed (106) hide show
  1. package/CHANGELOG.md +117 -0
  2. package/dist/auth/acting-as.d.ts +21 -0
  3. package/dist/auth/acting-as.d.ts.map +1 -0
  4. package/dist/auth/acting-as.js +68 -0
  5. package/dist/auth/acting-as.js.map +1 -0
  6. package/dist/auth/index.d.ts +2 -0
  7. package/dist/auth/index.d.ts.map +1 -0
  8. package/dist/auth/index.js +2 -0
  9. package/dist/auth/index.js.map +1 -0
  10. package/dist/core/env/index.d.ts +2 -0
  11. package/dist/core/env/index.d.ts.map +1 -0
  12. package/dist/core/env/index.js +2 -0
  13. package/dist/core/env/index.js.map +1 -0
  14. package/dist/core/env/test-env.d.ts +9 -0
  15. package/dist/core/env/test-env.d.ts.map +1 -0
  16. package/dist/core/env/test-env.js +14 -0
  17. package/dist/core/env/test-env.js.map +1 -0
  18. package/dist/core/http/fetch-mock.d.ts +236 -0
  19. package/dist/core/http/fetch-mock.d.ts.map +1 -0
  20. package/dist/core/http/fetch-mock.js +290 -0
  21. package/dist/core/http/fetch-mock.js.map +1 -0
  22. package/dist/core/http/fetch-mock.types.d.ts +48 -0
  23. package/dist/core/http/fetch-mock.types.d.ts.map +1 -0
  24. package/dist/core/http/fetch-mock.types.js +2 -0
  25. package/dist/core/http/fetch-mock.types.js.map +1 -0
  26. package/dist/core/http/index.d.ts +6 -0
  27. package/dist/core/http/index.d.ts.map +1 -0
  28. package/dist/core/http/index.js +5 -0
  29. package/dist/core/http/index.js.map +1 -0
  30. package/dist/core/http/test-http-client.d.ts +54 -0
  31. package/dist/core/http/test-http-client.d.ts.map +1 -0
  32. package/dist/core/http/test-http-client.js +75 -0
  33. package/dist/core/http/test-http-client.js.map +1 -0
  34. package/dist/core/http/test-http-request.d.ts +60 -0
  35. package/dist/core/http/test-http-request.d.ts.map +1 -0
  36. package/dist/core/http/test-http-request.js +106 -0
  37. package/dist/core/http/test-http-request.js.map +1 -0
  38. package/dist/core/http/test-response.d.ts +161 -0
  39. package/dist/core/http/test-response.d.ts.map +1 -0
  40. package/dist/core/http/test-response.js +309 -0
  41. package/dist/core/http/test-response.js.map +1 -0
  42. package/dist/core/index.d.ts +7 -0
  43. package/dist/core/index.d.ts.map +1 -0
  44. package/dist/core/index.js +7 -0
  45. package/dist/core/index.js.map +1 -0
  46. package/dist/core/override/index.d.ts +2 -0
  47. package/dist/core/override/index.d.ts.map +1 -0
  48. package/dist/core/override/index.js +2 -0
  49. package/dist/core/override/index.js.map +1 -0
  50. package/dist/core/override/provider-override-builder.d.ts +77 -0
  51. package/dist/core/override/provider-override-builder.d.ts.map +1 -0
  52. package/dist/core/override/provider-override-builder.js +94 -0
  53. package/dist/core/override/provider-override-builder.js.map +1 -0
  54. package/dist/core/test.d.ts +48 -0
  55. package/dist/core/test.d.ts.map +1 -0
  56. package/dist/core/test.js +53 -0
  57. package/dist/core/test.js.map +1 -0
  58. package/dist/core/testing-module-builder.d.ts +99 -0
  59. package/dist/core/testing-module-builder.d.ts.map +1 -0
  60. package/dist/core/testing-module-builder.js +156 -0
  61. package/dist/core/testing-module-builder.js.map +1 -0
  62. package/dist/core/testing-module.d.ts +101 -0
  63. package/dist/core/testing-module.d.ts.map +1 -0
  64. package/dist/core/testing-module.js +178 -0
  65. package/dist/core/testing-module.js.map +1 -0
  66. package/dist/errors/index.d.ts +3 -0
  67. package/dist/errors/index.d.ts.map +1 -0
  68. package/dist/errors/index.js +3 -0
  69. package/dist/errors/index.js.map +1 -0
  70. package/dist/errors/setup-error.d.ts +9 -0
  71. package/dist/errors/setup-error.d.ts.map +1 -0
  72. package/dist/errors/setup-error.js +11 -0
  73. package/dist/errors/setup-error.js.map +1 -0
  74. package/dist/errors/test-error.d.ts +9 -0
  75. package/dist/errors/test-error.d.ts.map +1 -0
  76. package/dist/errors/test-error.js +15 -0
  77. package/dist/errors/test-error.js.map +1 -0
  78. package/dist/index.d.ts +15 -0
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/index.js +6 -3
  81. package/dist/index.js.map +1 -1
  82. package/dist/mocks/index.d.ts +3 -0
  83. package/dist/mocks/index.d.ts.map +1 -0
  84. package/dist/mocks/index.js +3 -0
  85. package/dist/mocks/index.js.map +1 -0
  86. package/dist/mocks/nodemailer.d.ts +10 -0
  87. package/dist/mocks/nodemailer.d.ts.map +1 -0
  88. package/dist/mocks/nodemailer.js +9 -0
  89. package/dist/mocks/nodemailer.js.map +1 -0
  90. package/dist/mocks/zenstack-language.d.ts +46 -0
  91. package/dist/mocks/zenstack-language.d.ts.map +1 -0
  92. package/dist/mocks/zenstack-language.js +47 -0
  93. package/dist/mocks/zenstack-language.js.map +1 -0
  94. package/dist/storage/fake-storage.service.d.ts +114 -0
  95. package/dist/storage/fake-storage.service.d.ts.map +1 -0
  96. package/dist/storage/fake-storage.service.js +233 -0
  97. package/dist/storage/fake-storage.service.js.map +1 -0
  98. package/dist/storage/index.d.ts +2 -0
  99. package/dist/storage/index.d.ts.map +1 -0
  100. package/dist/storage/index.js +2 -0
  101. package/dist/storage/index.js.map +1 -0
  102. package/dist/types.d.ts +5 -0
  103. package/dist/types.d.ts.map +1 -0
  104. package/dist/types.js +3 -0
  105. package/dist/types.js.map +1 -0
  106. package/package.json +27 -10
@@ -0,0 +1,290 @@
1
+ import { fetchMock } from 'cloudflare:test';
2
+ /**
3
+ * Wrapper around Cloudflare's fetchMock for declarative fetch mocking in tests
4
+ *
5
+ * Based on undici's MockAgent, fetchMock.get(origin) returns a MockPool for that origin.
6
+ * The MockPool's intercept() method is used to define which requests to mock.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { createFetchMock } from '@stratal/testing'
11
+ *
12
+ * const mock = createFetchMock()
13
+ *
14
+ * beforeEach(() => {
15
+ * mock.activate()
16
+ * mock.disableNetConnect()
17
+ * })
18
+ *
19
+ * afterEach(() => {
20
+ * mock.reset()
21
+ * })
22
+ *
23
+ * it('should mock external API', async () => {
24
+ * // Using helper method
25
+ * mock.mockJsonResponse('https://api.example.com/data', { success: true })
26
+ *
27
+ * // Or using direct API
28
+ * mock.get('https://api.example.com')
29
+ * .intercept({ path: '/users', method: 'POST' })
30
+ * .reply(201, JSON.stringify({ created: true }))
31
+ *
32
+ * const response = await fetch('https://api.example.com/data')
33
+ * const json = await response.json()
34
+ *
35
+ * expect(json.success).toBe(true)
36
+ * mock.assertNoPendingInterceptors()
37
+ * })
38
+ * ```
39
+ */
40
+ export class FetchMock {
41
+ /**
42
+ * Get a MockPool for the specified origin
43
+ *
44
+ * This is the underlying fetchMock.get() method that returns a MockPool
45
+ * for mocking requests to the specified origin.
46
+ *
47
+ * @param origin - The origin URL (e.g., 'https://api.example.com')
48
+ * @returns MockPool for chaining .intercept() and .reply()
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * // Mock a GET request
53
+ * mock.get('https://api.example.com')
54
+ * .intercept({ path: '/users', method: 'GET' })
55
+ * .reply(200, JSON.stringify({ users: [] }))
56
+ *
57
+ * // Mock a POST request with body matching
58
+ * mock.get('https://api.example.com')
59
+ * .intercept({
60
+ * path: '/users',
61
+ * method: 'POST',
62
+ * body: (body) => body.includes('test')
63
+ * })
64
+ * .reply(201, JSON.stringify({ created: true }))
65
+ * ```
66
+ */
67
+ get(origin) {
68
+ return fetchMock.get(origin);
69
+ }
70
+ /**
71
+ * Activate fetch mocking for the current test
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * beforeEach(() => {
76
+ * mock.activate()
77
+ * })
78
+ * ```
79
+ */
80
+ activate() {
81
+ fetchMock.activate();
82
+ }
83
+ /**
84
+ * Deactivate fetch mocking
85
+ */
86
+ deactivate() {
87
+ fetchMock.deactivate();
88
+ }
89
+ /**
90
+ * Disable all network connections, forcing all requests to use mocks
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * beforeEach(() => {
95
+ * mock.activate()
96
+ * mock.disableNetConnect() // Ensure all requests are mocked
97
+ * })
98
+ * ```
99
+ */
100
+ disableNetConnect() {
101
+ fetchMock.disableNetConnect();
102
+ }
103
+ /**
104
+ * Enable network connections for specific hosts
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * mock.enableNetConnect('localhost')
109
+ * mock.enableNetConnect(/^https:\/\/trusted\.com/)
110
+ * ```
111
+ */
112
+ enableNetConnect(host) {
113
+ if (host) {
114
+ fetchMock.enableNetConnect(host);
115
+ return;
116
+ }
117
+ fetchMock.enableNetConnect();
118
+ }
119
+ /**
120
+ * Assert that all defined interceptors were called
121
+ *
122
+ * @throws {Error} If there are pending interceptors that weren't matched
123
+ *
124
+ * @example
125
+ * ```typescript
126
+ * it('should call all mocked endpoints', async () => {
127
+ * mock.mockJsonResponse('https://api.example.com/data', { data: [] })
128
+ *
129
+ * await fetch('https://api.example.com/data')
130
+ *
131
+ * mock.assertNoPendingInterceptors() // Pass
132
+ * })
133
+ * ```
134
+ */
135
+ assertNoPendingInterceptors() {
136
+ fetchMock.assertNoPendingInterceptors();
137
+ }
138
+ /**
139
+ * Reset all mocks and interceptors
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * afterEach(() => {
144
+ * mock.reset()
145
+ * })
146
+ * ```
147
+ */
148
+ reset() {
149
+ // Reset by deactivating
150
+ fetchMock.deactivate();
151
+ }
152
+ /**
153
+ * Helper method to mock JSON responses
154
+ *
155
+ * Automatically parses the URL into origin and path, and sets up the mock.
156
+ *
157
+ * @param url - Full URL to mock (e.g., 'https://api.example.com/users')
158
+ * @param data - JSON data to return
159
+ * @param options - Additional options for the mock
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * // Mock a GET request
164
+ * mock.mockJsonResponse('https://api.example.com/users', { users: [] })
165
+ *
166
+ * // Mock a POST request
167
+ * mock.mockJsonResponse(
168
+ * 'https://api.example.com/users',
169
+ * { created: true },
170
+ * { method: 'POST', status: 201 }
171
+ * )
172
+ *
173
+ * // With custom headers and delay
174
+ * mock.mockJsonResponse(
175
+ * 'https://api.example.com/users',
176
+ * { users: [] },
177
+ * {
178
+ * status: 200,
179
+ * method: 'GET',
180
+ * headers: { 'X-Custom': 'value' },
181
+ * delay: 100
182
+ * }
183
+ * )
184
+ * ```
185
+ */
186
+ mockJsonResponse(url, data, options = {}) {
187
+ const parsedUrl = new URL(url);
188
+ const origin = `${parsedUrl.protocol}//${parsedUrl.host}`;
189
+ const path = options.path ?? parsedUrl.pathname;
190
+ const method = options.method ?? 'GET';
191
+ const { status = 200, headers = {}, delay } = options;
192
+ const defaultHeaders = {
193
+ 'Content-Type': 'application/json',
194
+ ...headers,
195
+ };
196
+ const mock = fetchMock
197
+ .get(origin)
198
+ .intercept({ path, method })
199
+ .reply(status, JSON.stringify(data), { headers: defaultHeaders });
200
+ if (delay) {
201
+ return mock.delay(delay);
202
+ }
203
+ return mock;
204
+ }
205
+ /**
206
+ * Helper method to mock error responses
207
+ *
208
+ * @param url - Full URL to mock
209
+ * @param status - HTTP error status code
210
+ * @param message - Optional error message
211
+ * @param options - Additional options for the error mock
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * // Mock a 401 error
216
+ * mock.mockError('https://api.example.com/fail', 401, 'Unauthorized')
217
+ *
218
+ * // Mock a 500 error with custom method
219
+ * mock.mockError(
220
+ * 'https://api.example.com/fail',
221
+ * 500,
222
+ * 'Server Error',
223
+ * { method: 'POST' }
224
+ * )
225
+ * ```
226
+ */
227
+ mockError(url, status, message, options = {}) {
228
+ const parsedUrl = new URL(url);
229
+ const origin = `${parsedUrl.protocol}//${parsedUrl.host}`;
230
+ const path = options.path ?? parsedUrl.pathname;
231
+ const method = options.method ?? 'GET';
232
+ const { headers = {} } = options;
233
+ const body = message ? JSON.stringify({ error: message }) : '';
234
+ const responseHeaders = message
235
+ ? { 'Content-Type': 'application/json', ...headers }
236
+ : headers;
237
+ return fetchMock
238
+ .get(origin)
239
+ .intercept({ path, method })
240
+ .reply(status, body, { headers: responseHeaders });
241
+ }
242
+ /**
243
+ * Generic helper to mock any HTTP request
244
+ *
245
+ * @param origin - The origin URL (e.g., 'https://api.example.com')
246
+ * @param options - Request matching and response options
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * mock.mockRequest('https://api.example.com', {
251
+ * path: '/users',
252
+ * method: 'PUT',
253
+ * status: 200,
254
+ * body: { updated: true }
255
+ * })
256
+ * ```
257
+ */
258
+ mockRequest(origin, options) {
259
+ const { path, method = 'GET', status = 200, body } = options;
260
+ return fetchMock
261
+ .get(origin)
262
+ .intercept({ path, method })
263
+ .reply(status, body ? JSON.stringify(body) : '');
264
+ }
265
+ }
266
+ /**
267
+ * Factory function to create a new FetchMock instance
268
+ *
269
+ * @returns A new FetchMock instance
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * import { createFetchMock } from '@stratal/testing'
274
+ *
275
+ * const mock = createFetchMock()
276
+ *
277
+ * beforeEach(() => {
278
+ * mock.activate()
279
+ * mock.disableNetConnect()
280
+ * })
281
+ *
282
+ * afterEach(() => {
283
+ * mock.reset()
284
+ * })
285
+ * ```
286
+ */
287
+ export function createFetchMock() {
288
+ return new FetchMock();
289
+ }
290
+ //# sourceMappingURL=fetch-mock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-mock.js","sourceRoot":"","sources":["../../../src/core/http/fetch-mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAG3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,OAAO,SAAS;IACrB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,GAAG,CAAC,MAAc;QACjB,OAAO,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAC7B,CAAC;IAED;;;;;;;;;OASG;IACH,QAAQ;QACP,SAAS,CAAC,QAAQ,EAAE,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,UAAU;QACT,SAAS,CAAC,UAAU,EAAE,CAAC;IACxB,CAAC;IAED;;;;;;;;;;OAUG;I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AAO,SAAS;aACd,GAAG,CAAC,MAAM,CAAC;aACX,SAAS,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;aAC3B,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IAClD,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,eAAe;IAC9B,OAAO,IAAI,SAAS,EAAE,CAAA;AACvB,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Options for mocking JSON responses
3
+ */
4
+ export interface MockJsonOptions {
5
+ /**
6
+ * HTTP status code for the response
7
+ * @default 200
8
+ */
9
+ status?: number;
10
+ /**
11
+ * Custom headers to include in the response
12
+ */
13
+ headers?: Record<string, string>;
14
+ /**
15
+ * Delay in milliseconds before responding
16
+ */
17
+ delay?: number;
18
+ /**
19
+ * HTTP method to match (GET, POST, PUT, PATCH, DELETE, etc.)
20
+ * @default 'GET'
21
+ */
22
+ method?: string;
23
+ /**
24
+ * Specific path to override URL pathname
25
+ * If not provided, the pathname from the URL will be used
26
+ */
27
+ path?: string;
28
+ }
29
+ /**
30
+ * Options for mocking error responses
31
+ */
32
+ export interface MockErrorOptions {
33
+ /**
34
+ * Custom headers to include in the error response
35
+ */
36
+ headers?: Record<string, string>;
37
+ /**
38
+ * HTTP method to match (GET, POST, PUT, PATCH, DELETE, etc.)
39
+ * @default 'GET'
40
+ */
41
+ method?: string;
42
+ /**
43
+ * Specific path to override URL pathname
44
+ * If not provided, the pathname from the URL will be used
45
+ */
46
+ path?: string;
47
+ }
48
+ //# sourceMappingURL=fetch-mock.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-mock.types.d.ts","sourceRoot":"","sources":["../../../src/core/http/fetch-mock.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAEhC;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IAEd;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;CACb;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAEhC;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;CACb"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=fetch-mock.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-mock.types.js","sourceRoot":"","sources":["../../../src/core/http/fetch-mock.types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ export { TestHttpClient } from './test-http-client';
2
+ export { TestHttpRequest } from './test-http-request';
3
+ export { TestResponse } from './test-response';
4
+ export { FetchMock, createFetchMock } from './fetch-mock';
5
+ export type { MockJsonOptions, MockErrorOptions } from './fetch-mock.types';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AACzD,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA"}
@@ -0,0 +1,5 @@
1
+ export { TestHttpClient } from './test-http-client';
2
+ export { TestHttpRequest } from './test-http-request';
3
+ export { TestResponse } from './test-response';
4
+ export { FetchMock, createFetchMock } from './fetch-mock';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/http/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA"}
@@ -0,0 +1,54 @@
1
+ import type { TestingModule } from '../testing-module';
2
+ import { TestHttpRequest } from './test-http-request';
3
+ /**
4
+ * TestHttpClient
5
+ *
6
+ * Fluent HTTP client for making test requests.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const response = await module.http
11
+ * .forHost('example.com')
12
+ * .post('/api/v1/users')
13
+ * .withBody({ name: 'Test' })
14
+ * .send()
15
+ *
16
+ * response.assertCreated()
17
+ * ```
18
+ */
19
+ export declare class TestHttpClient {
20
+ private readonly module;
21
+ private defaultHeaders;
22
+ private host;
23
+ constructor(module: TestingModule);
24
+ /**
25
+ * Set the host for the request
26
+ */
27
+ forHost(host: string): this;
28
+ /**
29
+ * Set default headers for all requests
30
+ */
31
+ withHeaders(headers: Record<string, string>): this;
32
+ /**
33
+ * Create a GET request
34
+ */
35
+ get(path: string): TestHttpRequest;
36
+ /**
37
+ * Create a POST request
38
+ */
39
+ post(path: string): TestHttpRequest;
40
+ /**
41
+ * Create a PUT request
42
+ */
43
+ put(path: string): TestHttpRequest;
44
+ /**
45
+ * Create a PATCH request
46
+ */
47
+ patch(path: string): TestHttpRequest;
48
+ /**
49
+ * Create a DELETE request
50
+ */
51
+ delete(path: string): TestHttpRequest;
52
+ private createRequest;
53
+ }
54
+ //# sourceMappingURL=test-http-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-http-client.d.ts","sourceRoot":"","sources":["../../../src/core/http/test-http-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAErD;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,cAAc;IAIb,OAAO,CAAC,QAAQ,CAAC,MAAM;IAHnC,OAAO,CAAC,cAAc,CAAyB;IAC/C,OAAO,CAAC,IAAI,CAAsB;gBAEL,MAAM,EAAE,aAAa;IAElD;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAK3B;;OAEG;IACH,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAOlD;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe;IAIlC;;OAEG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe;IAInC;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe;IAIlC;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe;IAIpC;;OAEG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe;IAIrC,OAAO,CAAC,aAAa;CAGtB"}
@@ -0,0 +1,75 @@
1
+ import { TestHttpRequest } from './test-http-request';
2
+ /**
3
+ * TestHttpClient
4
+ *
5
+ * Fluent HTTP client for making test requests.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const response = await module.http
10
+ * .forHost('example.com')
11
+ * .post('/api/v1/users')
12
+ * .withBody({ name: 'Test' })
13
+ * .send()
14
+ *
15
+ * response.assertCreated()
16
+ * ```
17
+ */
18
+ export class TestHttpClient {
19
+ module;
20
+ defaultHeaders = new Headers();
21
+ host = null;
22
+ constructor(module) {
23
+ this.module = module;
24
+ }
25
+ /**
26
+ * Set the host for the request
27
+ */
28
+ forHost(host) {
29
+ this.host = host;
30
+ return this;
31
+ }
32
+ /**
33
+ * Set default headers for all requests
34
+ */
35
+ withHeaders(headers) {
36
+ for (const [key, value] of Object.entries(headers)) {
37
+ this.defaultHeaders.set(key, value);
38
+ }
39
+ return this;
40
+ }
41
+ /**
42
+ * Create a GET request
43
+ */
44
+ get(path) {
45
+ return this.createRequest('GET', path);
46
+ }
47
+ /**
48
+ * Create a POST request
49
+ */
50
+ post(path) {
51
+ return this.createRequest('POST', path);
52
+ }
53
+ /**
54
+ * Create a PUT request
55
+ */
56
+ put(path) {
57
+ return this.createRequest('PUT', path);
58
+ }
59
+ /**
60
+ * Create a PATCH request
61
+ */
62
+ patch(path) {
63
+ return this.createRequest('PATCH', path);
64
+ }
65
+ /**
66
+ * Create a DELETE request
67
+ */
68
+ delete(path) {
69
+ return this.createRequest('DELETE', path);
70
+ }
71
+ createRequest(method, path) {
72
+ return new TestHttpRequest(method, path, this.defaultHeaders, this.module, this.host);
73
+ }
74
+ }
75
+ //# sourceMappingURL=test-http-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-http-client.js","sourceRoot":"","sources":["../../../src/core/http/test-http-client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAErD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,cAAc;IAII;IAHrB,cAAc,GAAY,IAAI,OAAO,EAAE,CAAA;IACvC,IAAI,GAAkB,IAAI,CAAA;IAElC,YAA6B,MAAqB;QAArB,WAAM,GAAN,MAAM,CAAe;IAAI,CAAC;IAEvD;;OAEG;IACH,OAAO,CAAC,IAAY;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;OAEG;IACH,WAAW,CAAC,OAA+B;QACzC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACnD,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QACrC,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACxC,CAAC;IAED;;OAEG;IACH,IAAI,CAAC,IAAY;QACf,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IACzC,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;IACxC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAY;QAChB,OAAO,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;IAC1C,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,IAAY;QACjB,OAAO,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAC3C,CAAC;IAEO,aAAa,CAAC,MAAc,EAAE,IAAY;QAChD,OAAO,IAAI,eAAe,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,cAAc,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;IACvF,CAAC;CACF"}
@@ -0,0 +1,60 @@
1
+ import type { TestingModule } from '../testing-module';
2
+ import { TestResponse } from './test-response';
3
+ /**
4
+ * TestHttpRequest
5
+ *
6
+ * Request builder with fluent API for configuring test HTTP requests.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const response = await module.http
11
+ * .post('/api/v1/register')
12
+ * .withBody({ name: 'Test School' })
13
+ * .withHeaders({ 'X-Custom': 'value' })
14
+ * .send()
15
+ * ```
16
+ *
17
+ * @example Authenticated request
18
+ * ```typescript
19
+ * const response = await module.http
20
+ * .get('/api/v1/profile')
21
+ * .actingAs({ id: user.id })
22
+ * .send()
23
+ * ```
24
+ */
25
+ export declare class TestHttpRequest {
26
+ private readonly method;
27
+ private readonly path;
28
+ private readonly module;
29
+ private readonly host;
30
+ private body;
31
+ private requestHeaders;
32
+ private actingAsUser;
33
+ constructor(method: string, path: string, headers: Headers, module: TestingModule, host?: string | null);
34
+ /**
35
+ * Set the request body
36
+ */
37
+ withBody(data: unknown): this;
38
+ /**
39
+ * Add headers to the request
40
+ */
41
+ withHeaders(headers: Record<string, string>): this;
42
+ /**
43
+ * Set Content-Type to application/json
44
+ */
45
+ asJson(): this;
46
+ /**
47
+ * Authenticate the request as a specific user
48
+ */
49
+ actingAs(user: {
50
+ id: string;
51
+ }): this;
52
+ /**
53
+ * Send the request and return response
54
+ *
55
+ * Calls module.fetch() - NOT SELF.fetch()
56
+ */
57
+ send(): Promise<TestResponse>;
58
+ private applyAuthentication;
59
+ }
60
+ //# sourceMappingURL=test-http-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-http-request.d.ts","sourceRoot":"","sources":["../../../src/core/http/test-http-request.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,eAAe;IAM1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAErB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IATtB,OAAO,CAAC,IAAI,CAAgB;IAC5B,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,YAAY,CAA8B;gBAGhC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EAC7B,OAAO,EAAE,OAAO,EACC,MAAM,EAAE,aAAa,EACrB,IAAI,GAAE,MAAM,GAAG,IAAW;IAK5C;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAK7B;;OAEG;IACH,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAOlD;;OAEG;IACH,MAAM,IAAI,IAAI;IAKd;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;IAKpC;;;;OAIG;IACG,IAAI,IAAI,OAAO,CAAC,YAAY,CAAC;YAqBrB,mBAAmB;CAajC"}
@@ -0,0 +1,106 @@
1
+ import { AUTH_SERVICE } from '@stratal/framework/auth';
2
+ import { ActingAs } from '../../auth';
3
+ import { TestResponse } from './test-response';
4
+ /**
5
+ * TestHttpRequest
6
+ *
7
+ * Request builder with fluent API for configuring test HTTP requests.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const response = await module.http
12
+ * .post('/api/v1/register')
13
+ * .withBody({ name: 'Test School' })
14
+ * .withHeaders({ 'X-Custom': 'value' })
15
+ * .send()
16
+ * ```
17
+ *
18
+ * @example Authenticated request
19
+ * ```typescript
20
+ * const response = await module.http
21
+ * .get('/api/v1/profile')
22
+ * .actingAs({ id: user.id })
23
+ * .send()
24
+ * ```
25
+ */
26
+ export class TestHttpRequest {
27
+ method;
28
+ path;
29
+ module;
30
+ host;
31
+ body = null;
32
+ requestHeaders;
33
+ actingAsUser = null;
34
+ constructor(method, path, headers, module, host = null) {
35
+ this.method = method;
36
+ this.path = path;
37
+ this.module = module;
38
+ this.host = host;
39
+ this.requestHeaders = new Headers(headers);
40
+ }
41
+ /**
42
+ * Set the request body
43
+ */
44
+ withBody(data) {
45
+ this.body = data;
46
+ return this;
47
+ }
48
+ /**
49
+ * Add headers to the request
50
+ */
51
+ withHeaders(headers) {
52
+ for (const [key, value] of Object.entries(headers)) {
53
+ this.requestHeaders.set(key, value);
54
+ }
55
+ return this;
56
+ }
57
+ /**
58
+ * Set Content-Type to application/json
59
+ */
60
+ asJson() {
61
+ this.requestHeaders.set('Content-Type', 'application/json');
62
+ return this;
63
+ }
64
+ /**
65
+ * Authenticate the request as a specific user
66
+ */
67
+ actingAs(user) {
68
+ this.actingAsUser = user;
69
+ return this;
70
+ }
71
+ /**
72
+ * Send the request and return response
73
+ *
74
+ * Calls module.fetch() - NOT SELF.fetch()
75
+ */
76
+ async send() {
77
+ await this.applyAuthentication();
78
+ // Auto-set Content-Type for body
79
+ if (this.body && !this.requestHeaders.has('Content-Type')) {
80
+ this.requestHeaders.set('Content-Type', 'application/json');
81
+ }
82
+ // Build request
83
+ const url = new URL(this.path, `http://${this.host ?? 'localhost'}`);
84
+ const request = new Request(url.toString(), {
85
+ method: this.method,
86
+ headers: this.requestHeaders,
87
+ body: this.body ? JSON.stringify(this.body) : null,
88
+ });
89
+ // Call module.fetch() - NO SELF.fetch()
90
+ const response = await this.module.fetch(request);
91
+ return new TestResponse(response);
92
+ }
93
+ async applyAuthentication() {
94
+ if (!this.actingAsUser)
95
+ return;
96
+ await this.module.runInRequestScope(async () => {
97
+ const authService = this.module.get(AUTH_SERVICE);
98
+ const actingAs = new ActingAs(authService);
99
+ const authHeaders = this.actingAsUser ? await actingAs.createSessionForUser(this.actingAsUser) : new Headers();
100
+ for (const [key, value] of authHeaders.entries()) {
101
+ this.requestHeaders.set(key, value);
102
+ }
103
+ });
104
+ }
105
+ }
106
+ //# sourceMappingURL=test-http-request.js.map