fix: close request bodies when HTTP client rejects before transport
This commit is contained in:
@@ -15,6 +15,31 @@ import (
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
type trackedBody struct {
|
||||
io.Reader
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (b *trackedBody) Close() error { b.closed = true; return nil }
|
||||
|
||||
func TestRejectedRequestClosesBody(t *testing.T) {
|
||||
for _, closed := range []bool{false, true} {
|
||||
client, _ := New(Config{DisableLogging: true})
|
||||
body := &trackedBody{Reader: strings.NewReader("body")}
|
||||
request, _ := http.NewRequest("POST", "http://unused.invalid", body)
|
||||
if closed {
|
||||
client.Close()
|
||||
} else {
|
||||
request.URL = nil
|
||||
}
|
||||
_, err := client.Do(request)
|
||||
client.Close()
|
||||
if err == nil || !body.closed {
|
||||
t.Fatalf("error=%v bodyClosed=%v", err, body.closed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetryCorrelationPoolAndWriteSafety(t *testing.T) {
|
||||
var calls, connections atomic.Int32
|
||||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user