gitee.com/liu-zhao234568/cntest@v1.0.0/ethstats/ethstats_test.go (about) 1 package ethstats 2 3 import ( 4 "strconv" 5 "testing" 6 ) 7 8 func TestParseEthstatsURL(t *testing.T) { 9 cases := []struct { 10 url string 11 node, pass, host string 12 }{ 13 { 14 url: `"debug meowsbits":mypass@ws://mordor.dash.fault.dev:3000`, 15 node: "debug meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000", 16 }, 17 { 18 url: `"debug @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`, 19 node: "debug @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000", 20 }, 21 { 22 url: `"debug: @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`, 23 node: "debug: @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000", 24 }, 25 { 26 url: `name:@ws://mordor.dash.fault.dev:3000`, 27 node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000", 28 }, 29 { 30 url: `name@ws://mordor.dash.fault.dev:3000`, 31 node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000", 32 }, 33 { 34 url: `:mypass@ws://mordor.dash.fault.dev:3000`, 35 node: "", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000", 36 }, 37 { 38 url: `:@ws://mordor.dash.fault.dev:3000`, 39 node: "", pass: "", host: "ws://mordor.dash.fault.dev:3000", 40 }, 41 } 42 43 for i, c := range cases { 44 parts, err := parseEthstatsURL(c.url) 45 if err != nil { 46 t.Fatal(err) 47 } 48 node, pass, host := parts[0], parts[1], parts[2] 49 50 // unquote because the value provided will be used as a CLI flag value, so unescaped quotes will be removed 51 nodeUnquote, err := strconv.Unquote(node) 52 if err == nil { 53 node = nodeUnquote 54 } 55 56 if node != c.node { 57 t.Errorf("case=%d mismatch node value, got: %v ,want: %v", i, node, c.node) 58 } 59 if pass != c.pass { 60 t.Errorf("case=%d mismatch pass value, got: %v ,want: %v", i, pass, c.pass) 61 } 62 if host != c.host { 63 t.Errorf("case=%d mismatch host value, got: %v ,want: %v", i, host, c.host) 64 } 65 } 66 67 }