WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 78e84d4

Browse files
committed
test: improve HTTP test robustness and resource management
- Use NewRequestWithContext with a background context in tests instead of NewRequest - Add error handling and fail tests immediately if RoundTrip returns an error - Ensure HTTP response bodies are closed after each test to prevent resource leaks Signed-off-by: appleboy <[email protected]>
1 parent 47fee0e commit 78e84d4

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

core/transport/transport_test.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package transport
22

33
import (
4+
"context"
45
"errors"
56
"net/http"
67
"reflect"
@@ -33,8 +34,14 @@ func TestDefaultHeaderTransport_CustomHeaders(t *testing.T) {
3334
AppName: "myapp",
3435
AppVersion: "1.2.3",
3536
}
36-
req, _ := http.NewRequest("GET", "http://example.com", nil)
37-
tr.RoundTrip(req)
37+
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
38+
resp, err := tr.RoundTrip(req)
39+
if err != nil {
40+
t.Fatalf("RoundTrip error: %v", err)
41+
}
42+
if resp != nil && resp.Body != nil {
43+
resp.Body.Close()
44+
}
3845

3946
want := http.Header{
4047
"X-Test": {"abc"},
@@ -58,8 +65,14 @@ func TestDefaultHeaderTransport_EmptyHeadersAndAppInfo(t *testing.T) {
5865
AppName: "",
5966
AppVersion: "",
6067
}
61-
req, _ := http.NewRequest("GET", "http://example.com", nil)
62-
tr.RoundTrip(req)
68+
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
69+
resp, err := tr.RoundTrip(req)
70+
if err != nil {
71+
t.Fatalf("RoundTrip error: %v", err)
72+
}
73+
if resp != nil && resp.Body != nil {
74+
resp.Body.Close()
75+
}
6376

6477
// x-app-name/version should not be present
6578
if req.Header.Get("x-app-name") != "" {
@@ -78,8 +91,11 @@ func TestDefaultHeaderTransport_OriginErrorPropagation(t *testing.T) {
7891
AppName: "",
7992
AppVersion: "",
8093
}
81-
req, _ := http.NewRequest("GET", "http://example.com", nil)
82-
_, err := tr.RoundTrip(req)
94+
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
95+
resp, err := tr.RoundTrip(req)
96+
if resp != nil && resp.Body != nil {
97+
resp.Body.Close()
98+
}
8399
if err == nil || err.Error() != "mock error" {
84100
t.Errorf("Expected error 'mock error', got %v", err)
85101
}
@@ -95,8 +111,14 @@ func TestDefaultHeaderTransport_MultipleHeaderValues(t *testing.T) {
95111
AppName: "app",
96112
AppVersion: "v",
97113
}
98-
req, _ := http.NewRequest("GET", "http://example.com", nil)
99-
tr.RoundTrip(req)
114+
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
115+
resp, err := tr.RoundTrip(req)
116+
if err != nil {
117+
t.Fatalf("RoundTrip error: %v", err)
118+
}
119+
if resp != nil && resp.Body != nil {
120+
resp.Body.Close()
121+
}
100122

101123
got := req.Header.Values("X-Multi")
102124
want := []string{"a", "b"}

0 commit comments

Comments
 (0)