gitea源码

api_repo_file_get_test.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "net/url"
  7. "testing"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/tests"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIGetRawFileOrLFS(t *testing.T) {
  14. defer tests.PrepareTestEnv(t)()
  15. // Test with raw file
  16. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/README.md")
  17. resp := MakeRequest(t, req, http.StatusOK)
  18. assert.Equal(t, "# repo1\n\nDescription for repo1", resp.Body.String())
  19. // Test with LFS
  20. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  21. httpContext := NewAPITestContext(t, "user2", "repo-lfs-test", auth_model.AccessTokenScopeWriteRepository)
  22. doAPICreateRepository(httpContext, false, func(t *testing.T, repository api.Repository) {
  23. u.Path = httpContext.GitPath()
  24. dstPath := t.TempDir()
  25. u.Path = httpContext.GitPath()
  26. u.User = url.UserPassword("user2", userPassword)
  27. t.Run("Clone", doGitClone(dstPath, u))
  28. dstPath2 := t.TempDir()
  29. t.Run("Partial Clone", doPartialGitClone(dstPath2, u))
  30. lfs := lfsCommitAndPushTest(t, dstPath, testFileSizeSmall)[0]
  31. reqLFS := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/"+lfs)
  32. respLFS := MakeRequestNilResponseRecorder(t, reqLFS, http.StatusOK)
  33. assert.Equal(t, testFileSizeSmall, respLFS.Length)
  34. })
  35. })
  36. }