67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"git.sendico.io/tech/sxchnge/product/app/api"
|
|
"git.sendico.io/tech/sxchnge/shared/errors"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestIncompleteConfigReturnsProjectError(t *testing.T) {
|
|
for _, data := range []string{"api: {}", "api: {middleware: {}}\ndatabase: {driver: mongodb}", "api: {middleware: {}}\ndatabase: {driver: mongodb}\nhttp_server: {}"} {
|
|
instance := &Imp{runtime: api.RuntimeConfig{}}
|
|
if err := instance.decodeConfig([]byte(data)); !errors.Is(err, merrors.ErrInvalidArg) {
|
|
t.Fatalf("expected configuration error, got %v", err)
|
|
}
|
|
}
|
|
}
|
|
func TestExampleConfigMatchesCopiedRuntime(t *testing.T) {
|
|
data, err := os.ReadFile("../../../config.example.yml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
instance := &Imp{runtime: api.RuntimeConfig{}}
|
|
if err := instance.decodeConfig(data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := instance.config.API.SharedConfig().Mw.Token.Validate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := instance.config.API.SharedConfig().Mw.CORS.Validate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestVersionSelectionPreservesOmittedAndEmptyLists(t *testing.T) {
|
|
data, err := os.ReadFile("../../../config.example.yml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name, setting string
|
|
wantNil bool
|
|
wantLength int
|
|
}{
|
|
{name: "omitted", wantNil: true},
|
|
{name: "disabled", setting: " versions: []\n"},
|
|
{name: "explicit", setting: " versions: [{version: v1, path: /gateway/v1}]\n", wantLength: 1},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
instance := &Imp{runtime: api.RuntimeConfig{}}
|
|
configured := strings.Replace(string(data), "api:\n", "api:\n"+tc.setting, 1)
|
|
if err := instance.decodeConfig([]byte(configured)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
versions := instance.config.API.SharedConfig().Versions
|
|
if (versions == nil) != tc.wantNil || len(versions) != tc.wantLength {
|
|
t.Fatalf("unexpected versions: %#v", versions)
|
|
}
|
|
if tc.wantLength == 1 && (versions[0].Version != "v1" || versions[0].Path != "/gateway/v1") {
|
|
t.Fatalf("lost version configuration: %#v", versions)
|
|
}
|
|
})
|
|
}
|
|
}
|