spikard 0.6.2 → 0.7.1

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 (68) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +90 -508
  3. data/ext/spikard_rb/Cargo.lock +3287 -0
  4. data/ext/spikard_rb/Cargo.toml +1 -1
  5. data/ext/spikard_rb/extconf.rb +3 -3
  6. data/lib/spikard/app.rb +72 -49
  7. data/lib/spikard/background.rb +38 -7
  8. data/lib/spikard/testing.rb +42 -4
  9. data/lib/spikard/version.rb +1 -1
  10. data/sig/spikard.rbs +4 -0
  11. data/vendor/crates/spikard-bindings-shared/Cargo.toml +1 -1
  12. data/vendor/crates/spikard-bindings-shared/tests/config_extractor_behavior.rs +191 -0
  13. data/vendor/crates/spikard-core/Cargo.toml +1 -1
  14. data/vendor/crates/spikard-core/src/http.rs +1 -0
  15. data/vendor/crates/spikard-core/src/lifecycle.rs +63 -0
  16. data/vendor/crates/spikard-core/tests/bindings_response_tests.rs +136 -0
  17. data/vendor/crates/spikard-core/tests/di_dependency_defaults.rs +37 -0
  18. data/vendor/crates/spikard-core/tests/error_mapper.rs +761 -0
  19. data/vendor/crates/spikard-core/tests/parameters_edge_cases.rs +106 -0
  20. data/vendor/crates/spikard-core/tests/parameters_full.rs +701 -0
  21. data/vendor/crates/spikard-core/tests/parameters_schema_and_formats.rs +301 -0
  22. data/vendor/crates/spikard-core/tests/request_data_roundtrip.rs +67 -0
  23. data/vendor/crates/spikard-core/tests/validation_coverage.rs +250 -0
  24. data/vendor/crates/spikard-core/tests/validation_error_paths.rs +45 -0
  25. data/vendor/crates/spikard-http/Cargo.toml +1 -1
  26. data/vendor/crates/spikard-http/src/jsonrpc/http_handler.rs +502 -0
  27. data/vendor/crates/spikard-http/src/jsonrpc/method_registry.rs +648 -0
  28. data/vendor/crates/spikard-http/src/jsonrpc/mod.rs +58 -0
  29. data/vendor/crates/spikard-http/src/jsonrpc/protocol.rs +1207 -0
  30. data/vendor/crates/spikard-http/src/jsonrpc/router.rs +2262 -0
  31. data/vendor/crates/spikard-http/src/testing/test_client.rs +155 -2
  32. data/vendor/crates/spikard-http/src/testing.rs +171 -0
  33. data/vendor/crates/spikard-http/src/websocket.rs +79 -6
  34. data/vendor/crates/spikard-http/tests/auth_integration.rs +647 -0
  35. data/vendor/crates/spikard-http/tests/common/test_builders.rs +633 -0
  36. data/vendor/crates/spikard-http/tests/di_handler_error_responses.rs +162 -0
  37. data/vendor/crates/spikard-http/tests/middleware_stack_integration.rs +389 -0
  38. data/vendor/crates/spikard-http/tests/request_extraction_full.rs +513 -0
  39. data/vendor/crates/spikard-http/tests/server_auth_middleware_behavior.rs +244 -0
  40. data/vendor/crates/spikard-http/tests/server_configured_router_behavior.rs +200 -0
  41. data/vendor/crates/spikard-http/tests/server_cors_preflight.rs +82 -0
  42. data/vendor/crates/spikard-http/tests/server_handler_wrappers.rs +464 -0
  43. data/vendor/crates/spikard-http/tests/server_method_router_additional_behavior.rs +286 -0
  44. data/vendor/crates/spikard-http/tests/server_method_router_coverage.rs +118 -0
  45. data/vendor/crates/spikard-http/tests/server_middleware_behavior.rs +99 -0
  46. data/vendor/crates/spikard-http/tests/server_middleware_branches.rs +206 -0
  47. data/vendor/crates/spikard-http/tests/server_openapi_jsonrpc_static.rs +281 -0
  48. data/vendor/crates/spikard-http/tests/server_router_behavior.rs +121 -0
  49. data/vendor/crates/spikard-http/tests/sse_full_behavior.rs +584 -0
  50. data/vendor/crates/spikard-http/tests/sse_handler_behavior.rs +130 -0
  51. data/vendor/crates/spikard-http/tests/test_client_requests.rs +167 -0
  52. data/vendor/crates/spikard-http/tests/testing_helpers.rs +87 -0
  53. data/vendor/crates/spikard-http/tests/testing_module_coverage.rs +156 -0
  54. data/vendor/crates/spikard-http/tests/urlencoded_content_type.rs +82 -0
  55. data/vendor/crates/spikard-http/tests/websocket_full_behavior.rs +440 -0
  56. data/vendor/crates/spikard-http/tests/websocket_integration.rs +152 -0
  57. data/vendor/crates/spikard-rb/Cargo.toml +1 -1
  58. data/vendor/crates/spikard-rb/src/gvl.rs +80 -0
  59. data/vendor/crates/spikard-rb/src/handler.rs +12 -9
  60. data/vendor/crates/spikard-rb/src/lib.rs +137 -124
  61. data/vendor/crates/spikard-rb/src/request.rs +342 -0
  62. data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +1 -8
  63. data/vendor/crates/spikard-rb/src/server.rs +1 -8
  64. data/vendor/crates/spikard-rb/src/testing/client.rs +168 -9
  65. data/vendor/crates/spikard-rb/src/websocket.rs +119 -30
  66. data/vendor/crates/spikard-rb-macros/Cargo.toml +14 -0
  67. data/vendor/crates/spikard-rb-macros/src/lib.rs +52 -0
  68. metadata +44 -1
@@ -0,0 +1,502 @@
1
+ //! JSON-RPC HTTP handler for processing JSON-RPC requests over HTTP
2
+ //!
3
+ //! This module provides the HTTP endpoint handler that accepts JSON-RPC requests
4
+ //! and routes them through the JSON-RPC router. It handles single and batch requests,
5
+ //! validates content-type headers, and returns properly formatted JSON-RPC responses.
6
+ //!
7
+ //! # Request Processing
8
+ //!
9
+ //! The handler:
10
+ //! 1. Validates that the Content-Type header is `application/json`
11
+ //! 2. Parses the request body as JSON-RPC 2.0 (single or batch)
12
+ //! 3. Routes requests through the JsonRpcRouter
13
+ //! 4. Returns HTTP 200 with JSON-RPC responses
14
+ //! 5. Returns appropriate HTTP error codes for non-JSON-RPC errors
15
+ //!
16
+ //! # Response Codes
17
+ //!
18
+ //! - HTTP 200: Valid JSON-RPC request (even if the response contains an error)
19
+ //! - HTTP 400: Parse error or invalid request format
20
+ //! - HTTP 415: Invalid Content-Type header
21
+ //!
22
+ //! # Example
23
+ //!
24
+ //! ```ignore
25
+ //! use axum::{
26
+ //! routing::post,
27
+ //! Router,
28
+ //! };
29
+ //! use std::sync::Arc;
30
+ //! use spikard_http::jsonrpc::{JsonRpcRouter, JsonRpcMethodRegistry};
31
+ //!
32
+ //! let registry = Arc::new(JsonRpcMethodRegistry::new());
33
+ //! let router = Arc::new(JsonRpcRouter::new(registry, true, 100));
34
+ //! let state = Arc::new(JsonRpcState { router });
35
+ //!
36
+ //! let app = Router::new()
37
+ //! .route("/rpc", post(handle_jsonrpc))
38
+ //! .with_state(state);
39
+ //! ```
40
+
41
+ use super::router::{JsonRpcRequestOrBatch, JsonRpcRouter};
42
+ use crate::handler_trait::RequestData;
43
+ use crate::server::request_extraction::extract_headers;
44
+ use axum::{
45
+ body::Body,
46
+ extract::State,
47
+ http::{HeaderMap, Request, StatusCode, header},
48
+ response::{IntoResponse, Response as AxumResponse},
49
+ };
50
+ use std::collections::HashMap;
51
+ use std::sync::Arc;
52
+
53
+ /// State passed to the JSON-RPC HTTP handler
54
+ ///
55
+ /// Contains the shared JSON-RPC router instance that dispatches requests
56
+ /// to registered method handlers.
57
+ #[derive(Clone)]
58
+ pub struct JsonRpcState {
59
+ /// The JSON-RPC router for handling requests
60
+ pub router: Arc<JsonRpcRouter>,
61
+ }
62
+
63
+ /// Main JSON-RPC HTTP handler
64
+ ///
65
+ /// Accepts POST requests with JSON-RPC 2.0 payloads (single or batch).
66
+ /// Always returns HTTP 200 for valid JSON-RPC requests, with JSON-RPC error
67
+ /// codes in the response body if the method invocation failed.
68
+ ///
69
+ /// # Arguments
70
+ ///
71
+ /// * `state` - The application state containing the JSON-RPC router
72
+ /// * `headers` - HTTP request headers (used for Content-Type validation)
73
+ /// * `uri` - HTTP request URI (used for extracting path and query params)
74
+ /// * `body` - The raw request body as a string
75
+ ///
76
+ /// # Returns
77
+ ///
78
+ /// An Axum response with:
79
+ /// - HTTP 200 and JSON-RPC response for valid JSON-RPC requests
80
+ /// - HTTP 415 if Content-Type is not application/json
81
+ /// - HTTP 400 if the request body cannot be parsed as JSON-RPC
82
+ ///
83
+ /// # Example
84
+ ///
85
+ /// Valid single request:
86
+ /// ```text
87
+ /// POST /rpc HTTP/1.1
88
+ /// Content-Type: application/json
89
+ ///
90
+ /// {"jsonrpc":"2.0","method":"add","params":[1,2],"id":1}
91
+ /// ```
92
+ ///
93
+ /// Valid batch request:
94
+ /// ```text
95
+ /// POST /rpc HTTP/1.1
96
+ /// Content-Type: application/json
97
+ ///
98
+ /// [{"jsonrpc":"2.0","method":"add","params":[1,2],"id":1},
99
+ /// {"jsonrpc":"2.0","method":"multiply","params":[3,4],"id":2}]
100
+ /// ```
101
+ pub async fn handle_jsonrpc(
102
+ State(state): State<Arc<JsonRpcState>>,
103
+ headers: HeaderMap,
104
+ uri: axum::http::Uri,
105
+ body: String,
106
+ ) -> AxumResponse {
107
+ if !validate_content_type(&headers) {
108
+ return create_error_response(
109
+ StatusCode::UNSUPPORTED_MEDIA_TYPE,
110
+ "Content-Type must be application/json",
111
+ );
112
+ }
113
+
114
+ let request = match JsonRpcRouter::parse_request(&body) {
115
+ Ok(req) => req,
116
+ Err(error_response) => {
117
+ let json = serde_json::to_string(&error_response).expect("Error serialization should never fail");
118
+ return create_jsonrpc_response(json);
119
+ }
120
+ };
121
+
122
+ let request_data = create_jsonrpc_request_data(&headers, &uri);
123
+
124
+ let http_request = Request::builder()
125
+ .method("POST")
126
+ .uri(uri.clone())
127
+ .body(Body::empty())
128
+ .unwrap_or_else(|_| Request::builder().method("POST").uri("/").body(Body::empty()).unwrap());
129
+
130
+ let response = match request {
131
+ JsonRpcRequestOrBatch::Single(req) => {
132
+ let response = state.router.route_single(req, http_request, &request_data).await;
133
+ serde_json::to_string(&response).expect("Response serialization should never fail")
134
+ }
135
+ JsonRpcRequestOrBatch::Batch(batch) => {
136
+ let http_request = Request::builder()
137
+ .method("POST")
138
+ .uri(uri.clone())
139
+ .body(Body::empty())
140
+ .unwrap_or_else(|_| Request::builder().method("POST").uri("/").body(Body::empty()).unwrap());
141
+ match state.router.route_batch(batch, http_request, &request_data).await {
142
+ Ok(responses) => {
143
+ serde_json::to_string(&responses).expect("Batch response serialization should never fail")
144
+ }
145
+ Err(error_response) => {
146
+ serde_json::to_string(&error_response).expect("Error serialization should never fail")
147
+ }
148
+ }
149
+ }
150
+ };
151
+
152
+ create_jsonrpc_response(response)
153
+ }
154
+
155
+ /// Helper function to create RequestData from JSON-RPC HTTP context
156
+ ///
157
+ /// Creates a minimal RequestData with headers and path info extracted from the HTTP request.
158
+ /// Query parameters are extracted from the URI.
159
+ fn create_jsonrpc_request_data(headers: &HeaderMap, uri: &axum::http::Uri) -> RequestData {
160
+ RequestData {
161
+ path_params: Arc::new(HashMap::new()),
162
+ query_params: serde_json::json!({}),
163
+ validated_params: None,
164
+ raw_query_params: Arc::new(HashMap::new()),
165
+ body: serde_json::json!({}),
166
+ raw_body: None,
167
+ headers: Arc::new(extract_headers(headers)),
168
+ cookies: Arc::new(HashMap::new()),
169
+ method: "POST".to_string(),
170
+ path: uri.path().to_string(),
171
+ #[cfg(feature = "di")]
172
+ dependencies: None,
173
+ }
174
+ }
175
+
176
+ /// Validates that the Content-Type header is application/json
177
+ ///
178
+ /// Performs case-insensitive matching per HTTP specification.
179
+ ///
180
+ /// # Arguments
181
+ ///
182
+ /// * `headers` - The HTTP headers to validate
183
+ ///
184
+ /// # Returns
185
+ ///
186
+ /// `true` if Content-Type is application/json or absent (defaults to JSON),
187
+ /// `false` if Content-Type is present but not JSON
188
+ fn validate_content_type(headers: &HeaderMap) -> bool {
189
+ match headers.get(header::CONTENT_TYPE) {
190
+ None => true,
191
+ Some(value) => {
192
+ if let Ok(ct) = value.to_str() {
193
+ ct.to_lowercase().contains("application/json")
194
+ } else {
195
+ false
196
+ }
197
+ }
198
+ }
199
+ }
200
+
201
+ /// Creates a JSON-RPC response with proper HTTP headers
202
+ ///
203
+ /// Returns HTTP 200 OK with Content-Type: application/json
204
+ ///
205
+ /// # Arguments
206
+ ///
207
+ /// * `json` - The JSON response body as a string
208
+ ///
209
+ /// # Returns
210
+ ///
211
+ /// An Axum response ready to send to the client
212
+ fn create_jsonrpc_response(json: String) -> AxumResponse {
213
+ (StatusCode::OK, [(header::CONTENT_TYPE, "application/json")], json).into_response()
214
+ }
215
+
216
+ /// Creates a generic error response for HTTP-level errors
217
+ ///
218
+ /// Returns an appropriate HTTP status code with plain text error message.
219
+ /// Used for Content-Type validation failures and other HTTP-level errors.
220
+ ///
221
+ /// # Arguments
222
+ ///
223
+ /// * `status` - The HTTP status code
224
+ /// * `message` - The error message as plain text
225
+ ///
226
+ /// # Returns
227
+ ///
228
+ /// An Axum response ready to send to the client
229
+ fn create_error_response(status: StatusCode, message: &str) -> AxumResponse {
230
+ (
231
+ status,
232
+ [(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
233
+ message.to_string(),
234
+ )
235
+ .into_response()
236
+ }
237
+
238
+ #[cfg(test)]
239
+ mod tests {
240
+ use super::*;
241
+ use crate::jsonrpc::{method_registry::JsonRpcMethodRegistry, router::JsonRpcRouter};
242
+ use serde_json::json;
243
+
244
+ /// Helper to create a test state
245
+ fn create_test_state() -> Arc<JsonRpcState> {
246
+ let registry = Arc::new(JsonRpcMethodRegistry::new());
247
+ let router = Arc::new(JsonRpcRouter::new(registry, true, 100));
248
+ Arc::new(JsonRpcState { router })
249
+ }
250
+
251
+ /// Helper to create headers with JSON content type
252
+ fn create_json_headers() -> HeaderMap {
253
+ let mut headers = HeaderMap::new();
254
+ headers.insert(header::CONTENT_TYPE, "application/json".parse().unwrap());
255
+ headers
256
+ }
257
+
258
+ /// Helper to create headers with wrong content type
259
+ fn create_wrong_content_type_headers() -> HeaderMap {
260
+ let mut headers = HeaderMap::new();
261
+ headers.insert(header::CONTENT_TYPE, "text/plain".parse().unwrap());
262
+ headers
263
+ }
264
+
265
+ /// Helper to create empty headers
266
+ fn create_empty_headers() -> HeaderMap {
267
+ HeaderMap::new()
268
+ }
269
+
270
+ /// Helper to create a test URI
271
+ fn create_test_uri() -> axum::http::Uri {
272
+ axum::http::Uri::from_static("/rpc")
273
+ }
274
+
275
+ #[tokio::test]
276
+ async fn test_handle_jsonrpc_single_request() {
277
+ let state = create_test_state();
278
+ let headers = create_json_headers();
279
+ let uri = create_test_uri();
280
+ let body = r#"{"jsonrpc":"2.0","method":"test.method","params":{},"id":1}"#.to_string();
281
+
282
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
283
+
284
+ assert_eq!(response.status(), StatusCode::OK);
285
+
286
+ let content_type = response.headers().get(header::CONTENT_TYPE).unwrap().to_str().unwrap();
287
+ assert!(content_type.contains("application/json"));
288
+ }
289
+
290
+ #[tokio::test]
291
+ async fn test_handle_jsonrpc_batch_request() {
292
+ let state = create_test_state();
293
+ let headers = create_json_headers();
294
+ let uri = create_test_uri();
295
+ let body = r#"[
296
+ {"jsonrpc":"2.0","method":"test.method","params":{},"id":1},
297
+ {"jsonrpc":"2.0","method":"test.method","params":{},"id":2}
298
+ ]"#
299
+ .to_string();
300
+
301
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
302
+
303
+ assert_eq!(response.status(), StatusCode::OK);
304
+
305
+ let content_type = response.headers().get(header::CONTENT_TYPE).unwrap().to_str().unwrap();
306
+ assert!(content_type.contains("application/json"));
307
+ }
308
+
309
+ #[tokio::test]
310
+ async fn test_invalid_content_type() {
311
+ let state = create_test_state();
312
+ let headers = create_wrong_content_type_headers();
313
+ let uri = create_test_uri();
314
+ let body = r#"{"jsonrpc":"2.0","method":"test","id":1}"#.to_string();
315
+
316
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
317
+
318
+ assert_eq!(response.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
319
+
320
+ let content_type = response.headers().get(header::CONTENT_TYPE).unwrap().to_str().unwrap();
321
+ assert!(content_type.contains("text/plain"));
322
+ }
323
+
324
+ #[tokio::test]
325
+ async fn test_missing_content_type_defaults_to_json() {
326
+ let state = create_test_state();
327
+ let headers = create_empty_headers();
328
+ let uri = create_test_uri();
329
+ let body = r#"{"jsonrpc":"2.0","method":"test.method","params":{},"id":1}"#.to_string();
330
+
331
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
332
+
333
+ assert_eq!(response.status(), StatusCode::OK);
334
+ }
335
+
336
+ #[tokio::test]
337
+ async fn test_invalid_json_parse_error() {
338
+ let state = create_test_state();
339
+ let headers = create_json_headers();
340
+ let uri = create_test_uri();
341
+ let body = r#"{"invalid json"}"#.to_string();
342
+
343
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
344
+
345
+ assert_eq!(response.status(), StatusCode::OK);
346
+ }
347
+
348
+ #[tokio::test]
349
+ async fn test_notification_in_batch() {
350
+ let state = create_test_state();
351
+ let headers = create_json_headers();
352
+ let uri = create_test_uri();
353
+ let body = r#"[
354
+ {"jsonrpc":"2.0","method":"test","params":{},"id":1},
355
+ {"jsonrpc":"2.0","method":"test","params":{}},
356
+ {"jsonrpc":"2.0","method":"test","params":{},"id":2}
357
+ ]"#
358
+ .to_string();
359
+
360
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
361
+
362
+ assert_eq!(response.status(), StatusCode::OK);
363
+ }
364
+
365
+ #[test]
366
+ fn test_validate_content_type_valid() {
367
+ let mut headers = HeaderMap::new();
368
+ headers.insert(header::CONTENT_TYPE, "application/json".parse().unwrap());
369
+ assert!(validate_content_type(&headers));
370
+ }
371
+
372
+ #[test]
373
+ fn test_validate_content_type_valid_with_charset() {
374
+ let mut headers = HeaderMap::new();
375
+ headers.insert(header::CONTENT_TYPE, "application/json; charset=utf-8".parse().unwrap());
376
+ assert!(validate_content_type(&headers));
377
+ }
378
+
379
+ #[test]
380
+ fn test_validate_content_type_invalid() {
381
+ let mut headers = HeaderMap::new();
382
+ headers.insert(header::CONTENT_TYPE, "text/plain".parse().unwrap());
383
+ assert!(!validate_content_type(&headers));
384
+ }
385
+
386
+ #[test]
387
+ fn test_validate_content_type_missing() {
388
+ let headers = HeaderMap::new();
389
+ assert!(validate_content_type(&headers));
390
+ }
391
+
392
+ #[test]
393
+ fn test_create_jsonrpc_response() {
394
+ let json = r#"{"jsonrpc":"2.0","result":42,"id":1}"#.to_string();
395
+ let response = create_jsonrpc_response(json);
396
+
397
+ assert_eq!(response.status(), StatusCode::OK);
398
+ let content_type = response.headers().get(header::CONTENT_TYPE).unwrap().to_str().unwrap();
399
+ assert_eq!(content_type, "application/json");
400
+ }
401
+
402
+ #[test]
403
+ fn test_create_error_response() {
404
+ let response = create_error_response(StatusCode::UNSUPPORTED_MEDIA_TYPE, "Invalid content type");
405
+
406
+ assert_eq!(response.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
407
+ let content_type = response.headers().get(header::CONTENT_TYPE).unwrap().to_str().unwrap();
408
+ assert!(content_type.contains("text/plain"));
409
+ }
410
+
411
+ #[tokio::test]
412
+ async fn test_method_not_found_in_single_request() {
413
+ let state = create_test_state();
414
+ let headers = create_json_headers();
415
+ let uri = create_test_uri();
416
+ let body = r#"{"jsonrpc":"2.0","method":"nonexistent.method","params":{},"id":1}"#.to_string();
417
+
418
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
419
+
420
+ assert_eq!(response.status(), StatusCode::OK);
421
+ }
422
+
423
+ #[tokio::test]
424
+ async fn test_batch_disabled() {
425
+ let registry = Arc::new(JsonRpcMethodRegistry::new());
426
+ let router = Arc::new(JsonRpcRouter::new(registry, false, 100));
427
+ let state = Arc::new(JsonRpcState { router });
428
+ let headers = create_json_headers();
429
+ let uri = create_test_uri();
430
+ let body = r#"[
431
+ {"jsonrpc":"2.0","method":"test","params":{},"id":1}
432
+ ]"#
433
+ .to_string();
434
+
435
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
436
+
437
+ assert_eq!(response.status(), StatusCode::OK);
438
+ }
439
+
440
+ #[tokio::test]
441
+ async fn test_batch_size_exceeded() {
442
+ let registry = Arc::new(JsonRpcMethodRegistry::new());
443
+ let router = Arc::new(JsonRpcRouter::new(registry, true, 2));
444
+ let state = Arc::new(JsonRpcState { router });
445
+ let headers = create_json_headers();
446
+ let uri = create_test_uri();
447
+ let body = r#"[
448
+ {"jsonrpc":"2.0","method":"test","params":{},"id":1},
449
+ {"jsonrpc":"2.0","method":"test","params":{},"id":2},
450
+ {"jsonrpc":"2.0","method":"test","params":{},"id":3}
451
+ ]"#
452
+ .to_string();
453
+
454
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
455
+
456
+ assert_eq!(response.status(), StatusCode::OK);
457
+ }
458
+
459
+ #[tokio::test]
460
+ async fn test_empty_batch() {
461
+ let state = create_test_state();
462
+ let headers = create_json_headers();
463
+ let uri = create_test_uri();
464
+ let body = r#"[]"#.to_string();
465
+
466
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
467
+
468
+ assert_eq!(response.status(), StatusCode::OK);
469
+ }
470
+
471
+ #[tokio::test]
472
+ async fn test_response_with_params() {
473
+ let state = create_test_state();
474
+ let headers = create_json_headers();
475
+ let uri = create_test_uri();
476
+ let params = json!({"key": "value", "number": 42});
477
+ let body = serde_json::to_string(&json!({
478
+ "jsonrpc": "2.0",
479
+ "method": "test.method",
480
+ "params": params,
481
+ "id": 1
482
+ }))
483
+ .unwrap();
484
+
485
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
486
+
487
+ assert_eq!(response.status(), StatusCode::OK);
488
+ }
489
+
490
+ #[tokio::test]
491
+ async fn test_content_type_case_insensitive() {
492
+ let state = create_test_state();
493
+ let mut headers = HeaderMap::new();
494
+ headers.insert(header::CONTENT_TYPE, "Application/JSON".parse().unwrap());
495
+ let uri = create_test_uri();
496
+ let body = r#"{"jsonrpc":"2.0","method":"test","id":1}"#.to_string();
497
+
498
+ let response = handle_jsonrpc(State(state), headers, uri, body).await;
499
+
500
+ assert_eq!(response.status(), StatusCode::OK);
501
+ }
502
+ }