Skip to content

Commit

Permalink
Add bearer query
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Oct 25, 2023
1 parent d7bc0e3 commit 294d565
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crates/web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ mod tests {
};
}

#[tokio::test]
async fn test_bearer() -> anyhow::Result<()> {
let query = r#"{"query":"query { bearer }"}"#;
let expected = r#"{"data":{"bearer":"bearer1"}}"#;
let app = route();
let method = "POST";
let uri = "/graphql";
let body = query;
let body: axum::body::Body = body.into();
let request = axum::http::Request::builder()
.method(method)
.uri(uri)
.header(axum::http::header::CONTENT_TYPE, "application/json")
.header(axum::http::header::AUTHORIZATION, "Bearer bearer1")
.body(body)?;
let response = send_request(app, request).await?;
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.into_body_as_string().await?, expected);
Ok(())
}

#[tokio::test]
async fn test_hello() -> anyhow::Result<()> {
test_query(
Expand Down
6 changes: 6 additions & 0 deletions crates/web/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod check_list;
mod item;

use async_graphql::Context;
use axum::headers::authorization::Bearer;

use crate::infra::store::Store;

Expand All @@ -12,6 +13,11 @@ pub struct QueryRoot;

#[async_graphql::Object]
impl QueryRoot {
async fn bearer<'a>(&self, context: &Context<'a>) -> &'a str {
let bearer = context.data_unchecked::<Bearer>();
bearer.token()
}

async fn hello(&self) -> &'static str {
"Hello, World!"
}
Expand Down

0 comments on commit 294d565

Please sign in to comment.