From bd452404bf4f00c537359cfb970b5fee41accd4f Mon Sep 17 00:00:00 2001 From: ukolovda Date: Tue, 2 Mar 2021 10:56:47 +0400 Subject: [PATCH] Distributor: append several tests for http parser. --- pkg/distributor/http_test.go | 87 ++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 pkg/distributor/http_test.go diff --git a/pkg/distributor/http_test.go b/pkg/distributor/http_test.go new file mode 100644 index 0000000000000..a17ebffcb7488 --- /dev/null +++ b/pkg/distributor/http_test.go @@ -0,0 +1,87 @@ +package distributor + +import ( + "bytes" + "compress/gzip" + "log" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +// GZip source string and return compressed string +func gzipString(source string) string { + var buf bytes.Buffer + zw := gzip.NewWriter(&buf) + if _, err := zw.Write([]byte(source)); err != nil { + log.Fatal(err) + } + if err := zw.Close(); err != nil { + log.Fatal(err) + } + return buf.String() +} + +func TestParseRequest(t *testing.T) { + var tests = []struct { + path string + body string + contentType string + contentEncoding string + valid bool + }{ + { + path: `/loki/api/v1/push`, + body: ``, + contentType: `application/json`, + valid: false}, + { + path: `/loki/api/v1/push`, + body: `{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`, + contentType: ``, + valid: false}, + { + path: `/loki/api/v1/push`, + body: `{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`, + contentType: `application/json`, + valid: true}, + { + path: `/loki/api/v1/push`, + body: `{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`, + contentType: `application/json`, + contentEncoding: ``, + valid: true}, + { + path: `/loki/api/v1/push`, + body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), + contentType: `application/json`, + contentEncoding: `gzip`, + valid: true}, + { + path: `/loki/api/v1/push`, + body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), + contentType: `application/json`, + contentEncoding: `snappy`, + valid: false}} + + // Testing input array + for index, test := range tests { + request := httptest.NewRequest("POST", test.path, strings.NewReader(test.body)) + if len(test.contentType) > 0 { + request.Header.Add("Content-Type", test.contentType) + } + if len(test.contentEncoding) > 0 { + request.Header.Add("Content-Encoding", test.contentEncoding) + } + data, err := ParseRequest(request) + if test.valid { + assert.Nil(t, err, "Should not give error for %d", index) + assert.NotNil(t, data, "Should give data for %d", index) + } else { + assert.NotNil(t, err, "Should give error for %d", index) + assert.Nil(t, data, "Should not give data for %d", index) + } + } +}