make the code of init_web_app in common between most tests

This commit is contained in:
Tamo 2024-09-03 11:40:05 +02:00
parent 1040e5e2b4
commit 005204e9e5
2 changed files with 17 additions and 37 deletions

View File

@ -1,27 +1,21 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::marker::PhantomData; use std::marker::PhantomData;
use std::num::NonZeroUsize;
use std::path::Path; use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use actix_http::body::MessageBody; use actix_http::body::MessageBody;
use actix_web::dev::ServiceResponse; use actix_web::dev::ServiceResponse;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::web::Data;
use byte_unit::{Byte, Unit}; use byte_unit::{Byte, Unit};
use clap::Parser; use clap::Parser;
use meilisearch::option::{IndexerOpts, MaxMemory, MaxThreads, Opt}; use meilisearch::option::{IndexerOpts, MaxMemory, MaxThreads, Opt};
use meilisearch::search_queue::SearchQueue; use meilisearch::setup_meilisearch;
use meilisearch::{analytics, create_app, setup_meilisearch, SubscriberForSecondLayer};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use tempfile::TempDir; use tempfile::TempDir;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
use tokio::time::sleep; use tokio::time::sleep;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::Layer;
use uuid::Uuid; use uuid::Uuid;
use super::index::Index; use super::index::Index;
@ -267,33 +261,7 @@ impl<State> Server<State> {
Response = ServiceResponse<impl MessageBody>, Response = ServiceResponse<impl MessageBody>,
Error = actix_web::Error, Error = actix_web::Error,
> { > {
let (_route_layer, route_layer_handle) = self.service.init_web_app().await
tracing_subscriber::reload::Layer::new(None.with_filter(
tracing_subscriber::filter::Targets::new().with_target("", LevelFilter::OFF),
));
let (_stderr_layer, stderr_layer_handle) = tracing_subscriber::reload::Layer::new(
(Box::new(
tracing_subscriber::fmt::layer()
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE),
)
as Box<dyn tracing_subscriber::Layer<SubscriberForSecondLayer> + Send + Sync>)
.with_filter(tracing_subscriber::filter::Targets::new()),
);
let search_queue = SearchQueue::new(
self.service.options.experimental_search_queue_size,
NonZeroUsize::new(1).unwrap(),
);
actix_web::test::init_service(create_app(
self.service.index_scheduler.clone().into(),
self.service.auth.clone().into(),
Data::new(search_queue),
self.service.options.clone(),
(route_layer_handle, stderr_layer_handle),
analytics::MockAnalytics::new(&self.service.options),
true,
))
.await
} }
pub async fn list_api_keys(&self, params: &str) -> (Value, StatusCode) { pub async fn list_api_keys(&self, params: &str) -> (Value, StatusCode) {

View File

@ -1,6 +1,8 @@
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
use std::sync::Arc; use std::sync::Arc;
use actix_web::body::MessageBody;
use actix_web::dev::ServiceResponse;
use actix_web::http::header::ContentType; use actix_web::http::header::ContentType;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::test; use actix_web::test;
@ -109,7 +111,13 @@ impl Service {
self.request(req).await self.request(req).await
} }
pub async fn request(&self, mut req: test::TestRequest) -> (Value, StatusCode) { pub async fn init_web_app(
&self,
) -> impl actix_web::dev::Service<
actix_http::Request,
Response = ServiceResponse<impl MessageBody>,
Error = actix_web::Error,
> {
let (_route_layer, route_layer_handle) = let (_route_layer, route_layer_handle) =
tracing_subscriber::reload::Layer::new(None.with_filter( tracing_subscriber::reload::Layer::new(None.with_filter(
tracing_subscriber::filter::Targets::new().with_target("", LevelFilter::OFF), tracing_subscriber::filter::Targets::new().with_target("", LevelFilter::OFF),
@ -127,7 +135,7 @@ impl Service {
NonZeroUsize::new(1).unwrap(), NonZeroUsize::new(1).unwrap(),
); );
let app = test::init_service(create_app( actix_web::test::init_service(create_app(
self.index_scheduler.clone().into(), self.index_scheduler.clone().into(),
self.auth.clone().into(), self.auth.clone().into(),
Data::new(search_queue), Data::new(search_queue),
@ -136,7 +144,11 @@ impl Service {
analytics::MockAnalytics::new(&self.options), analytics::MockAnalytics::new(&self.options),
true, true,
)) ))
.await; .await
}
pub async fn request(&self, mut req: test::TestRequest) -> (Value, StatusCode) {
let app = self.init_web_app().await;
if let Some(api_key) = &self.api_key { if let Some(api_key) = &self.api_key {
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat())); req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));