grapsus-proxy 0.5.12

A security-first reverse proxy built on Pingora with sleepable ops at the edge
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
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Integration tests for service type functionality
//!
//! Tests static file serving, API validation, and custom error pages

use anyhow::Result;
use http::{Request, StatusCode};
use http_body_util::BodyExt;
use serde_json::json;
use std::collections::HashMap;
use tempfile::TempDir;
use tokio::fs;
use grapsus_config::{
    ApiSchemaConfig, ErrorFormat, ErrorPage, ErrorPageConfig, ServiceType, StaticFileConfig,
};
use grapsus_proxy::{ErrorHandler, SchemaValidator, StaticFileServer};

#[cfg(test)]
mod tests {
    use super::*;

    /// Test static file serving functionality
    #[tokio::test]
    async fn test_static_file_serving() -> Result<()> {
        // Create a temporary directory with test files
        let temp_dir = TempDir::new()?;
        let static_root = temp_dir.path().to_path_buf();

        // Create test files
        let index_content = b"<html><body>Hello World</body></html>";
        let css_content = b"body { margin: 0; }";
        let js_content = b"console.log('test');";

        fs::write(static_root.join("index.html"), index_content).await?;
        fs::write(static_root.join("style.css"), css_content).await?;
        fs::create_dir(static_root.join("js")).await?;
        fs::write(static_root.join("js/app.js"), js_content).await?;

        // Configure static file server
        let config = StaticFileConfig {
            root: static_root.clone(),
            index: "index.html".to_string(),
            directory_listing: false,
            cache_control: "public, max-age=3600".to_string(),
            compress: true,
            mime_types: HashMap::new(),
            fallback: None,
        };

        let server = StaticFileServer::new(config);

        // Test serving index file
        let req = Request::get("/").body(()).unwrap();
        let response = server.serve(&req, "/").await?;
        assert_eq!(response.status(), StatusCode::OK);
        let body = response.into_body().collect().await?.to_bytes();
        assert_eq!(&body[..], index_content);

        // Test serving CSS file
        let req = Request::get("/style.css").body(()).unwrap();
        let response = server.serve(&req, "/style.css").await?;
        assert_eq!(response.status(), StatusCode::OK);
        assert!(response
            .headers()
            .get("content-type")
            .unwrap()
            .to_str()?
            .contains("text/css"));

        // Test serving JS file from subdirectory
        let req = Request::get("/js/app.js").body(()).unwrap();
        let response = server.serve(&req, "/js/app.js").await?;
        assert_eq!(response.status(), StatusCode::OK);
        assert!(response
            .headers()
            .get("content-type")
            .unwrap()
            .to_str()?
            .contains("javascript"));

        // Test 404 for non-existent file
        let req = Request::get("/nonexistent.txt").body(()).unwrap();
        let response = server.serve(&req, "/nonexistent.txt").await;
        assert!(response.is_err() || response.unwrap().status() == StatusCode::NOT_FOUND);

        Ok(())
    }

    /// Test SPA fallback functionality
    #[tokio::test]
    async fn test_spa_fallback() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let static_root = temp_dir.path().to_path_buf();

        // Create SPA index file
        let spa_content = b"<html><body>SPA App</body></html>";
        fs::write(static_root.join("index.html"), spa_content).await?;

        // Configure with fallback
        let config = StaticFileConfig {
            root: static_root.clone(),
            index: "index.html".to_string(),
            directory_listing: false,
            cache_control: "public, max-age=3600".to_string(),
            compress: false,
            mime_types: HashMap::new(),
            fallback: Some("index.html".to_string()),
        };

        let server = StaticFileServer::new(config);

        // Test that non-existent route falls back to index.html
        let req = Request::get("/app/route/123").body(()).unwrap();
        let response = server.serve(&req, "/app/route/123").await?;
        assert_eq!(response.status(), StatusCode::OK);
        let body = response.into_body().collect().await?.to_bytes();
        assert_eq!(&body[..], spa_content);

        Ok(())
    }

    /// Test API schema validation
    #[tokio::test]
    async fn test_api_validation() -> Result<()> {
        // Define a simple schema
        let schema = json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "minLength": 3,
                    "maxLength": 50
                },
                "age": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 150
                },
                "email": {
                    "type": "string",
                    "format": "email"
                }
            },
            "required": ["name", "email"]
        });

        let config = ApiSchemaConfig {
            schema_file: None,
            schema_content: None,
            request_schema: Some(schema),
            response_schema: None,
            validate_requests: true,
            validate_responses: false,
            strict_mode: false,
        };

        let validator = SchemaValidator::new(config)?;

        // Test valid request
        let valid_body = json!({
            "name": "John Doe",
            "age": 30,
            "email": "john@example.com"
        });

        let req = Request::post("/api/users")
            .header("Content-Type", "application/json")
            .body(())
            .unwrap();

        let result = validator
            .validate_request(
                &req,
                &serde_json::to_vec(&valid_body)?,
                "/api/users",
                "req-123",
            )
            .await;
        assert!(result.is_ok());

        // Test invalid request (missing required field)
        let invalid_body = json!({
            "name": "John",
            // Missing email
            "age": 30
        });

        let result = validator
            .validate_request(
                &req,
                &serde_json::to_vec(&invalid_body)?,
                "/api/users",
                "req-124",
            )
            .await;
        assert!(result.is_err());

        // Test invalid request (wrong type)
        let invalid_body = json!({
            "name": "Jo", // Too short
            "email": "not-an-email", // Invalid format
            "age": "thirty" // Wrong type
        });

        let result = validator
            .validate_request(
                &req,
                &serde_json::to_vec(&invalid_body)?,
                "/api/users",
                "req-125",
            )
            .await;
        assert!(result.is_err());

        Ok(())
    }

    /// Test custom error page generation
    #[tokio::test]
    async fn test_error_pages() -> Result<()> {
        // Test JSON error pages for API
        let mut api_pages = HashMap::new();
        api_pages.insert(
            404,
            ErrorPage {
                format: ErrorFormat::Json,
                template: None,
                message: Some("Resource not found".to_string()),
                headers: {
                    let mut h = HashMap::new();
                    h.insert("X-Error-Code".to_string(), "NOT_FOUND".to_string());
                    h
                },
            },
        );

        let api_error_config = ErrorPageConfig {
            pages: api_pages,
            default_format: ErrorFormat::Json,
            include_stack_trace: false,
            template_dir: None,
        };

        let api_handler = ErrorHandler::new(ServiceType::Api, Some(api_error_config));

        // Generate 404 error
        let response = api_handler.generate_response(
            StatusCode::NOT_FOUND,
            Some("User not found".to_string()),
            "req-001",
            None,
        )?;

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
        assert_eq!(
            response.headers().get("Content-Type").unwrap(),
            "application/json; charset=utf-8"
        );
        assert_eq!(response.headers().get("X-Error-Code").unwrap(), "NOT_FOUND");

        // Verify JSON response body
        let body = response.into_body().collect().await?.to_bytes();
        let json: serde_json::Value = serde_json::from_slice(&body)?;
        assert_eq!(json["status"], 404);
        assert_eq!(json["request_id"], "req-001");

        // Test HTML error pages for Web
        let mut web_pages = HashMap::new();
        web_pages.insert(
            500,
            ErrorPage {
                format: ErrorFormat::Html,
                template: None,
                message: Some("Internal server error".to_string()),
                headers: HashMap::new(),
            },
        );

        let web_error_config = ErrorPageConfig {
            pages: web_pages,
            default_format: ErrorFormat::Html,
            include_stack_trace: false,
            template_dir: None,
        };

        let web_handler = ErrorHandler::new(ServiceType::Web, Some(web_error_config));

        // Generate 500 error
        let response = web_handler.generate_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            None,
            "req-002",
            None,
        )?;

        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        assert_eq!(
            response.headers().get("Content-Type").unwrap(),
            "text/html; charset=utf-8"
        );

        // Verify HTML response contains expected elements
        let body = response.into_body().collect().await?.to_bytes();
        let html = String::from_utf8(body.to_vec())?;
        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("500"));
        assert!(html.contains("req-002"));

        Ok(())
    }

    /// Test different error formats
    #[tokio::test]
    async fn test_error_formats() -> Result<()> {
        // Test Text format
        let text_config = ErrorPageConfig {
            pages: HashMap::new(),
            default_format: ErrorFormat::Text,
            include_stack_trace: false,
            template_dir: None,
        };

        let text_handler = ErrorHandler::new(ServiceType::Api, Some(text_config));
        let response = text_handler.generate_response(
            StatusCode::BAD_REQUEST,
            Some("Invalid input".to_string()),
            "req-003",
            None,
        )?;

        assert_eq!(
            response.headers().get("Content-Type").unwrap(),
            "text/plain; charset=utf-8"
        );

        // Test XML format
        let xml_config = ErrorPageConfig {
            pages: HashMap::new(),
            default_format: ErrorFormat::Xml,
            include_stack_trace: false,
            template_dir: None,
        };

        let xml_handler = ErrorHandler::new(ServiceType::Api, Some(xml_config));
        let response = xml_handler.generate_response(
            StatusCode::FORBIDDEN,
            Some("Access denied".to_string()),
            "req-004",
            None,
        )?;

        assert_eq!(
            response.headers().get("Content-Type").unwrap(),
            "application/xml; charset=utf-8"
        );

        let body = response.into_body().collect().await?.to_bytes();
        let xml = String::from_utf8(body.to_vec())?;
        assert!(xml.contains("<?xml version="));
        assert!(xml.contains("<error>"));
        assert!(xml.contains("<status>403</status>"));

        Ok(())
    }

    /// Test cache headers for static files
    #[tokio::test]
    async fn test_static_cache_headers() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let static_root = temp_dir.path().to_path_buf();

        fs::write(static_root.join("test.txt"), b"test content").await?;

        let config = StaticFileConfig {
            root: static_root.clone(),
            index: "index.html".to_string(),
            directory_listing: false,
            cache_control: "public, max-age=86400, immutable".to_string(),
            compress: false,
            mime_types: HashMap::new(),
            fallback: None,
        };

        let server = StaticFileServer::new(config);

        let req = Request::get("/test.txt").body(()).unwrap();
        let response = server.serve(&req, "/test.txt").await?;

        assert_eq!(
            response.headers().get("Cache-Control").unwrap(),
            "public, max-age=86400, immutable"
        );
        assert!(response.headers().contains_key("ETag"));
        assert!(response.headers().contains_key("Last-Modified"));

        Ok(())
    }

    /// Test validation error response structure
    #[tokio::test]
    async fn test_validation_error_response() -> Result<()> {
        let schema = json!({
            "type": "object",
            "properties": {
                "username": {
                    "type": "string",
                    "minLength": 3
                }
            },
            "required": ["username"]
        });

        let config = ApiSchemaConfig {
            schema_file: None,
            schema_content: None,
            request_schema: Some(schema),
            response_schema: None,
            validate_requests: true,
            validate_responses: false,
            strict_mode: false,
        };

        let validator = SchemaValidator::new(config)?;

        // Invalid request with too short username
        let invalid_body = json!({
            "username": "ab"
        });

        let req = Request::post("/api/login").body(()).unwrap();

        let result = validator
            .validate_request(
                &req,
                &serde_json::to_vec(&invalid_body)?,
                "/api/login",
                "req-005",
            )
            .await;

        assert!(result.is_err());
        let error = result.unwrap_err();
        let error_str = error.to_string();
        assert!(error_str.contains("Validation"));

        Ok(())
    }
}