github.com/simpleiot/simpleiot@v0.18.3/client/uri_test.go (about) 1 package client 2 3 import ( 4 "testing" 5 ) 6 7 func TestNatsURIParts(t *testing.T) { 8 proto, server, port, err := parseURI("wss://myserver.com:443") 9 if err != nil { 10 t.Error(err) 11 } 12 13 if proto != "wss" { 14 t.Error("Wrong proto, expected wss, got: ", proto) 15 } 16 17 if server != "myserver.com" { 18 t.Error("Wrong server, expected myserver.com, got: ", server) 19 } 20 21 if port != "443" { 22 t.Error("Wrong port, expected 443, got: ", port) 23 } 24 } 25 26 func TestNatsURIPartsNoPort(t *testing.T) { 27 proto, server, port, err := parseURI("wss://myserver.com") 28 if err != nil { 29 t.Error(err) 30 } 31 32 if proto != "wss" { 33 t.Error("Wrong proto, expected wss, got: ", proto) 34 } 35 36 if server != "myserver.com" { 37 t.Error("Wrong server, expected myserver.com, got: ", server) 38 } 39 40 if port != "" { 41 t.Error("Wrong blank port, got: ", port) 42 } 43 } 44 45 func TestNatsURIPartsSpaces(t *testing.T) { 46 proto, server, port, err := parseURI(" wss://myserver.com:443 ") 47 if err != nil { 48 t.Error(err) 49 } 50 51 if proto != "wss" { 52 t.Error("Wrong proto, expected wss, got: ", proto) 53 } 54 55 if server != "myserver.com" { 56 t.Error("Wrong server, expected myserver.com, got: ", server) 57 } 58 59 if port != "443" { 60 t.Error("Wrong port, expected 443, got: ", port) 61 } 62 } 63 64 type sanitizeTests struct { 65 in string 66 exp string 67 } 68 69 func TestSanitizeURI(t *testing.T) { 70 tests := []sanitizeTests{ 71 {"nats://myserver.com", "nats://myserver.com:4222"}, 72 {"ws://myserver.com:8118", "ws://myserver.com:8118"}, 73 {"ws://myserver.com", "ws://myserver.com:80"}, 74 {"wss://myserver.com", "wss://myserver.com:443"}, 75 {"wsss://myserver.com", "wsss://myserver.com:4222"}, 76 } 77 78 for _, test := range tests { 79 s, err := sanitizeURI(test.in) 80 if err != nil { 81 t.Errorf("Error sanitizing %v: %v", test.in, err) 82 } 83 84 if s != test.exp { 85 t.Errorf("Error sanitizing %v, got %v, expected %v", test.in, s, test.exp) 86 } 87 } 88 } 89 90 func TestSanitizeURIError(t *testing.T) { 91 92 _, err := sanitizeURI("nats:/myserver.com") 93 if err == nil { 94 t.Error("Expected error") 95 } 96 }