2020-11-07 11:59:33 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"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"))
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(res.Body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "resp", string(b))
|
|
|
|
}
|