Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/integrationtest/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,31 @@ func (env *Environment) HTTPServeFile(port uint16, path *paths.Path) *url.URL {

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
}