90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLoadLocalConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cfg, err := Load(filepath.Join("..", "..", "config", "local.yaml"))
|
|
if err != nil {
|
|
t.Fatalf("Load returned error: %v", err)
|
|
}
|
|
|
|
if cfg.App.Name != "chatappgateway" {
|
|
t.Fatalf("unexpected app name: %s", cfg.App.Name)
|
|
}
|
|
if cfg.App.HTTPAddr != ":8080" {
|
|
t.Fatalf("unexpected http addr: %s", cfg.App.HTTPAddr)
|
|
}
|
|
if cfg.GRPC.User.Target != "127.0.0.1:9001" {
|
|
t.Fatalf("unexpected user target: %s", cfg.GRPC.User.Target)
|
|
}
|
|
if cfg.GRPC.Pay.Timeout != 3*time.Second {
|
|
t.Fatalf("unexpected pay timeout: %s", cfg.GRPC.Pay.Timeout)
|
|
}
|
|
}
|
|
|
|
func TestLoadInvalidConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
path := filepath.Join(t.TempDir(), "invalid.yaml")
|
|
content := strings.TrimSpace(`
|
|
app:
|
|
name: chatappgateway
|
|
http_addr: "bad-address"
|
|
grpc:
|
|
user:
|
|
target: ""
|
|
timeout: 3s
|
|
pay:
|
|
target: "127.0.0.1:9002"
|
|
timeout: 0s
|
|
`)
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("WriteFile returned error: %v", err)
|
|
}
|
|
|
|
_, err := Load(path)
|
|
if err == nil {
|
|
t.Fatal("Load returned nil error")
|
|
}
|
|
if !strings.Contains(err.Error(), "app.http_addr is invalid") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadInvalidUpstream(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
path := filepath.Join(t.TempDir(), "invalid-upstream.yaml")
|
|
content := strings.TrimSpace(`
|
|
app:
|
|
name: chatappgateway
|
|
http_addr: ":8080"
|
|
grpc:
|
|
user:
|
|
target: "127.0.0.1:9001"
|
|
timeout: 3s
|
|
pay:
|
|
target: "invalid"
|
|
timeout: 3s
|
|
`)
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("WriteFile returned error: %v", err)
|
|
}
|
|
|
|
_, err := Load(path)
|
|
if err == nil {
|
|
t.Fatal("Load returned nil error")
|
|
}
|
|
if !strings.Contains(err.Error(), "grpc.pay.target is invalid") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|