http3: correctly use the quic.Transport (#3869)

* use quic.Transport in http3

* add intergrationtests to dial server with different server names

* update test
This commit is contained in:
Glonee 2023-06-01 14:31:20 +08:00 committed by GitHub
parent 21549fcb4a
commit c96fbd2e4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 90 deletions

View file

@ -6,13 +6,11 @@ import (
"crypto/tls"
"errors"
"io"
"net"
"net/http"
"sync/atomic"
"time"
"github.com/quic-go/quic-go"
mockquic "github.com/quic-go/quic-go/internal/mocks/quic"
"github.com/quic-go/quic-go/internal/qerr"
"github.com/golang/mock/gomock"
@ -368,66 +366,4 @@ var _ = Describe("RoundTripper", func() {
Expect(len(rt.clients)).To(Equal(0))
})
})
Context("reusing udpconn", func() {
var originalDialer func(ctx context.Context, pconn net.PacketConn, remoteAddr net.Addr, tlsConf *tls.Config, config *quic.Config) (quic.EarlyConnection, error)
var req1, req2 *http.Request
BeforeEach(func() {
var err error
originalDialer = quicDialer
req1, err = http.NewRequest("GET", "https://site1.com", nil)
Expect(err).ToNot(HaveOccurred())
req2, err = http.NewRequest("GET", "https://site2.com", nil)
Expect(err).ToNot(HaveOccurred())
Expect(req1.Host).ToNot(Equal(req2.Host))
})
AfterEach(func() {
quicDialer = originalDialer
err := rt.Close()
Expect(err).ToNot(HaveOccurred())
})
It("creates udpconn at first request", func() {
Expect(rt.udpConn).To(BeNil())
rt.newClient = func(hostname string, tlsConf *tls.Config, opts *roundTripperOpts, conf *quic.Config, dialer dialFunc) (roundTripCloser, error) {
cl := NewMockRoundTripCloser(mockCtrl)
cl.EXPECT().RoundTripOpt(gomock.Any(), gomock.Any())
cl.EXPECT().Close()
return cl, nil
}
_, err := rt.RoundTrip(req1)
Expect(err).ToNot(HaveOccurred())
Expect(rt.udpConn).ToNot(BeNil())
})
It("reuses udpconn in different hosts", func() {
Expect(rt.udpConn).To(BeNil())
quicDialer = func(_ context.Context, pconn net.PacketConn, _ net.Addr, _ *tls.Config, _ *quic.Config) (quic.EarlyConnection, error) {
conn := mockquic.NewMockEarlyConnection(mockCtrl)
conn.EXPECT().LocalAddr().Return(pconn.LocalAddr())
return conn, nil
}
rt.newClient = func(hostname string, tlsConf *tls.Config, _ *roundTripperOpts, conf *quic.Config, dialer dialFunc) (roundTripCloser, error) {
cl := NewMockRoundTripCloser(mockCtrl)
cl.EXPECT().RoundTripOpt(gomock.Any(), gomock.Any()).DoAndReturn(func(_ *http.Request, _ RoundTripOpt) (*http.Response, error) {
header := make(http.Header)
quicConn, err := dialer(context.Background(), hostname, tlsConf, conf)
Expect(err).ToNot(HaveOccurred())
header.Set("udpconn", quicConn.LocalAddr().String())
return &http.Response{Header: header}, nil
})
cl.EXPECT().Close()
return cl, nil
}
rsp1, err := rt.RoundTrip(req1)
Expect(err).ToNot(HaveOccurred())
Expect(rsp1.Header.Get("udpconn")).ToNot(Equal(""))
rsp2, err := rt.RoundTrip(req2)
Expect(err).ToNot(HaveOccurred())
Expect(rsp2.Header.Get("udpconn")).ToNot(Equal(""))
Expect(rsp1.Header.Get("udpconn")).To(Equal(rsp2.Header.Get("udpconn")))
})
})
})