murgamu 0.8.4

A NestJS-inspired web framework for Rust
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// TODO: analyse, I ask to ai build it but it smell bad
//! Test Client for HTTP Request Testing
//!
//! This module provides a test client that allows making HTTP requests to the application
//! without starting a real server. It's designed for integration testing controllers.

use crate::server::aliases::MurRes;
use crate::server::http::MurHttpResponse;
use crate::server::http::MurRequestContext;
use crate::server::middleware::MurMiddleware;
use crate::server::router::MurRouter;
use crate::server::service::MurServiceContainer;
use http::{Method, StatusCode};
use hyper::body::Bytes;
use serde::Serialize;
use std::collections::HashMap;
use std::sync::Arc;

// =============================================================================
// Test Client
// =============================================================================

/// A test client for making HTTP requests to the application.
///
/// The test client allows you to make requests to your application without
/// starting a real HTTP server, making tests faster and more isolated.
///
/// # Example
///
/// ```ignore
/// let client = MurTestClient::new(router, container);
///
/// let response = client.get("/api/users").send().await;
/// assert_eq!(response.status(), StatusCode::OK);
/// ```
#[derive(Clone)]
pub struct MurTestClient {
	router: Arc<MurRouter>,
	container: Arc<MurServiceContainer>,
	middleware: Vec<Arc<dyn MurMiddleware>>,
	default_headers: HashMap<String, String>,
}

impl MurTestClient {
	/// Create a new test client with the given router and container.
	pub fn new(router: Arc<MurRouter>, container: Arc<MurServiceContainer>) -> Self {
		Self {
			router,
			container,
			middleware: Vec::new(),
			default_headers: HashMap::new(),
		}
	}

	/// Add middleware to the test client.
	pub fn with_middleware<M: MurMiddleware>(mut self, middleware: M) -> Self {
		self.middleware.push(Arc::new(middleware));
		self
	}

	/// Add a default header that will be sent with every request.
	pub fn with_default_header(
		mut self,
		name: impl Into<String>,
		value: impl Into<String>,
	) -> Self {
		self.default_headers.insert(name.into(), value.into());
		self
	}

	/// Add default Authorization header with Bearer token.
	pub fn with_bearer_token(self, token: impl Into<String>) -> Self {
		self.with_default_header("Authorization", format!("Bearer {}", token.into()))
	}

	/// Create a GET request builder.
	pub fn get(&self, path: &str) -> MurTestRequestBuilder {
		self.request(Method::GET, path)
	}

	/// Create a POST request builder.
	pub fn post(&self, path: &str) -> MurTestRequestBuilder {
		self.request(Method::POST, path)
	}

	/// Create a PUT request builder.
	pub fn put(&self, path: &str) -> MurTestRequestBuilder {
		self.request(Method::PUT, path)
	}

	/// Create a PATCH request builder.
	pub fn patch(&self, path: &str) -> MurTestRequestBuilder {
		self.request(Method::PATCH, path)
	}

	/// Create a DELETE request builder.
	pub fn delete(&self, path: &str) -> MurTestRequestBuilder {
		self.request(Method::DELETE, path)
	}

	/// Create a HEAD request builder.
	pub fn head(&self, path: &str) -> MurTestRequestBuilder {
		self.request(Method::HEAD, path)
	}

	/// Create an OPTIONS request builder.
	pub fn options(&self, path: &str) -> MurTestRequestBuilder {
		self.request(Method::OPTIONS, path)
	}

	/// Create a request builder with the given method and path.
	pub fn request(&self, method: Method, path: &str) -> MurTestRequestBuilder {
		let headers = self.default_headers.clone();

		MurTestRequestBuilder {
			client: self.clone(),
			method,
			path: path.to_string(),
			headers,
			body: None,
			query_params: HashMap::new(),
		}
	}
}

// =============================================================================
// Test Request Builder
// =============================================================================

/// Builder for constructing test requests.
///
/// # Example
///
/// ```ignore
/// let response = client
///     .post("/api/users")
///     .header("Content-Type", "application/json")
///     .json(&CreateUserDto { name: "John".into() })
///     .send()
///     .await;
/// ```
pub struct MurTestRequestBuilder {
	client: MurTestClient,
	method: Method,
	path: String,
	headers: HashMap<String, String>,
	body: Option<Bytes>,
	query_params: HashMap<String, String>,
}

impl MurTestRequestBuilder {
	/// Add a header to the request.
	pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
		self.headers.insert(name.into(), value.into());
		self
	}

	/// Add multiple headers to the request.
	pub fn headers(mut self, headers: impl IntoIterator<Item = (String, String)>) -> Self {
		for (name, value) in headers {
			self.headers.insert(name, value);
		}
		self
	}

	/// Set the request body as JSON.
	pub fn json<T: Serialize>(mut self, body: &T) -> Self {
		match serde_json::to_vec(body) {
			Ok(bytes) => {
				self.body = Some(Bytes::from(bytes));
				self.headers
					.insert("Content-Type".to_string(), "application/json".to_string());
			}
			Err(e) => {
				eprintln!("Failed to serialize JSON body: {}", e);
			}
		}
		self
	}

	/// Set the request body as raw bytes.
	pub fn body(mut self, body: impl Into<Bytes>) -> Self {
		self.body = Some(body.into());
		self
	}

	/// Set the request body as text.
	pub fn text(mut self, body: impl Into<String>) -> Self {
		self.body = Some(Bytes::from(body.into()));
		self.headers
			.insert("Content-Type".to_string(), "text/plain".to_string());
		self
	}

	/// Set the request body as form data.
	pub fn form<T: Serialize>(mut self, body: &T) -> Self {
		match serde_urlencoded::to_string(body) {
			Ok(encoded) => {
				self.body = Some(Bytes::from(encoded));
				self.headers.insert(
					"Content-Type".to_string(),
					"application/x-www-form-urlencoded".to_string(),
				);
			}
			Err(e) => {
				eprintln!("Failed to encode form body: {}", e);
			}
		}
		self
	}

	/// Add a query parameter to the request.
	pub fn query(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
		self.query_params.insert(name.into(), value.into());
		self
	}

	/// Add multiple query parameters to the request.
	pub fn query_params(mut self, params: impl IntoIterator<Item = (String, String)>) -> Self {
		for (name, value) in params {
			self.query_params.insert(name, value);
		}
		self
	}

	/// Set the Authorization header with a Bearer token.
	pub fn bearer_token(self, token: impl Into<String>) -> Self {
		self.header("Authorization", format!("Bearer {}", token.into()))
	}

	/// Set the Authorization header with Basic auth.
	pub fn basic_auth(self, username: impl AsRef<str>, password: impl AsRef<str>) -> Self {
		let credentials = format!("{}:{}", username.as_ref(), password.as_ref());
		let encoded = base64_encode(credentials.as_bytes());
		self.header("Authorization", format!("Basic {}", encoded))
	}

	/// Build the full path including query parameters.
	fn build_path(&self) -> String {
		if self.query_params.is_empty() {
			self.path.clone()
		} else {
			let query_string: Vec<String> = self
				.query_params
				.iter()
				.map(|(k, v)| format!("{}={}", urlencoding::encode(k), urlencoding::encode(v)))
				.collect();
			format!("{}?{}", self.path, query_string.join("&"))
		}
	}

	/// Send the request and return the response.
	pub async fn send(mut self) -> MurTestResponse {
		let path = self.build_path();

		// Build the request parts
		let mut builder = http::Request::builder()
			.method(self.method.clone())
			.uri(&path);

		for (name, value) in &self.headers {
			builder = builder.header(name, value);
		}

		let body = self.body.take().unwrap_or_default();

		// Create the request
		let request = match builder.body(body.clone()) {
			Ok(req) => req,
			Err(e) => {
				return MurTestResponse::error(format!("Failed to build request: {}", e));
			}
		};

		// Extract parts
		let (parts, _) = request.into_parts();

		// Create request context
		let ctx = MurRequestContext::new(
			parts,
			Some(body),
			HashMap::new(),
			self.client.container.clone(),
		);

		// Find and execute the route handler
		let response = self.execute_request(ctx).await;

		MurTestResponse::from_response(response)
	}

	async fn execute_request(self, mut ctx: MurRequestContext) -> MurRes {
		let path = ctx.path().to_string();
		let method = ctx.method().to_string();

		// Try to find matching route params first
		if let Some(params) = self.client.router.find_route_params(&method, &path) {
			ctx.path_params = params;
		}

		// Use the router's handle method to execute the request
		// This properly handles middleware, guards, etc.
		match self
			.client
			.router
			.execute_matched_route(&method, &path, ctx)
			.await
		{
			Some(result) => result,
			None => {
				// Route not found
				MurHttpResponse::not_found().json(serde_json::json!({
					"error": "Not Found",
					"message": format!("No route found for {} {}", method, path)
				}))
			}
		}
	}
}

// =============================================================================
// Test Request (for sending)
// =============================================================================

/// Represents a test request that has been built and is ready to send.
pub struct MurTestRequest {
	method: Method,
	path: String,
	headers: HashMap<String, String>,
	body: Option<Bytes>,
}

impl MurTestRequest {
	/// Get the request method.
	pub fn method(&self) -> &Method {
		&self.method
	}

	/// Get the request path.
	pub fn path(&self) -> &str {
		&self.path
	}

	/// Get the request headers.
	pub fn headers(&self) -> &HashMap<String, String> {
		&self.headers
	}

	/// Get the request body.
	pub fn body(&self) -> Option<&Bytes> {
		self.body.as_ref()
	}
}

// =============================================================================
// Test Response
// =============================================================================

/// Response from a test request.
///
/// Provides methods to inspect the response status, headers, and body.
///
/// # Example
///
/// ```ignore
/// let response = client.get("/api/users").send().await;
///
/// assert_eq!(response.status(), StatusCode::OK);
/// let body: Vec<User> = response.json().unwrap();
/// assert!(!body.is_empty());
/// ```
pub struct MurTestResponse {
	status: StatusCode,
	headers: HashMap<String, String>,
	body: Bytes,
	error: Option<String>,
}

impl MurTestResponse {
	/// Create a response from a MurRes.
	pub(crate) fn from_response(result: MurRes) -> Self {
		match result {
			Ok(response) => {
				let status = response.status();
				let headers: HashMap<String, String> = response
					.headers()
					.iter()
					.map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
					.collect();

				// Get body synchronously for testing
				// Note: In real implementation, you'd need to collect the body
				let body = Bytes::new();

				Self {
					status,
					headers,
					body,
					error: None,
				}
			}
			Err(e) => Self::error(format!("Request failed: {}", e)),
		}
	}

	/// Create an error response.
	pub(crate) fn error(message: String) -> Self {
		Self {
			status: StatusCode::INTERNAL_SERVER_ERROR,
			headers: HashMap::new(),
			body: Bytes::from(message.clone()),
			error: Some(message),
		}
	}

	/// Get the response status code.
	pub fn status(&self) -> StatusCode {
		self.status
	}

	/// Check if the response was successful (2xx status code).
	pub fn is_success(&self) -> bool {
		self.status.is_success()
	}

	/// Check if the response was a client error (4xx status code).
	pub fn is_client_error(&self) -> bool {
		self.status.is_client_error()
	}

	/// Check if the response was a server error (5xx status code).
	pub fn is_server_error(&self) -> bool {
		self.status.is_server_error()
	}

	/// Get the response headers.
	pub fn headers(&self) -> &HashMap<String, String> {
		&self.headers
	}

	/// Get a specific header value.
	pub fn header(&self, name: &str) -> Option<&str> {
		self.headers.get(name).map(|s| s.as_str())
	}

	/// Get the raw response body.
	pub fn body(&self) -> &Bytes {
		&self.body
	}

	/// Get the response body as a string.
	pub fn text(&self) -> Result<String, std::string::FromUtf8Error> {
		String::from_utf8(self.body.to_vec())
	}

	/// Parse the response body as JSON.
	pub fn json<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
		serde_json::from_slice(&self.body)
	}

	/// Get the error message if the request failed.
	pub fn error_message(&self) -> Option<&str> {
		self.error.as_deref()
	}

	/// Check if the request resulted in an error.
	pub fn is_error(&self) -> bool {
		self.error.is_some()
	}
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Simple base64 encoding for Basic auth.
fn base64_encode(data: &[u8]) -> String {
	const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

	let mut result = String::new();
	let chunks = data.chunks(3);

	for chunk in chunks {
		let mut buffer = [0u8; 3];
		buffer[..chunk.len()].copy_from_slice(chunk);

		let b0 = buffer[0];
		let b1 = buffer[1];
		let b2 = buffer[2];

		result.push(ALPHABET[(b0 >> 2) as usize] as char);
		result.push(ALPHABET[(((b0 & 0x03) << 4) | (b1 >> 4)) as usize] as char);

		if chunk.len() > 1 {
			result.push(ALPHABET[(((b1 & 0x0f) << 2) | (b2 >> 6)) as usize] as char);
		} else {
			result.push('=');
		}

		if chunk.len() > 2 {
			result.push(ALPHABET[(b2 & 0x3f) as usize] as char);
		} else {
			result.push('=');
		}
	}

	result
}

// =============================================================================
// Tests
// =============================================================================

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

	#[test]
	fn test_base64_encode() {
		assert_eq!(base64_encode(b"hello"), "aGVsbG8=");
		assert_eq!(base64_encode(b"world"), "d29ybGQ=");
		assert_eq!(base64_encode(b"user:pass"), "dXNlcjpwYXNz");
	}

	#[test]
	fn test_test_response_status_checks() {
		let success = MurTestResponse {
			status: StatusCode::OK,
			headers: HashMap::new(),
			body: Bytes::new(),
			error: None,
		};
		assert!(success.is_success());
		assert!(!success.is_client_error());
		assert!(!success.is_server_error());

		let not_found = MurTestResponse {
			status: StatusCode::NOT_FOUND,
			headers: HashMap::new(),
			body: Bytes::new(),
			error: None,
		};
		assert!(!not_found.is_success());
		assert!(not_found.is_client_error());
		assert!(!not_found.is_server_error());

		let server_error = MurTestResponse {
			status: StatusCode::INTERNAL_SERVER_ERROR,
			headers: HashMap::new(),
			body: Bytes::new(),
			error: None,
		};
		assert!(!server_error.is_success());
		assert!(!server_error.is_client_error());
		assert!(server_error.is_server_error());
	}

	#[test]
	fn test_query_params_building() {
		let container = Arc::new(MurServiceContainer::new());
		let builder = MurTestRequestBuilder {
			client: MurTestClient {
				router: Arc::new(MurRouter::new(Arc::clone(&container))),
				container,
				middleware: Vec::new(),
				default_headers: HashMap::new(),
			},
			method: Method::GET,
			path: "/api/users".to_string(),
			headers: HashMap::new(),
			body: None,
			query_params: HashMap::new(),
		};

		// Without query params
		assert_eq!(builder.build_path(), "/api/users");

		// With query params
		let builder = builder.query("page", "1").query("limit", "10");
		let path = builder.build_path();
		assert!(path.starts_with("/api/users?"));
		assert!(path.contains("page=1"));
		assert!(path.contains("limit=10"));
	}
}