Fix formatting and apply clippy suggestion

This commit is contained in:
Andrey "MOU" Larionov 2022-10-10 11:04:46 +02:00
parent 9dbc71cb6d
commit 343a677566
No known key found for this signature in database
GPG Key ID: 5FF293FC94C01D6A
4 changed files with 28 additions and 17 deletions

View File

@ -1,10 +1,10 @@
use std::io::Write;
use actix_http::header::TryIntoHeaderPair;
use bytes::Bytes;
use flate2::write::{GzEncoder, ZlibEncoder};
use flate2::Compression;
use std::io::Write;
#[derive(Clone,Copy)]
#[derive(Clone, Copy)]
pub enum Encoder {
Plain,
Gzip,
@ -17,18 +17,24 @@ impl Encoder {
match self {
Self::Gzip => {
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(&body.into()).expect("Failed to encode request body");
encoder
.write_all(&body.into())
.expect("Failed to encode request body");
encoder.finish().expect("Failed to encode request body")
}
Self::Deflate => {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(&body.into()).expect("Failed to encode request body");
encoder
.write_all(&body.into())
.expect("Failed to encode request body");
encoder.finish().unwrap()
}
Self::Plain => Vec::from(body.into()),
Self::Brotli => {
let mut encoder = brotli::CompressorWriter::new(Vec::new(), 32 * 1024, 3, 22);
encoder.write_all(&body.into()).expect("Failed to encode request body");
encoder
.write_all(&body.into())
.expect("Failed to encode request body");
encoder.flush().expect("Failed to encode request body");
encoder.into_inner()
}
@ -45,6 +51,8 @@ impl Encoder {
}
pub fn iterator() -> impl Iterator<Item = Self> {
[Self::Plain, Self::Gzip, Self::Deflate, Self::Brotli].iter().copied()
[Self::Plain, Self::Gzip, Self::Deflate, Self::Brotli]
.iter()
.copied()
}
}

View File

@ -190,7 +190,9 @@ impl Index<'_> {
pub async fn update_settings(&self, settings: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/settings", urlencode(self.uid.as_ref()));
self.service.patch_encoded(url, settings, self.encoder).await
self.service
.patch_encoded(url, settings, self.encoder)
.await
}
pub async fn delete_settings(&self) -> (Value, StatusCode) {
@ -230,7 +232,11 @@ impl Index<'_> {
pub async fn search_get(&self, query: Value) -> (Value, StatusCode) {
let params = yaup::to_string(&query).unwrap();
let url = format!("/indexes/{}/search?{}", urlencode(self.uid.as_ref()), params);
let url = format!(
"/indexes/{}/search?{}",
urlencode(self.uid.as_ref()),
params
);
self.service.get(url).await
}

View File

@ -1,10 +1,10 @@
use crate::common::{GetAllDocumentsOptions, Server};
use actix_web::test;
use crate::common::encoder::Encoder;
use meilisearch_http::{analytics, create_app};
use serde_json::{json, Value};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use crate::common::encoder::Encoder;
/// This is the basic usage of our API and every other tests uses the content-type application/json
#[actix_rt::test]
@ -115,7 +115,7 @@ async fn add_single_document_gzip_encoded() {
server.service.options,
analytics::MockAnalytics::new(&server.service.options).0
))
.await;
.await;
// post
let document = serde_json::to_string(&document).unwrap();
let encoder = Encoder::Gzip;
@ -164,19 +164,18 @@ async fn add_single_document_with_every_encoding() {
server.service.options,
analytics::MockAnalytics::new(&server.service.options).0
))
.await;
.await;
// post
let mut task_uid = 0;
let document = serde_json::to_string(&document).unwrap();
for encoder in Encoder::iterator() {
for (task_uid, encoder) in Encoder::iterator().enumerate() {
let mut req = test::TestRequest::post()
.uri("/indexes/dog/documents")
.set_payload(encoder.encode(document.clone()))
.insert_header(("content-type", "application/json"));
req = match encoder.header() {
Some(header) => req.insert_header(header),
None => req
None => req,
};
let req = req.to_request();
let res = test::call_service(&app, req).await;
@ -185,9 +184,7 @@ async fn add_single_document_with_every_encoding() {
let response: Value = serde_json::from_slice(&body).unwrap_or_default();
assert_eq!(status_code, 202);
assert_eq!(response["taskUid"], task_uid);
task_uid += 1;
}
}
/// any other content-type is must be refused

View File

@ -1,7 +1,7 @@
use crate::common::{GetAllDocumentsOptions, Server};
use serde_json::json;
use crate::common::encoder::Encoder;
use serde_json::json;
#[actix_rt::test]
async fn error_document_update_create_index_bad_uid() {