github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/http2/client_test.go (about)

     1  package http2
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/Asutorufa/yuhaiin/pkg/net/dialer"
    10  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    11  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/simple"
    12  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/protocol"
    13  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    14  )
    15  
    16  func TestClient(t *testing.T) {
    17  	lis, err := dialer.ListenContext(context.TODO(), "tcp", "127.0.0.1:8082")
    18  	assert.NoError(t, err)
    19  	defer lis.Close()
    20  
    21  	lis = newServer(lis)
    22  
    23  	go func() {
    24  		for {
    25  			conn, err := lis.Accept()
    26  			if err != nil {
    27  				t.Error(err)
    28  				break
    29  			}
    30  
    31  			go func() {
    32  				defer conn.Close()
    33  
    34  				_, _ = io.Copy(io.MultiWriter(os.Stdout, conn), conn)
    35  			}()
    36  		}
    37  	}()
    38  
    39  	sm := simple.NewClient(&protocol.Protocol_Simple{
    40  		Simple: &protocol.Simple{
    41  			Host: "127.0.0.1",
    42  			Port: 8082,
    43  		},
    44  	})
    45  
    46  	c := NewClient(&protocol.Protocol_Http2{
    47  		Http2: &protocol.Http2{
    48  			Concurrency: 1,
    49  		},
    50  	})
    51  
    52  	p, err := sm(nil)
    53  	if err != nil {
    54  		t.Error(err)
    55  		t.FailNow()
    56  	}
    57  
    58  	p, err = c(p)
    59  	if err != nil {
    60  		t.Error(err)
    61  		t.FailNow()
    62  	}
    63  
    64  	conn, err := p.Conn(context.TODO(), netapi.EmptyAddr)
    65  	if err != nil {
    66  		t.Error(err)
    67  		t.FailNow()
    68  	}
    69  
    70  	t.Log("start write bbbb")
    71  	_, err = conn.Write([]byte("bbbb"))
    72  	if err != nil {
    73  		t.Error(err)
    74  		return
    75  	}
    76  
    77  	buf := make([]byte, 1024)
    78  	n, err := conn.Read(buf)
    79  	if err != nil {
    80  		t.Error(err)
    81  		return
    82  	}
    83  
    84  	t.Log(string(buf[:n]))
    85  
    86  	_, err = conn.Write([]byte("ccc"))
    87  	if err != nil {
    88  		t.Error(err)
    89  	}
    90  
    91  	n, err = conn.Read(buf)
    92  	if err != nil {
    93  		t.Error(err)
    94  		return
    95  	}
    96  
    97  	t.Log(string(buf[:n]))
    98  }
    99  
   100  func TestXxx(t *testing.T) {
   101  	for i := 0; i < 100; i++ {
   102  		t.Log(i % 2)
   103  	}
   104  }