package referer_test import ( "testing" "github.com/stretchr/testify/assert" "sneak.berlin/go/webhooker/internal/referer" ) // entrypointUUID is the secret part of an entrypoint URL; no case may // return it. const entrypointUUID = "0b6f7d3e-6a39-4e3c-9f0a-2c4d8e1b5a77" // redacted is what SchemeAndHost returns for a Referer it cannot // reduce to a scheme and host. const redacted = "(redacted)" func TestSchemeAndHost(t *testing.T) { t.Parallel() for _, tc := range []struct { name string referer string want string }{ { name: "no Referer", referer: "", want: "", }, { name: "entrypoint URL with a query", referer: "http://webhooker.example/h/" + entrypointUUID + "?src=ci", want: "http://webhooker.example", }, { name: "host with a port", referer: "https://webhooker.example:8443/h/" + entrypointUUID, want: "https://webhooker.example:8443", }, { name: "path without a scheme or host", referer: "/h/" + entrypointUUID, want: redacted, }, { name: "host without a scheme", referer: "webhooker.example/h/" + entrypointUUID, want: redacted, }, { name: "not a URL", referer: "http://[::1/h/" + entrypointUUID, want: redacted, }, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() got := referer.SchemeAndHost(tc.referer) assert.Equal(t, tc.want, got) assert.NotContains(t, got, entrypointUUID) }) } }