Files
probo/pkg/server/api/complianceportal/v1/seo_test.go
Émile Ré 52310dbf65 Read portal SEO origin from request context
SNI middleware already stores scheme://host; SEOFromRequest
no longer takes a base URL or re-strips the path. Expose
context helpers and drop the obsolete strip tests.

Signed-off-by: Émile Ré <emile@probo.com>
2026-07-22 12:19:25 +02:00

104 lines
3.0 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package complianceportal_v1_test
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.probo.inc/probo/pkg/server/api/complianceportal"
complianceportal_v1 "go.probo.inc/probo/pkg/server/api/complianceportal/v1"
)
func requestWithPortalOrigin(t *testing.T, rawURL, origin string) *http.Request {
t.Helper()
req, err := http.NewRequest(http.MethodGet, rawURL, nil)
require.NoError(t, err)
if origin != "" {
req = req.WithContext(
complianceportal.ContextWithCompliancePortalBaseURL(req.Context(), origin),
)
}
return req
}
func TestSEOFromRequest(t *testing.T) {
t.Parallel()
req := requestWithPortalOrigin(
t,
"https://acme.probopage.localhost/fr/documents",
"https://acme.probopage.localhost",
)
lang, canonical, hreflang := complianceportal_v1.SEOFromRequest(req)
assert.Equal(t, "fr", lang)
assert.Equal(t, "https://acme.probopage.localhost/fr/documents", canonical)
require.NotEmpty(t, hreflang)
var (
xDefault string
enHref string
)
for _, link := range hreflang {
if link.Lang == "x-default" {
xDefault = link.Href
}
if link.Lang == "en" {
enHref = link.Href
}
}
assert.Equal(t, "https://acme.probopage.localhost/en/documents", enHref)
assert.Equal(t, enHref, xDefault)
}
func TestSEOFromRequest_EscapesPathSegments(t *testing.T) {
t.Parallel()
req := requestWithPortalOrigin(
t,
"https://acme.probopage.localhost/en/docs/foo%20bar",
"https://acme.probopage.localhost",
)
_, canonical, _ := complianceportal_v1.SEOFromRequest(req)
assert.Equal(t, "https://acme.probopage.localhost/en/docs/foo%20bar", canonical)
}
func TestSEOFromRequest_MissingOriginOmitsLinks(t *testing.T) {
t.Parallel()
req := requestWithPortalOrigin(t, "https://trust.acme.com/fr/documents", "")
lang, canonical, hreflang := complianceportal_v1.SEOFromRequest(req)
assert.Equal(t, "fr", lang)
assert.Empty(t, canonical)
assert.Nil(t, hreflang)
}