blackhole_test.go 990 B

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