Files
mc-gateway/cmd/gateway/handle_request_test.go
tursom 935fe4b808
Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
test: 补全单元测试
2026-06-23 13:06:31 +08:00

98 lines
2.0 KiB
Go

package main
import (
"bytes"
"errors"
"net"
"testing"
"time"
)
func TestHandleRequestProxiesAndClosesConnections(t *testing.T) {
defer saveGatewayState(t)()
packet := gatewayTestPacket("play.example")
source := newGatewayTestConn(packet)
upstream := newGatewayTestConn([]byte("reply"))
config.Hosts = map[string]string{
"play.example": "backend.example:25565",
}
registerGatewayUpstreamHook(
t,
func(net.Conn, string) bool { return true },
func(net.Conn, string) (net.Conn, error) {
return upstream, nil
},
)
handleRequest(source)
if !source.closed {
t.Fatal("source connection was not closed")
}
if !upstream.closed {
t.Fatal("upstream connection was not closed")
}
if !bytes.Equal(upstream.writeBuf.Bytes(), packet) {
t.Fatalf("upstream initial packet = %v, want %v", upstream.writeBuf.Bytes(), packet)
}
if got := source.writeBuf.String(); got != "reply" {
t.Fatalf("proxied reply = %q, want reply", got)
}
}
func TestHandleRequestRecoversAndClosesConnection(t *testing.T) {
defer saveGatewayState(t)()
source := &panicReadGatewayConn{gatewayTestConn: newGatewayTestConn(nil)}
handleRequest(source)
if !source.closed {
t.Fatal("source connection was not closed after panic")
}
}
func TestGatewayHandleConnStartsRequestGoroutine(t *testing.T) {
defer saveGatewayState(t)()
source := newGatewayTestConn(nil)
source.readErr = errors.New("read failed")
(&Gateway{}).HandleConn(source)
deadline := time.After(2 * time.Second)
ticker := time.NewTicker(time.Millisecond)
defer ticker.Stop()
for {
select {
case <-deadline:
t.Fatal("timed out waiting for HandleConn goroutine")
case <-ticker.C:
if source.isClosed() {
return
}
}
}
}
func TestGatewayTestOpPanics(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("TestOp() did not panic")
}
}()
(&Gateway{}).TestOp()
}
type panicReadGatewayConn struct {
*gatewayTestConn
}
func (c *panicReadGatewayConn) Read([]byte) (int, error) {
panic("read panic")
}