1
0

url_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package common
  2. import "testing"
  3. func TestEnsureURLScheme(t *testing.T) {
  4. tests := []struct {
  5. name string
  6. in string
  7. want string
  8. }{
  9. {"empty", "", ""},
  10. {"whitespace only", " ", ""},
  11. {"bare telegram handle", "t.me/xui_support", "https://t.me/xui_support"},
  12. {"bare domain with path", "example.com/help", "https://example.com/help"},
  13. {"already https", "https://t.me/xui_support", "https://t.me/xui_support"},
  14. {"already http", "http://example.com", "http://example.com"},
  15. {"telegram deep link", "tg://resolve?domain=xui_support", "tg://resolve?domain=xui_support"},
  16. {"mailto", "mailto:[email protected]", "mailto:[email protected]"},
  17. {"tel", "tel:+1234567890", "tel:+1234567890"},
  18. {"trims whitespace", " t.me/xui_support ", "https://t.me/xui_support"},
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.name, func(t *testing.T) {
  22. if got := EnsureURLScheme(tt.in); got != tt.want {
  23. t.Errorf("EnsureURLScheme(%q) = %q, want %q", tt.in, got, tt.want)
  24. }
  25. })
  26. }
  27. }
  28. func TestParseRemoteRoutingURLKeepsInlineCompatibility(t *testing.T) {
  29. tests := []struct {
  30. name string
  31. input string
  32. wantSource string
  33. wantRemote bool
  34. wantErr bool
  35. }{
  36. {name: "deeplink stays inline", input: "happ://routing/onadd/abc"},
  37. {name: "plain HTTP stays inline", input: "http://example.com/rules"},
  38. {name: "multiline stays inline", input: "https://example.com/rules\nMATCH,PROXY"},
  39. {name: "HTTPS source", input: " https://example.com/rules#ignored ", wantSource: "https://example.com/rules", wantRemote: true},
  40. {name: "uppercase scheme", input: "HTTPS://example.com/rules", wantSource: "https://example.com/rules", wantRemote: true},
  41. {name: "credentials rejected", input: "https://user:[email protected]/rules", wantRemote: true, wantErr: true},
  42. {name: "missing host rejected", input: "https:///rules", wantRemote: true, wantErr: true},
  43. }
  44. for _, tt := range tests {
  45. t.Run(tt.name, func(t *testing.T) {
  46. got, remote, err := ParseRemoteRoutingURL(tt.input)
  47. if got != tt.wantSource || remote != tt.wantRemote || (err != nil) != tt.wantErr {
  48. t.Fatalf("got=%q remote=%v err=%v", got, remote, err)
  49. }
  50. })
  51. }
  52. }