Skip to content

Commit f0479ba

Browse files
[skip-changelog] Add function to spawn a server that responds with error code (#1936)
1 parent bdd0cf9 commit f0479ba

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: internal/integrationtest/http_server.go

+28
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,31 @@ func (env *Environment) HTTPServeFile(port uint16, path *paths.Path) *url.URL {
5151

5252
return fileURL
5353
}
54+
55+
// HTTPServeFileError spawns an http server that serves a single file and responds
56+
// with the given error code.
57+
func (env *Environment) HTTPServeFileError(port uint16, path *paths.Path, code int) *url.URL {
58+
mux := http.NewServeMux()
59+
mux.HandleFunc("/"+path.Base(), func(w http.ResponseWriter, r *http.Request) {
60+
w.WriteHeader(code)
61+
})
62+
server := &http.Server{
63+
Addr: fmt.Sprintf(":%d", port),
64+
Handler: mux,
65+
}
66+
67+
t := env.T()
68+
fileURL, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/%s", port, path.Base()))
69+
require.NoError(t, err)
70+
71+
go func() {
72+
err := server.ListenAndServe()
73+
require.Equal(t, err, http.ErrServerClosed)
74+
}()
75+
76+
env.RegisterCleanUpCallback(func() {
77+
server.Close()
78+
})
79+
80+
return fileURL
81+
}

0 commit comments

Comments
 (0)