2022-10-18 15:14:18 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
use actix_web::http::header::ContentType;
|
2022-10-20 18:00:07 +02:00
|
|
|
use actix_web::http::StatusCode;
|
|
|
|
use actix_web::test;
|
2022-10-09 19:43:51 +02:00
|
|
|
use actix_web::test::TestRequest;
|
2022-10-18 11:57:00 +02:00
|
|
|
use index_scheduler::IndexScheduler;
|
2024-02-12 11:06:37 +01:00
|
|
|
use meilisearch::{analytics, create_app, Opt, SubscriberForSecondLayer};
|
2022-12-08 16:31:15 +01:00
|
|
|
use meilisearch_auth::AuthController;
|
2024-01-31 17:46:36 +01:00
|
|
|
use tracing::level_filters::LevelFilter;
|
|
|
|
use tracing_subscriber::Layer;
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
use crate::common::encoder::Encoder;
|
2023-09-11 16:50:53 +02:00
|
|
|
use crate::common::Value;
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2021-09-28 18:10:09 +02:00
|
|
|
pub struct Service {
|
2022-10-18 15:14:18 +02:00
|
|
|
pub index_scheduler: Arc<IndexScheduler>,
|
2023-04-06 13:38:47 +02:00
|
|
|
pub auth: Arc<AuthController>,
|
2021-09-28 18:10:09 +02:00
|
|
|
pub options: Opt,
|
2021-11-08 18:31:27 +01:00
|
|
|
pub api_key: Option<String>,
|
2021-09-28 18:10:09 +02:00
|
|
|
}
|
2021-02-18 19:50:52 +01:00
|
|
|
|
|
|
|
impl Service {
|
|
|
|
pub async fn post(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
self.post_encoded(url, body, Encoder::Plain).await
|
|
|
|
}
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
pub async fn post_encoded(
|
|
|
|
&self,
|
|
|
|
url: impl AsRef<str>,
|
|
|
|
body: Value,
|
|
|
|
encoder: Encoder,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let mut req = test::TestRequest::post().uri(url.as_ref());
|
|
|
|
req = self.encode(req, body, encoder);
|
|
|
|
self.request(req).await
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 13:59:58 +01:00
|
|
|
/// Send a test post request from a text body.
|
2021-03-15 18:11:10 +01:00
|
|
|
pub async fn post_str(
|
|
|
|
&self,
|
|
|
|
url: impl AsRef<str>,
|
|
|
|
body: impl AsRef<str>,
|
2023-02-16 13:59:58 +01:00
|
|
|
headers: Vec<(&str, &str)>,
|
2021-03-15 18:11:10 +01:00
|
|
|
) -> (Value, StatusCode) {
|
2023-02-16 13:59:58 +01:00
|
|
|
let mut req =
|
|
|
|
test::TestRequest::post().uri(url.as_ref()).set_payload(body.as_ref().to_string());
|
|
|
|
for header in headers {
|
|
|
|
req = req.insert_header(header);
|
|
|
|
}
|
2022-10-09 19:43:51 +02:00
|
|
|
self.request(req).await
|
2021-02-22 14:23:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 19:50:52 +01:00
|
|
|
pub async fn get(&self, url: impl AsRef<str>) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let req = test::TestRequest::get().uri(url.as_ref());
|
|
|
|
self.request(req).await
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
2021-02-18 20:28:10 +01:00
|
|
|
|
|
|
|
pub async fn put(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
self.put_encoded(url, body, Encoder::Plain).await
|
|
|
|
}
|
2021-11-08 18:31:27 +01:00
|
|
|
|
2023-02-16 13:59:58 +01:00
|
|
|
/// Send a test put request from a text body.
|
|
|
|
pub async fn put_str(
|
|
|
|
&self,
|
|
|
|
url: impl AsRef<str>,
|
|
|
|
body: impl AsRef<str>,
|
|
|
|
headers: Vec<(&str, &str)>,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let mut req =
|
|
|
|
test::TestRequest::put().uri(url.as_ref()).set_payload(body.as_ref().to_string());
|
|
|
|
for header in headers {
|
|
|
|
req = req.insert_header(header);
|
|
|
|
}
|
|
|
|
self.request(req).await
|
|
|
|
}
|
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
pub async fn put_encoded(
|
|
|
|
&self,
|
|
|
|
url: impl AsRef<str>,
|
|
|
|
body: Value,
|
|
|
|
encoder: Encoder,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let mut req = test::TestRequest::put().uri(url.as_ref());
|
|
|
|
req = self.encode(req, body, encoder);
|
|
|
|
self.request(req).await
|
2021-11-08 18:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn patch(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
self.patch_encoded(url, body, Encoder::Plain).await
|
|
|
|
}
|
2021-02-18 20:28:10 +01:00
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
pub async fn patch_encoded(
|
|
|
|
&self,
|
|
|
|
url: impl AsRef<str>,
|
|
|
|
body: Value,
|
|
|
|
encoder: Encoder,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let mut req = test::TestRequest::patch().uri(url.as_ref());
|
|
|
|
req = self.encode(req, body, encoder);
|
|
|
|
self.request(req).await
|
2021-02-18 20:28:10 +01:00
|
|
|
}
|
2021-02-18 20:44:33 +01:00
|
|
|
|
|
|
|
pub async fn delete(&self, url: impl AsRef<str>) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let req = test::TestRequest::delete().uri(url.as_ref());
|
|
|
|
self.request(req).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn request(&self, mut req: test::TestRequest) -> (Value, StatusCode) {
|
2024-01-31 17:46:36 +01:00
|
|
|
let (_route_layer, route_layer_handle) =
|
|
|
|
tracing_subscriber::reload::Layer::new(None.with_filter(
|
|
|
|
tracing_subscriber::filter::Targets::new().with_target("", LevelFilter::OFF),
|
|
|
|
));
|
2024-02-12 11:06:37 +01:00
|
|
|
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()),
|
|
|
|
);
|
2024-01-31 17:46:36 +01:00
|
|
|
|
2022-10-18 15:14:18 +02:00
|
|
|
let app = test::init_service(create_app(
|
|
|
|
self.index_scheduler.clone().into(),
|
2023-04-06 13:38:47 +02:00
|
|
|
self.auth.clone().into(),
|
2022-10-18 15:14:18 +02:00
|
|
|
self.options.clone(),
|
2024-02-12 11:06:37 +01:00
|
|
|
(route_layer_handle, stderr_layer_handle),
|
2022-10-18 15:14:18 +02:00
|
|
|
analytics::MockAnalytics::new(&self.options),
|
2021-10-12 14:32:44 +02:00
|
|
|
true,
|
|
|
|
))
|
|
|
|
.await;
|
2021-02-18 20:44:33 +01:00
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
if let Some(api_key) = &self.api_key {
|
|
|
|
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));
|
|
|
|
}
|
|
|
|
let req = req.to_request();
|
2021-03-22 19:17:18 +01:00
|
|
|
let res = test::call_service(&app, req).await;
|
2021-02-18 20:44:33 +01:00
|
|
|
let status_code = res.status();
|
|
|
|
|
|
|
|
let body = test::read_body(res).await;
|
|
|
|
let response = serde_json::from_slice(&body).unwrap_or_default();
|
|
|
|
(response, status_code)
|
|
|
|
}
|
2022-10-09 19:43:51 +02:00
|
|
|
|
|
|
|
fn encode(&self, req: TestRequest, body: Value, encoder: Encoder) -> TestRequest {
|
|
|
|
let bytes = serde_json::to_string(&body).expect("Failed to serialize test data to json");
|
|
|
|
let encoded_body = encoder.encode(bytes);
|
|
|
|
let header = encoder.header();
|
|
|
|
match header {
|
|
|
|
Some(header) => req.insert_header(header),
|
|
|
|
None => req,
|
|
|
|
}
|
|
|
|
.set_payload(encoded_body)
|
|
|
|
.insert_header(ContentType::json())
|
|
|
|
}
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|