Created
May 22, 2020 16:21
-
-
Save wontheone1/65d9a698ec863e207bce92d8fb2962c0 to your computer and use it in GitHub Desktop.
Actix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use actix_web::{App, web, HttpResponse, HttpServer, Result}; | |
use std::sync::Mutex; | |
use serde::{Deserialize, Serialize}; | |
use serde_json; | |
use std::ops::Deref; | |
fn dd() -> &'static str { | |
r#"{ | |
"id": "8381a025-61fc-4479-b335-cc60b0b87b60", | |
"name": "Generic cotton t-shirt", | |
"properties": { | |
"label": "textile, cotton", | |
"value": 2.01, | |
"unit": "Kg", | |
"context": { | |
"origin": "India", | |
} | |
}, | |
"parent_id": "", | |
"created_at": "2020-04-29T12:57:08+00:00" | |
"modified_at": "2020-04-29T12:57:08+00:00" | |
}"# | |
} | |
struct Product { | |
id: String, | |
name: String, | |
parent_id: String, | |
} | |
struct AppStateWithProducts { | |
products: Mutex<Vec<Product>>, | |
} | |
#[derive(Debug, PartialEq, Serialize, Deserialize)] | |
struct ProductsResponse { | |
products: Vec<Product> | |
} | |
async fn index(data: web::Data<AppStateWithProducts>) -> Result<HttpResponse> { | |
let product_response = ProductsResponse { | |
products:*data.products.lock().unwrap() | |
}; | |
Ok(HttpResponse::Ok().json(product_response)) | |
} | |
fn generate_initial_product() -> Vec<Product> { | |
vec![Product { | |
id: String::from("8381a025-61fc-4479-b335-cc60b0b87b60"), | |
name: String::from("Generic cotton t-shirt"), | |
parent_id: String::from(""), | |
}] | |
} | |
#[actix_rt::main] | |
async fn main() -> std::io::Result<()> { | |
let data = web::Data::new(AppStateWithProducts { | |
products: Mutex::new(generate_initial_product()), | |
}); | |
HttpServer::new(move || { | |
App::new() | |
.app_data(data.clone()) | |
.route("/", web::get().to(index)) | |
}) | |
.bind("127.0.0.1:8088")? | |
.run() | |
.await | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use actix_web::{App, http, test, web}; | |
use actix_web::dev::AppService; | |
#[actix_rt::test] | |
async fn test_index_get() { | |
let mut app = test::init_service( | |
App::new() | |
.data(AppStateWithProducts { | |
products: Mutex::new(generate_initial_product()), | |
}) | |
.route("/", web::get().to(index)), | |
).await; | |
let req = test::TestRequest::get().uri("/").to_request(); | |
let resp = test::call_service(&mut app, req).await; | |
let status = resp.status(); | |
let body = serde_json::from_slice(&test::read_body(resp).await).unwrap(); | |
assert_eq!(status, http::StatusCode::OK, "GET '/' should return status 200"); | |
assert_eq!(ProductsResponse { | |
products: generate_initial_product(), | |
}, | |
body, | |
"Response body contains right contents"); | |
} | |
#[actix_rt::test] | |
async fn test_index_post() { | |
let mut app = test::init_service(App::new().route("/", web::get().to(index))).await; | |
let req = test::TestRequest::post().uri("/").to_request(); | |
let resp = test::call_service(&mut app, req).await; | |
assert!(resp.status().is_client_error(), "Unsupported HTTP method returns a client error"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment