feat: reorganize workbook into 3 tracks (core, engineering-2026, ecosystems)
This commit is contained in:
1 parent
82677455b2
commit
2e002b6176
64 files changed
+2295
-196
No files matched your search
@@ -0,0 +1,110 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct HealthResponse {
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct EchoRequest {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct EchoResponse {
|
||||
pub echoed: String,
|
||||
pub len: usize,
|
||||
}
|
||||
|
||||
async fn health_handler() -> Json<HealthResponse> {
|
||||
todo!("Zwróć Json(HealthResponse {{ status: \"ok\".into() }})")
|
||||
}
|
||||
|
||||
async fn echo_handler(Json(payload): Json<EchoRequest>) -> Result<Json<EchoResponse>, StatusCode> {
|
||||
todo!("Odrzuć pusty message jako Err(StatusCode::BAD_REQUEST), inaczej zwróć EchoResponse")
|
||||
}
|
||||
|
||||
pub fn build_router() -> Router {
|
||||
Router::new()
|
||||
.route("/health", get(health_handler))
|
||||
.route("/echo", post(echo_handler))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::{body::Body, http::Request};
|
||||
use http_body_util::BodyExt;
|
||||
use tower::ServiceExt;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_endpoint_returns_ok_json() {
|
||||
let app = build_router();
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/health")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let bytes = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let body: HealthResponse = serde_json::from_slice(&bytes).unwrap();
|
||||
assert_eq!(
|
||||
body,
|
||||
HealthResponse {
|
||||
status: String::from("ok")
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn echo_endpoint_validates_and_echoes() {
|
||||
let app = build_router();
|
||||
let ok_resp = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/echo")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(r#"{"message":"rust"}"#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(ok_resp.status(), StatusCode::OK);
|
||||
let bytes = ok_resp.into_body().collect().await.unwrap().to_bytes();
|
||||
let body: EchoResponse = serde_json::from_slice(&bytes).unwrap();
|
||||
assert_eq!(
|
||||
body,
|
||||
EchoResponse {
|
||||
echoed: String::from("rust"),
|
||||
len: 4
|
||||
}
|
||||
);
|
||||
|
||||
let bad_resp = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/echo")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(r#"{"message":" "}"#))
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(bad_resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user