1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 19:14:36 +01:00
actix-website/examples/testing/src/main.rs
2022-02-26 03:56:24 +00:00

45 lines
1.1 KiB
Rust

pub mod integration_one;
pub mod integration_two;
pub mod stream_response;
use actix_web::{http, web, App, HttpRequest, HttpResponse};
async fn index(req: HttpRequest) -> HttpResponse {
if let Some(hdr) = req.headers().get(http::header::CONTENT_TYPE) {
if let Ok(_s) = hdr.to_str() {
return HttpResponse::Ok().into();
}
}
HttpResponse::BadRequest().into()
}
fn main() {
App::new().route("/", web::get().to(index));
}
// <unit-tests>
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{
http::{self, header::ContentType},
test,
};
#[actix_web::test]
async fn test_index_ok() {
let req = test::TestRequest::default()
.insert_header(ContentType::plaintext())
.to_http_request();
let resp = index(req).await;
assert_eq!(resp.status(), http::StatusCode::OK);
}
#[actix_web::test]
async fn test_index_not_ok() {
let req = test::TestRequest::default().to_http_request();
let resp = index(req).await;
assert_eq!(resp.status(), http::StatusCode::BAD_REQUEST);
}
}
// </unit-tests>