gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/test"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func TestGitSmartHTTP(t *testing.T) {
  16. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  17. testGitSmartHTTP(t, u)
  18. testRenamedRepoRedirect(t)
  19. })
  20. }
  21. func testGitSmartHTTP(t *testing.T, u *url.URL) {
  22. kases := []struct {
  23. method, path string
  24. code int
  25. }{
  26. {
  27. path: "user2/repo1/info/refs",
  28. code: http.StatusOK,
  29. },
  30. {
  31. method: "HEAD",
  32. path: "user2/repo1/info/refs",
  33. code: http.StatusOK,
  34. },
  35. {
  36. path: "user2/repo1/HEAD",
  37. code: http.StatusOK,
  38. },
  39. {
  40. path: "user2/repo1/objects/info/alternates",
  41. code: http.StatusNotFound,
  42. },
  43. {
  44. path: "user2/repo1/objects/info/http-alternates",
  45. code: http.StatusNotFound,
  46. },
  47. {
  48. path: "user2/repo1/../../custom/conf/app.ini",
  49. code: http.StatusNotFound,
  50. },
  51. {
  52. path: "user2/repo1/objects/info/../../../../custom/conf/app.ini",
  53. code: http.StatusNotFound,
  54. },
  55. {
  56. path: `user2/repo1/objects/info/..\..\..\..\custom\conf\app.ini`,
  57. code: http.StatusBadRequest,
  58. },
  59. }
  60. for _, kase := range kases {
  61. t.Run(kase.path, func(t *testing.T) {
  62. req, err := http.NewRequest(util.IfZero(kase.method, "GET"), u.String()+kase.path, nil)
  63. require.NoError(t, err)
  64. req.SetBasicAuth("user2", userPassword)
  65. resp, err := http.DefaultClient.Do(req)
  66. require.NoError(t, err)
  67. defer resp.Body.Close()
  68. assert.Equal(t, kase.code, resp.StatusCode)
  69. _, err = io.ReadAll(resp.Body)
  70. require.NoError(t, err)
  71. })
  72. }
  73. }
  74. func testRenamedRepoRedirect(t *testing.T) {
  75. defer test.MockVariableValue(&setting.Service.RequireSignInViewStrict, true)()
  76. // git client requires to get a 301 redirect response before 401 unauthorized response
  77. req := NewRequest(t, "GET", "/user2/oldrepo1/info/refs")
  78. resp := MakeRequest(t, req, http.StatusMovedPermanently)
  79. redirect := resp.Header().Get("Location")
  80. assert.Equal(t, "/user2/repo1/info/refs", redirect)
  81. req = NewRequest(t, "GET", redirect)
  82. resp = MakeRequest(t, req, http.StatusUnauthorized)
  83. assert.Equal(t, "Unauthorized\n", resp.Body.String())
  84. req = NewRequest(t, "GET", redirect).AddBasicAuth("user2")
  85. resp = MakeRequest(t, req, http.StatusOK)
  86. assert.Contains(t, resp.Body.String(), "65f1bf27bc3bf70f64657658635e66094edbcb4d\trefs/tags/v1.1")
  87. }