Skip to content

Commit

Permalink
add test for introspect token
Browse files Browse the repository at this point in the history
  • Loading branch information
NewbMiao committed Jun 26, 2023
1 parent 37cca19 commit a2266cd
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,45 @@ pub async fn user_handler(
.await?;
Ok(Json(res))
}

#[cfg(test)]
mod test {
use std::sync::Arc;

use axum::{routing::get, Extension, Router};
use hyper::{http::HeaderValue, Request, StatusCode};
use tower::{ServiceBuilder, ServiceExt};

use crate::{
config::Config, errors::ServerError, extensions::keycloak_auth::KeycloakAuth,
handlers::user::user_handler,
};

#[tokio::test]
async fn test_log_request_response() -> Result<(), ServerError> {
let req = Request::builder()
.method("GET")
.uri("/")
.header(
"Authorization",
HeaderValue::from_static("Bearer test.test.test"),
)
.body(hyper::Body::empty())
.unwrap();

let config = Config::from_env()?;
// create a simple router to test the middleware
let app = Router::new().route("/", get(user_handler)).layer(
ServiceBuilder::new()
.layer(Extension(Arc::new(KeycloakAuth::new(config.keycloak))))
.into_inner(),
);

// send the request through the middleware
let res = app.clone().oneshot(req).await.unwrap();

// make sure the response has a status code of 200
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
Ok(())
}
}

0 comments on commit a2266cd

Please sign in to comment.