Path: blob/main/components/ws-proxy/pkg/proxy/cookies_test.go
2500 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package proxy56import (7"net/http"8"net/http/httptest"9"testing"1011"github.com/google/go-cmp/cmp"12)1314// TestReadCookies gives more confidence that our clone&own version of readCookies produces15// at least a subset of what the stdlib version provides.16func TestReadCookies(t *testing.T) {17var (18domain = "test-domain.com"19sessionCookie = &http.Cookie{Domain: domain, Name: "_test_domain_com_", Value: "fobar"}20portAuthCookie = &http.Cookie{Domain: domain, Name: "_test_domain_com_ws_77f6b236_3456_4b88_8284_81ca543a9d65_port_auth_", Value: "some-token"}21ownerCookie = &http.Cookie{Domain: domain, Name: "_test_domain_com_ws_77f6b236_3456_4b88_8284_81ca543a9d65_owner_", Value: "some-other-token"}22miscCookie = &http.Cookie{Domain: domain, Name: "some-other-cookie", Value: "I like cookies"}23)24tests := []struct {25Name string26Input []*http.Cookie27}{28{"no cookies", []*http.Cookie{}},29{"session cookie", []*http.Cookie{sessionCookie, miscCookie}},30{"portAuth cookie", []*http.Cookie{portAuthCookie, miscCookie}},31{"owner cookie", []*http.Cookie{ownerCookie, miscCookie}},32{"misc cookie", []*http.Cookie{miscCookie}},33}34for _, test := range tests {35t.Run(test.Name, func(t *testing.T) {36req := httptest.NewRequest("GET", "http://"+domain, nil)37for _, c := range test.Input {38req.AddCookie(c)39}4041us := readCookies(req.Header, "")42them := req.Cookies()4344if diff := cmp.Diff(them, us); diff != "" {45t.Errorf("unexpected result (-want +got):\n%s", diff)46}47})48}49}505152