Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/proxy/plugins/analytics/analytics_test.go
2500 views
1
// Copyright (c) 2023 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 analytics
6
7
import (
8
"encoding/base64"
9
"net/http"
10
"net/http/httptest"
11
"strings"
12
"testing"
13
)
14
15
type mockHandler struct{}
16
17
func (m mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
18
w.Write([]byte("mock handler response"))
19
return nil
20
}
21
22
func TestServeHTTP(t *testing.T) {
23
tests := []struct {
24
name string
25
trustedSegmentKey string
26
untrustedSegmentKey string
27
providedSegmentKey string
28
expectedResponseBodyPrefix string
29
}{
30
{
31
name: "trusted segment key",
32
trustedSegmentKey: "trusted-key",
33
untrustedSegmentKey: "untrusted-key",
34
providedSegmentKey: "trusted-key",
35
expectedResponseBodyPrefix: "trusted",
36
},
37
{
38
name: "untrusted dummy segment key",
39
trustedSegmentKey: "trusted-key",
40
untrustedSegmentKey: "untrusted-key",
41
providedSegmentKey: dummyUntrustedSegmentKey,
42
expectedResponseBodyPrefix: "untrusted",
43
},
44
{
45
name: "untrusted segment key",
46
trustedSegmentKey: "trusted-key",
47
untrustedSegmentKey: "untrusted-key",
48
providedSegmentKey: "untrusted-key",
49
// on purpose to ensure that cliens remove references to untrusted keys
50
expectedResponseBodyPrefix: "mock",
51
},
52
{
53
name: "empty segment key",
54
trustedSegmentKey: "trusted-key",
55
untrustedSegmentKey: "untrusted-key",
56
providedSegmentKey: "",
57
expectedResponseBodyPrefix: "untrusted",
58
},
59
{
60
name: "no match",
61
trustedSegmentKey: "trusted-key",
62
untrustedSegmentKey: "untrusted-key",
63
providedSegmentKey: "other-key",
64
expectedResponseBodyPrefix: "mock",
65
},
66
{
67
name: "both keys empty",
68
trustedSegmentKey: "",
69
untrustedSegmentKey: "",
70
providedSegmentKey: "",
71
expectedResponseBodyPrefix: "mock",
72
},
73
{
74
name: "only trusted key empty",
75
trustedSegmentKey: "",
76
untrustedSegmentKey: "untrusted-key",
77
providedSegmentKey: dummyUntrustedSegmentKey,
78
expectedResponseBodyPrefix: "untrusted",
79
},
80
{
81
name: "only untrusted key empty",
82
trustedSegmentKey: "trusted-key",
83
untrustedSegmentKey: "",
84
providedSegmentKey: "",
85
expectedResponseBodyPrefix: "mock",
86
},
87
{
88
name: "both keys empty, provided key not empty",
89
trustedSegmentKey: "",
90
untrustedSegmentKey: "",
91
providedSegmentKey: "other-key",
92
expectedResponseBodyPrefix: "mock",
93
},
94
}
95
96
for _, tt := range tests {
97
t.Run(tt.name, func(t *testing.T) {
98
a := &Analytics{
99
trustedSegmentKey: tt.trustedSegmentKey,
100
untrustedSegmentKey: tt.untrustedSegmentKey,
101
// Configure segmentProxy to return different responses for trusted and untrusted.
102
segmentProxy: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
103
if r.Header.Get("Authorization") == "Basic "+base64.StdEncoding.EncodeToString([]byte(tt.trustedSegmentKey+":")) {
104
w.Write([]byte("trusted proxy response"))
105
} else {
106
w.Write([]byte("untrusted proxy response"))
107
}
108
}),
109
}
110
111
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
112
if tt.providedSegmentKey != "" {
113
req.SetBasicAuth(tt.providedSegmentKey, "")
114
}
115
rec := httptest.NewRecorder()
116
117
err := a.ServeHTTP(rec, req, mockHandler{})
118
119
if err != nil {
120
t.Errorf("ServeHTTP() failed with error: %v", err)
121
}
122
if rec.Code != http.StatusOK {
123
t.Errorf("ServeHTTP() returned status %d, expected %d", rec.Code, http.StatusOK)
124
}
125
if !strings.Contains(rec.Body.String(), tt.expectedResponseBodyPrefix) {
126
t.Errorf("ServeHTTP() response body doesn't contain expected prefix. Got: %s, expected prefix: %s", rec.Body.String(), tt.expectedResponseBodyPrefix)
127
}
128
})
129
}
130
}
131
132