Skip to content

Instantly share code, notes, and snippets.

@wontheone1
Created May 15, 2020 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wontheone1/4e7df5fcc45a8285ea1b402627783a03 to your computer and use it in GitHub Desktop.
Save wontheone1/4e7df5fcc45a8285ea1b402627783a03 to your computer and use it in GitHub Desktop.
main.rs
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
async fn index() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
async fn index2() -> impl Responder {
HttpResponse::Ok().body("Hello world again!")
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/again", web::get().to(index2))
})
.bind("127.0.0.1:8088")?
.run()
.await
}
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{http};
#[actix_rt::test]
async fn test_index_status_code() {
let resp = index().await;
assert_eq!(resp.status(), http::StatusCode::OK);
}
#[actix_rt::test]
async fn test_index_body() {
let resp = index().await;
let (body, mut resp) = resp.take_body().into_future().await;
assert_eq!(body, "moi");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment