73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
|
// Copyright (C) 2024 Umorpha Systems
|
||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestRouteGitHTTP(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
type testcase struct {
|
||
|
InRequestLine string
|
||
|
ExpRepo string
|
||
|
ExpFile string
|
||
|
ExpService string
|
||
|
ExpErr string
|
||
|
}
|
||
|
testcases := map[string]testcase{
|
||
|
// These first few testcases are taken from the
|
||
|
// gitprotocol-http(5) manpage, as of Git 2.43.0.
|
||
|
"manpage-1": testcase{
|
||
|
InRequestLine: "/git/repo.git/objects/d0/49f6c27a2244e12041955e262a404c7faba355",
|
||
|
ExpRepo: "/git/repo.git",
|
||
|
ExpFile: "/objects/d0/49f6c27a2244e12041955e262a404c7faba355",
|
||
|
ExpService: "",
|
||
|
},
|
||
|
"manpage-2": testcase{
|
||
|
InRequestLine: "/daemon.cgi?svc=git&q=/info/refs&service=git-receive-pack",
|
||
|
ExpRepo: "/daemon.cgi?svc=git&q=",
|
||
|
ExpFile: "/info/refs",
|
||
|
ExpService: "git-receive-pack",
|
||
|
},
|
||
|
"manpage-3": testcase{
|
||
|
InRequestLine: "/git/repo.git/path/submodule.git/info/refs",
|
||
|
ExpRepo: "/git/repo.git/path/submodule.git",
|
||
|
ExpFile: "/info/refs",
|
||
|
ExpService: "",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for tcName, tc := range testcases {
|
||
|
tc := tc
|
||
|
t.Run(tcName, func(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
uri, err := url.ParseRequestURI(tc.InRequestLine)
|
||
|
require.NoError(t, err)
|
||
|
req := &http.Request{
|
||
|
URL: uri,
|
||
|
RequestURI: tc.InRequestLine,
|
||
|
}
|
||
|
|
||
|
actRepo, actFile, actService, actErr := routeGitHTTP(req)
|
||
|
|
||
|
assert.Equal(t, tc.ExpRepo, actRepo)
|
||
|
assert.Equal(t, tc.ExpFile, actFile)
|
||
|
assert.Equal(t, tc.ExpService, actService)
|
||
|
if tc.ExpErr == "" {
|
||
|
assert.NoError(t, actErr)
|
||
|
} else {
|
||
|
assert.EqualError(t, err, tc.ExpErr)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|