setting_remote_routing_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package service
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  6. )
  7. func TestValidateRemoteRoutingURLSettings(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. value string
  11. want string
  12. wantError string
  13. }{
  14. {name: "valid HTTPS", value: " https://example.com/rules#fragment ", want: "https://example.com/rules"},
  15. {name: "credentials", value: "https://user:[email protected]/rules", wantError: "must not contain URL credentials"},
  16. {name: "missing host", value: "https:///rules", wantError: "absolute HTTPS URL"},
  17. {name: "legacy HTTP stays inline", value: "http://example.com/rules", want: "http://example.com/rules"},
  18. {name: "multiline Clash stays inline", value: "https://example.com/rules\nMATCH,PROXY", want: "https://example.com/rules\nMATCH,PROXY"},
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.name, func(t *testing.T) {
  22. settings := &entity.AllSetting{SubRoutingRules: tt.value}
  23. err := validateSettingsURLs(settings)
  24. if tt.wantError != "" {
  25. if err == nil || !strings.Contains(err.Error(), tt.wantError) {
  26. t.Fatalf("err=%v, want %q", err, tt.wantError)
  27. }
  28. return
  29. }
  30. if err != nil || settings.SubRoutingRules != tt.want {
  31. t.Fatalf("value=%q err=%v", settings.SubRoutingRules, err)
  32. }
  33. })
  34. }
  35. }