mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
268 lines
7.1 KiB
Go
268 lines
7.1 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package url
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/git/modules/httplib"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestParseGitURLs(t *testing.T) {
|
|
kases := []struct {
|
|
kase string
|
|
expected *GitURL
|
|
}{
|
|
{
|
|
kase: "[email protected]:hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "ssh",
|
|
User: url.User("git"),
|
|
Host: "127.0.0.1",
|
|
Path: "hanzoai/forge.git",
|
|
},
|
|
extraMark: 1,
|
|
},
|
|
},
|
|
{
|
|
kase: "git@[fe80::14fc:cec5:c174:d88%2510]:hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "ssh",
|
|
User: url.User("git"),
|
|
Host: "[fe80::14fc:cec5:c174:d88%10]",
|
|
Path: "hanzoai/forge.git",
|
|
},
|
|
extraMark: 1,
|
|
},
|
|
},
|
|
{
|
|
kase: "git@[::1]:hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "ssh",
|
|
User: url.User("git"),
|
|
Host: "[::1]",
|
|
Path: "hanzoai/forge.git",
|
|
},
|
|
extraMark: 1,
|
|
},
|
|
},
|
|
{
|
|
kase: "[email protected]:hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "ssh",
|
|
User: url.User("git"),
|
|
Host: "github.com",
|
|
Path: "hanzoai/forge.git",
|
|
},
|
|
extraMark: 1,
|
|
},
|
|
},
|
|
{
|
|
kase: "ssh://[email protected]/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "ssh",
|
|
User: url.User("git"),
|
|
Host: "github.com",
|
|
Path: "/hanzoai/forge.git",
|
|
},
|
|
extraMark: 0,
|
|
},
|
|
},
|
|
{
|
|
kase: "ssh://git@[::1]/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "ssh",
|
|
User: url.User("git"),
|
|
Host: "[::1]",
|
|
Path: "/hanzoai/forge.git",
|
|
},
|
|
extraMark: 0,
|
|
},
|
|
},
|
|
{
|
|
kase: "/repositories/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "file",
|
|
Path: "/repositories/hanzoai/forge.git",
|
|
},
|
|
extraMark: 2,
|
|
},
|
|
},
|
|
{
|
|
kase: "file:///repositories/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "file",
|
|
Path: "/repositories/hanzoai/forge.git",
|
|
},
|
|
extraMark: 0,
|
|
},
|
|
},
|
|
{
|
|
kase: "https://github.com/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "https",
|
|
Host: "github.com",
|
|
Path: "/hanzoai/forge.git",
|
|
},
|
|
extraMark: 0,
|
|
},
|
|
},
|
|
{
|
|
kase: "https://git:[email protected]/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "https",
|
|
Host: "github.com",
|
|
User: url.UserPassword("git", "git"),
|
|
Path: "/hanzoai/forge.git",
|
|
},
|
|
extraMark: 0,
|
|
},
|
|
},
|
|
{
|
|
kase: "https://[fe80::14fc:cec5:c174:d88%2510]:20/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "https",
|
|
Host: "[fe80::14fc:cec5:c174:d88%10]:20",
|
|
Path: "/hanzoai/forge.git",
|
|
},
|
|
extraMark: 0,
|
|
},
|
|
},
|
|
|
|
{
|
|
kase: "git://github.com/hanzoai/forge.git",
|
|
expected: &GitURL{
|
|
URL: &url.URL{
|
|
Scheme: "git",
|
|
Host: "github.com",
|
|
Path: "/hanzoai/forge.git",
|
|
},
|
|
extraMark: 0,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, kase := range kases {
|
|
t.Run(kase.kase, func(t *testing.T) {
|
|
u, err := ParseGitURL(kase.kase)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, kase.expected.extraMark, u.extraMark)
|
|
assert.Equal(t, *kase.expected, *u)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseRepositoryURL(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.AppURL, "https://localhost:3000")()
|
|
defer test.MockVariableValue(&setting.SSH.Domain, "try.forge.example")()
|
|
|
|
ctxURL, _ := url.Parse("https://forge")
|
|
ctxReq := &http.Request{URL: ctxURL, Header: http.Header{}}
|
|
ctxReq.Host = ctxURL.Host
|
|
ctxReq.Header.Add("X-Forwarded-Proto", ctxURL.Scheme)
|
|
ctx := context.WithValue(t.Context(), httplib.RequestContextKey, ctxReq)
|
|
cases := []struct {
|
|
input string
|
|
ownerName, repoName, remaining string
|
|
}{
|
|
{input: "/user/repo"},
|
|
|
|
{input: "https://localhost:3000/user/repo", ownerName: "user", repoName: "repo"},
|
|
{input: "https://external:3000/user/repo"},
|
|
|
|
{input: "https://localhost:3000/user/repo.git/other", ownerName: "user", repoName: "repo", remaining: "/other"},
|
|
|
|
{input: "https://forge/user/repo", ownerName: "user", repoName: "repo"},
|
|
{input: "https://forge:3333/user/repo"},
|
|
|
|
{input: "ssh://try.forge.example:2222/user/repo", ownerName: "user", repoName: "repo"},
|
|
{input: "ssh://external:2222/user/repo"},
|
|
|
|
{input: "git+ssh://[email protected]/user/repo.git", ownerName: "user", repoName: "repo"},
|
|
{input: "git+ssh://user@external/user/repo.git"},
|
|
|
|
{input: "[email protected]:user/repo.git", ownerName: "user", repoName: "repo"},
|
|
{input: "root@forge:user/repo.git", ownerName: "user", repoName: "repo"},
|
|
{input: "root@external:user/repo.git"},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.input, func(t *testing.T) {
|
|
ret, _ := ParseRepositoryURL(ctx, c.input)
|
|
assert.Equal(t, c.ownerName, ret.OwnerName)
|
|
assert.Equal(t, c.repoName, ret.RepoName)
|
|
assert.Equal(t, c.remaining, ret.RemainingPath)
|
|
})
|
|
}
|
|
|
|
t.Run("WithSubpath", func(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.AppURL, "https://localhost:3000/subpath")()
|
|
defer test.MockVariableValue(&setting.AppSubURL, "/subpath")()
|
|
cases = []struct {
|
|
input string
|
|
ownerName, repoName, remaining string
|
|
}{
|
|
{input: "https://localhost:3000/user/repo"},
|
|
{input: "https://localhost:3000/subpath/user/repo.git/other", ownerName: "user", repoName: "repo", remaining: "/other"},
|
|
|
|
{input: "ssh://try.forge.example:2222/user/repo", ownerName: "user", repoName: "repo"},
|
|
{input: "ssh://external:2222/user/repo"},
|
|
|
|
{input: "git+ssh://[email protected]/user/repo.git", ownerName: "user", repoName: "repo"},
|
|
{input: "git+ssh://user@external/user/repo.git"},
|
|
|
|
{input: "[email protected]:user/repo.git", ownerName: "user", repoName: "repo"},
|
|
{input: "root@external:user/repo.git"},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.input, func(t *testing.T) {
|
|
ret, _ := ParseRepositoryURL(ctx, c.input)
|
|
assert.Equal(t, c.ownerName, ret.OwnerName)
|
|
assert.Equal(t, c.repoName, ret.RepoName)
|
|
assert.Equal(t, c.remaining, ret.RemainingPath)
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestMakeRepositoryBaseLink(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.AppURL, "https://localhost:3000/subpath")()
|
|
defer test.MockVariableValue(&setting.AppSubURL, "/subpath")()
|
|
|
|
u, err := ParseRepositoryURL(t.Context(), "https://localhost:3000/subpath/user/repo.git")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "/subpath/user/repo", MakeRepositoryWebLink(u))
|
|
|
|
u, err = ParseRepositoryURL(t.Context(), "https://github.com/owner/repo.git")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "https://github.com/owner/repo", MakeRepositoryWebLink(u))
|
|
|
|
u, err = ParseRepositoryURL(t.Context(), "[email protected]:owner/repo.git")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "https://github.com/owner/repo", MakeRepositoryWebLink(u))
|
|
|
|
u, err = ParseRepositoryURL(t.Context(), "git+ssh://other:123/owner/repo.git")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "https://other/owner/repo", MakeRepositoryWebLink(u))
|
|
}
|