2021-02-19 02:50:52 +08:00
|
|
|
use actix_web::{http::StatusCode, test};
|
2021-11-09 01:31:27 +08:00
|
|
|
use meilisearch_auth::AuthController;
|
2021-09-29 00:10:09 +08:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2021-02-19 02:50:52 +08:00
|
|
|
use serde_json::Value;
|
|
|
|
|
2021-10-12 20:32:44 +08:00
|
|
|
use meilisearch_http::{analytics, create_app, Opt};
|
2021-02-19 02:50:52 +08:00
|
|
|
|
2021-09-29 00:10:09 +08:00
|
|
|
pub struct Service {
|
|
|
|
pub meilisearch: MeiliSearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
pub auth: AuthController,
|
2021-09-29 00:10:09 +08:00
|
|
|
pub options: Opt,
|
2021-11-09 01:31:27 +08:00
|
|
|
pub api_key: Option<String>,
|
2021-09-29 00:10:09 +08:00
|
|
|
}
|
2021-02-19 02:50:52 +08:00
|
|
|
|
|
|
|
impl Service {
|
|
|
|
pub async fn post(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
|
2021-10-12 20:32:44 +08:00
|
|
|
let app = test::init_service(create_app!(
|
|
|
|
&self.meilisearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
&self.auth,
|
2021-10-12 20:32:44 +08:00
|
|
|
true,
|
2022-08-22 13:22:09 +08:00
|
|
|
self.options,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics::MockAnalytics::new(&self.options).0
|
2021-10-12 20:32:44 +08:00
|
|
|
))
|
|
|
|
.await;
|
2021-02-19 02:50:52 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
let mut req = test::TestRequest::post().uri(url.as_ref()).set_json(&body);
|
|
|
|
if let Some(api_key) = &self.api_key {
|
|
|
|
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));
|
|
|
|
}
|
|
|
|
let req = req.to_request();
|
2021-03-23 02:17:18 +08:00
|
|
|
let res = test::call_service(&app, req).await;
|
2021-02-19 02:50:52 +08: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)
|
|
|
|
}
|
|
|
|
|
2021-02-22 21:23:17 +08:00
|
|
|
/// Send a test post request from a text body, with a `content-type:application/json` header.
|
2021-03-16 01:11:10 +08:00
|
|
|
pub async fn post_str(
|
|
|
|
&self,
|
|
|
|
url: impl AsRef<str>,
|
|
|
|
body: impl AsRef<str>,
|
|
|
|
) -> (Value, StatusCode) {
|
2021-10-12 20:32:44 +08:00
|
|
|
let app = test::init_service(create_app!(
|
|
|
|
&self.meilisearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
&self.auth,
|
2021-10-12 20:32:44 +08:00
|
|
|
true,
|
2022-08-22 13:22:09 +08:00
|
|
|
self.options,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics::MockAnalytics::new(&self.options).0
|
2021-10-12 20:32:44 +08:00
|
|
|
))
|
|
|
|
.await;
|
2021-02-22 21:23:17 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
let mut req = test::TestRequest::post()
|
2021-02-22 21:23:17 +08:00
|
|
|
.uri(url.as_ref())
|
|
|
|
.set_payload(body.as_ref().to_string())
|
2021-11-09 01:31:27 +08:00
|
|
|
.insert_header(("content-type", "application/json"));
|
|
|
|
if let Some(api_key) = &self.api_key {
|
|
|
|
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));
|
|
|
|
}
|
|
|
|
let req = req.to_request();
|
2021-03-23 02:17:18 +08:00
|
|
|
let res = test::call_service(&app, req).await;
|
2021-02-22 21:23:17 +08: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)
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:50:52 +08:00
|
|
|
pub async fn get(&self, url: impl AsRef<str>) -> (Value, StatusCode) {
|
2021-10-12 20:32:44 +08:00
|
|
|
let app = test::init_service(create_app!(
|
|
|
|
&self.meilisearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
&self.auth,
|
2021-10-12 20:32:44 +08:00
|
|
|
true,
|
2022-08-22 13:22:09 +08:00
|
|
|
self.options,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics::MockAnalytics::new(&self.options).0
|
2021-10-12 20:32:44 +08:00
|
|
|
))
|
|
|
|
.await;
|
2021-02-19 02:50:52 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
let mut req = test::TestRequest::get().uri(url.as_ref());
|
|
|
|
if let Some(api_key) = &self.api_key {
|
|
|
|
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));
|
|
|
|
}
|
|
|
|
let req = req.to_request();
|
2021-03-23 02:17:18 +08:00
|
|
|
let res = test::call_service(&app, req).await;
|
2021-02-19 02:50:52 +08: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)
|
|
|
|
}
|
2021-02-19 03:28:10 +08:00
|
|
|
|
|
|
|
pub async fn put(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
|
2021-10-12 20:32:44 +08:00
|
|
|
let app = test::init_service(create_app!(
|
|
|
|
&self.meilisearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
&self.auth,
|
2021-10-12 20:32:44 +08:00
|
|
|
true,
|
2022-08-22 13:22:09 +08:00
|
|
|
self.options,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics::MockAnalytics::new(&self.options).0
|
2021-10-12 20:32:44 +08:00
|
|
|
))
|
|
|
|
.await;
|
2021-02-19 03:28:10 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
let mut req = test::TestRequest::put().uri(url.as_ref()).set_json(&body);
|
|
|
|
if let Some(api_key) = &self.api_key {
|
|
|
|
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));
|
|
|
|
}
|
|
|
|
let req = req.to_request();
|
|
|
|
let res = test::call_service(&app, req).await;
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn patch(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
|
|
|
|
let app = test::init_service(create_app!(
|
|
|
|
&self.meilisearch,
|
|
|
|
&self.auth,
|
|
|
|
true,
|
2022-08-22 13:22:09 +08:00
|
|
|
self.options,
|
2021-11-09 01:31:27 +08:00
|
|
|
analytics::MockAnalytics::new(&self.options).0
|
|
|
|
))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let mut req = test::TestRequest::patch().uri(url.as_ref()).set_json(&body);
|
|
|
|
if let Some(api_key) = &self.api_key {
|
|
|
|
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));
|
|
|
|
}
|
|
|
|
let req = req.to_request();
|
2021-03-23 02:17:18 +08:00
|
|
|
let res = test::call_service(&app, req).await;
|
2021-02-19 03:28:10 +08: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)
|
|
|
|
}
|
2021-02-19 03:44:33 +08:00
|
|
|
|
|
|
|
pub async fn delete(&self, url: impl AsRef<str>) -> (Value, StatusCode) {
|
2021-10-12 20:32:44 +08:00
|
|
|
let app = test::init_service(create_app!(
|
|
|
|
&self.meilisearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
&self.auth,
|
2021-10-12 20:32:44 +08:00
|
|
|
true,
|
2022-08-22 13:22:09 +08:00
|
|
|
self.options,
|
2021-10-29 22:10:58 +08:00
|
|
|
analytics::MockAnalytics::new(&self.options).0
|
2021-10-12 20:32:44 +08:00
|
|
|
))
|
|
|
|
.await;
|
2021-02-19 03:44:33 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
let mut req = test::TestRequest::delete().uri(url.as_ref());
|
|
|
|
if let Some(api_key) = &self.api_key {
|
|
|
|
req = req.insert_header(("Authorization", ["Bearer ", api_key].concat()));
|
|
|
|
}
|
|
|
|
let req = req.to_request();
|
2021-03-23 02:17:18 +08:00
|
|
|
let res = test::call_service(&app, req).await;
|
2021-02-19 03:44:33 +08: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)
|
|
|
|
}
|
2021-02-19 02:50:52 +08:00
|
|
|
}
|