trpc.group/trpc-go/trpc-go@v1.0.3/server/attachment_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package server_test
    15  
    16  import (
    17  	"bytes"
    18  	"context"
    19  	"io"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	trpc "trpc.group/trpc-go/trpc-go"
    25  	"trpc.group/trpc-go/trpc-go/internal/attachment"
    26  	"trpc.group/trpc-go/trpc-go/server"
    27  )
    28  
    29  func TestAttachment(t *testing.T) {
    30  	msg := trpc.Message(context.Background())
    31  	attm := server.GetAttachment(msg)
    32  	require.Equal(t, attachment.NoopAttachment{}, attm.Request())
    33  
    34  	attm.SetResponse(bytes.NewReader([]byte("attachment")))
    35  	responseAttm, ok := attachment.ServerResponseAttachment(msg)
    36  	require.True(t, ok)
    37  	bts, err := io.ReadAll(responseAttm)
    38  	require.Nil(t, err)
    39  	require.Equal(t, []byte("attachment"), bts)
    40  }