mirror of
https://github.com/actix/actix-website
synced 2024-11-26 01:32:42 +01:00
bc4e0422c8
* update testing to actix-web 2.0.0 and improve stream testing code
34 lines
834 B
Rust
34 lines
834 B
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
struct AppState {
|
|
count: i32,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
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_rt::test]
|
|
async fn test_index_get() {
|
|
let mut app = test::init_service(
|
|
App::new()
|
|
.data(AppState { count: 4 })
|
|
.route("/", web::get().to(index)),
|
|
).await;
|
|
let req = test::TestRequest::get().uri("/").to_request();
|
|
let resp: AppState = test::read_response_json(&mut app, req).await;
|
|
|
|
assert_eq!(resp.count, 4);
|
|
}
|
|
}
|
|
// </integration-two>
|