2
0
mirror of https://github.com/offen/website.git synced 2024-11-22 17:10:29 +01:00

add opt-out mechanism via cookie

This commit is contained in:
Frederik Ring 2019-06-24 22:26:50 +02:00
parent b0c7f69271
commit 53bd8f39b2
3 changed files with 9 additions and 20 deletions

View File

@ -55,6 +55,7 @@ services:
environment: environment:
- SERVER_HOST=http://localhost:8080 - SERVER_HOST=http://localhost:8080
- KMS_HOST=http://localhost:8081 - KMS_HOST=http://localhost:8081
- SCRIPT_HOST=http://localhost:9977
- AUDITORIUM_HOST=http://localhost:9955 - AUDITORIUM_HOST=http://localhost:9955
script: script:
@ -69,7 +70,7 @@ services:
ports: ports:
- 9966:9966 - 9966:9966
environment: environment:
- VAULT_HOST=http://localhost:9977 - VAULT_HOST=https://vault-alpha.offen.dev
auditorium: auditorium:
build: build:

View File

@ -18,9 +18,9 @@ func ContentTypeMiddleware(next http.Handler, contentType string) http.Handler {
}) })
} }
func DoNotTrackMiddleware(next http.Handler) http.Handler { func OptoutMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if dnt := r.Header.Get("DNT"); dnt == "1" { if _, err := r.Cookie("optout"); err == nil {
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
return return
} }

View File

@ -34,14 +34,16 @@ func TestContentTypeMiddleware(t *testing.T) {
}) })
} }
func TestDoNotTrackMiddleware(t *testing.T) { func TestOptoutMiddleware(t *testing.T) {
wrapped := DoNotTrackMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { wrapped := OptoutMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hey there")) w.Write([]byte("hey there"))
})) }))
t.Run("with header", func(t *testing.T) { t.Run("with header", func(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil) r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("DNT", "1") r.AddCookie(&http.Cookie{
Name: "optout",
})
wrapped.ServeHTTP(w, r) wrapped.ServeHTTP(w, r)
if w.Code != http.StatusNoContent { if w.Code != http.StatusNoContent {
@ -61,20 +63,6 @@ func TestDoNotTrackMiddleware(t *testing.T) {
t.Errorf("Unexpected status code %d", w.Code) t.Errorf("Unexpected status code %d", w.Code)
} }
if w.Body.String() != "hey there" {
t.Errorf("Unexpected response body %s", w.Body.String())
}
})
t.Run("with header allowing", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("DNT", "0")
wrapped.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Unexpected status code %d", w.Code)
}
if w.Body.String() != "hey there" { if w.Body.String() != "hey there" {
t.Errorf("Unexpected response body %s", w.Body.String()) t.Errorf("Unexpected response body %s", w.Body.String())
} }