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