From 745f1d8ed2fcf2dcd0f5509935e3ff115d42d526 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Tue, 3 May 2016 15:14:24 +0200 Subject: [PATCH] add some h2quic server tests fixes #44 --- h2quic/server.go | 2 +- h2quic/server_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/h2quic/server.go b/h2quic/server.go index fbd27d5d1..665c49394 100644 --- a/h2quic/server.go +++ b/h2quic/server.go @@ -30,7 +30,7 @@ func NewServer(certPath string) (*Server, error) { return s, nil } -//ListenAndServe listens on the network address and calls the handler. +// ListenAndServe listens on the network address and calls the handler. func (s *Server) ListenAndServe(addr string, handler http.Handler) error { if handler != nil { s.handler = handler diff --git a/h2quic/server_test.go b/h2quic/server_test.go index 5b21a2d6e..d940e18cd 100644 --- a/h2quic/server_test.go +++ b/h2quic/server_test.go @@ -1,3 +1,89 @@ package h2quic -// TODO: Write server tests +import ( + "net/http" + "os" + + "golang.org/x/net/http2" + "golang.org/x/net/http2/hpack" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Response Writer", func() { + certPath := os.Getenv("GOPATH") + "/src/github.com/lucas-clemente/quic-go/example/" + + var ( + s *Server + ) + + BeforeEach(func() { + var err error + s, err = NewServer(certPath) + Expect(err).NotTo(HaveOccurred()) + Expect(s).NotTo(BeNil()) + }) + + It("uses default handler", func() { + // We try binding to a low port number, s.t. it always fails + err := s.ListenAndServe("localhost:80", nil) + Expect(err).To(HaveOccurred()) + Expect(s.handler).To(Equal(http.DefaultServeMux)) + }) + + It("sets handler properly", func() { + h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) + // We try binding to a low port number, s.t. it always fails + err := s.ListenAndServe("localhost:80", h) + Expect(err).To(HaveOccurred()) + Expect(s.handler).NotTo(Equal(http.DefaultServeMux)) + }) + + Context("handling requests", func() { + var ( + h2framer *http2.Framer + hpackDecoder *hpack.Decoder + headerStream *mockStream + ) + + BeforeEach(func() { + headerStream = &mockStream{} + hpackDecoder = hpack.NewDecoder(4096, nil) + h2framer = http2.NewFramer(nil, headerStream) + }) + + It("handles a sample request", func() { + var handlerCalled bool + s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + Expect(r.Host).To(Equal("www.example.com")) + handlerCalled = true + }) + headerStream.Write([]byte{ + 0x0, 0x0, 0x11, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, + // Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding + 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, + }) + err := s.handleRequest(nil, headerStream, hpackDecoder, h2framer) + Expect(err).NotTo(HaveOccurred()) + Eventually(func() bool { return handlerCalled }).Should(BeTrue()) + }) + + }) + + It("handles the header stream", func() { + var handlerCalled bool + s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + Expect(r.Host).To(Equal("www.example.com")) + handlerCalled = true + }) + headerStream := &mockStream{} + headerStream.Write([]byte{ + 0x0, 0x0, 0x11, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, + // Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding + 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, + }) + s.handleStream(nil, headerStream) + Eventually(func() bool { return handlerCalled }).Should(BeTrue()) + }) +})