Just wondering how I can test Start method below or should I actually test it? If I am to just call Start in my test, it hangs because I believe it actually starts the server which is something we want to avoid.
Thanks
package http
import (
"net/http"
)
type Server struct {
*http.Server
}
func NewServer(adr string) Server {
return Server{
&http.Server{
Addr: adr,
Handler: nil,
},
}
}
func (s Server) Start() error {
if err := s.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
}
The test function could start a go routine that calls Shutdown after a 1 second sleep. This would allow you to call the Start function in your test. But do you really need to test such a simple function ?
Here is an example code that will call Shutdown and stop the server after 1 second.