github.com/cloudwego/hertz@v0.9.3/pkg/common/compress/compress_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * The MIT License (MIT) 17 * 18 * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors 19 * 20 * Permission is hereby granted, free of charge, to any person obtaining a copy 21 * of this software and associated documentation files (the "Software"), to deal 22 * in the Software without restriction, including without limitation the rights 23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 24 * copies of the Software, and to permit persons to whom the Software is 25 * furnished to do so, subject to the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be included in 28 * all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 36 * THE SOFTWARE. 37 * 38 * This file may have been modified by CloudWeGo authors. All CloudWeGo 39 * Modifications are Copyright 2022 CloudWeGo Authors. 40 */ 41 42 package compress 43 44 import ( 45 "io" 46 "testing" 47 ) 48 49 func TestCompressNewCompressWriterPoolMap(t *testing.T) { 50 pool := newCompressWriterPoolMap() 51 if len(pool) != 12 { 52 t.Fatalf("Unexpected number for WriterPoolMap: %d. Expecting 12", len(pool)) 53 } 54 } 55 56 func TestCompressAppendGunzipBytes(t *testing.T) { 57 dst1 := []byte("") 58 // src unzip -> "hello". The src must the string that has been gunzipped. 59 src1 := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 7, 0, 0, 0, 255, 255} 60 expectedRes1 := "hello" 61 res1, err1 := AppendGunzipBytes(dst1, src1) 62 // gzip will wrap io.EOF to io.ErrUnexpectedEOF 63 // just ignore in this case 64 if err1 != io.ErrUnexpectedEOF { 65 t.Fatalf("Unexpected error: %s", err1) 66 } 67 if string(res1) != expectedRes1 { 68 t.Fatalf("Unexpected : %s. Expecting : %s", res1, expectedRes1) 69 } 70 71 dst2 := []byte("!!!") 72 src2 := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 7, 0, 0, 0, 255, 255} 73 expectedRes2 := "!!!hello" 74 res2, err2 := AppendGunzipBytes(dst2, src2) 75 if err2 != io.ErrUnexpectedEOF { 76 t.Fatalf("Unexpected error: %s", err2) 77 } 78 if string(res2) != expectedRes2 { 79 t.Fatalf("Unexpected : %s. Expecting : %s", res2, expectedRes2) 80 } 81 82 dst3 := []byte("!!!") 83 src3 := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 255, 255} 84 expectedRes3 := "!!!" 85 res3, err3 := AppendGunzipBytes(dst3, src3) 86 if err3 != io.ErrUnexpectedEOF { 87 t.Fatalf("Unexpected error: %s", err3) 88 } 89 if string(res3) != expectedRes3 { 90 t.Fatalf("Unexpected : %s. Expecting : %s", res3, expectedRes3) 91 } 92 } 93 94 func TestCompressAppendGzipBytesLevel(t *testing.T) { 95 // test the byteSliceWriter case for WriteGzipLevel 96 dst1 := []byte("") 97 src1 := []byte("hello") 98 res1 := AppendGzipBytesLevel(dst1, src1, 5) 99 expectedRes1 := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 7, 4, 0, 0, 255, 255, 134, 166, 16, 54, 5, 0, 0, 0} 100 if string(res1) != string(expectedRes1) { 101 t.Fatalf("Unexpected : %s. Expecting : %s", res1, expectedRes1) 102 } 103 } 104 105 func TestCompressWriteGzipLevel(t *testing.T) { 106 // test default case for WriteGzipLevel 107 var w defaultByteWriter 108 p := []byte("hello") 109 expectedW := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 7, 4, 0, 0, 255, 255, 134, 166, 16, 54, 5, 0, 0, 0} 110 num, err := WriteGzipLevel(&w, p, 5) 111 if string(expectedW) != string(w.b) { 112 t.Fatalf("Unexpected : %s. Expecting: %s.", w.b, expectedW) 113 } 114 if num != len(p) { 115 t.Fatalf("Unexpected number of compressed bytes: %d", num) 116 } 117 if err != nil { 118 t.Fatalf("Unexpected error: %s", err) 119 } 120 } 121 122 type defaultByteWriter struct { 123 b []byte 124 } 125 126 func (w *defaultByteWriter) Write(p []byte) (int, error) { 127 w.b = append(w.b, p...) 128 return len(p), nil 129 }