github.com/cloudwego/hertz@v0.9.3/pkg/common/test/mock/body_data.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 mock
    18  
    19  import "fmt"
    20  
    21  func CreateFixedBody(bodySize int) []byte {
    22  	var b []byte
    23  	for i := 0; i < bodySize; i++ {
    24  		b = append(b, byte(i%10)+'0')
    25  	}
    26  	return b
    27  }
    28  
    29  func CreateChunkedBody(body []byte, trailer map[string]string, hasTrailer bool) []byte {
    30  	var b []byte
    31  	chunkSize := 1
    32  	for len(body) > 0 {
    33  		if chunkSize > len(body) {
    34  			chunkSize = len(body)
    35  		}
    36  		b = append(b, []byte(fmt.Sprintf("%x\r\n", chunkSize))...)
    37  		b = append(b, body[:chunkSize]...)
    38  		b = append(b, []byte("\r\n")...)
    39  		body = body[chunkSize:]
    40  		chunkSize++
    41  	}
    42  	if hasTrailer {
    43  		b = append(b, "0\r\n"...)
    44  		for k, v := range trailer {
    45  			b = append(b, k...)
    46  			b = append(b, ": "...)
    47  			b = append(b, v...)
    48  			b = append(b, "\r\n"...)
    49  		}
    50  		b = append(b, "\r\n"...)
    51  	}
    52  	return b
    53  }