mirror of
https://github.com/actix/actix-website
synced 2025-03-14 04:06:28 +01:00
36 lines
860 B
Rust
36 lines
860 B
Rust
use actix_web::{get, web, HttpResponse, Responder};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
struct AppState {
|
|
count: i32,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
#[get("/")]
|
|
async fn index(data: web::Data<AppState>) -> impl Responder {
|
|
HttpResponse::Ok().json(data.get_ref())
|
|
}
|
|
|
|
// <integration-two>
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use actix_web::{test, web, App};
|
|
|
|
#[actix_web::test]
|
|
async fn test_index_get() {
|
|
let app = test::init_service(
|
|
App::new()
|
|
.app_data(web::Data::new(AppState { count: 4 }))
|
|
.service(index),
|
|
)
|
|
.await;
|
|
let req = test::TestRequest::get().uri("/").to_request();
|
|
let resp: AppState = test::call_and_read_body_json(&app, req).await;
|
|
|
|
assert_eq!(resp.count, 4);
|
|
}
|
|
}
|
|
// </integration-two>
|