2019-04-28 23:49:43 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-01-07 00:41:10 +01:00
|
|
|
"context"
|
2019-04-28 23:49:43 +02:00
|
|
|
"testing"
|
2021-12-12 01:20:19 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-28 23:49:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_routesImpl_FindBackendForServerAddress(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
serverAddress string
|
|
|
|
}
|
|
|
|
type mapping struct {
|
|
|
|
serverAddress string
|
|
|
|
backend string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
mapping mapping
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "typical",
|
|
|
|
mapping: mapping{
|
|
|
|
serverAddress: "typical.my.domain", backend: "backend:25565",
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
serverAddress: `typical.my.domain`,
|
|
|
|
},
|
|
|
|
want: "backend:25565",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "forge",
|
|
|
|
mapping: mapping{
|
|
|
|
serverAddress: "forge.my.domain", backend: "backend:25566",
|
|
|
|
},
|
|
|
|
args: args{
|
2019-04-29 00:02:21 +02:00
|
|
|
serverAddress: "forge.my.domain\x00FML2\x00",
|
2019-04-28 23:49:43 +02:00
|
|
|
},
|
|
|
|
want: "backend:25566",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := NewRoutes()
|
|
|
|
|
2022-01-07 00:41:10 +01:00
|
|
|
r.CreateMapping(tt.mapping.serverAddress, tt.mapping.backend, func(ctx context.Context) error { return nil })
|
2019-04-28 23:49:43 +02:00
|
|
|
|
2022-01-07 00:41:10 +01:00
|
|
|
if got, server, _ := r.FindBackendForServerAddress(context.Background(), tt.args.serverAddress); got != tt.want {
|
2019-04-28 23:49:43 +02:00
|
|
|
t.Errorf("routesImpl.FindBackendForServerAddress() = %v, want %v", got, tt.want)
|
2019-07-14 23:34:46 +02:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, tt.mapping.serverAddress, server)
|
2019-04-28 23:49:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|