fix(go-models): harden Novita default transport handling (#15350)

## Summary
- Harden `NewNovitaModel` to avoid panics when `http.DefaultTransport`
is a custom non-`*http.Transport` RoundTripper.
- Fallback to a safe transport (`ProxyFromEnvironment`) while preserving
existing pooling/timeout settings.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
glorydavid03023
2026-05-29 01:28:46 -05:00
committed by GitHub
parent ea3a5dba11
commit b79f79d9b9
2 changed files with 29 additions and 1 deletions

View File

@@ -57,7 +57,15 @@ type NovitaModel struct {
// clone http.DefaultTransport, override the connection-pool fields,
// no client-level Timeout so SSE streams are not capped.
func NewNovitaModel(baseURL map[string]string, urlSuffix URLSuffix) *NovitaModel {
transport := http.DefaultTransport.(*http.Transport).Clone()
defaultTransport, ok := http.DefaultTransport.(*http.Transport)
var transport *http.Transport
if ok {
transport = defaultTransport.Clone()
} else {
transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
}
transport.MaxIdleConns = 100
transport.MaxIdleConnsPerHost = 10
transport.IdleConnTimeout = 90 * time.Second

View File

@@ -9,6 +9,12 @@ import (
"testing"
)
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
func newNovitaServer(t *testing.T, expectedPath string, handler func(t *testing.T, body map[string]interface{}, w http.ResponseWriter)) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -58,6 +64,20 @@ func newNovitaForTest(baseURL string) *NovitaModel {
)
}
func TestNovitaNewModelWithCustomDefaultTransport(t *testing.T) {
original := http.DefaultTransport
http.DefaultTransport = roundTripperFunc(func(*http.Request) (*http.Response, error) {
return nil, nil
})
t.Cleanup(func() {
http.DefaultTransport = original
})
if model := NewNovitaModel(map[string]string{"default": "http://unused"}, URLSuffix{}); model == nil {
t.Fatal("NewNovitaModel returned nil")
}
}
// newNovitaSSEServer asserts the SSE-chat wire contract (POST, path,
// Authorization, Content-Type) the same way newNovitaServer does for
// the JSON-chat path, then writes the supplied SSE payload. Closes