mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-01 04:54:09 +00:00
ec8f162b33
(cherry picked from commit0a3388f93f
) (cherry picked from commit7eba0a440a
) (cherry picked from commiteb9646c7ef
) (cherry picked from commitf1972578f5
) Conflicts: (cherry picked from commit7f96222fb4
) (cherry picked from commite3c7c9fe7b
) (cherry picked from commit84fdead902
) (cherry picked from commit85148e1196
) (cherry picked from commitc0086bd70d
) (cherry picked from commitd1e31ef318
) (cherry picked from commit681d3ed5c4
) (cherry picked from commit76a3001f5b
) (cherry picked from commita55a9567d3
) (cherry picked from commitaa7adc167d
) (cherry picked from commitd5354cb52c
) (cherry picked from commit472c489996
) (cherry picked from commitdc816d065b
) (cherry picked from commit4795f9ea85
) (cherry picked from commitddd4ae5343
) (cherry picked from commit0e95f2a36b
) (cherry picked from commit47a41a4e7b
) (cherry picked from commitd4416fcd3e
) (cherry picked from commitf279d153b6
) (cherry picked from commit959f908ffd
) (cherry picked from commit82df953101
) (cherry picked from commit8f8c9fd9e3
) (cherry picked from commitd4a0db7706
) (cherry picked from commit44594d6239
) (cherry picked from commit62b1de579e
) (cherry picked from commit507abee353
) (cherry picked from commit8c36ac42c7
) (cherry picked from commit72f74067f4
) (cherry picked from commit8e5a9e187b
) (cherry picked from commitcff8f43c7e
) (cherry picked from commit493cc93e6d
) (cherry picked from commitc94352b4d4
) (cherry picked from commit3f622c51a7
) (cherry picked from commit84c62ad447
) (cherry picked from commitba186c8ee4
) (cherry picked from commit4238ef291d
) (cherry picked from commit3ef1bd61b9
) (cherry picked from commitf304086bb6
) (cherry picked from commit64a2edabcb
) (cherry picked from commit6accea6ba7
) (cherry picked from commit2296e93a42
) (cherry picked from commit2bf0be3b1b
) (cherry picked from commitfb4652b197
) (cherry picked from commit7d462cf353
) (cherry picked from commitf5319660dd
)
101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package httpcache
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func countFormalHeaders(h http.Header) (c int) {
|
|
for k := range h {
|
|
// ignore our headers for internal usage
|
|
if strings.HasPrefix(k, "X-Gitea-") {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(k, "X-Forgejo-") {
|
|
continue
|
|
}
|
|
c++
|
|
}
|
|
return c
|
|
}
|
|
|
|
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)
|
|
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"))
|
|
})
|
|
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)
|
|
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"))
|
|
})
|
|
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)
|
|
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)
|
|
})
|
|
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)
|
|
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"))
|
|
})
|
|
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)
|
|
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)
|
|
})
|
|
}
|