Replies: 3 comments 2 replies
-
Hi, interesting question. I don't know the answer, but here is what I can tell:
Here is a test without httpexpect that reproduces your problem: package httpexpect
import (
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnexpectedRetryBehavior(t *testing.T) {
var requestCount int32
handler := func(w http.ResponseWriter, r *http.Request) {
println("request: " r.URL.Path)
if r.URL.Path == "/pass" {
return
}
atomic.AddInt32(&requestCount, 1)
hj, ok := w.(http.Hijacker)
assert.True(t, ok)
conn, _, err := hj.Hijack()
assert.NoError(t, err)
err = conn.Close()
assert.NoError(t, err)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
println(">>> sending request 1")
_, _ = http.Get(server.URL)
assert.Equal(t, int32(1), atomic.LoadInt32(&requestCount))
println(">>> sending /pass")
_, _ = http.Get(server.URL "/pass")
println(">>> sending request 2")
_, _ = http.Get(server.URL)
assert.Equal(t, int32(2), atomic.LoadInt32(&requestCount))
} Here is the output:
See that if _, ok := err.(transportReadFromServerError); ok {
// We got some non-EOF net.Conn.Read failure reading
// the 1st response byte from the server.
println("----- HERE -----")
return true
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Disabling KeepAlives solve the problem. @gavv , thank you so much for pointing me in the right direction. func TestHTTPExpectUnexpected(t *testing.T) {
var requestCount int32
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DisableKeepAlives = true
client := &http.Client{Transport: transport}
handler := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/pass" {
return
}
atomic.AddInt32(&requestCount, 1)
hj, ok := w.(http.Hijacker)
assert.True(t, ok)
conn, _, err := hj.Hijack()
assert.NoError(t, err)
err = conn.Close()
assert.NoError(t, err)
}
t.Run("test with stdlib client", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
_, _ = client.Get(server.URL)
assert.Equal(t, int32(1), atomic.LoadInt32(&requestCount))
_, _ = client.Get(server.URL "/pass")
_, _ = client.Get(server.URL)
assert.Equal(t, int32(2), atomic.LoadInt32(&requestCount))
})
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
Moved to discussions |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
After we make a request with httpexpect, http.DefaultClient starts to retry requests on fails which is unexpected behavior.
Can you please explain why it happens and how to avoid this behavior?
Beta Was this translation helpful? Give feedback.
All reactions