zitadel/internal/api/http/middleware/cache_interceptor_test.go
Livio Amstutz 1c59d18fee
fix: improvements for login and oidc (#227)
* add csrf

* caching

* caching

* caching

* caching

* security headers

* csp and security headers

* error handler csp

* select user with display name

* csp

* user selection styling

* username to loginname

* regenerate grpc

* regenerate

* change to login name
2020-06-17 08:06:40 +02:00

83 lines
1.9 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCache_serializeHeaders(t *testing.T) {
type fields struct {
Cacheability Cacheability
NoCache bool
NoStore bool
MaxAge time.Duration
SharedMaxAge time.Duration
NoTransform bool
Revalidation Revalidation
}
tests := []struct {
name string
fields fields
wantControl string
wantExpires string
wantPragma string
}{
{
"no-store",
fields{
NoStore: true,
},
"no-store",
time.Now().UTC().Add(-1 * time.Hour).Format(http.TimeFormat),
"no-cache",
},
{
"private and max-age",
fields{
Cacheability: CacheabilityPrivate,
MaxAge: 1 * time.Hour,
SharedMaxAge: 1 * time.Hour,
},
"private, max-age=3600",
time.Now().UTC().Add(1 * time.Hour).Format(http.TimeFormat),
"",
},
{
"public, no-cache, proxy-revalidate",
fields{
Cacheability: CacheabilityPublic,
NoCache: true,
Revalidation: RevalidationProxy,
},
"public, max-age=0, no-cache, proxy-revalidate",
time.Now().UTC().Add(-1 * time.Hour).Format(http.TimeFormat),
"no-cache",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
recorder := httptest.NewRecorder()
c := &Cache{
Cacheability: tt.fields.Cacheability,
NoCache: tt.fields.NoCache,
NoStore: tt.fields.NoStore,
MaxAge: tt.fields.MaxAge,
SharedMaxAge: tt.fields.SharedMaxAge,
NoTransform: tt.fields.NoTransform,
Revalidation: tt.fields.Revalidation,
}
c.serializeHeaders(recorder)
cc := recorder.Result().Header.Get("cache-control")
assert.Equal(t, tt.wantControl, cc)
exp := recorder.Result().Header.Get("expires")
assert.Equal(t, tt.wantExpires, exp)
pragma := recorder.Result().Header.Get("pragma")
assert.Equal(t, tt.wantPragma, pragma)
})
}
}