add TestLFSSupport

This commit is contained in:
6543 2022-07-24 17:43:34 +02:00
parent 7a7cbac171
commit aa83b307db
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE
2 changed files with 22 additions and 2 deletions

View file

@ -103,6 +103,19 @@ func TestFollowSymlink(t *testing.T) {
assert.EqualValues(t, "abc\n", string(body)) assert.EqualValues(t, "abc\n", string(body))
} }
func TestLFSSupport(t *testing.T) {
log.Printf("=== TestLFSSupport ===\n")
resp, err := getTestHTTPSClient().Get("https://6543.localhost.mock.directory:4430/tests_for_pages-server/@main/lfs.txt")
assert.NoError(t, err)
if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) {
t.FailNow()
}
body := strings.TrimSpace(string(getBytes(resp.Body)))
assert.EqualValues(t, 12, len(body))
assert.EqualValues(t, "actual value", body)
}
func getTestHTTPSClient() *http.Client { func getTestHTTPSClient() *http.Client {
cookieJar, _ := cookiejar.New(nil) cookieJar, _ := cookiejar.New(nil)
return &http.Client{ return &http.Client{

View file

@ -27,6 +27,7 @@ type Client struct {
contentTimeout time.Duration contentTimeout time.Duration
followSymlinks bool followSymlinks bool
supportLFS bool
} }
// TODO: once golang v1.19 is min requirement, we can switch to 'JoinPath()' of 'net/url' package // TODO: once golang v1.19 is min requirement, we can switch to 'JoinPath()' of 'net/url' package
@ -55,6 +56,7 @@ func NewClient(giteaRoot, giteaAPIToken string) (*Client, error) {
fastClient: getFastHTTPClient(), fastClient: getFastHTTPClient(),
followSymlinks: true, followSymlinks: true,
supportLFS: true,
}, err }, err
} }
@ -67,8 +69,13 @@ func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource str
} }
func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (*fasthttp.Response, error) { func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (*fasthttp.Response, error) {
url := joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)) var apiURL string
resp, err := client.do(client.contentTimeout, url) if client.supportLFS {
apiURL = joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "media", resource+"?ref="+url.QueryEscape(ref))
} else {
apiURL = joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref))
}
resp, err := client.do(client.contentTimeout, apiURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }