Skip to content

Commit

Permalink
added tests for gzip server compression
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed Nov 27, 2024
1 parent dda1480 commit bfb94a0
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
177 changes: 177 additions & 0 deletions pkg/testsuite/golden/testing_gzip_compressed_content.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// 🌟 This was machine generated. Do not edit. 🌟
//
// Frame[0] {
// "typeVersion": [
// 0,
// 0
// ],
// "custom": {
// "query": {
// "refId": "",
// "type": "csv",
// "format": "",
// "source": "url",
// "url": "http://127.0.0.1:8080",
// "url_options": {
// "method": "",
// "params": null,
// "headers": null,
// "data": "",
// "body_type": "",
// "body_content_type": "",
// "body_form": null,
// "body_graphql_query": "",
// "body_graphql_variables": ""
// },
// "data": "",
// "parser": "backend",
// "filterExpression": "",
// "summarizeExpression": "",
// "summarizeBy": "",
// "summarizeAlias": "",
// "uql": "",
// "groq": "",
// "csv_options": {
// "delimiter": "",
// "skip_empty_lines": false,
// "skip_lines_with_error": false,
// "relax_column_count": false,
// "columns": "",
// "comment": ""
// },
// "json_options": {
// "root_is_not_array": false,
// "columnar": false
// },
// "root_selector": "",
// "columns": [],
// "computed_columns": [],
// "filters": null,
// "seriesCount": 0,
// "expression": "",
// "alias": "",
// "dataOverrides": null,
// "global_query_id": "",
// "query_mode": ""
// },
// "data": "name,age\nfoo,123\nbar,456",
// "responseCodeFromServer": 200,
// "duration": 123,
// "error": ""
// },
// "executedQueryString": "###############\n## URL\n###############\n\nhttp://127.0.0.1:8080\n\n###############\n## Curl Command\n###############\n\ncurl -X 'GET' -H 'Accept: text/csv; charset=utf-8' -H 'Accept-Encoding: gzip' 'http://127.0.0.1:8080'"
// }
// Name: response
// Dimensions: 2 Fields by 2 Rows
// +-----------------+-----------------+
// | Name: age | Name: name |
// | Labels: | Labels: |
// | Type: []*string | Type: []*string |
// +-----------------+-----------------+
// | 123 | foo |
// | 456 | bar |
// +-----------------+-----------------+
//
//
// 🌟 This was machine generated. Do not edit. 🌟
{
"status": 200,
"frames": [
{
"schema": {
"name": "response",
"meta": {
"typeVersion": [
0,
0
],
"custom": {
"query": {
"refId": "",
"type": "csv",
"format": "",
"source": "url",
"url": "http://127.0.0.1:8080",
"url_options": {
"method": "",
"params": null,
"headers": null,
"data": "",
"body_type": "",
"body_content_type": "",
"body_form": null,
"body_graphql_query": "",
"body_graphql_variables": ""
},
"data": "",
"parser": "backend",
"filterExpression": "",
"summarizeExpression": "",
"summarizeBy": "",
"summarizeAlias": "",
"uql": "",
"groq": "",
"csv_options": {
"delimiter": "",
"skip_empty_lines": false,
"skip_lines_with_error": false,
"relax_column_count": false,
"columns": "",
"comment": ""
},
"json_options": {
"root_is_not_array": false,
"columnar": false
},
"root_selector": "",
"columns": [],
"computed_columns": [],
"filters": null,
"seriesCount": 0,
"expression": "",
"alias": "",
"dataOverrides": null,
"global_query_id": "",
"query_mode": ""
},
"data": "name,age\nfoo,123\nbar,456",
"responseCodeFromServer": 200,
"duration": 123,
"error": ""
},
"executedQueryString": "###############\n## URL\n###############\n\nhttp://127.0.0.1:8080\n\n###############\n## Curl Command\n###############\n\ncurl -X 'GET' -H 'Accept: text/csv; charset=utf-8' -H 'Accept-Encoding: gzip' 'http://127.0.0.1:8080'"
},
"fields": [
{
"name": "age",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
}
},
{
"name": "name",
"type": "string",
"typeInfo": {
"frame": "string",
"nullable": true
}
}
]
},
"data": {
"values": [
[
"123",
"456"
],
[
"foo",
"bar"
]
]
}
}
]
}
24 changes: 24 additions & 0 deletions pkg/testsuite/handler_querydata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1403,3 +1403,27 @@ func TestQuery(t *testing.T) {
})
})
}

func TestResponseEncoding(t *testing.T) {
t.Run("testing gzip compressed content", func(t *testing.T) {
server := getServerWithGZipCompressedResponse(t, strings.Join([]string{`name,age`, `foo,123`, `bar,456`}, "\n"))
server.Start()
defer server.Close()
ds := getds(t, backend.DataSourceInstanceSettings{
JSONData: []byte(`{"is_mock": true}`),
DecryptedSecureJSONData: map[string]string{},
})
res, err := ds.QueryData(context.Background(), &backend.QueryDataRequest{
Queries: []backend.DataQuery{{RefID: "A", JSON: []byte(fmt.Sprintf(`{
"type" : "csv",
"source" : "url",
"parser" : "backend",
"url" : "%s"
}`, server.URL))}},
})
require.Nil(t, err)
require.NotNil(t, res)
resItem := res.Responses["A"]
experimental.CheckGoldenJSONResponse(t, "golden", strings.ReplaceAll(t.Name(), "TestResponseEncoding/", ""), &resItem, UPDATE_GOLDEN_DATA)
})
}
17 changes: 17 additions & 0 deletions pkg/testsuite/testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testsuite_test

import (
"bytes"
"compress/gzip"
"context"
"crypto/tls"
"crypto/x509"
Expand Down Expand Up @@ -79,6 +80,22 @@ func getServerWithStaticResponse(t *testing.T, content string, isFile bool) *htt
return server
}

func getServerWithGZipCompressedResponse(t *testing.T, content string) *httptest.Server {
t.Helper()
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Encoding", "gzip")
gw := gzip.NewWriter(w)
defer gw.Close()
_, err := gw.Write([]byte(content))
require.Nil(t, err)
}))
listener, err := net.Listen("tcp", "127.0.0.1:8080")
require.Nil(t, err)
server.Listener.Close()
server.Listener = listener
return server
}

func getServerCertificate(serverName string) *tls.Config {
caPool := x509.NewCertPool()
if ok := caPool.AppendCertsFromPEM([]byte(mockPEMClientCACet)); !ok {
Expand Down

0 comments on commit bfb94a0

Please sign in to comment.