Unverified Commit f0479ba1 authored by MatteoPologruto's avatar MatteoPologruto Committed by GitHub

[skip-changelog] Add function to spawn a server that responds with error code (#1936)

parent bdd0cf92
...@@ -51,3 +51,31 @@ func (env *Environment) HTTPServeFile(port uint16, path *paths.Path) *url.URL { ...@@ -51,3 +51,31 @@ func (env *Environment) HTTPServeFile(port uint16, path *paths.Path) *url.URL {
return fileURL return fileURL
} }
// HTTPServeFileError spawns an http server that serves a single file and responds
// with the given error code.
func (env *Environment) HTTPServeFileError(port uint16, path *paths.Path, code int) *url.URL {
mux := http.NewServeMux()
mux.HandleFunc("/"+path.Base(), func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
})
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}
t := env.T()
fileURL, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/%s", port, path.Base()))
require.NoError(t, err)
go func() {
err := server.ListenAndServe()
require.Equal(t, err, http.ErrServerClosed)
}()
env.RegisterCleanUpCallback(func() {
server.Close()
})
return fileURL
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment