package common

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestIsSidecarURL(t *testing.T) {
	t.Parallel()
	for _, tc := range []struct {
		name, target, proxy string
		want                bool
	}{
		{"route", "http://localhost:16383/ticket-manager", "http://localhost:16383/", true},
		{"prefix", "http://localhost:16383/proxy/ticket-manager", "http://localhost:16383/proxy", true},
		{"equal prefix", "http://localhost:16383/proxy", "http://localhost:16383/proxy", true},
		{"equal root", "http://localhost:16383", "http://localhost:16383", true},
		{"root trailing slash", "http://localhost:16383", "http://localhost:16383/", true},
		{"target trailing slash", "http://localhost:16383/proxy/", "http://localhost:16383/proxy", true},
		{"proxy trailing slash", "http://localhost:16383/proxy", "http://localhost:16383/proxy/", true},
		{"both trailing slash", "http://localhost:16383/proxy/", "http://localhost:16383/proxy/", true},
		{"parent outside prefix", "http://localhost:16383", "http://localhost:16383/proxy", false},
		{"no proxy", "https://ticket-manager.example", "", false},
		{"different host", "https://ticket-manager.example/dev", "http://localhost:16383", false},
		{"different port", "http://localhost:16384/ticket-manager", "http://localhost:16383", false},
		{"different scheme", "https://localhost:16383/ticket-manager", "http://localhost:16383", false},
		{"prefix boundary", "http://localhost:16383/proxy-other/ticket-manager", "http://localhost:16383/proxy", false},
		{"userinfo", "http://user@localhost:16383/ticket-manager", "http://localhost:16383", false},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			require.Equal(t, tc.want, isSidecarURL(tc.target, tc.proxy))
		})
	}
}
