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 axum::{ | |
http::StatusCode, | |
response::{IntoResponse, Response}, | |
}; | |
.......... | |
pub async fn call_external_service() -> Response { | |
info!("call_external_service called"); | |
let base_url = std::env::var("EXTERNAL_SERVICE_URL") //catching the url to be used |
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
#[tokio::test] | |
async fn test_post_body_data() { | |
let body_data = Message { //creating the body | |
code: 777, | |
message_text: "Received body data successfully!".to_string(), | |
}; | |
let response = post_body_data(Json(body_data)).await; //transofrming to JSON and sending | |
let body = response.into_body().collect().await.unwrap().to_bytes(); |
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
#[tokio::test] | |
async fn test_get_question() { | |
let query = Query(FilterParams { //creating the query using FilterParams | |
name: Option::from("Jack".to_string()), | |
age: Option::from(25), | |
active: Option::from(true), | |
}); | |
let response = get_question(query).await; | |
let body = response.into_body().collect().await.unwrap().to_bytes(); |
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
#[tokio::test] | |
async fn test_get_params() { | |
let param_in = Path(Params { //creating the parameters using Params type as the guide | |
param_1: 1, | |
param_2: "test2".to_string(), | |
}); | |
let response = get_params(param_in).await; | |
let body = response.into_body().collect().await.unwrap().to_bytes(); //getting the body in a hard way | |
assert_eq!(body, "Parameter 1: 1, Parameter 2: test2"); |
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
............ | |
#[tokio::test] | |
async fn test_protected_route_no_auth() { //failure route | |
let headers = HeaderMap::new(); //create empty headers | |
let response = protected_route(headers).await; | |
assert_eq!(response.into_response().status(), StatusCode::UNAUTHORIZED); //verify status code | |
} |
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 axum::Json; | |
........ | |
pub async fn post_body_data(Json(payload): Json<Message>) -> Response { | |
info!("Received payload: {:?}", payload); | |
let response = format!( | |
"Received message with code: {}, text: {}", | |
payload.code, payload.message_text | |
); |
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 axum::extract::Query; | |
......... | |
pub async fn get_question(Query(params): Query<FilterParams>) -> Response { | |
let response = format!( | |
"Filters: name={}, age={}, active={}", | |
params.name.unwrap_or_default(), //as all parameters are Optional<> | |
params.age.unwrap_or_default(), //the parameters need a default method | |
params.active.unwrap_or_default() |
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 axum::{ | |
extract::Path, | |
http::StatusCode, | |
}; | |
........ | |
pub async fn get_params(Path(Params { param_1, param_2 }): Path<Params>) -> Response { | |
( | |
StatusCode::OK, |
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 axum::http::{HeaderMap, StatusCode}; | |
......... | |
pub async fn protected_route(headers: HeaderMap) -> Response { | |
// Verifies if the specific header exists and have the correct value | |
match headers.get("X-Custom-Header") { //get the specific header | |
Some(header_value) if header_value == "secret-value" => { //test the header value | |
(StatusCode::OK, "Access Granted!").into_response() //OK -> GO | |
} |
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 serde::{Deserialize, Serialize}; | |
use sqlx::FromRow; | |
#[derive(Serialize, Deserialize, FromRow, Debug, Default)] | |
pub struct Message { | |
pub code: i32, | |
pub message_text: String, | |
} | |
#[derive(Deserialize)] |
NewerOlder