|
1 | | -/// Test server for WebSocket and Express integration tests. |
| 1 | +/// Test server for WebSocket tests (no Express dependency). |
2 | 2 | library; |
3 | 3 |
|
4 | 4 | import 'dart:js_interop'; |
5 | 5 | import 'dart:js_interop_unsafe'; |
6 | 6 |
|
7 | 7 | import 'package:dart_node_core/dart_node_core.dart'; |
8 | | -import 'package:dart_node_express/dart_node_express.dart'; |
9 | 8 | import 'package:dart_node_ws/dart_node_ws.dart'; |
10 | | -import 'package:nadz/nadz.dart'; |
11 | | - |
12 | | -/// Port for HTTP server |
13 | | -const httpPort = 3456; |
14 | 9 |
|
15 | 10 | /// Port for WebSocket server |
16 | 11 | const wsPort = 3457; |
17 | 12 |
|
18 | 13 | void main() { |
19 | | - final wsServer = _startWebSocketServer(); |
20 | | - _startHttpServer(wsServer); |
| 14 | + _startWebSocketServer(); |
21 | 15 | } |
22 | 16 |
|
23 | 17 | WebSocketServer _startWebSocketServer() { |
@@ -110,89 +104,3 @@ Map<String, Object?>? _parseJson(String jsonStr) { |
110 | 104 | _ => null, |
111 | 105 | }; |
112 | 106 | } |
113 | | - |
114 | | -/// JSON body parser middleware |
115 | | -JSFunction _jsonParser() { |
116 | | - final expressModule = switch (requireModule('express')) { |
117 | | - final JSObject o => o, |
118 | | - _ => throw StateError('Express module not found'), |
119 | | - }; |
120 | | - final jsonFn = switch (expressModule['json']) { |
121 | | - final JSFunction f => f, |
122 | | - _ => throw StateError('Express json function not found'), |
123 | | - }; |
124 | | - return switch (jsonFn.callAsFunction()) { |
125 | | - final JSFunction f => f, |
126 | | - _ => throw StateError('Failed to create JSON parser'), |
127 | | - }; |
128 | | -} |
129 | | - |
130 | | -void _startHttpServer(WebSocketServer wsServer) { |
131 | | - express() |
132 | | - ..use(_jsonParser()) |
133 | | - ..get( |
134 | | - '/health', |
135 | | - handler((req, res) { |
136 | | - res.jsonMap({'status': 'ok', 'wsPort': wsPort}); |
137 | | - }), |
138 | | - ) |
139 | | - ..get( |
140 | | - '/echo/:message', |
141 | | - handler((req, res) { |
142 | | - final message = req.params['message'].toString(); |
143 | | - res.jsonMap({'echo': message}); |
144 | | - }), |
145 | | - ) |
146 | | - ..post( |
147 | | - '/json', |
148 | | - handler((req, res) { |
149 | | - final body = req.body; |
150 | | - res.jsonMap({'received': body.dartify(), 'success': true}); |
151 | | - }), |
152 | | - ) |
153 | | - ..get( |
154 | | - '/error', |
155 | | - asyncHandler((req, res) async { |
156 | | - await Future<void>.value(); |
157 | | - throw const NotFoundError('Test error'); |
158 | | - }), |
159 | | - ) |
160 | | - ..get( |
161 | | - '/status/:code', |
162 | | - handler((req, res) { |
163 | | - final code = int.tryParse(req.params['code'].toString()) ?? 200; |
164 | | - res |
165 | | - ..status(code) |
166 | | - ..jsonMap({'statusCode': code}); |
167 | | - }), |
168 | | - ) |
169 | | - ..postWithMiddleware('/validated', [ |
170 | | - validateBody(_testSchema), |
171 | | - handler((req, res) { |
172 | | - switch (getValidatedBody<TestUserData>(req)) { |
173 | | - case Error(:final error): |
174 | | - res |
175 | | - ..status(400) |
176 | | - ..jsonMap({'error': error}); |
177 | | - case Success(:final value): |
178 | | - res.jsonMap({'name': value.name, 'age': value.age}); |
179 | | - } |
180 | | - }), |
181 | | - ]) |
182 | | - ..use(errorHandler()) |
183 | | - ..listen( |
184 | | - httpPort, |
185 | | - (() { |
186 | | - consoleLog('HTTP server running on http://localhost:$httpPort'); |
187 | | - }).toJS, |
188 | | - ); |
189 | | -} |
190 | | - |
191 | | -/// Test user data type |
192 | | -typedef TestUserData = ({String name, int age}); |
193 | | - |
194 | | -/// Test validation schema |
195 | | -final _testSchema = schema<TestUserData>({ |
196 | | - 'name': string().minLength(1).maxLength(100), |
197 | | - 'age': int_().min(0).max(150), |
198 | | -}, (map) => (name: map['name']! as String, age: map['age']! as int)); |
0 commit comments