github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/interop/http09/http_test.go (about)

     1  package http09
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"io"
     7  	"net"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"time"
    11  
    12  	"github.com/metacubex/quic-go"
    13  	"github.com/metacubex/quic-go/internal/testdata"
    14  
    15  	. "github.com/onsi/ginkgo/v2"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("HTTP 0.9 integration tests", func() {
    20  	var (
    21  		server *Server
    22  		saddr  net.Addr
    23  		done   chan struct{}
    24  	)
    25  
    26  	http.HandleFunc("/helloworld", func(w http.ResponseWriter, r *http.Request) {
    27  		_, _ = w.Write([]byte("Hello World!"))
    28  	})
    29  
    30  	BeforeEach(func() {
    31  		server = &Server{
    32  			Server: &http.Server{TLSConfig: testdata.GetTLSConfig()},
    33  		}
    34  		done = make(chan struct{})
    35  		go func() {
    36  			defer GinkgoRecover()
    37  			defer close(done)
    38  			_ = server.ListenAndServe()
    39  		}()
    40  		var ln *quic.EarlyListener
    41  		Eventually(func() *quic.EarlyListener {
    42  			server.mutex.Lock()
    43  			defer server.mutex.Unlock()
    44  			ln = server.listener
    45  			return server.listener
    46  		}, 5*time.Second).ShouldNot(BeNil())
    47  		saddr = ln.Addr()
    48  		saddr.(*net.UDPAddr).IP = net.IP{127, 0, 0, 1}
    49  	})
    50  
    51  	AfterEach(func() {
    52  		Expect(server.Close()).To(Succeed())
    53  		Eventually(done).Should(BeClosed())
    54  	})
    55  
    56  	It("performs request", func() {
    57  		rt := &RoundTripper{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
    58  		defer rt.Close()
    59  		req := httptest.NewRequest(
    60  			http.MethodGet,
    61  			fmt.Sprintf("https://%s/helloworld", saddr),
    62  			nil,
    63  		)
    64  		rsp, err := rt.RoundTrip(req)
    65  		Expect(err).ToNot(HaveOccurred())
    66  		data, err := io.ReadAll(rsp.Body)
    67  		Expect(err).ToNot(HaveOccurred())
    68  		Expect(data).To(Equal([]byte("Hello World!")))
    69  	})
    70  
    71  	It("allows setting of headers", func() {
    72  		http.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) {
    73  			w.Header().Add("foo", "bar")
    74  			w.WriteHeader(1337)
    75  			_, _ = w.Write([]byte("done"))
    76  		})
    77  
    78  		rt := &RoundTripper{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
    79  		defer rt.Close()
    80  		req := httptest.NewRequest(
    81  			http.MethodGet,
    82  			fmt.Sprintf("https://%s/headers", saddr),
    83  			nil,
    84  		)
    85  		rsp, err := rt.RoundTrip(req)
    86  		Expect(err).ToNot(HaveOccurred())
    87  		data, err := io.ReadAll(rsp.Body)
    88  		Expect(err).ToNot(HaveOccurred())
    89  		Expect(data).To(Equal([]byte("done")))
    90  	})
    91  })