From c4c78f6646250cbdfd82005bb856435ea71f0571 Mon Sep 17 00:00:00 2001 From: matst80 Date: Mon, 20 Jul 2026 00:15:42 +0200 Subject: [PATCH] update --- conv/country.go | 21 +++++++++++++++++++++ conv/country_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 conv/country.go create mode 100644 conv/country_test.go diff --git a/conv/country.go b/conv/country.go new file mode 100644 index 0000000..84e62b8 --- /dev/null +++ b/conv/country.go @@ -0,0 +1,21 @@ +package conv + +import "strings" + +// CountryFromHost extracts a country code from a hostname (or URL) by looking +// for known subdomain suffixes. Returns "se" when no country is detected. +// +// This replaces three duplicated implementations across the cart, checkout, +// and tracking services. The detection is deliberately coarse — hostnames +// like "shop-no.example.com" and URLs like "https://shop-no.example.com/page" +// both match. +func CountryFromHost(host string) string { + lower := strings.ToLower(host) + if strings.Contains(lower, "-no") { + return "no" + } + if strings.Contains(lower, "-se") { + return "se" + } + return "se" +} diff --git a/conv/country_test.go b/conv/country_test.go new file mode 100644 index 0000000..f4717ae --- /dev/null +++ b/conv/country_test.go @@ -0,0 +1,29 @@ +package conv + +import "testing" + +func TestCountryFromHost(t *testing.T) { + tests := []struct { + host string + want string + }{ + {"shop-no.example.com", "no"}, + {"SHOP-NO.EXAMPLE.COM", "no"}, + {"shop-se.example.com", "se"}, + {"shop-se", "se"}, + {"shop-no", "no"}, + {"example.com", "se"}, + {"", "se"}, + {"localhost", "se"}, + {"https://shop-no.example.com/page", "no"}, + {"https://shop-se.example.com", "se"}, + {"-no-", "no"}, + {"nose.example.com", "se"}, // no "-no" substring — "nose" has no hyphen + } + for _, tt := range tests { + got := CountryFromHost(tt.host) + if got != tt.want { + t.Errorf("CountryFromHost(%q) = %q, want %q", tt.host, got, tt.want) + } + } +}