From 2ad0b24efad7e2616d2fe087904e4668943b352d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?x=C2=B3u=C2=B3?= Date: Mon, 25 Mar 2024 11:13:01 +0800 Subject: [PATCH] fix: set http response chartset to utf-8 when using table format (#3571) --- src/servers/src/http/table_result.rs | 2 +- src/servers/tests/http/http_handler_test.rs | 40 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/servers/src/http/table_result.rs b/src/servers/src/http/table_result.rs index e601213c08..a7fac46e89 100644 --- a/src/servers/src/http/table_result.rs +++ b/src/servers/src/http/table_result.rs @@ -135,7 +135,7 @@ impl IntoResponse for TableResponse { let mut resp = ( [( header::CONTENT_TYPE, - HeaderValue::from_static(mime::PLAIN.as_ref()), + HeaderValue::from_static(mime::TEXT_PLAIN_UTF_8.as_ref()), )], self.to_string(), ) diff --git a/src/servers/tests/http/http_handler_test.rs b/src/servers/tests/http/http_handler_test.rs index afcd44c262..0f0c8966ca 100644 --- a/src/servers/tests/http/http_handler_test.rs +++ b/src/servers/tests/http/http_handler_test.rs @@ -46,7 +46,7 @@ async fn test_sql_not_provided() { script_handler: None, }; - for format in ["greptimedb_v1", "influxdb_v1", "csv"] { + for format in ["greptimedb_v1", "influxdb_v1", "csv", "table"] { let query = http_handler::SqlQuery { db: None, sql: None, @@ -82,7 +82,7 @@ async fn test_sql_output_rows() { script_handler: None, }; - for format in ["greptimedb_v1", "influxdb_v1", "csv"] { + for format in ["greptimedb_v1", "influxdb_v1", "csv", "table"] { let query = create_query(format); let json = http_handler::sql( State(api_state.clone()), @@ -154,6 +154,23 @@ async fn test_sql_output_rows() { hyper::body::Bytes::from_static(b"4950\n"), ); } + HttpResponse::Table(resp) => { + use http_body::Body as HttpBody; + let mut resp = resp.into_response(); + assert_eq!( + resp.headers().get(header::CONTENT_TYPE), + Some(HeaderValue::from_static(mime::TEXT_PLAIN_UTF_8.as_ref())).as_ref(), + ); + assert_eq!( + resp.body_mut().data().await.unwrap().unwrap(), + hyper::body::Bytes::from( + r#"┌─SUM(numbers.uint32s)─┐ +│ 4950 │ +└──────────────────────┘ +"# + ), + ); + } _ => unreachable!(), } } @@ -172,7 +189,7 @@ async fn test_sql_form() { script_handler: None, }; - for format in ["greptimedb_v1", "influxdb_v1", "csv"] { + for format in ["greptimedb_v1", "influxdb_v1", "csv", "table"] { let form = create_form(format); let json = http_handler::sql( State(api_state.clone()), @@ -244,6 +261,23 @@ async fn test_sql_form() { hyper::body::Bytes::from_static(b"4950\n"), ); } + HttpResponse::Table(resp) => { + use http_body::Body as HttpBody; + let mut resp = resp.into_response(); + assert_eq!( + resp.headers().get(header::CONTENT_TYPE), + Some(HeaderValue::from_static(mime::TEXT_PLAIN_UTF_8.as_ref())).as_ref(), + ); + assert_eq!( + resp.body_mut().data().await.unwrap().unwrap(), + hyper::body::Bytes::from( + r#"┌─SUM(numbers.uint32s)─┐ +│ 4950 │ +└──────────────────────┘ +"# + ), + ); + } _ => unreachable!(), } }