Ejemplo n.º 1
0
// Tuns function starts an HTTP server on localhost on a random port. This
// server will run in a goroutine until the listener returned here gets
// closed. Its in the best interest of the caller to defer a close to ensure
// that the http server goroutine gets shut down properly.
func runHttpServer(T *testlib.T) net.Listener {
	listener, err := net.Listen("tcp", ":0")
	T.ExpectSuccess(err)
	T.NotEqual(listener, nil)

	// Setup the HTTP server.
	s := &http.Server{
		Handler:        &httpHandler{},
		ReadTimeout:    1 * time.Second,
		WriteTimeout:   1 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	go func() {
		s.Serve(listener)
	}()

	// Return the listener object.
	return listener
}