1
0
mirror of https://github.com/actix/actix-website synced 2024-11-26 01:32:42 +01:00
actix-website/examples/testing/src/integration_two.rs

34 lines
834 B
Rust
Raw Normal View History

2019-06-24 23:43:31 +02:00
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
2019-06-17 21:39:58 +02:00
2019-06-24 23:43:31 +02:00
#[derive(Deserialize, Serialize, Debug)]
struct AppState {
count: i32,
}
2019-06-17 21:39:58 +02:00
2019-06-24 23:43:31 +02:00
#[allow(dead_code)]
async fn index(data: web::Data<AppState>) -> impl Responder {
2019-06-24 23:43:31 +02:00
HttpResponse::Ok().json(data.get_ref())
}
2019-06-17 21:39:58 +02:00
2019-06-24 23:43:31 +02:00
// <integration-two>
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{test, web, App};
2019-06-17 21:39:58 +02:00
#[actix_rt::test]
async fn test_index_get() {
2019-06-24 23:43:31 +02:00
let mut app = test::init_service(
App::new()
.data(AppState { count: 4 })
.route("/", web::get().to(index)),
).await;
2019-06-24 23:43:31 +02:00
let req = test::TestRequest::get().uri("/").to_request();
let resp: AppState = test::read_response_json(&mut app, req).await;
2019-06-17 21:39:58 +02:00
assert_eq!(resp.count, 4);
2019-06-24 23:43:31 +02:00
}
}
2019-06-17 21:39:58 +02:00
// </integration-two>