src.dualinventive.com/go/websocketserver/internal/mtiwss/testutil/testutil.go

51 lines
919 B
Go

package testutil
import (
"io"
"net/http"
"net/http/httptest"
)
// Server wraps a httptest server for use as a MTIWss stub
type Server struct {
url string
ep string
resp *string
srv *httptest.Server
}
// New creates a stubbed mtiwss endpoint on URI with ep as endpoint path
func New(ep string) *Server {
t := &Server{ep: ep}
mux := http.NewServeMux()
mux.HandleFunc(t.ep, func(w http.ResponseWriter, r *http.Request) {
if t.resp == nil {
return
}
_, err := io.WriteString(w, *t.resp)
_ = err
})
t.srv = httptest.NewServer(mux)
t.url = t.srv.URL + t.ep
return t
}
// SetResponse sets the HTTP call response
func (t *Server) SetResponse(resp string) {
t.resp = &resp
}
// Close graceful stops the mtiwss endpoint server
func (t *Server) Close() error {
t.srv.Close()
return nil
}
// URL returns the current MTIWss URL with endpoint
func (t *Server) URL() string {
return t.url
}