mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-06 13:42:26 +08:00
Compare commits
2 Commits
v2.1.10
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8157cad2cb | ||
|
|
be6e95e987 |
16
util/http.go
16
util/http.go
@@ -292,19 +292,13 @@ func httpWithTLS(rootCa, key string) (*http.Client, error) {
|
|||||||
Certificates: []tls.Certificate{cert},
|
Certificates: []tls.Certificate{cert},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安全地获取 *http.Transport
|
var baseTransport http.RoundTripper
|
||||||
var trans *http.Transport
|
|
||||||
// 尝试从 DefaultHTTPClient 获取 Transport,如果失败则使用默认值
|
|
||||||
if DefaultHTTPClient.Transport != nil {
|
if DefaultHTTPClient.Transport != nil {
|
||||||
if t, ok := DefaultHTTPClient.Transport.(*http.Transport); ok {
|
baseTransport = DefaultHTTPClient.Transport
|
||||||
trans = t.Clone()
|
} else {
|
||||||
}
|
baseTransport = http.DefaultTransport
|
||||||
}
|
}
|
||||||
// 如果无法获取有效的 Transport,使用默认值
|
trans := baseTransport.(*http.Transport).Clone()
|
||||||
if trans == nil {
|
|
||||||
trans = http.DefaultTransport.(*http.Transport).Clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
trans.TLSClientConfig = config
|
trans.TLSClientConfig = config
|
||||||
trans.DisableCompression = true
|
trans.DisableCompression = true
|
||||||
client = &http.Client{Transport: trans}
|
client = &http.Client{Transport: trans}
|
||||||
|
|||||||
@@ -1,81 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestHttpWithTLS_NilTransport tests the scenario where DefaultHTTPClient.Transport is nil
|
|
||||||
func TestHttpWithTLS_NilTransport(t *testing.T) {
|
|
||||||
// Save original transport
|
|
||||||
originalTransport := DefaultHTTPClient.Transport
|
|
||||||
defer func() {
|
|
||||||
DefaultHTTPClient.Transport = originalTransport
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Set Transport to nil to simulate the bug scenario
|
|
||||||
DefaultHTTPClient.Transport = nil
|
|
||||||
|
|
||||||
// This should not panic after the fix
|
|
||||||
// Note: This will fail due to invalid cert path, but shouldn't panic on type assertion
|
|
||||||
_, err := httpWithTLS("./testdata/invalid_cert.p12", "password")
|
|
||||||
|
|
||||||
// We expect an error (cert file not found), but NOT a panic
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Expected error due to invalid cert path, but got nil")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestHttpWithTLS_CustomTransport tests the scenario where DefaultHTTPClient has a custom Transport
|
|
||||||
func TestHttpWithTLS_CustomTransport(t *testing.T) {
|
|
||||||
// Save original transport
|
|
||||||
originalTransport := DefaultHTTPClient.Transport
|
|
||||||
defer func() {
|
|
||||||
DefaultHTTPClient.Transport = originalTransport
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Set a custom http.Transport
|
|
||||||
customTransport := &http.Transport{
|
|
||||||
MaxIdleConns: 100,
|
|
||||||
}
|
|
||||||
DefaultHTTPClient.Transport = customTransport
|
|
||||||
|
|
||||||
// This should not panic
|
|
||||||
_, err := httpWithTLS("./testdata/invalid_cert.p12", "password")
|
|
||||||
|
|
||||||
// We expect an error (cert file not found), but NOT a panic
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Expected error due to invalid cert path, but got nil")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CustomRoundTripper is a custom implementation of http.RoundTripper
|
|
||||||
type CustomRoundTripper struct{}
|
|
||||||
|
|
||||||
func (c *CustomRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
||||||
return http.DefaultTransport.RoundTrip(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestHttpWithTLS_CustomRoundTripper tests the edge case where DefaultHTTPClient has a custom RoundTripper
|
|
||||||
// that is NOT *http.Transport
|
|
||||||
func TestHttpWithTLS_CustomRoundTripper(t *testing.T) {
|
|
||||||
// Save original transport
|
|
||||||
originalTransport := DefaultHTTPClient.Transport
|
|
||||||
defer func() {
|
|
||||||
DefaultHTTPClient.Transport = originalTransport
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Set a custom RoundTripper that is NOT *http.Transport
|
|
||||||
customRoundTripper := &CustomRoundTripper{}
|
|
||||||
DefaultHTTPClient.Transport = customRoundTripper
|
|
||||||
|
|
||||||
// Create a recovery handler to catch potential panic
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
t.Errorf("httpWithTLS panicked with custom RoundTripper: %v", r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// This might panic if the code doesn't handle non-*http.Transport RoundTripper properly
|
|
||||||
_, _ = httpWithTLS("./testdata/invalid_cert.p12", "password")
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user