Added test to verify response encoding

Alongside request encoding (compression) support, it is helpful to verify that the server respect `Accept-Encoding` headers and apply the corresponding compression to responses.
This commit is contained in:
Andrey "MOU" Larionov 2022-10-13 00:56:57 +02:00
parent 99e2788ee7
commit b69f8d67c3
No known key found for this signature in database
GPG key ID: 5FF293FC94C01D6A
3 changed files with 115 additions and 2 deletions

View file

@ -1,5 +1,9 @@
use crate::common::encoder::Encoder;
use crate::common::Server;
use actix_web::http::header::ContentType;
use actix_web::test;
use http::header::ACCEPT_ENCODING;
use meilisearch_http::{analytics, create_app};
use serde_json::{json, Value};
#[actix_rt::test]
@ -36,6 +40,44 @@ async fn create_index_with_gzip_encoded_request() {
assert_eq!(response["details"]["primaryKey"], Value::Null);
}
#[actix_rt::test]
async fn create_index_with_gzip_encoded_request_and_receiving_brotli_encoded_response() {
let server = Server::new().await;
let app = test::init_service(create_app!(
&server.service.meilisearch,
&server.service.auth,
true,
server.service.options,
analytics::MockAnalytics::new(&server.service.options).0
))
.await;
let body = serde_json::to_string(&json!({
"uid": "test",
"primaryKey": None::<&str>,
}))
.unwrap();
let req = test::TestRequest::post()
.uri("/indexes")
.insert_header(Encoder::Gzip.header().unwrap())
.insert_header((ACCEPT_ENCODING, "br"))
.insert_header(ContentType::json())
.set_payload(Encoder::Gzip.encode(body))
.to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), 202);
let bytes = test::read_body(res).await;
let decoded = Encoder::Brotli.decode(bytes);
let parsed_response =
serde_json::from_slice::<Value>(&decoded.into().as_ref()).expect("Expecting valid json");
assert_eq!(parsed_response["taskUid"], 0);
assert_eq!(parsed_response["indexUid"], "test");
}
#[actix_rt::test]
async fn create_index_with_zlib_encoded_request() {
let server = Server::new().await;