From 5f927121363baa536ccae6c584052dedd67ddb2c Mon Sep 17 00:00:00 2001 From: xxxuuu Date: Sat, 23 Mar 2024 20:53:00 +0800 Subject: [PATCH] fix: set http response chartset to utf-8 when using table format --- 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 e601213c08bb..a7fac46e89a7 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 afcd44c26296..0f0c8966ca5d 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!(), } }