gitea源码

util_test.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package backend
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/modules/setting"
  7. "code.gitea.io/gitea/modules/test"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestToInternalLFSURL(t *testing.T) {
  11. defer test.MockVariableValue(&setting.LocalURL, "http://localurl/")()
  12. defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
  13. cases := []struct {
  14. url string
  15. expected string
  16. }{
  17. {"http://appurl/any", ""},
  18. {"http://appurl/sub/any", ""},
  19. {"http://appurl/sub/owner/repo/any", ""},
  20. {"http://appurl/sub/owner/repo/info/any", ""},
  21. {"http://appurl/sub/owner/repo/info/lfs/any", "http://localurl/api/internal/repo/owner/repo/info/lfs/any"},
  22. }
  23. for _, c := range cases {
  24. assert.Equal(t, c.expected, toInternalLFSURL(c.url), c.url)
  25. }
  26. }
  27. func TestIsInternalLFSURL(t *testing.T) {
  28. defer test.MockVariableValue(&setting.LocalURL, "http://localurl/")()
  29. defer test.MockVariableValue(&setting.InternalToken, "mock-token")()
  30. cases := []struct {
  31. url string
  32. expected bool
  33. }{
  34. {"", false},
  35. {"http://otherurl/api/internal/repo/owner/repo/info/lfs/any", false},
  36. {"http://localurl/api/internal/repo/owner/repo/info/lfs/any", true},
  37. {"http://localurl/api/internal/repo/owner/repo/info", false},
  38. {"http://localurl/api/internal/misc/owner/repo/info/lfs/any", false},
  39. {"http://localurl/api/internal/owner/repo/info/lfs/any", false},
  40. {"http://localurl/api/internal/foo/bar", false},
  41. }
  42. for _, c := range cases {
  43. req := newInternalRequestLFS(t.Context(), c.url, "GET", nil, nil)
  44. assert.Equal(t, c.expected, req != nil, c.url)
  45. assert.Equal(t, c.expected, isInternalLFSURL(c.url), c.url)
  46. }
  47. }