Skip to content

Commit a3b3b15

Browse files
authored
chore(integration): refactor test git server auth to http mw (#137)
1 parent ece89cd commit a3b3b15

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

integration/integration_test.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -742,16 +742,35 @@ type gitServerOptions struct {
742742
files map[string]string
743743
username string
744744
password string
745+
authMW func(http.Handler) http.Handler
745746
}
746747

747748
// createGitServer creates a git repository with an in-memory filesystem
748749
// and serves it over HTTP using a httptest.Server.
749750
func createGitServer(t *testing.T, opts gitServerOptions) string {
750751
t.Helper()
751-
srv := httptest.NewServer(createGitHandler(t, opts))
752+
if opts.authMW == nil {
753+
opts.authMW = checkBasicAuth(opts.username, opts.password)
754+
}
755+
srv := httptest.NewServer(opts.authMW(createGitHandler(t, opts)))
752756
return srv.URL
753757
}
754758

759+
func checkBasicAuth(username, password string) func(http.Handler) http.Handler {
760+
return func(next http.Handler) http.Handler {
761+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
762+
if username != "" && password != "" {
763+
authUser, authPass, ok := r.BasicAuth()
764+
if !ok || username != authUser || password != authPass {
765+
w.WriteHeader(http.StatusUnauthorized)
766+
return
767+
}
768+
}
769+
next.ServeHTTP(w, r)
770+
})
771+
}
772+
}
773+
755774
func createGitHandler(t *testing.T, opts gitServerOptions) http.Handler {
756775
t.Helper()
757776
fs := memfs.New()
@@ -773,16 +792,7 @@ func createGitHandler(t *testing.T, opts gitServerOptions) http.Handler {
773792
require.NoError(t, err)
774793
_, err = repo.CommitObject(commit)
775794
require.NoError(t, err)
776-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
777-
if opts.username != "" || opts.password != "" {
778-
username, password, ok := r.BasicAuth()
779-
if !ok || username != opts.username || password != opts.password {
780-
w.WriteHeader(http.StatusUnauthorized)
781-
return
782-
}
783-
}
784-
gittest.NewServer(fs).ServeHTTP(w, r)
785-
})
795+
return gittest.NewServer(fs)
786796
}
787797

788798
// cleanOldEnvbuilders removes any old envbuilder containers.

0 commit comments

Comments
 (0)