add more e2e test (#2505)

This commit is contained in:
fatedier
2021-08-02 13:07:28 +08:00
committed by GitHub
parent 2a68c1152f
commit 09f39de74e
20 changed files with 1448 additions and 154 deletions

View File

@@ -1,6 +1,7 @@
package httpserver
import (
"crypto/tls"
"fmt"
"net"
"net/http"
@@ -12,8 +13,9 @@ type Server struct {
bindPort int
hanlder http.Handler
l net.Listener
hs *http.Server
l net.Listener
tlsConfig *tls.Config
hs *http.Server
}
type Option func(*Server) *Server
@@ -43,6 +45,13 @@ func WithBindPort(port int) Option {
}
}
func WithTlsConfig(tlsConfig *tls.Config) Option {
return func(s *Server) *Server {
s.tlsConfig = tlsConfig
return s
}
}
func WithHandler(h http.Handler) Option {
return func(s *Server) *Server {
s.hanlder = h
@@ -50,6 +59,15 @@ func WithHandler(h http.Handler) Option {
}
}
func WithResponse(resp []byte) Option {
return func(s *Server) *Server {
s.hanlder = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(resp)
})
return s
}
}
func (s *Server) Run() error {
if err := s.initListener(); err != nil {
return err
@@ -57,11 +75,17 @@ func (s *Server) Run() error {
addr := net.JoinHostPort(s.bindAddr, strconv.Itoa(s.bindPort))
hs := &http.Server{
Addr: addr,
Handler: s.hanlder,
Addr: addr,
Handler: s.hanlder,
TLSConfig: s.tlsConfig,
}
s.hs = hs
go hs.Serve(s.l)
if s.tlsConfig == nil {
go hs.Serve(s.l)
} else {
go hs.ServeTLS(s.l, "", "")
}
return nil
}