links_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package sub
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. func TestSplitLinkLines(t *testing.T) {
  9. cases := []struct {
  10. name string
  11. in string
  12. want []string
  13. }{
  14. {"single_line", "vless://abc", []string{"vless://abc"}},
  15. {"two_lines", "vless://abc\nvmess://xyz", []string{"vless://abc", "vmess://xyz"}},
  16. {"trims_each_line", " vless://abc \n\tvmess://xyz\t", []string{"vless://abc", "vmess://xyz"}},
  17. {"skips_blank_lines", "vless://abc\n\n\nvmess://xyz\n", []string{"vless://abc", "vmess://xyz"}},
  18. }
  19. for _, c := range cases {
  20. t.Run(c.name, func(t *testing.T) {
  21. got := splitLinkLines(c.in)
  22. if !reflect.DeepEqual(got, c.want) {
  23. t.Fatalf("splitLinkLines(%q) = %#v, want %#v", c.in, got, c.want)
  24. }
  25. })
  26. }
  27. }
  28. func TestSplitLinkLines_EmptyInputIsNil(t *testing.T) {
  29. if got := splitLinkLines(""); got != nil {
  30. t.Fatalf("splitLinkLines(\"\") = %#v, want nil", got)
  31. }
  32. }
  33. func TestSplitLinkLines_WhitespaceOnlyHasNoEntries(t *testing.T) {
  34. got := splitLinkLines(" \n\t \n")
  35. if len(got) != 0 {
  36. t.Fatalf("splitLinkLines(whitespace) = %#v, want empty slice", got)
  37. }
  38. }
  39. func TestLinksForClient_UsesHostEndpoints(t *testing.T) {
  40. seedSubDB(t)
  41. inbound := seedSubInbound(t, "s-gate", "gate", 4431, 1, `{"network":"tcp","security":"none"}`)
  42. seedHost(t, &model.Host{
  43. InboundId: inbound.Id, Remark: "public", Address: "proxy.example.com",
  44. Port: 443, Security: "same",
  45. })
  46. links := NewLinkProvider().LinksForClient("req.example.com", inbound, "gate@e")
  47. if len(links) != 1 {
  48. t.Fatalf("links = %d, want 1: %v", len(links), links)
  49. }
  50. if !strings.Contains(links[0], "proxy.example.com:443") {
  51. t.Fatalf("link = %q, want the host endpoint proxy.example.com:443", links[0])
  52. }
  53. }