|
6 | 6 | package http |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "bytes" |
| 10 | + "compress/gzip" |
| 11 | + "crypto/md5" |
| 12 | + "encoding/hex" |
| 13 | + "fmt" |
9 | 14 | "github.com/GuanceCloud/cliutils/testutil" |
10 | 15 | "io" |
11 | 16 | "net/http" |
| 17 | + "strings" |
12 | 18 | "testing" |
13 | 19 | "time" |
14 | 20 |
|
15 | 21 | "github.com/gin-gonic/gin" |
16 | 22 | ) |
17 | 23 |
|
| 24 | +const testText = `观测云提供的系统全链路可观测解决方案, |
| 25 | +可实现从底层基础设施到通用技术组件, |
| 26 | +再到业务应用系统的全链路可观测, |
| 27 | +将不可预知性变为确定已知性。 |
| 28 | +观测云提供快速实现系统可观测的解决方案,满足云、云原生、应用和业务上的监测需求。 |
| 29 | +通过自定义监测方案,实现实时可交互仪表板、高效观测基础设施、全链路应用性能可观测等功能,保障系统稳定性 |
| 30 | +观测云、全链路可观测、实时监测、自定义监测、云原生 |
| 31 | +观测云提供的系统全链路可观测解决方案, |
| 32 | +可实现从底层基础设施到通用技术组件, |
| 33 | +再到业务应用系统的全链路可观测, |
| 34 | +将不可预知性变为确定已知性。 |
| 35 | +观测云提供快速实现系统可观测的解决方案,满足云、云原生、应用和业务上的监测需求。 |
| 36 | +通过自定义监测方案,实现实时可交互仪表板、高效观测基础设施、全链路应用性能可观测等功能,保障系统稳定性 |
| 37 | +观测云、全链路可观测、实时监测、自定义监测、云原生 |
| 38 | +观测云提供的系统全链路可观测解决方案, |
| 39 | +可实现从底层基础设施到通用技术组件, |
| 40 | +再到业务应用系统的全链路可观测, |
| 41 | +将不可预知性变为确定已知性。 |
| 42 | +观测云提供快速实现系统可观测的解决方案,满足云、云原生、应用和业务上的监测需求。 |
| 43 | +通过自定义监测方案,实现实时可交互仪表板、高效观测基础设施、全链路应用性能可观测等功能,保障系统稳定性 |
| 44 | +观测云、全链路可观测、实时监测、自定义监测、云原生 |
| 45 | +` |
| 46 | + |
18 | 47 | func BenchmarkAllMiddlewares(b *testing.B) { |
19 | 48 | cases := []struct { |
20 | 49 | name string |
@@ -200,3 +229,137 @@ func TestMiddlewares(t *testing.T) { |
200 | 229 | resp.Body.Close() |
201 | 230 | } |
202 | 231 | } |
| 232 | + |
| 233 | +func TestNewHashReader(t *testing.T) { |
| 234 | + src := []byte(testText) |
| 235 | + |
| 236 | + r := NewReaderWithHash(bytes.NewReader(src), md5.New()) |
| 237 | + |
| 238 | + all, err := io.ReadAll(r) |
| 239 | + testutil.Ok(t, err) |
| 240 | + testutil.Equals(t, src, all) |
| 241 | + |
| 242 | + md5Sum := md5.Sum(src) |
| 243 | + s := r.Sum() |
| 244 | + |
| 245 | + testutil.Equals(t, md5Sum[:], s) |
| 246 | + |
| 247 | + fmt.Println(r.SumHex()) |
| 248 | + fmt.Println(hex.EncodeToString(md5Sum[:])) |
| 249 | + testutil.Equals(t, hex.EncodeToString(md5Sum[:]), r.SumHex()) |
| 250 | +} |
| 251 | + |
| 252 | +type testCase struct { |
| 253 | + name string |
| 254 | + body []byte |
| 255 | + contentEncoding string |
| 256 | +} |
| 257 | + |
| 258 | +func TestGzipReadWithMD5(t *testing.T) { |
| 259 | + |
| 260 | + gzipOut := &bytes.Buffer{} |
| 261 | + gw := gzip.NewWriter(gzipOut) |
| 262 | + _, err := gw.Write([]byte(testText)) |
| 263 | + testutil.Ok(t, err) |
| 264 | + err = gw.Close() |
| 265 | + testutil.Ok(t, err) |
| 266 | + |
| 267 | + testCases := []testCase{ |
| 268 | + { |
| 269 | + name: "plain body", |
| 270 | + body: []byte(testText), |
| 271 | + contentEncoding: "", |
| 272 | + }, |
| 273 | + { |
| 274 | + name: "gzip body", |
| 275 | + body: gzipOut.Bytes(), |
| 276 | + contentEncoding: "gzip", |
| 277 | + }, |
| 278 | + } |
| 279 | + |
| 280 | + for _, tc := range testCases { |
| 281 | + t.Run(tc.name, func(t *testing.T) { |
| 282 | + |
| 283 | + req1, err := http.NewRequest(http.MethodPost, "/", bytes.NewReader(tc.body)) |
| 284 | + testutil.Ok(t, err) |
| 285 | + if tc.contentEncoding != "" { |
| 286 | + req1.Header.Set("Content-Encoding", tc.contentEncoding) |
| 287 | + } |
| 288 | + |
| 289 | + req2, err := http.NewRequest(http.MethodPost, "/", bytes.NewReader(tc.body)) |
| 290 | + testutil.Ok(t, err) |
| 291 | + if tc.contentEncoding != "" { |
| 292 | + req2.Header.Set("Content-Encoding", tc.contentEncoding) |
| 293 | + } |
| 294 | + |
| 295 | + ginCtx := &gin.Context{Request: req1} |
| 296 | + body1, sum1, err := GinReadWithMD5(ginCtx) |
| 297 | + testutil.Ok(t, err) |
| 298 | + |
| 299 | + body2, sum2, err := GzipReadWithMD5(req2) |
| 300 | + testutil.Ok(t, err) |
| 301 | + |
| 302 | + testutil.Equals(t, body1, body2) |
| 303 | + testutil.Equals(t, sum1, sum2) |
| 304 | + |
| 305 | + }) |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +func BenchmarkGzipReadWithMD5(b *testing.B) { |
| 310 | + |
| 311 | + text := strings.Repeat(testText, 100000) |
| 312 | + |
| 313 | + gzipOut := &bytes.Buffer{} |
| 314 | + gw := gzip.NewWriter(gzipOut) |
| 315 | + _, err := gw.Write([]byte(text)) |
| 316 | + testutil.Ok(b, err) |
| 317 | + err = gw.Close() |
| 318 | + testutil.Ok(b, err) |
| 319 | + |
| 320 | + sr := strings.NewReader(text) |
| 321 | + |
| 322 | + b.Run("GinRead", func(t *testing.B) { |
| 323 | + sr.Reset(text) |
| 324 | + |
| 325 | + req, err := http.NewRequest(http.MethodPost, "/", sr) |
| 326 | + testutil.Ok(t, err) |
| 327 | + |
| 328 | + _, err = GinRead(&gin.Context{Request: req}) |
| 329 | + testutil.Ok(t, err) |
| 330 | + }) |
| 331 | + |
| 332 | + b.Run("GzipRead", func(t *testing.B) { |
| 333 | + |
| 334 | + sr.Reset(text) |
| 335 | + |
| 336 | + req, err := http.NewRequest(http.MethodPost, "/", sr) |
| 337 | + testutil.Ok(t, err) |
| 338 | + |
| 339 | + _, err = GzipRead(req) |
| 340 | + testutil.Ok(t, err) |
| 341 | + }) |
| 342 | + |
| 343 | + b.Run("GinReadWithMD5", func(t *testing.B) { |
| 344 | + |
| 345 | + req, err := http.NewRequest(http.MethodPost, "/", bytes.NewReader(gzipOut.Bytes())) |
| 346 | + testutil.Ok(t, err) |
| 347 | + req.Header.Set("Content-Encoding", "gzip") |
| 348 | + |
| 349 | + body, _, err := GinReadWithMD5(&gin.Context{Request: req}) |
| 350 | + testutil.Ok(t, err) |
| 351 | + testutil.Equals(t, string(body), text) |
| 352 | + }) |
| 353 | + |
| 354 | + b.Run("GzipReadWithMD5", func(t *testing.B) { |
| 355 | + |
| 356 | + req, err := http.NewRequest(http.MethodPost, "/", bytes.NewReader(gzipOut.Bytes())) |
| 357 | + testutil.Ok(t, err) |
| 358 | + req.Header.Set("Content-Encoding", "gzip") |
| 359 | + |
| 360 | + body, _, err := GzipReadWithMD5(req) |
| 361 | + testutil.Ok(t, err) |
| 362 | + testutil.Equals(t, string(body), text) |
| 363 | + }) |
| 364 | + |
| 365 | +} |
0 commit comments