Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Calculator Path

  • nest
[package]
name = "calculator"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
axum = "0.8.8"
mime = "0.3.17"
serde = { version = "1.0.228", features = ["derive"] }
tokio = { version = "1.50.0", features = ["full"] }
tracing = "0.1.44"
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }

[dev-dependencies]
headers = "0.4.1"
http-body-util = "0.1.3"
tower = { version = "0.5.3", features = ["util"] }

Code

use axum::{Router, response::Html, routing::get};

mod v1calc;
mod v2calc;

async fn main_page() -> Html<&'static str> {
    Html(
        r#"
        <h1>Calculator</h1>
        <a href="/v1/add/2/3">add 2 + 3</a><br>
    "#,
    )
}

fn create_router() -> Router {
    Router::new()
        .route("/", get(main_page))
        .nest("/v1", v1calc::create_router())
        .nest("/v2", v2calc::create_router())
}

#[tokio::main]
async fn main() {
    let app = create_router();

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("listening on {}", listener.local_addr().unwrap());
    axum::serve(listener, app).await.unwrap();
}

#[cfg(test)]
mod tests;
#![allow(unused)]
fn main() {
use axum::{Router, extract::Path, response::Html, routing::get};

async fn handle_add(Path((a, b)): Path<(u32, u32)>) -> Html<String> {
    let result = a + b;
    Html(format!("{a} + {b} = {result}"))
}
async fn handle_multiply(Path((a, b)): Path<(u32, u32)>) -> Html<String> {
    let result = a * b;
    Html(format!("{a} * {b} = {result}"))
}

async fn handle_divide(Path((a, b)): Path<(u32, u32)>) -> Html<String> {
    let result = a / b;
    Html(format!("{a} / {b} = {result}"))
}

async fn handle_subtraction(Path((a, b)): Path<(u32, u32)>) -> Html<String> {
    let result = a - b;
    Html(format!("{a} - {b} = {result}"))
}

pub fn create_router() -> Router {
    Router::new()
        .route("/add/{a}/{b}", get(handle_add))
        .route("/mul/{a}/{b}", get(handle_multiply))
        .route("/div/{a}/{b}", get(handle_divide))
        .route("/sub/{a}/{b}", get(handle_subtraction))
}
}
#![allow(unused)]
fn main() {
use axum::{
    Router, extract::Path, http::StatusCode, response::Html, response::IntoResponse, routing::get,
};

async fn handle_calc(Path((op, a, b)): Path<(String, u32, u32)>) -> impl IntoResponse {
    match op.as_str() {
        "add" => {
            let result = a + b;
            (StatusCode::OK, Html(format!("{a} + {b} = {result}")))
        }
        "sub" => {
            let result = a - b;
            (StatusCode::OK, Html(format!("{a} - {b} = {result}")))
        }
        "mul" => {
            let result = a * b;
            (StatusCode::OK, Html(format!("{a} * {b} = {result}")))
        }
        "div" => {
            let result = a / b;
            (StatusCode::OK, Html(format!("{a} / {b} = {result}")))
        }
        _ => (
            StatusCode::NOT_FOUND,
            Html(format!("Unhandled operator: {op}")),
        ),
    }
}

pub fn create_router() -> Router {
    Router::new().route("/{op}/{a}/{b}", get(handle_calc))
}
}

Tests

#![allow(unused)]
fn main() {
use axum::{body::Body, http::Request, http::StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;

use super::*;

#[tokio::test]
async fn test_main_page() {
    check_contains("/", "<h1>Calculator</h1>").await;
}

#[tokio::test]
async fn test_v1_add() {
    check_equals("/v1/add/2/3", "2 + 3 = 5").await;
    check_equals("/v1/add/7/8", "7 + 8 = 15").await;
}

#[tokio::test]
async fn test_v1_subtraction() {
    check_equals("/v1/sub/5/3", "5 - 3 = 2").await;
    check_equals("/v1/sub/8/7", "8 - 7 = 1").await;
}

#[tokio::test]
async fn test_v1_multiply() {
    check_equals("/v1/mul/2/3", "2 * 3 = 6").await;
    check_equals("/v1/mul/7/8", "7 * 8 = 56").await;
}

#[tokio::test]
async fn test_v1_divide() {
    check_equals("/v1/div/6/3", "6 / 3 = 2").await;
    check_equals("/v1/div/120/10", "120 / 10 = 12").await;
}

#[tokio::test]
async fn test_v1_other() {
    let uri = "/v1/other/6/3";
    let response = create_router()
        .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::NOT_FOUND);

    // No content-type
    assert!(response.headers().get("content-type").is_none());

    let body = response.into_body();
    let bytes = body.collect().await.unwrap().to_bytes();
    let content = String::from_utf8(bytes.to_vec()).unwrap();
    assert_eq!(content, "");
}

#[tokio::test]
async fn test_v2_add() {
    check_equals("/v2/add/2/3", "2 + 3 = 5").await;
    check_equals("/v2/add/7/8", "7 + 8 = 15").await;
}

#[tokio::test]
async fn test_v2_subtraction() {
    check_equals("/v2/sub/5/3", "5 - 3 = 2").await;
    check_equals("/v2/sub/8/7", "8 - 7 = 1").await;
}

#[tokio::test]
async fn test_v2_multiply() {
    check_equals("/v2/mul/2/3", "2 * 3 = 6").await;
    check_equals("/v2/mul/7/8", "7 * 8 = 56").await;
}

#[tokio::test]
async fn test_v2_divide() {
    check_equals("/v2/div/6/3", "6 / 3 = 2").await;
    check_equals("/v2/div/120/10", "120 / 10 = 12").await;
}

#[tokio::test]
async fn test_v2_other() {
    let uri = "/v2/other/6/3";
    let response = create_router()
        .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::NOT_FOUND);

    let content_type = response.headers().get("content-type").unwrap();
    assert_eq!(content_type.to_str().unwrap(), "text/html; charset=utf-8");

    let body = response.into_body();
    let bytes = body.collect().await.unwrap().to_bytes();
    let content = String::from_utf8(bytes.to_vec()).unwrap();
    assert_eq!(content, "Unhandled operator: other");
}

async fn check_contains(uri: &str, expected: &str) {
    let html = get_page(uri).await;
    assert!(html.contains(expected));
}
async fn check_equals(uri: &str, expected: &str) {
    let html = get_page(uri).await;
    assert_eq!(html, expected);
}

async fn get_page(uri: &str) -> String {
    let response = create_router()
        .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);

    let content_type = response.headers().get("content-type").unwrap();
    assert_eq!(content_type.to_str().unwrap(), "text/html; charset=utf-8");

    let body = response.into_body();
    let bytes = body.collect().await.unwrap().to_bytes();
    String::from_utf8(bytes.to_vec()).unwrap()
}
}