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)]
|
2020-01-21 22:40:37 +01:00
|
|
|
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
|
|
|
|
2020-01-21 22:40:37 +01: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)),
|
2020-01-21 22:40:37 +01:00
|
|
|
).await;
|
2019-06-24 23:43:31 +02:00
|
|
|
let req = test::TestRequest::get().uri("/").to_request();
|
2020-01-21 22:40:37 +01:00
|
|
|
let resp: AppState = test::read_response_json(&mut app, req).await;
|
2019-06-17 21:39:58 +02:00
|
|
|
|
2020-01-21 22:40:37 +01:00
|
|
|
assert_eq!(resp.count, 4);
|
2019-06-24 23:43:31 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 21:39:58 +02:00
|
|
|
// </integration-two>
|