gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/api/router/registry/registry_test.go (about) 1 package registry 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 "testing" 8 9 "gitee.com/liuxuezhan/go-micro-v1.18.0/api" 10 ) 11 12 func TestSetNamespace(t *testing.T) { 13 testCases := []struct { 14 namespace string 15 name string 16 expected string 17 }{ 18 // default dotted path 19 { 20 "go.micro.api", 21 "foo", 22 "go.micro.api.foo", 23 }, 24 // dotted end 25 { 26 "go.micro.api.", 27 "foo", 28 "go.micro.api.foo", 29 }, 30 // dashed end 31 { 32 "go-micro-api-", 33 "foo", 34 "go-micro-api-foo", 35 }, 36 // no namespace 37 { 38 "", 39 "foo", 40 "foo", 41 }, 42 { 43 "go-micro-api-", 44 "v2.foo", 45 "go-micro-api-v2-foo", 46 }, 47 } 48 49 for _, test := range testCases { 50 name := setNamespace(test.namespace, test.name) 51 if name != test.expected { 52 t.Fatalf("expected name %s got %s", test.expected, name) 53 } 54 } 55 } 56 57 func TestRouter(t *testing.T) { 58 r := newRouter() 59 60 compare := func(expect, got []string) bool { 61 // no data to compare, return true 62 if len(expect) == 0 && len(got) == 0 { 63 return true 64 } 65 // no data expected but got some return false 66 if len(expect) == 0 && len(got) > 0 { 67 return false 68 } 69 70 // compare expected with what we got 71 for _, e := range expect { 72 var seen bool 73 for _, g := range got { 74 if e == g { 75 seen = true 76 break 77 } 78 } 79 if !seen { 80 return false 81 } 82 } 83 84 // we're done, return true 85 return true 86 } 87 88 testData := []struct { 89 e *api.Endpoint 90 r *http.Request 91 m bool 92 }{ 93 { 94 e: &api.Endpoint{ 95 Name: "Foo.Bar", 96 Host: []string{"example.com"}, 97 Method: []string{"GET"}, 98 Path: []string{"/foo"}, 99 }, 100 r: &http.Request{ 101 Host: "example.com", 102 Method: "GET", 103 URL: &url.URL{ 104 Path: "/foo", 105 }, 106 }, 107 m: true, 108 }, 109 { 110 e: &api.Endpoint{ 111 Name: "Bar.Baz", 112 Host: []string{"example.com", "foo.com"}, 113 Method: []string{"GET", "POST"}, 114 Path: []string{"/foo/bar"}, 115 }, 116 r: &http.Request{ 117 Host: "foo.com", 118 Method: "POST", 119 URL: &url.URL{ 120 Path: "/foo/bar", 121 }, 122 }, 123 m: true, 124 }, 125 { 126 e: &api.Endpoint{ 127 Name: "Test.Cruft", 128 Host: []string{"example.com", "foo.com"}, 129 Method: []string{"GET", "POST"}, 130 Path: []string{"/xyz"}, 131 }, 132 r: &http.Request{ 133 Host: "fail.com", 134 Method: "DELETE", 135 URL: &url.URL{ 136 Path: "/test/fail", 137 }, 138 }, 139 m: false, 140 }, 141 } 142 143 for _, d := range testData { 144 key := fmt.Sprintf("%s:%s", "test.service", d.e.Name) 145 r.eps[key] = &api.Service{ 146 Endpoint: d.e, 147 } 148 } 149 150 for _, d := range testData { 151 e, err := r.Endpoint(d.r) 152 if d.m && err != nil { 153 t.Fatalf("expected match, got %v", err) 154 } 155 if !d.m && err == nil { 156 t.Fatal("expected error got match") 157 } 158 // skip testing the non match 159 if !d.m { 160 continue 161 } 162 163 ep := e.Endpoint 164 165 // test the match 166 if d.e.Name != ep.Name { 167 t.Fatalf("expected %v got %v", d.e.Name, ep.Name) 168 } 169 if ok := compare(d.e.Method, ep.Method); !ok { 170 t.Fatalf("expected %v got %v", d.e.Method, ep.Method) 171 } 172 if ok := compare(d.e.Path, ep.Path); !ok { 173 t.Fatalf("expected %v got %v", d.e.Path, ep.Path) 174 } 175 if ok := compare(d.e.Host, ep.Host); !ok { 176 t.Fatalf("expected %v got %v", d.e.Host, ep.Host) 177 } 178 179 } 180 181 }