Skip to content

Commit

Permalink
gittestserver: Add http middleware support
Browse files Browse the repository at this point in the history
Add http middleware support to be able to change the behavior of the
server. Usage could be for returning certain response and error or
introducing some delay in the server, etc.
The middlewares can be chained together based on the needs.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Nov 3, 2021
1 parent 3ab7cce commit d3a67d9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
24 changes: 22 additions & 2 deletions gittestserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
Expand Down Expand Up @@ -60,6 +61,9 @@ func NewGitServer(docroot string) *GitServer {
}
}

// HTTPMiddleware is a git http server middleware.
type HTTPMiddleware func(http.Handler) http.Handler

// GitServer is a git server for testing purposes.
// It can serve git repositories over HTTP and SSH.
type GitServer struct {
Expand All @@ -68,6 +72,12 @@ type GitServer struct {
sshServer *gitkit.SSH
// Set these to configure HTTP auth
username, password string
httpMiddlewares []HTTPMiddleware
}

// AddHTTPMiddlewares adds http middlewares to the git server.
func (s *GitServer) AddHTTPMiddlewares(httpMiddlewares ...HTTPMiddleware) {
s.httpMiddlewares = append(s.httpMiddlewares, httpMiddlewares...)
}

// AutoCreate enables the automatic creation of a non-existing Git
Expand Down Expand Up @@ -126,7 +136,8 @@ func (s *GitServer) StartHTTP() error {
if err := service.Setup(); err != nil {
return err
}
s.httpServer = httptest.NewServer(service)
handler := buildHTTPHandler(service, s.httpMiddlewares...)
s.httpServer = httptest.NewServer(handler)
return nil
}

Expand All @@ -142,7 +153,8 @@ func (s *GitServer) StartHTTPS(cert, key, ca []byte, serverName string) error {
if err := service.Setup(); err != nil {
return err
}
s.httpServer = httptest.NewUnstartedServer(service)
handler := buildHTTPHandler(service, s.httpMiddlewares...)
s.httpServer = httptest.NewUnstartedServer(handler)

config := tls.Config{}

Expand Down Expand Up @@ -365,3 +377,11 @@ func checkout(repo *gogit.Repository, branch string) error {
func getLocalURL(localPath string) string {
return fmt.Sprintf("file://%s", localPath)
}

// buildHTTPHandler chains a given http handler with the given middlewares.
func buildHTTPHandler(handler http.Handler, middlewares ...HTTPMiddleware) http.Handler {
for _, middleware := range middlewares {
handler = middleware(handler)
}
return handler
}
42 changes: 42 additions & 0 deletions gittestserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gittestserver

import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -194,3 +195,44 @@ func TestInitRepo(t *testing.T) {
})
}
}

func TestGitServer_AddHTTPMiddlewares(t *testing.T) {
repoPath := "bar/test-reponame"
initBranch := "test-branch"

srv, err := NewTempGitServer()
if err != nil {
t.Fatal(err)
}
srv.Auth("test-user", "test-pswd")
defer os.RemoveAll(srv.Root())
srv.KeyDir(srv.Root())

// Add a middleware.
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "something failed", http.StatusInternalServerError)
})
}
srv.AddHTTPMiddlewares(middleware)

if err = srv.StartHTTP(); err != nil {
t.Fatal(err)
}
defer srv.StopHTTP()

// Initialize a repo.
err = srv.InitRepo("testdata/git/repo1", initBranch, repoPath)
if err != nil {
t.Fatalf("failed to initialize repo: %v", err)
}
repoURL := srv.HTTPAddressWithCredentials() + "/" + repoPath

// Clone the branch.
_, err = gogit.PlainClone("some-non-existing-dir", false, &gogit.CloneOptions{
URL: repoURL,
})
if !strings.Contains(err.Error(), "status code: 500") {
t.Errorf("expected error status code 500, got: %v", err)
}
}

0 comments on commit d3a67d9

Please sign in to comment.