diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2021-10-06 07:25:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-06 01:25:46 +0200 |
commit | f0ba87fda88c7bb601eee17f3e3a72bea8a71521 (patch) | |
tree | cb5caf3a22fceaec8d2d421bec0cb094ecdb9c53 /modules/httpcache/httpcache_test.go | |
parent | 48c2578bd8ce4ddd90bf926bd40388f57c90fe14 (diff) | |
download | gitea-f0ba87fda88c7bb601eee17f3e3a72bea8a71521.tar.gz gitea-f0ba87fda88c7bb601eee17f3e3a72bea8a71521.zip |
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.
Diffstat (limited to 'modules/httpcache/httpcache_test.go')
-rw-r--r-- | modules/httpcache/httpcache_test.go | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/modules/httpcache/httpcache_test.go b/modules/httpcache/httpcache_test.go index fe5ca17956..68ac892c91 100644 --- a/modules/httpcache/httpcache_test.go +++ b/modules/httpcache/httpcache_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "os" + "strings" "testing" "time" @@ -24,6 +25,17 @@ func (m mockFileInfo) ModTime() time.Time { return time.Time{} } func (m mockFileInfo) IsDir() bool { return false } func (m mockFileInfo) Sys() interface{} { return nil } +func countFormalHeaders(h http.Header) (c int) { + for k := range h { + // ignore our headers for internal usage + if strings.HasPrefix(k, "X-Gitea-") { + continue + } + c++ + } + return c +} + func TestHandleFileETagCache(t *testing.T) { fi := mockFileInfo{} etag := `"MTBnaXRlYS50ZXN0TW9uLCAwMSBKYW4gMDAwMSAwMDowMDowMCBHTVQ="` @@ -35,7 +47,7 @@ func TestHandleFileETagCache(t *testing.T) { handled := HandleFileETagCache(req, w, fi) assert.False(t, handled) - assert.Len(t, w.Header(), 2) + assert.Equal(t, 2, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Cache-Control") assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) @@ -49,7 +61,7 @@ func TestHandleFileETagCache(t *testing.T) { handled := HandleFileETagCache(req, w, fi) assert.False(t, handled) - assert.Len(t, w.Header(), 2) + assert.Equal(t, 2, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Cache-Control") assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) @@ -63,7 +75,7 @@ func TestHandleFileETagCache(t *testing.T) { handled := HandleFileETagCache(req, w, fi) assert.True(t, handled) - assert.Len(t, w.Header(), 1) + assert.Equal(t, 1, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) assert.Equal(t, http.StatusNotModified, w.Code) @@ -80,7 +92,7 @@ func TestHandleGenericETagCache(t *testing.T) { handled := HandleGenericETagCache(req, w, etag) assert.False(t, handled) - assert.Len(t, w.Header(), 2) + assert.Equal(t, 2, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Cache-Control") assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) @@ -94,7 +106,7 @@ func TestHandleGenericETagCache(t *testing.T) { handled := HandleGenericETagCache(req, w, etag) assert.False(t, handled) - assert.Len(t, w.Header(), 2) + assert.Equal(t, 2, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Cache-Control") assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) @@ -108,7 +120,7 @@ func TestHandleGenericETagCache(t *testing.T) { handled := HandleGenericETagCache(req, w, etag) assert.True(t, handled) - assert.Len(t, w.Header(), 1) + assert.Equal(t, 1, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) assert.Equal(t, http.StatusNotModified, w.Code) @@ -122,7 +134,7 @@ func TestHandleGenericETagCache(t *testing.T) { handled := HandleGenericETagCache(req, w, etag) assert.False(t, handled) - assert.Len(t, w.Header(), 2) + assert.Equal(t, 2, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Cache-Control") assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) @@ -136,7 +148,7 @@ func TestHandleGenericETagCache(t *testing.T) { handled := HandleGenericETagCache(req, w, etag) assert.True(t, handled) - assert.Len(t, w.Header(), 1) + assert.Equal(t, 1, countFormalHeaders(w.Header())) assert.Contains(t, w.Header(), "Etag") assert.Equal(t, etag, w.Header().Get("Etag")) assert.Equal(t, http.StatusNotModified, w.Code) |