update cart
This commit is contained in:
286
main_test.go
Normal file
286
main_test.go
Normal file
@@ -0,0 +1,286 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetCountryFromHost(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
host string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "Norwegian host",
|
||||
host: "s10n-no.tornberg.me",
|
||||
expected: "no",
|
||||
},
|
||||
{
|
||||
name: "Swedish host",
|
||||
host: "s10n-se.tornberg.me",
|
||||
expected: "se",
|
||||
},
|
||||
{
|
||||
name: "Host with -no in the middle",
|
||||
host: "api-no-staging.tornberg.me",
|
||||
expected: "no",
|
||||
},
|
||||
{
|
||||
name: "Host without country suffix",
|
||||
host: "s10n.tornberg.me",
|
||||
expected: "se",
|
||||
},
|
||||
{
|
||||
name: "Host with different domain",
|
||||
host: "example-no.com",
|
||||
expected: "no",
|
||||
},
|
||||
{
|
||||
name: "Empty host",
|
||||
host: "",
|
||||
expected: "se",
|
||||
},
|
||||
{
|
||||
name: "Host with uppercase",
|
||||
host: "S10N-NO.TORNBERG.ME",
|
||||
expected: "no",
|
||||
},
|
||||
{
|
||||
name: "Host with mixed case",
|
||||
host: "S10n-No.Tornberg.Me",
|
||||
expected: "no",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := getCountryFromHost(tt.host)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getCountryFromHost(%q) = %q, want %q", tt.host, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCheckoutOrder(t *testing.T) {
|
||||
// Save original environment variable and restore after test
|
||||
originalCartBaseUrl := os.Getenv("CART_BASE_URL")
|
||||
defer func() {
|
||||
if originalCartBaseUrl == "" {
|
||||
os.Unsetenv("CART_BASE_URL")
|
||||
} else {
|
||||
os.Setenv("CART_BASE_URL", originalCartBaseUrl)
|
||||
}
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
host string
|
||||
cartId CartId
|
||||
cartBaseUrl string
|
||||
expectedUrls struct {
|
||||
terms string
|
||||
checkout string
|
||||
confirmation string
|
||||
validation string
|
||||
push string
|
||||
country string
|
||||
}
|
||||
}{
|
||||
{
|
||||
name: "Norwegian host with default cart base URL",
|
||||
host: "s10n-no.tornberg.me",
|
||||
cartId: ToCartId("test-cart-123"),
|
||||
cartBaseUrl: "", // Use default
|
||||
expectedUrls: struct {
|
||||
terms string
|
||||
checkout string
|
||||
confirmation string
|
||||
validation string
|
||||
push string
|
||||
country string
|
||||
}{
|
||||
terms: "https://s10n-no.tornberg.me/terms",
|
||||
checkout: "https://s10n-no.tornberg.me/checkout?order_id={checkout.order.id}",
|
||||
confirmation: "https://s10n-no.tornberg.me/confirmation/{checkout.order.id}",
|
||||
validation: "https://cart.tornberg.me/validation",
|
||||
push: "https://cart.tornberg.me/push?order_id={checkout.order.id}",
|
||||
country: "no",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Swedish host with default cart base URL",
|
||||
host: "s10n-se.tornberg.me",
|
||||
cartId: ToCartId("test-cart-456"),
|
||||
cartBaseUrl: "", // Use default
|
||||
expectedUrls: struct {
|
||||
terms string
|
||||
checkout string
|
||||
confirmation string
|
||||
validation string
|
||||
push string
|
||||
country string
|
||||
}{
|
||||
terms: "https://s10n-se.tornberg.me/terms",
|
||||
checkout: "https://s10n-se.tornberg.me/checkout?order_id={checkout.order.id}",
|
||||
confirmation: "https://s10n-se.tornberg.me/confirmation/{checkout.order.id}",
|
||||
validation: "https://cart.tornberg.me/validation",
|
||||
push: "https://cart.tornberg.me/push?order_id={checkout.order.id}",
|
||||
country: "se",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Norwegian host with custom cart base URL",
|
||||
host: "s10n-no.tornberg.me",
|
||||
cartId: ToCartId("test-cart-789"),
|
||||
cartBaseUrl: "https://custom-cart.example.com",
|
||||
expectedUrls: struct {
|
||||
terms string
|
||||
checkout string
|
||||
confirmation string
|
||||
validation string
|
||||
push string
|
||||
country string
|
||||
}{
|
||||
terms: "https://s10n-no.tornberg.me/terms",
|
||||
checkout: "https://s10n-no.tornberg.me/checkout?order_id={checkout.order.id}",
|
||||
confirmation: "https://s10n-no.tornberg.me/confirmation/{checkout.order.id}",
|
||||
validation: "https://custom-cart.example.com/validation",
|
||||
push: "https://custom-cart.example.com/push?order_id={checkout.order.id}",
|
||||
country: "no",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Host without country code defaults to Swedish",
|
||||
host: "s10n.tornberg.me",
|
||||
cartId: ToCartId("test-cart-default"),
|
||||
cartBaseUrl: "",
|
||||
expectedUrls: struct {
|
||||
terms string
|
||||
checkout string
|
||||
confirmation string
|
||||
validation string
|
||||
push string
|
||||
country string
|
||||
}{
|
||||
terms: "https://s10n.tornberg.me/terms",
|
||||
checkout: "https://s10n.tornberg.me/checkout?order_id={checkout.order.id}",
|
||||
confirmation: "https://s10n.tornberg.me/confirmation/{checkout.order.id}",
|
||||
validation: "https://cart.tornberg.me/validation",
|
||||
push: "https://cart.tornberg.me/push?order_id={checkout.order.id}",
|
||||
country: "se",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Set up environment variable for this test
|
||||
if tt.cartBaseUrl == "" {
|
||||
os.Unsetenv("CART_BASE_URL")
|
||||
} else {
|
||||
os.Setenv("CART_BASE_URL", tt.cartBaseUrl)
|
||||
}
|
||||
|
||||
result := getCheckoutOrder(tt.host, tt.cartId)
|
||||
|
||||
// Verify the result is not nil
|
||||
if result == nil {
|
||||
t.Fatal("getCheckoutOrder returned nil")
|
||||
}
|
||||
|
||||
// Check each URL field
|
||||
if result.Terms != tt.expectedUrls.terms {
|
||||
t.Errorf("Terms URL: got %q, want %q", result.Terms, tt.expectedUrls.terms)
|
||||
}
|
||||
|
||||
if result.Checkout != tt.expectedUrls.checkout {
|
||||
t.Errorf("Checkout URL: got %q, want %q", result.Checkout, tt.expectedUrls.checkout)
|
||||
}
|
||||
|
||||
if result.Confirmation != tt.expectedUrls.confirmation {
|
||||
t.Errorf("Confirmation URL: got %q, want %q", result.Confirmation, tt.expectedUrls.confirmation)
|
||||
}
|
||||
|
||||
if result.Validation != tt.expectedUrls.validation {
|
||||
t.Errorf("Validation URL: got %q, want %q", result.Validation, tt.expectedUrls.validation)
|
||||
}
|
||||
|
||||
if result.Push != tt.expectedUrls.push {
|
||||
t.Errorf("Push URL: got %q, want %q", result.Push, tt.expectedUrls.push)
|
||||
}
|
||||
|
||||
if result.Country != tt.expectedUrls.country {
|
||||
t.Errorf("Country: got %q, want %q", result.Country, tt.expectedUrls.country)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCheckoutOrderIntegration(t *testing.T) {
|
||||
// Test that both functions work together correctly
|
||||
hosts := []string{"s10n-no.tornberg.me", "s10n-se.tornberg.me"}
|
||||
cartId := ToCartId("integration-test-cart")
|
||||
|
||||
for _, host := range hosts {
|
||||
t.Run(host, func(t *testing.T) {
|
||||
// Get country from host
|
||||
country := getCountryFromHost(host)
|
||||
|
||||
// Get checkout order
|
||||
order := getCheckoutOrder(host, cartId)
|
||||
|
||||
// Verify that the country in the order matches what getCountryFromHost returns
|
||||
if order.Country != country {
|
||||
t.Errorf("Country mismatch: getCountryFromHost(%q) = %q, but order.Country = %q",
|
||||
host, country, order.Country)
|
||||
}
|
||||
|
||||
// Verify that all URLs contain the correct host
|
||||
expectedBaseUrl := "https://" + host
|
||||
|
||||
if !containsPrefix(order.Terms, expectedBaseUrl) {
|
||||
t.Errorf("Terms URL should start with %q, got %q", expectedBaseUrl, order.Terms)
|
||||
}
|
||||
|
||||
if !containsPrefix(order.Checkout, expectedBaseUrl) {
|
||||
t.Errorf("Checkout URL should start with %q, got %q", expectedBaseUrl, order.Checkout)
|
||||
}
|
||||
|
||||
if !containsPrefix(order.Confirmation, expectedBaseUrl) {
|
||||
t.Errorf("Confirmation URL should start with %q, got %q", expectedBaseUrl, order.Confirmation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to check if a string starts with a prefix
|
||||
func containsPrefix(s, prefix string) bool {
|
||||
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
||||
}
|
||||
|
||||
// Benchmark tests to measure performance
|
||||
func BenchmarkGetCountryFromHost(b *testing.B) {
|
||||
hosts := []string{
|
||||
"s10n-no.tornberg.me",
|
||||
"s10n-se.tornberg.me",
|
||||
"api-no-staging.tornberg.me",
|
||||
"s10n.tornberg.me",
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, host := range hosts {
|
||||
getCountryFromHost(host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetCheckoutOrder(b *testing.B) {
|
||||
host := "s10n-no.tornberg.me"
|
||||
cartId := ToCartId("benchmark-cart")
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
getCheckoutOrder(host, cartId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user