MeiliSearch/meilisearch-http/tests/common/server.rs

96 lines
2.6 KiB
Rust
Raw Normal View History

2021-03-24 11:03:01 +01:00
use std::path::Path;
2021-03-10 14:43:10 +01:00
use actix_web::http::StatusCode;
2021-02-18 19:50:52 +01:00
use byte_unit::{Byte, ByteUnit};
2021-02-19 19:14:25 +01:00
use serde_json::Value;
2021-03-10 14:43:10 +01:00
use tempdir::TempDir;
use urlencoding::encode;
2021-02-18 19:50:52 +01:00
use meilisearch_http::data::Data;
2021-03-15 18:11:10 +01:00
use meilisearch_http::option::{IndexerOpts, Opt};
2021-02-18 19:50:52 +01:00
use super::index::Index;
use super::service::Service;
pub struct Server {
2021-02-24 09:30:51 +01:00
pub service: Service,
2021-03-24 11:03:01 +01:00
// hold ownership to the tempdir while we use the server instance.
_dir: Option<tempdir::TempDir>,
2021-02-18 19:50:52 +01:00
}
impl Server {
pub async fn new() -> Self {
2021-03-11 17:59:47 +01:00
let dir = TempDir::new("meilisearch").unwrap();
2021-02-18 19:50:52 +01:00
2021-03-24 11:03:01 +01:00
let opt = default_settings(dir.path());
2021-02-18 19:50:52 +01:00
let data = Data::new(opt).unwrap();
let service = Service(data);
2021-03-24 11:29:11 +01:00
Server {
service,
_dir: Some(dir),
}
2021-03-24 11:03:01 +01:00
}
pub async fn new_with_options(opt: Opt) -> Self {
let data = Data::new(opt).unwrap();
let service = Service(data);
2021-03-24 11:29:11 +01:00
Server {
service,
_dir: None,
}
2021-02-18 19:50:52 +01:00
}
/// Returns a view to an index. There is no guarantee that the index exists.
2021-05-31 16:03:39 +02:00
pub fn index(&self, uid: impl AsRef<str>) -> Index<'_> {
2021-02-18 19:50:52 +01:00
Index {
2021-03-10 14:43:10 +01:00
uid: encode(uid.as_ref()),
2021-02-18 19:50:52 +01:00
service: &self.service,
}
}
2021-02-19 19:14:25 +01:00
pub async fn list_indexes(&self) -> (Value, StatusCode) {
self.service.get("/indexes").await
}
2021-03-15 19:08:19 +01:00
pub async fn version(&self) -> (Value, StatusCode) {
self.service.get("/version").await
}
2021-04-01 20:54:37 +02:00
pub async fn stats(&self) -> (Value, StatusCode) {
self.service.get("/stats").await
}
2021-02-18 19:50:52 +01:00
}
2021-03-24 11:03:01 +01:00
pub fn default_settings(dir: impl AsRef<Path>) -> Opt {
Opt {
db_path: dir.as_ref().join("db"),
dumps_dir: dir.as_ref().join("dump"),
http_addr: "127.0.0.1:7700".to_owned(),
master_key: None,
env: "development".to_owned(),
2021-06-16 17:12:49 +02:00
#[cfg(all(not(debug_assertions), feature = "analytics"))]
2021-03-24 11:03:01 +01:00
no_analytics: true,
max_mdb_size: Byte::from_unit(4.0, ByteUnit::GiB).unwrap(),
max_udb_size: Byte::from_unit(4.0, ByteUnit::GiB).unwrap(),
http_payload_size_limit: Byte::from_unit(10.0, ByteUnit::MiB).unwrap(),
ssl_cert_path: None,
ssl_key_path: None,
ssl_auth_path: None,
ssl_ocsp_path: None,
ssl_require_auth: false,
ssl_resumption: false,
ssl_tickets: false,
import_snapshot: None,
ignore_missing_snapshot: false,
ignore_snapshot_if_db_exists: false,
snapshot_dir: ".".into(),
schedule_snapshot: false,
snapshot_interval_sec: 0,
import_dump: None,
indexer_options: IndexerOpts::default(),
}
}