Skip to content

Commit

Permalink
e2e/tests: add TestProxyReuseConnection
Browse files Browse the repository at this point in the history
In order to reuse HTTP connection the body must be drained.
  • Loading branch information
Choraden committed Nov 16, 2023
1 parent 0505216 commit bc745c4
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions e2e/tests/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import (
"context"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/gorilla/websocket"
"github.com/saucelabs/forwarder/e2e/forwarder"
Expand Down Expand Up @@ -287,3 +291,47 @@ func TestProxyUpstream(t *testing.T) {
t.Fatalf("%s via header not found", forwarder.UpstreamProxyServiceName)
}
}

func TestProxyReuseConnection(t *testing.T) {
c := newClient(t, "http://wronghost", func(tr *http.Transport) {
var (
d net.Dialer
dialed atomic.Bool
)
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if dialed.CompareAndSwap(false, true) {
return d.DialContext(ctx, network, addr)
}
return nil, errors.New("only one dial is allowed")
}
})

for i := 0; i < 2; i++ {
r, w := io.Pipe()

written := make(chan struct{})
go func() {
zeros := make([]byte, 1024*1024)
if _, err := w.Write(zeros); err != nil {
t.Error(err)
}
if err := w.Close(); err != nil {
t.Error(err)
}
close(written)
}()

res := c.GET("/", func(req *http.Request) {
req.Body = r
})
if res.StatusCode != http.StatusBadGateway {
t.Fatalf("Expected status %d, got %d", http.StatusBadGateway, res.StatusCode)
}

select {
case <-written:
case <-time.After(10 * time.Second):
t.Fatal("timed-out waiting for body read")
}
}
}

0 comments on commit bc745c4

Please sign in to comment.