github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/proxy/blackhole/blackhole_test.go (about)

     1  package blackhole_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/xtls/xray-core/common"
     8  	"github.com/xtls/xray-core/common/buf"
     9  	"github.com/xtls/xray-core/common/serial"
    10  	"github.com/xtls/xray-core/common/session"
    11  	"github.com/xtls/xray-core/proxy/blackhole"
    12  	"github.com/xtls/xray-core/transport"
    13  	"github.com/xtls/xray-core/transport/pipe"
    14  )
    15  
    16  func TestBlackholeHTTPResponse(t *testing.T) {
    17  	ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{}})
    18  	handler, err := blackhole.New(ctx, &blackhole.Config{
    19  		Response: serial.ToTypedMessage(&blackhole.HTTPResponse{}),
    20  	})
    21  	common.Must(err)
    22  
    23  	reader, writer := pipe.New(pipe.WithoutSizeLimit())
    24  
    25  	var mb buf.MultiBuffer
    26  	var rerr error
    27  	go func() {
    28  		b, e := reader.ReadMultiBuffer()
    29  		mb = b
    30  		rerr = e
    31  	}()
    32  
    33  	link := transport.Link{
    34  		Reader: reader,
    35  		Writer: writer,
    36  	}
    37  	common.Must(handler.Process(ctx, &link, nil))
    38  	common.Must(rerr)
    39  	if mb.IsEmpty() {
    40  		t.Error("expect http response, but nothing")
    41  	}
    42  }