httplib: add Close() method to shut the server down and use it in tests

This commit is contained in:
Nick Craig-Wood 2018-02-19 14:02:19 +00:00
parent d8f78a7266
commit 334bf49d30
5 changed files with 32 additions and 9 deletions

View file

@ -21,7 +21,10 @@ import (
"github.com/stretchr/testify/require"
)
var updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
var (
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
httpServer *server
)
const (
testBindAddress = "localhost:51777"
@ -31,8 +34,8 @@ const (
func startServer(t *testing.T, f fs.Fs) {
opt := httplib.DefaultOpt
opt.ListenAddr = testBindAddress
s := newServer(f, &opt)
go s.serve()
httpServer = newServer(f, &opt)
go httpServer.serve()
// try to connect to the test server
pause := time.Millisecond
@ -228,3 +231,7 @@ func TestAddEntry(t *testing.T) {
{remote: "\"quotes\".txt", URL: "%22quotes%22.txt", Leaf: "\"quotes\".txt"},
}, es)
}
func TestFinalise(t *testing.T) {
httpServer.srv.Close()
}

View file

@ -14,3 +14,8 @@ func initServer(s *http.Server) {
s.ReadHeaderTimeout = 10 * time.Second // time to send the headers
s.IdleTimeout = 60 * time.Second // time to keep idle connections open
}
// closeServer closes the server in a non graceful way
func closeServer(s *http.Server) error {
return s.Close()
}

View file

@ -11,3 +11,8 @@ import (
// Initialise the http.Server for pre go1.8
func initServer(s *http.Server) {
}
// closeServer closes the server in a non graceful way
func closeServer(s *http.Server) error {
return nil
}

View file

@ -183,7 +183,15 @@ func (s *Server) Serve() {
} else {
err = s.httpServer.ListenAndServe()
}
log.Fatalf("Fatal error while serving HTTP: %v", err)
log.Printf("Error while serving HTTP: %v", err)
}
// Close shuts the running server down
func (s *Server) Close() {
err := closeServer(s.httpServer)
if err != nil {
log.Printf("Error on closing HTTP server: %v", err)
}
}
// URL returns the serving address of this server

View file

@ -39,11 +39,9 @@ func TestWebDav(t *testing.T) {
assert.NoError(t, err)
// Start the server
go func() {
w := newWebDAV(fremote, &opt)
w.serve()
}()
// FIXME shut it down somehow?
w := newWebDAV(fremote, &opt)
go w.serve()
defer w.srv.Close()
// Change directory to run the tests
err = os.Chdir("../../../backend/webdav")