@@ -2,10 +2,13 @@ package resolver
2
2
3
3
import (
4
4
"fmt"
5
+ "net/url"
6
+ "strings"
5
7
"testing"
6
8
7
9
pb "github.com/binchencoder/skylb-apiv2/proto"
8
10
"github.com/stretchr/testify/assert"
11
+ "google.golang.org/grpc/resolver"
9
12
)
10
13
11
14
func TestDirectTarget (t * testing.T ) {
@@ -23,3 +26,33 @@ func TestSkyLBTarget(t *testing.T) {
23
26
fmt .Println (target )
24
27
assert .Equal (t , "skylb://serviceName?ns=namespace&pn=portName" , target )
25
28
}
29
+
30
+ func TestParseTarget (t * testing.T ) {
31
+ target := SkyLBTarget (& pb.ServiceSpec {
32
+ Namespace : "namespace" ,
33
+ ServiceName : "serviceName" ,
34
+ PortName : "portName" ,
35
+ })
36
+
37
+ u , _ := url .Parse (target )
38
+ // For targets of the form "[scheme]://[authority]/endpoint, the endpoint
39
+ // value returned from url.Parse() contains a leading "/". Although this is
40
+ // in accordance with RFC 3986, we do not want to break existing resolver
41
+ // implementations which expect the endpoint without the leading "/". So, we
42
+ // end up stripping the leading "/" here. But this will result in an
43
+ // incorrect parsing for something like "unix:///path/to/socket". Since we
44
+ // own the "unix" resolver, we can workaround in the unix resolver by using
45
+ // the `URL` field instead of the `Endpoint` field.
46
+ endpoint := u .Path
47
+ if endpoint == "" {
48
+ endpoint = u .Opaque
49
+ }
50
+ endpoint = strings .TrimPrefix (endpoint , "/" )
51
+ resTarget := resolver.Target {
52
+ Scheme : u .Scheme ,
53
+ Authority : u .Host ,
54
+ Endpoint : endpoint ,
55
+ URL : * u ,
56
+ }
57
+ fmt .Printf ("target: %s, parsedTarget: %+v \n " , target , resTarget )
58
+ }
0 commit comments