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