@@ -742,16 +742,35 @@ type gitServerOptions struct {
742
742
files map [string ]string
743
743
username string
744
744
password string
745
+ authMW func (http.Handler ) http.Handler
745
746
}
746
747
747
748
// createGitServer creates a git repository with an in-memory filesystem
748
749
// and serves it over HTTP using a httptest.Server.
749
750
func createGitServer (t * testing.T , opts gitServerOptions ) string {
750
751
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 )))
752
756
return srv .URL
753
757
}
754
758
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
+
755
774
func createGitHandler (t * testing.T , opts gitServerOptions ) http.Handler {
756
775
t .Helper ()
757
776
fs := memfs .New ()
@@ -773,16 +792,7 @@ func createGitHandler(t *testing.T, opts gitServerOptions) http.Handler {
773
792
require .NoError (t , err )
774
793
_ , err = repo .CommitObject (commit )
775
794
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 )
786
796
}
787
797
788
798
// cleanOldEnvbuilders removes any old envbuilder containers.
0 commit comments