middleware/no_cache_test.go

44 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-11-07 11:59:33 +00:00
package middleware
import (
2023-12-25 00:47:12 +00:00
"io"
2020-11-07 11:59:33 +00:00
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNoCache(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "", r.Header.Get("ETag"))
_, err := w.Write([]byte("resp"))
require.NoError(t, err)
})
server := httptest.NewServer(NoCache(handler))
defer server.Close()
client := http.Client{}
2023-06-25 20:10:30 +00:00
req, err := http.NewRequest("GET", server.URL, http.NoBody)
2020-11-07 11:59:33 +00:00
require.Nil(t, err)
req.Header.Set("ETag", "ETagValue")
res, err := client.Do(req)
require.Nil(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, time.Unix(0, 0).Format(time.RFC1123), res.Header.Get("Expires"))
assert.Equal(t, "no-cache, no-store, no-transform, must-revalidate, private, max-age=0", res.Header.Get("Cache-Control"))
assert.Equal(t, "no-cache", res.Header.Get("Pragma"))
assert.Equal(t, "0", res.Header.Get("X-Accel-Expires"))
2023-12-25 00:47:12 +00:00
b, err := io.ReadAll(res.Body)
2020-11-07 11:59:33 +00:00
assert.NoError(t, err)
assert.Equal(t, "resp", string(b))
}