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