Skip to content

Commit 3082d7d

Browse files
committed
add more tests
1 parent ae81052 commit 3082d7d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

modules/httplib/request.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"net/url"
1616
"strings"
1717
"time"
18+
19+
"github.com/pkg/errors"
1820
)
1921

2022
var defaultSetting = Settings{"GiteaServer", 60 * time.Second, 60 * time.Second, nil, nil}
@@ -101,6 +103,9 @@ func (r *Request) Param(key, value string) *Request {
101103

102104
// Body adds request raw body. It supports string, []byte and io.Reader as body.
103105
func (r *Request) Body(data any) *Request {
106+
if r == nil {
107+
return nil
108+
}
104109
switch t := data.(type) {
105110
case nil: // do nothing
106111
case string:
@@ -193,6 +198,9 @@ func (r *Request) getResponse() (*http.Response, error) {
193198
// Response executes request client gets response manually.
194199
// Caller MUST close the response body if no error occurs
195200
func (r *Request) Response() (*http.Response, error) {
201+
if r == nil {
202+
return nil, errors.New("invalid request")
203+
}
196204
return r.getResponse()
197205
}
198206

modules/lfstransfer/backend/util.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11+
"net/url"
1112
"strings"
1213

1314
"code.gitea.io/gitea/modules/httplib"
1415
"code.gitea.io/gitea/modules/private"
1516
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/modules/util"
1618

1719
"github.com/charmbracelet/git-lfs-transfer/transfer"
1820
)
@@ -105,7 +107,30 @@ func toInternalLFSURL(s string) string {
105107
return setting.LocalURL + "api/internal/repo/" + routePath
106108
}
107109

110+
func isInternalLFSURL(s string) bool {
111+
if !strings.HasPrefix(s, setting.LocalURL) {
112+
return false
113+
}
114+
u, err := url.Parse(s)
115+
if err != nil {
116+
return false
117+
}
118+
routePath := util.PathJoinRelX(u.Path)
119+
subRoutePath, cut := strings.CutPrefix(routePath, "api/internal/repo/")
120+
if !cut {
121+
return false
122+
}
123+
fields := strings.SplitN(subRoutePath, "/", 3)
124+
if len(fields) < 3 || !strings.HasPrefix(fields[2], "info/lfs") {
125+
return false
126+
}
127+
return true
128+
}
129+
108130
func newInternalRequestLFS(ctx context.Context, internalURL, method string, headers map[string]string, body any) *httplib.Request {
131+
if !isInternalLFSURL(internalURL) {
132+
return nil
133+
}
109134
req := private.NewInternalRequest(ctx, internalURL, method)
110135
for k, v := range headers {
111136
req.Header(k, v)

modules/lfstransfer/backend/util_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,24 @@ func TestToInternalLFSURL(t *testing.T) {
2929
assert.Equal(t, c.expected, toInternalLFSURL(c.url), c.url)
3030
}
3131
}
32+
33+
func TestIsInternalLFSURL(t *testing.T) {
34+
defer test.MockVariableValue(&setting.LocalURL, "http://localurl/")()
35+
cases := []struct {
36+
url string
37+
expected bool
38+
}{
39+
{"", false},
40+
{"http://otherurl/api/internal/repo/owner/repo/info/lfs/any", false},
41+
{"http://localurl/api/internal/repo/owner/repo/info/lfs/any", true},
42+
{"http://localurl/api/internal/repo/owner/repo/info", false},
43+
{"http://localurl/api/internal/misc/owner/repo/info/lfs/any", false},
44+
{"http://localurl/api/internal/owner/repo/info/lfs/any", false},
45+
{"http://localurl/api/internal/foo/bar", false},
46+
}
47+
for _, c := range cases {
48+
req := newInternalRequestLFS(t.Context(), c.url, "GET", nil, nil)
49+
assert.Equal(t, c.expected, req != nil, c.url)
50+
assert.Equal(t, c.expected, isInternalLFSURL(c.url), c.url)
51+
}
52+
}

0 commit comments

Comments
 (0)