github.com/searKing/golang/go@v1.2.117/net/resolver/target_test.go (about) 1 // Copyright 2021 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 package resolver_test 5 6 import ( 7 "net/url" 8 "testing" 9 10 "github.com/searKing/golang/go/net/resolver" 11 ) 12 13 func TestParseTarget(t *testing.T) { 14 defScheme := resolver.GetDefaultScheme() 15 for i, test := range []struct { 16 target string 17 badScheme bool 18 want resolver.Target 19 }{ 20 // No scheme is specified. 21 {target: "", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: ""}}, 22 {target: "://", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "://"}}, 23 {target: ":///", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: ":///"}}, 24 {target: "://a/", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "://a/"}}, 25 {target: ":///a", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: ":///a"}}, 26 {target: "://a/b", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "://a/b"}}, 27 {target: "/", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "/"}}, 28 {target: "a/b", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a/b"}}, 29 {target: "a//b", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a//b"}}, 30 {target: "google.com", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "google.com"}}, 31 {target: "google.com/?a=b", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "google.com/"}}, 32 {target: "/unix/socket/address", badScheme: true, want: resolver.Target{Scheme: "", Authority: "", Endpoint: "/unix/socket/address"}}, 33 34 // A scheme is specified. 35 {target: "dns:///google.com", want: resolver.Target{Scheme: "dns", Authority: "", Endpoint: "google.com"}}, 36 {target: "dns://a.server.com/google.com", want: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com"}}, 37 {target: "dns://a.server.com/google.com/?a=b", want: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com/"}}, 38 {target: "unix:///a/b/c", want: resolver.Target{Scheme: "unix", Authority: "", Endpoint: "a/b/c"}}, 39 {target: "unix-abstract:a/b/c", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a/b/c"}}, 40 {target: "unix-abstract:a b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a b"}}, 41 {target: "unix-abstract:a:b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a:b"}}, 42 {target: "unix-abstract:a-b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a-b"}}, 43 {target: "unix-abstract:/ a///://::!@#$%25^&*()b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: " a///://::!@"}}, 44 {target: "unix-abstract:passthrough:abc", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "passthrough:abc"}}, 45 {target: "unix-abstract:unix:///abc", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "unix:///abc"}}, 46 {target: "unix-abstract:///a/b/c", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a/b/c"}}, 47 {target: "unix-abstract:///", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: ""}}, 48 {target: "passthrough:///unix:///a/b/c", want: resolver.Target{Scheme: "passthrough", Authority: "", Endpoint: "unix:///a/b/c"}}, 49 50 // Cases for `scheme:absolute-path`. 51 {target: "dns:/a/b/c", want: resolver.Target{Scheme: "dns", Authority: "", Endpoint: "a/b/c"}}, 52 } { 53 target := test.target 54 if test.badScheme { 55 target = defScheme + ":///" + target 56 } 57 url, err := url.Parse(target) 58 if err != nil { 59 t.Fatalf("Unexpected error parsing URL: %v", err) 60 } 61 if test.badScheme { 62 url.Scheme = "" 63 } 64 test.want.URL = *url 65 got := resolver.ParseTarget(test.target) 66 if got != test.want { 67 t.Errorf("#%d: ParseTarget(%q) = %+v, want %+v", i, test.target, got, test.want) 68 } 69 } 70 }