github.com/cloudwego/hertz@v0.9.3/pkg/common/utils/chunk_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  
    17  package utils
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    23  	"github.com/cloudwego/hertz/pkg/common/test/mock"
    24  )
    25  
    26  func TestChunkParseChunkSizeGetCorrect(t *testing.T) {
    27  	// iterate the hexMap, and judge the difference between dec and ParseChunkSize
    28  	hexMap := map[int]string{0: "0", 10: "a", 100: "64", 1000: "3e8"}
    29  	for dec, hex := range hexMap {
    30  		chunkSizeBody := hex + "\r\n"
    31  		zr := mock.NewZeroCopyReader(chunkSizeBody)
    32  		chunkSize, err := ParseChunkSize(zr)
    33  		assert.DeepEqual(t, nil, err)
    34  		assert.DeepEqual(t, chunkSize, dec)
    35  	}
    36  }
    37  
    38  func TestChunkParseChunkSizeGetError(t *testing.T) {
    39  	// test err from -----n, err := bytesconv.ReadHexInt(r)-----
    40  	chunkSizeBody := ""
    41  	zr := mock.NewZeroCopyReader(chunkSizeBody)
    42  	chunkSize, err := ParseChunkSize(zr)
    43  	assert.NotNil(t, err)
    44  	assert.DeepEqual(t, -1, chunkSize)
    45  	// test err from -----c, err := r.ReadByte()-----
    46  	chunkSizeBody = "0"
    47  	zr = mock.NewZeroCopyReader(chunkSizeBody)
    48  	chunkSize, err = ParseChunkSize(zr)
    49  	assert.NotNil(t, err)
    50  	assert.DeepEqual(t, -1, chunkSize)
    51  	// test err from -----c, err := r.ReadByte()-----
    52  	chunkSizeBody = "0" + "\r"
    53  	zr = mock.NewZeroCopyReader(chunkSizeBody)
    54  	chunkSize, err = ParseChunkSize(zr)
    55  	assert.NotNil(t, err)
    56  	assert.DeepEqual(t, -1, chunkSize)
    57  	// test err from -----c, err := r.ReadByte()-----
    58  	chunkSizeBody = "0" + "\r" + "\r"
    59  	zr = mock.NewZeroCopyReader(chunkSizeBody)
    60  	chunkSize, err = ParseChunkSize(zr)
    61  	assert.NotNil(t, err)
    62  	assert.DeepEqual(t, -1, chunkSize)
    63  }
    64  
    65  func TestChunkParseChunkSizeCorrectWhiteSpace(t *testing.T) {
    66  	// test the whitespace
    67  	whiteSpace := ""
    68  	for i := 0; i < 10; i++ {
    69  		whiteSpace += " "
    70  		chunkSizeBody := "0" + whiteSpace + "\r\n"
    71  		zr := mock.NewZeroCopyReader(chunkSizeBody)
    72  		chunkSize, err := ParseChunkSize(zr)
    73  		assert.DeepEqual(t, nil, err)
    74  		assert.DeepEqual(t, 0, chunkSize)
    75  	}
    76  }
    77  
    78  func TestChunkParseChunkSizeNonCRLF(t *testing.T) {
    79  	// test non-"\r\n"
    80  	chunkSizeBody := "0" + "\n\r"
    81  	zr := mock.NewZeroCopyReader(chunkSizeBody)
    82  	chunkSize, err := ParseChunkSize(zr)
    83  	assert.DeepEqual(t, true, err != nil)
    84  	assert.DeepEqual(t, -1, chunkSize)
    85  }
    86  
    87  func TestChunkReadTrueCRLF(t *testing.T) {
    88  	CRLF := "\r\n"
    89  	zr := mock.NewZeroCopyReader(CRLF)
    90  	err := SkipCRLF(zr)
    91  	assert.DeepEqual(t, nil, err)
    92  }
    93  
    94  func TestChunkReadFalseCRLF(t *testing.T) {
    95  	CRLF := "\n\r"
    96  	zr := mock.NewZeroCopyReader(CRLF)
    97  	err := SkipCRLF(zr)
    98  	assert.DeepEqual(t, errBrokenChunk, err)
    99  }