code.gitea.io/gitea@v1.21.7/cmd/hook_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"bufio"
     8  	"bytes"
     9  	"context"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestPktLine(t *testing.T) {
    17  	// test read
    18  	ctx := context.Background()
    19  	s := strings.NewReader("0000")
    20  	r := bufio.NewReader(s)
    21  	result, err := readPktLine(ctx, r, pktLineTypeFlush)
    22  	assert.NoError(t, err)
    23  	assert.Equal(t, pktLineTypeFlush, result.Type)
    24  
    25  	s = strings.NewReader("0006a\n")
    26  	r = bufio.NewReader(s)
    27  	result, err = readPktLine(ctx, r, pktLineTypeData)
    28  	assert.NoError(t, err)
    29  	assert.Equal(t, pktLineTypeData, result.Type)
    30  	assert.Equal(t, []byte("a\n"), result.Data)
    31  
    32  	// test write
    33  	w := bytes.NewBuffer([]byte{})
    34  	err = writeFlushPktLine(ctx, w)
    35  	assert.NoError(t, err)
    36  	assert.Equal(t, []byte("0000"), w.Bytes())
    37  
    38  	w.Reset()
    39  	err = writeDataPktLine(ctx, w, []byte("a\nb"))
    40  	assert.NoError(t, err)
    41  	assert.Equal(t, []byte("0007a\nb"), w.Bytes())
    42  }