2021-04-12 14:49:26 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-04-12 14:49:26 +00:00
|
|
|
|
|
|
|
package httpcache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
"strings"
|
2021-04-12 14:49:26 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-20 17:46:10 +00:00
|
|
|
type mockFileInfo struct{}
|
2021-04-12 14:49:26 +00:00
|
|
|
|
|
|
|
func (m mockFileInfo) Name() string { return "gitea.test" }
|
|
|
|
func (m mockFileInfo) Size() int64 { return int64(10) }
|
|
|
|
func (m mockFileInfo) Mode() os.FileMode { return os.ModePerm }
|
|
|
|
func (m mockFileInfo) ModTime() time.Time { return time.Time{} }
|
|
|
|
func (m mockFileInfo) IsDir() bool { return false }
|
|
|
|
func (m mockFileInfo) Sys() interface{} { return nil }
|
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
func countFormalHeaders(h http.Header) (c int) {
|
|
|
|
for k := range h {
|
|
|
|
// ignore our headers for internal usage
|
|
|
|
if strings.HasPrefix(k, "X-Gitea-") {
|
|
|
|
continue
|
|
|
|
}
|
2023-01-14 07:16:30 +00:00
|
|
|
if strings.HasPrefix(k, "X-Forgejo-") {
|
|
|
|
continue
|
|
|
|
}
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
c++
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-04-12 14:49:26 +00:00
|
|
|
func TestHandleFileETagCache(t *testing.T) {
|
|
|
|
fi := mockFileInfo{}
|
|
|
|
etag := `"MTBnaXRlYS50ZXN0TW9uLCAwMSBKYW4gMDAwMSAwMDowMDowMCBHTVQ="`
|
|
|
|
|
|
|
|
t.Run("No_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handled := HandleFileETagCache(req, w, fi)
|
|
|
|
|
|
|
|
assert.False(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 2, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Cache-Control")
|
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
})
|
|
|
|
t.Run("Wrong_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req.Header.Set("If-None-Match", `"wrong etag"`)
|
|
|
|
|
|
|
|
handled := HandleFileETagCache(req, w, fi)
|
|
|
|
|
|
|
|
assert.False(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 2, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Cache-Control")
|
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
})
|
|
|
|
t.Run("Correct_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req.Header.Set("If-None-Match", etag)
|
|
|
|
|
|
|
|
handled := HandleFileETagCache(req, w, fi)
|
|
|
|
|
|
|
|
assert.True(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 1, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
assert.Equal(t, http.StatusNotModified, w.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleGenericETagCache(t *testing.T) {
|
|
|
|
etag := `"test"`
|
|
|
|
|
|
|
|
t.Run("No_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handled := HandleGenericETagCache(req, w, etag)
|
|
|
|
|
|
|
|
assert.False(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 2, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Cache-Control")
|
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
})
|
|
|
|
t.Run("Wrong_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req.Header.Set("If-None-Match", `"wrong etag"`)
|
|
|
|
|
|
|
|
handled := HandleGenericETagCache(req, w, etag)
|
|
|
|
|
|
|
|
assert.False(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 2, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Cache-Control")
|
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
})
|
|
|
|
t.Run("Correct_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req.Header.Set("If-None-Match", etag)
|
|
|
|
|
|
|
|
handled := HandleGenericETagCache(req, w, etag)
|
|
|
|
|
|
|
|
assert.True(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 1, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
assert.Equal(t, http.StatusNotModified, w.Code)
|
|
|
|
})
|
|
|
|
t.Run("Multiple_Wrong_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req.Header.Set("If-None-Match", `"wrong etag", "wrong etag "`)
|
|
|
|
|
|
|
|
handled := HandleGenericETagCache(req, w, etag)
|
|
|
|
|
|
|
|
assert.False(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 2, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Cache-Control")
|
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
})
|
|
|
|
t.Run("Multiple_Correct_If-None-Match", func(t *testing.T) {
|
|
|
|
req := &http.Request{Header: make(http.Header)}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req.Header.Set("If-None-Match", `"wrong etag", `+etag)
|
|
|
|
|
|
|
|
handled := HandleGenericETagCache(req, w, etag)
|
|
|
|
|
|
|
|
assert.True(t, handled)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
assert.Equal(t, 1, countFormalHeaders(w.Header()))
|
2021-04-12 14:49:26 +00:00
|
|
|
assert.Contains(t, w.Header(), "Etag")
|
|
|
|
assert.Equal(t, etag, w.Header().Get("Etag"))
|
|
|
|
assert.Equal(t, http.StatusNotModified, w.Code)
|
|
|
|
})
|
|
|
|
}
|