-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.test.ts
More file actions
179 lines (156 loc) · 5.7 KB
/
http.test.ts
File metadata and controls
179 lines (156 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { describe, it, expect } from 'vitest';
import {
HttpMethod,
HttpMethodSchema,
HttpRequestSchema,
CorsConfigSchema,
RateLimitConfigSchema,
StaticMountSchema,
} from './http.zod';
describe('HttpMethod', () => {
it('should accept all valid HTTP methods', () => {
const valid = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
valid.forEach((v) => {
expect(HttpMethod.parse(v)).toBe(v);
});
});
it('should reject lowercase methods', () => {
expect(() => HttpMethod.parse('get')).toThrow();
expect(() => HttpMethod.parse('post')).toThrow();
});
it('should reject invalid methods', () => {
expect(() => HttpMethod.parse('CONNECT')).toThrow();
expect(() => HttpMethod.parse('')).toThrow();
});
});
describe('CorsConfigSchema', () => {
it('should accept empty object and apply defaults', () => {
const result = CorsConfigSchema.parse({});
expect(result.enabled).toBe(true);
expect(result.origins).toBe('*');
expect(result.credentials).toBe(false);
});
it('should accept fully specified config', () => {
const result = CorsConfigSchema.parse({
enabled: true,
origins: ['http://localhost:3000', 'https://app.example.com'],
methods: ['GET', 'POST', 'PUT', 'DELETE'],
credentials: true,
maxAge: 86400,
});
expect(result.enabled).toBe(true);
expect(result.origins).toEqual(['http://localhost:3000', 'https://app.example.com']);
expect(result.methods).toEqual(['GET', 'POST', 'PUT', 'DELETE']);
expect(result.credentials).toBe(true);
expect(result.maxAge).toBe(86400);
});
it('should accept string origin', () => {
const result = CorsConfigSchema.parse({ origins: 'https://example.com' });
expect(result.origins).toBe('https://example.com');
});
it('should accept array origin', () => {
const result = CorsConfigSchema.parse({ origins: ['https://a.com', 'https://b.com'] });
expect(result.origins).toEqual(['https://a.com', 'https://b.com']);
});
it('should have optional methods and maxAge', () => {
const result = CorsConfigSchema.parse({});
expect(result.methods).toBeUndefined();
expect(result.maxAge).toBeUndefined();
});
it('should reject invalid methods in array', () => {
expect(() =>
CorsConfigSchema.parse({ methods: ['get'] }),
).toThrow();
});
});
describe('RateLimitConfigSchema', () => {
it('should accept empty object and apply defaults', () => {
const result = RateLimitConfigSchema.parse({});
expect(result.enabled).toBe(false);
expect(result.windowMs).toBe(60000);
expect(result.maxRequests).toBe(100);
});
it('should accept fully specified config', () => {
const result = RateLimitConfigSchema.parse({
enabled: true,
windowMs: 30000,
maxRequests: 50,
});
expect(result.enabled).toBe(true);
expect(result.windowMs).toBe(30000);
expect(result.maxRequests).toBe(50);
});
it('should reject non-integer windowMs', () => {
expect(() => RateLimitConfigSchema.parse({ windowMs: 1.5 })).toThrow();
});
it('should reject non-integer maxRequests', () => {
expect(() => RateLimitConfigSchema.parse({ maxRequests: 10.5 })).toThrow();
});
});
describe('StaticMountSchema', () => {
it('should accept valid static mount config', () => {
const result = StaticMountSchema.parse({
path: '/static',
directory: './public',
});
expect(result.path).toBe('/static');
expect(result.directory).toBe('./public');
});
it('should accept config with optional cacheControl', () => {
const result = StaticMountSchema.parse({
path: '/static',
directory: './public',
cacheControl: 'public, max-age=31536000',
});
expect(result.cacheControl).toBe('public, max-age=31536000');
});
it('should have optional cacheControl', () => {
const result = StaticMountSchema.parse({
path: '/assets',
directory: './dist',
});
expect(result.cacheControl).toBeUndefined();
});
it('should reject missing path', () => {
expect(() => StaticMountSchema.parse({ directory: './public' })).toThrow();
});
it('should reject missing directory', () => {
expect(() => StaticMountSchema.parse({ path: '/static' })).toThrow();
});
});
// ============================================================================
// Issue #8: HttpMethodSchema and HttpRequestSchema migrated to shared
// ============================================================================
describe('HttpMethodSchema (migrated from view.zod)', () => {
it('should accept common HTTP methods', () => {
const valid = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
valid.forEach(m => {
expect(HttpMethodSchema.parse(m)).toBe(m);
});
});
it('should reject HEAD and OPTIONS (subset for UI data sources)', () => {
expect(() => HttpMethodSchema.parse('HEAD')).toThrow();
expect(() => HttpMethodSchema.parse('OPTIONS')).toThrow();
});
});
describe('HttpRequestSchema (migrated from view.zod)', () => {
it('should accept minimal request with url only', () => {
const result = HttpRequestSchema.parse({ url: 'https://api.example.com/data' });
expect(result.url).toBe('https://api.example.com/data');
expect(result.method).toBe('GET');
});
it('should accept full request with all fields', () => {
const result = HttpRequestSchema.parse({
url: 'https://api.example.com/data',
method: 'POST',
headers: { 'Authorization': 'Bearer token' },
params: { page: 1 },
body: { name: 'test' },
});
expect(result.method).toBe('POST');
expect(result.headers?.['Authorization']).toBe('Bearer token');
});
it('should reject request without url', () => {
expect(() => HttpRequestSchema.parse({})).toThrow();
});
});