spikard-http 0.16.0-rc.2

High-performance HTTP server for Spikard with tower-http middleware stack
Documentation
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use axum::body::Body;
use axum::http::{Request, StatusCode};
use serde_json::Value;
use spikard_core::router::JsonRpcMethodInfo;
use spikard_http::server::build_router_with_handlers_and_config;
use spikard_http::{
    Handler, HandlerResult, JsonRpcConfig, Method, OpenApiConfig, RateLimitConfig, RequestData, Route, RouteMetadata,
    ServerConfig, StaticFilesConfig,
};
use std::future::Future;
use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;

struct EchoHandler;

impl Handler for EchoHandler {
    fn call(
        &self,
        _request: Request<Body>,
        request_data: RequestData,
    ) -> Pin<Box<dyn Future<Output = HandlerResult> + Send + '_>> {
        Box::pin(async move {
            let response_json = serde_json::json!({
                "method": request_data.method,
                "path": request_data.path,
                "path_params": &*request_data.path_params,
                "body": request_data.body,
                "raw_body_json": request_data
                    .raw_body
                    .as_ref()
                    .and_then(|bytes| serde_json::from_slice::<Value>(bytes.as_ref()).ok()),
            });

            Ok(axum::http::Response::builder()
                .status(StatusCode::OK)
                .header("content-type", "application/json")
                .body(Body::from(response_json.to_string()))
                .expect("response builder"))
        })
    }
}

fn api_items_path() -> String {
    ["api/items/", "{", "id:uuid", "}"].concat()
}

fn build_routes(path: &str) -> Vec<(Route, Arc<dyn Handler>)> {
    vec![
        (
            Route {
                method: Method::Get,
                path: path.to_string(),
                handler_name: "echo_get".to_string(),
                expects_json_body: false,
                cors: None,
                is_async: true,
                file_params: None,
                request_validator: None,
                response_validator: None,
                parameter_validator: None,
                jsonrpc_method: Some(JsonRpcMethodInfo {
                    method_name: "spikard.test.echo".to_string(),
                    description: Some("Echo JSON-RPC".to_string()),
                    params_schema: None,
                    result_schema: None,
                    deprecated: false,
                    tags: vec!["test".to_string()],
                }),
                compression: None,
                #[cfg(feature = "di")]
                handler_dependencies: Vec::new(),
            },
            Arc::new(EchoHandler) as Arc<dyn Handler>,
        ),
        (
            Route {
                method: Method::Post,
                path: path.to_string(),
                handler_name: "echo_post".to_string(),
                expects_json_body: true,
                cors: None,
                is_async: true,
                file_params: None,
                request_validator: None,
                response_validator: None,
                parameter_validator: None,
                jsonrpc_method: None,
                compression: None,
                #[cfg(feature = "di")]
                handler_dependencies: Vec::new(),
            },
            Arc::new(EchoHandler) as Arc<dyn Handler>,
        ),
    ]
}

fn build_route_metadata(path: &str) -> Vec<RouteMetadata> {
    let request_schema = serde_json::json!({
        "type": "object",
        "properties": {
            "name": {"type": "string"}
        },
        "required": ["name"]
    });

    let response_schema = serde_json::json!({
        "type": "object",
        "properties": {
            "ok": {"type": "boolean"}
        },
        "required": ["ok"]
    });

    vec![
        RouteMetadata {
            method: "GET".to_string(),
            path: path.to_string(),
            handler_name: "echo_get".to_string(),
            request_schema: None,
            response_schema: None,
            parameter_schema: None,
            file_params: None,
            is_async: true,
            cors: None,
            body_param_name: None,
            #[cfg(feature = "di")]
            handler_dependencies: None,
            jsonrpc_method: Some(
                serde_json::to_value(JsonRpcMethodInfo {
                    method_name: "spikard.test.echo".to_string(),
                    description: Some("Echo JSON-RPC".to_string()),
                    params_schema: None,
                    result_schema: None,
                    deprecated: false,
                    tags: vec!["test".to_string()],
                })
                .expect("jsonrpc method info"),
            ),
            static_response: None,
            compression: None,
        },
        RouteMetadata {
            method: "POST".to_string(),
            path: path.to_string(),
            handler_name: "echo_post".to_string(),
            request_schema: Some(request_schema),
            response_schema: Some(response_schema),
            parameter_schema: None,
            file_params: None,
            is_async: true,
            cors: None,
            body_param_name: Some("body".to_string()),
            #[cfg(feature = "di")]
            handler_dependencies: None,
            jsonrpc_method: None,
            compression: None,
            static_response: None,
        },
    ]
}

fn build_config(static_dir: &Path) -> ServerConfig {
    ServerConfig {
        openapi: Some(OpenApiConfig {
            enabled: true,
            title: "Spikard Test API".to_string(),
            version: "0.1.0".to_string(),
            ..OpenApiConfig::default()
        }),
        jsonrpc: Some(JsonRpcConfig::default()),
        static_files: vec![StaticFilesConfig {
            directory: static_dir.to_string_lossy().into_owned(),
            route_prefix: "/static".to_string(),
            index_file: true,
            cache_control: Some("public, max-age=60".to_string()),
        }],
        rate_limit: Some(RateLimitConfig {
            per_second: 100,
            burst: 10,
            ip_based: false,
        }),
        ..Default::default()
    }
}

async fn assert_openapi_docs_and_redoc(server: &axum_test::TestServer) {
    let openapi_response = server.get("/openapi.json").await;
    assert_eq!(openapi_response.status_code(), StatusCode::OK);
    let openapi: Value = serde_json::from_str(&openapi_response.text()).expect("openapi json");
    assert!(openapi.get("openapi").is_some());
    assert!(openapi.get("paths").is_some());

    let swagger_html = server.get("/docs").await;
    assert_eq!(swagger_html.status_code(), StatusCode::OK);
    assert!(swagger_html.text().contains("SwaggerUIBundle"));
    assert!(swagger_html.text().contains("/openapi.json"));

    let redoc_html = server.get("/redoc").await;
    assert_eq!(redoc_html.status_code(), StatusCode::OK);
    assert!(redoc_html.text().contains("<redoc"));
    assert!(redoc_html.text().contains("/openapi.json"));
}

async fn assert_static_files(server: &axum_test::TestServer) {
    let static_index = server.get("/static/").await;
    assert_eq!(static_index.status_code(), StatusCode::OK);
    assert!(static_index.text().contains("static index"));
    assert_eq!(
        static_index.header("cache-control").to_str().expect("cache-control"),
        "public, max-age=60"
    );

    let static_file = server.get("/static/hello.txt").await;
    assert_eq!(static_file.status_code(), StatusCode::OK);
    assert_eq!(static_file.text(), "hello world");
    assert_eq!(
        static_file.header("cache-control").to_str().expect("cache-control"),
        "public, max-age=60"
    );
}

async fn assert_jsonrpc_and_http_routes(server: &axum_test::TestServer) {
    let rpc_request = serde_json::json!({
        "jsonrpc": "2.0",
        "method": "spikard.test.echo",
        "params": {"any": "thing"},
        "id": 1
    });
    let rpc_response = server.post("/rpc").json(&rpc_request).await;
    assert_eq!(rpc_response.status_code(), StatusCode::OK);
    let rpc_json: Value = serde_json::from_str(&rpc_response.text()).expect("jsonrpc response");
    assert_eq!(rpc_json["jsonrpc"], "2.0");
    assert_eq!(rpc_json["id"], 1);
    assert_eq!(rpc_json["result"]["path"], "/rpc");
    assert_eq!(rpc_json["result"]["method"], "POST");

    let openrpc_response = server.get("/openrpc.json").await;
    assert_eq!(openrpc_response.status_code(), StatusCode::OK);
    let openrpc: Value = serde_json::from_str(&openrpc_response.text()).expect("openrpc json");
    assert_eq!(openrpc["openrpc"], "1.3.2");
    assert_eq!(openrpc["methods"][0]["name"], "spikard.test.echo");

    let ok_get = server.get("/api/items/550e8400-e29b-41d4-a716-446655440000").await;
    assert_eq!(ok_get.status_code(), StatusCode::OK);
    let ok_get_json: Value = serde_json::from_str(&ok_get.text()).expect("get json");
    assert_eq!(ok_get_json["path_params"]["id"], "550e8400-e29b-41d4-a716-446655440000");

    let ok_post = server
        .post("/api/items/550e8400-e29b-41d4-a716-446655440000")
        .json(&serde_json::json!({"name": "spikard"}))
        .await;
    assert_eq!(ok_post.status_code(), StatusCode::OK);
    let ok_post_json: Value = serde_json::from_str(&ok_post.text()).expect("post json");
    assert_eq!(ok_post_json["raw_body_json"]["name"], "spikard");
}

#[tokio::test]
async fn router_supports_openapi_jsonrpc_and_static_files_in_one_config() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(dir.path().join("index.html"), "<h1>static index</h1>").expect("write index.html");
    std::fs::write(dir.path().join("hello.txt"), "hello world").expect("write hello.txt");

    let api_items_path = api_items_path();
    let route_entries = build_routes(&api_items_path);
    let route_metadata = build_route_metadata(&api_items_path);
    let config = build_config(dir.path());

    let app_router = build_router_with_handlers_and_config(route_entries, config, route_metadata).expect("router");
    let server = axum_test::TestServer::new(app_router);

    assert_openapi_docs_and_redoc(&server).await;
    assert_static_files(&server).await;
    assert_jsonrpc_and_http_routes(&server).await;
}

#[test]
fn router_returns_error_for_invalid_cache_control_header_value() {
    let routes: Vec<(Route, Arc<dyn Handler>)> = Vec::new();

    let config = ServerConfig {
        static_files: vec![StaticFilesConfig {
            directory: "/tmp".to_string(),
            route_prefix: "/static".to_string(),
            index_file: true,
            cache_control: Some("\n".to_string()),
        }],
        ..Default::default()
    };

    let err = build_router_with_handlers_and_config(routes, config, Vec::new()).expect_err("invalid header");
    assert!(err.contains("Invalid cache-control header"));
}

/// Verify that a route registered with `StaticResponseHandler` serves the
/// pre-built response and that the handler's `call()` is never invoked.
#[tokio::test]
async fn static_response_route_serves_pre_built_response() {
    use spikard_http::{StaticResponse, StaticResponseHandler};
    use std::sync::atomic::{AtomicBool, Ordering};

    // A handler that tracks whether call() was invoked.
    static HANDLER_CALLED: AtomicBool = AtomicBool::new(false);
    struct SpyHandler {
        inner: StaticResponseHandler,
    }
    impl Handler for SpyHandler {
        fn call(
            &self,
            req: axum::http::Request<Body>,
            data: RequestData,
        ) -> Pin<Box<dyn Future<Output = spikard_http::HandlerResult> + Send + '_>> {
            HANDLER_CALLED.store(true, Ordering::SeqCst);
            self.inner.call(req, data)
        }
        fn static_response(&self) -> Option<StaticResponse> {
            self.inner.static_response()
        }
    }

    HANDLER_CALLED.store(false, Ordering::SeqCst);

    let handler = SpyHandler {
        inner: StaticResponseHandler::from_parts(200, r#"{"status":"healthy"}"#, Some("application/json"), vec![]),
    };

    let route_meta = RouteMetadata {
        method: "GET".to_string(),
        path: "/health".to_string(),
        handler_name: "health_check".to_string(),
        request_schema: None,
        response_schema: None,
        parameter_schema: None,
        file_params: None,
        is_async: false,
        cors: None,
        body_param_name: None,
        #[cfg(feature = "di")]
        handler_dependencies: None,
        jsonrpc_method: None,
        compression: None,
        static_response: None,
    };

    let route = spikard_http::Route::from_metadata(route_meta.clone(), &spikard_http::SchemaRegistry::new())
        .expect("route creation");

    let routes: Vec<(spikard_http::Route, Arc<dyn Handler>)> = vec![(route, Arc::new(handler) as Arc<dyn Handler>)];

    let config = ServerConfig::default();
    let app = build_router_with_handlers_and_config(routes, config, vec![route_meta]).expect("router");
    let server = axum_test::TestServer::new(app);

    let resp = server.get("/health").await;
    assert_eq!(resp.status_code(), StatusCode::OK);
    assert_eq!(resp.text(), r#"{"status":"healthy"}"#);
    assert_eq!(resp.header("content-type").to_str().unwrap(), "application/json");
    // The static fast-path should have served the response without calling the handler.
    assert!(
        !HANDLER_CALLED.load(Ordering::SeqCst),
        "Handler.call() should not be invoked for static response routes"
    );
}

/// Verify that a static route coexists with dynamic routes on the same server.
#[tokio::test]
async fn static_and_dynamic_routes_coexist() {
    use spikard_http::StaticResponseHandler;

    let static_handler = StaticResponseHandler::from_parts(200, "OK", None, vec![]);
    let echo_handler = EchoHandler;

    let api_items_path = api_items_path();

    let static_meta = RouteMetadata {
        method: "GET".to_string(),
        path: "/health".to_string(),
        handler_name: "health".to_string(),
        request_schema: None,
        response_schema: None,
        parameter_schema: None,
        file_params: None,
        is_async: false,
        cors: None,
        body_param_name: None,
        #[cfg(feature = "di")]
        handler_dependencies: None,
        jsonrpc_method: None,
        compression: None,
        static_response: None,
    };

    let dynamic_meta = RouteMetadata {
        method: "GET".to_string(),
        path: api_items_path.clone(),
        handler_name: "echo_get".to_string(),
        request_schema: None,
        response_schema: None,
        parameter_schema: None,
        file_params: None,
        is_async: true,
        cors: None,
        body_param_name: None,
        #[cfg(feature = "di")]
        handler_dependencies: None,
        jsonrpc_method: None,
        compression: None,
        static_response: None,
    };

    let registry = spikard_http::SchemaRegistry::new();
    let static_route = spikard_http::Route::from_metadata(static_meta.clone(), &registry).expect("static route");
    let dynamic_route = spikard_http::Route::from_metadata(dynamic_meta.clone(), &registry).expect("dynamic route");

    let routes: Vec<(spikard_http::Route, Arc<dyn Handler>)> = vec![
        (static_route, Arc::new(static_handler) as Arc<dyn Handler>),
        (dynamic_route, Arc::new(echo_handler) as Arc<dyn Handler>),
    ];

    let config = ServerConfig::default();
    let app = build_router_with_handlers_and_config(routes, config, vec![static_meta, dynamic_meta]).expect("router");
    let server = axum_test::TestServer::new(app);

    // Static route
    let resp = server.get("/health").await;
    assert_eq!(resp.status_code(), StatusCode::OK);
    assert_eq!(resp.text(), "OK");

    // Dynamic route still works
    let resp = server.get("/api/items/550e8400-e29b-41d4-a716-446655440000").await;
    assert_eq!(resp.status_code(), StatusCode::OK);
    let json: serde_json::Value = serde_json::from_str(&resp.text()).expect("json");
    assert_eq!(json["path_params"]["id"], "550e8400-e29b-41d4-a716-446655440000");
}