Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions common/httpx/http11_fallback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package httpx

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNew_HTTP11DisablesRetryableHTTP2Fallback(t *testing.T) {
opts := DefaultOptions
opts.Protocol = HTTP11

ht, err := New(&opts)
require.NoError(t, err)
Comment on lines +9 to +14
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Restore GODEBUG after this test.

New mutates the process-wide GODEBUG when HTTP11 is forced, so this test can leak http2client=0 into later cases and make the suite order-dependent.

💡 Proposed fix
 import (
+	"os"
 	"testing"
 
 	"github.com/stretchr/testify/require"
 )
 
 func TestNew_HTTP11DisablesRetryableHTTP2Fallback(t *testing.T) {
+	origGODEBUG, hadGODEBUG := os.LookupEnv("GODEBUG")
+	t.Cleanup(func() {
+		if hadGODEBUG {
+			_ = os.Setenv("GODEBUG", origGODEBUG)
+		} else {
+			_ = os.Unsetenv("GODEBUG")
+		}
+	})
+
 	opts := DefaultOptions
 	opts.Protocol = HTTP11
 
 	ht, err := New(&opts)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func TestNew_HTTP11DisablesRetryableHTTP2Fallback(t *testing.T) {
opts := DefaultOptions
opts.Protocol = HTTP11
ht, err := New(&opts)
require.NoError(t, err)
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestNew_HTTP11DisablesRetryableHTTP2Fallback(t *testing.T) {
origGODEBUG, hadGODEBUG := os.LookupEnv("GODEBUG")
t.Cleanup(func() {
if hadGODEBUG {
_ = os.Setenv("GODEBUG", origGODEBUG)
} else {
_ = os.Unsetenv("GODEBUG")
}
})
opts := DefaultOptions
opts.Protocol = HTTP11
ht, err := New(&opts)
require.NoError(t, err)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@common/httpx/http11_fallback_test.go` around lines 9 - 14,
TestNew_HTTP11DisablesRetryableHTTP2Fallback calls New which mutates the
process-wide GODEBUG; save the original GODEBUG value before calling New and
restore it after the test (use os.Getenv to capture and defer restoring with
os.Setenv or os.Unsetenv as appropriate) so the test does not leak http2client=0
to other tests; update TestNew_HTTP11DisablesRetryableHTTP2Fallback to perform
this save/restore around the call to New.

require.NotNil(t, ht)
require.NotNil(t, ht.client)
require.NotNil(t, ht.client.HTTPClient)
require.NotNil(t, ht.client.HTTPClient2)

// Protocol pinning expectation: when forced to HTTP/1.1, retry fallback should
// stay on the same client/transport instead of switching to dedicated HTTP/2.
require.Same(t, ht.client.HTTPClient, ht.client.HTTPClient2)
}
7 changes: 7 additions & 0 deletions common/httpx/httpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ func New(options *Options) (*HTTPX, error) {
CheckRedirect: redirectFunc,
}, retryablehttpOptions)

// When protocol is explicitly forced to HTTP/1.1, prevent retryablehttp from
// falling back to its dedicated HTTP/2 client path on malformed HTTP/2 errors.
// Keep retries enabled, but pinned to the same HTTP/1.1 transport settings.
if httpx.Options.Protocol == HTTP11 {
httpx.client.HTTPClient2 = httpx.client.HTTPClient
}

transport2 := &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
Expand Down