github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/loghttp/push/push_test.go (about) 1 package push 2 3 import ( 4 "bytes" 5 "compress/flate" 6 "compress/gzip" 7 "log" 8 "net/http/httptest" 9 "strings" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 14 util_log "github.com/grafana/loki/pkg/util/log" 15 ) 16 17 // GZip source string and return compressed string 18 func gzipString(source string) string { 19 var buf bytes.Buffer 20 zw := gzip.NewWriter(&buf) 21 if _, err := zw.Write([]byte(source)); err != nil { 22 log.Fatal(err) 23 } 24 if err := zw.Close(); err != nil { 25 log.Fatal(err) 26 } 27 return buf.String() 28 } 29 30 // Deflate source string and return compressed string 31 func deflateString(source string) string { 32 var buf bytes.Buffer 33 zw, _ := flate.NewWriter(&buf, 6) 34 if _, err := zw.Write([]byte(source)); err != nil { 35 log.Fatal(err) 36 } 37 if err := zw.Close(); err != nil { 38 log.Fatal(err) 39 } 40 return buf.String() 41 } 42 43 func TestParseRequest(t *testing.T) { 44 tests := []struct { 45 path string 46 body string 47 contentType string 48 contentEncoding string 49 valid bool 50 }{ 51 { 52 path: `/loki/api/v1/push`, 53 body: ``, 54 contentType: `application/json`, 55 valid: false, 56 }, 57 { 58 path: `/loki/api/v1/push`, 59 body: `{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`, 60 contentType: ``, 61 valid: false, 62 }, 63 { 64 path: `/loki/api/v1/push`, 65 body: `{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`, 66 contentType: `application/json`, 67 valid: true, 68 }, 69 { 70 path: `/loki/api/v1/push`, 71 body: `{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`, 72 contentType: `application/json`, 73 contentEncoding: ``, 74 valid: true, 75 }, 76 { 77 path: `/loki/api/v1/push`, 78 body: `{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", {"fizz": "buzz"} ] ] }]}`, 79 contentType: `application/json`, 80 contentEncoding: ``, 81 valid: false, 82 }, 83 { 84 path: `/loki/api/v1/push`, 85 body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 86 contentType: `application/json`, 87 contentEncoding: `gzip`, 88 valid: true, 89 }, 90 { 91 path: `/loki/api/v1/push`, 92 body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 93 contentType: `application/json`, 94 contentEncoding: `deflate`, 95 valid: true, 96 }, 97 { 98 path: `/loki/api/v1/push`, 99 body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 100 contentType: `application/json`, 101 contentEncoding: `snappy`, 102 valid: false, 103 }, 104 { 105 path: `/loki/api/v1/push`, 106 body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 107 contentType: `application/json; charset=utf-8`, 108 contentEncoding: `gzip`, 109 valid: true, 110 }, 111 { 112 path: `/loki/api/v1/push`, 113 body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 114 contentType: `application/json; charset=utf-8`, 115 contentEncoding: `deflate`, 116 valid: true, 117 }, 118 { 119 path: `/loki/api/v1/push`, 120 body: gzipString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 121 contentType: `application/jsonn; charset=utf-8`, 122 contentEncoding: `gzip`, 123 valid: false, 124 }, 125 { 126 path: `/loki/api/v1/push`, 127 body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 128 contentType: `application/jsonn; charset=utf-8`, 129 contentEncoding: `deflate`, 130 valid: false, 131 }, 132 { 133 path: `/loki/api/v1/push`, 134 body: gzipString(`{"streams": [{ "stream": { "foo4": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 135 contentType: `application/json; charsetutf-8`, 136 contentEncoding: `gzip`, 137 valid: false, 138 }, 139 { 140 path: `/loki/api/v1/push`, 141 body: deflateString(`{"streams": [{ "stream": { "foo4": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 142 contentType: `application/json; charsetutf-8`, 143 contentEncoding: `deflate`, 144 valid: false, 145 }, 146 { 147 path: `/loki/api/v1/push`, 148 body: deflateString(`{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 149 contentType: `application/jsonn; charset=utf-8`, 150 contentEncoding: `deflate`, 151 valid: false, 152 }, 153 { 154 path: `/loki/api/v1/push`, 155 body: deflateString(`{"streams": [{ "stream": { "foo4": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`), 156 contentType: `application/json; charsetutf-8`, 157 contentEncoding: `deflate`, 158 valid: false, 159 }, 160 } 161 162 // Testing input array 163 for index, test := range tests { 164 request := httptest.NewRequest("POST", test.path, strings.NewReader(test.body)) 165 if len(test.contentType) > 0 { 166 request.Header.Add("Content-Type", test.contentType) 167 } 168 if len(test.contentEncoding) > 0 { 169 request.Header.Add("Content-Encoding", test.contentEncoding) 170 } 171 data, err := ParseRequest(util_log.Logger, "", request, nil) 172 if test.valid { 173 assert.Nil(t, err, "Should not give error for %d", index) 174 assert.NotNil(t, data, "Should give data for %d", index) 175 } else { 176 assert.NotNil(t, err, "Should give error for %d", index) 177 assert.Nil(t, data, "Should not give data for %d", index) 178 } 179 } 180 }