Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Schneider committed Mar 16, 2021
1 parent 88a44a6 commit 8771aba
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
34 changes: 34 additions & 0 deletions server/cookies_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package server

import (
"net/http"
"reflect"
"testing"
)

func TestCookies_StripSecureCookies(t *testing.T) {
header := http.Header{}

header.Add(setCookieHeader, "name=1; HttpOnly")
header.Add(setCookieHeader, "name=2; Path=path; Secure; HttpOnly")
header.Add(setCookieHeader, "name=2; Path=path; Secure; HttpOnly;")
header.Add(setCookieHeader, "name=3; Path=path; HttpOnly; Secure;")
header.Add(setCookieHeader, "name=4; Path=path; HttpOnly; secure")
header.Add(setCookieHeader, "name=secure; Path=path; HttpOnly")
header.Add(setCookieHeader, "name=Secure; Path=path; HttpOnly;")

stripSecureCookies(header)

exp := http.Header{}
exp.Add(setCookieHeader, "name=1; HttpOnly")
exp.Add(setCookieHeader, "name=2; Path=path; HttpOnly")
exp.Add(setCookieHeader, "name=2; Path=path; HttpOnly")
exp.Add(setCookieHeader, "name=3; Path=path; HttpOnly")
exp.Add(setCookieHeader, "name=4; Path=path; HttpOnly")
exp.Add(setCookieHeader, "name=secure; Path=path; HttpOnly")
exp.Add(setCookieHeader, "name=Secure; Path=path; HttpOnly;")

if !reflect.DeepEqual(header, exp) {
t.Errorf("Expected \n'%v', got: \n'%v'", exp, header)
}
}
55 changes: 55 additions & 0 deletions server/cookies_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package server_test

import (
"net"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/avenga/couper/internal/test"
)

func TestCookies_IntegrationStrip(t *testing.T) {
helper := test.New(t)
seenCh := make(chan struct{})

origin := httptest.NewUnstartedServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Set-Cookie", "n=v; Path=path; Secure")
rw.WriteHeader(http.StatusOK)

go func() {
seenCh <- struct{}{}
}()
}))
ln, err := net.Listen("tcp4", testProxyAddr[7:])
helper.Must(err)
origin.Listener = ln
origin.Start()
defer func() {
origin.Close()
ln.Close()
time.Sleep(time.Second)
}()

confPath := "testdata/settings/01_couper.hcl"
shutdown, _ := newCouper(confPath, test.New(t))
defer shutdown()

req, err := http.NewRequest(http.MethodGet, "http://anyserver:8080", nil)
helper.Must(err)

res, err := newClient().Do(req)
helper.Must(err)

if v := res.Header.Get("Set-Cookie"); v != "n=v; Path=path" {
t.Errorf("Unexpected Set-Cookie header given: %s", v)
}

timer := time.NewTimer(time.Second)
select {
case <-timer.C:
t.Error("Origin request failed")
case <-seenCh:
}
}
15 changes: 15 additions & 0 deletions server/testdata/settings/01_couper.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
server "settings" {
api {
endpoint "/" {
proxy {
backend {
origin = "http://example.com"
}
}
}
}
}

settings {
secure_cookies = "strip"
}

0 comments on commit 8771aba

Please sign in to comment.