1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-09 04:22:45 +03:00

handlers: add a test for OIDC discovery

This commit is contained in:
Quentin Gliech
2023-02-22 14:59:15 +01:00
parent 1e9ce8d6d6
commit 03583d2936
4 changed files with 75 additions and 14 deletions

View File

@@ -19,8 +19,8 @@ use axum::{
body::HttpBody,
extract::{FromRef, FromRequestParts},
};
use headers::{Authorization, ContentType, HeaderMapExt};
use hyper::{Request, Response, StatusCode};
use headers::{Authorization, ContentType, HeaderMapExt, HeaderName, HeaderValue};
use hyper::{header::CONTENT_TYPE, Request, Response, StatusCode};
use mas_axum_utils::http_client_factory::HttpClientFactory;
use mas_email::{MailTransport, Mailer};
use mas_keystore::{Encrypter, JsonWebKey, JsonWebKeySet, Keystore, PrivateKey};
@@ -31,7 +31,7 @@ use mas_storage_pg::PgRepository;
use mas_templates::Templates;
use rand::SeedableRng;
use rand_chacha::ChaChaRng;
use serde::Serialize;
use serde::{de::DeserializeOwned, Serialize};
use sqlx::PgPool;
use tokio::sync::Mutex;
use tower::{Service, ServiceExt};
@@ -366,6 +366,22 @@ pub(crate) trait ResponseExt {
///
/// Panics if the response has a different status code.
fn assert_status(&self, status: StatusCode);
/// Asserts that the response has the given header value.
///
/// # Panics
///
/// Panics if the response does not have the given header or if the header
/// value does not match.
fn assert_header_value(&self, header: HeaderName, value: &str);
/// Get the response body as JSON.
///
/// # Panics
///
/// Panics if the response is missing the `Content-Type: application/json`,
/// or if the body is not valid JSON.
fn json<T: DeserializeOwned>(&self) -> T;
}
impl ResponseExt for Response<String> {
@@ -380,4 +396,26 @@ impl ResponseExt for Response<String> {
self.body()
);
}
#[track_caller]
fn assert_header_value(&self, header: HeaderName, value: &str) {
let actual_value = self
.headers()
.get(&header)
.unwrap_or_else(|| panic!("Missing header {header}"));
assert_eq!(
actual_value,
value,
"Header mismatch: got {:?}, expected {:?}",
self.headers().get(header),
value
);
}
#[track_caller]
fn json<T: DeserializeOwned>(&self) -> T {
self.assert_header_value(CONTENT_TYPE, "application/json");
serde_json::from_str(self.body()).expect("JSON deserialization failed")
}
}