github.com/btccom/go-micro/v2@v2.9.3/api/router/router_test.go (about) 1 package router_test 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "log" 8 "net/http" 9 "testing" 10 "time" 11 12 "github.com/btccom/go-micro/v2/api" 13 "github.com/btccom/go-micro/v2/api/handler" 14 "github.com/btccom/go-micro/v2/api/handler/rpc" 15 "github.com/btccom/go-micro/v2/api/router" 16 rregistry "github.com/btccom/go-micro/v2/api/router/registry" 17 rstatic "github.com/btccom/go-micro/v2/api/router/static" 18 "github.com/btccom/go-micro/v2/client" 19 gcli "github.com/btccom/go-micro/v2/client/grpc" 20 rmemory "github.com/btccom/go-micro/v2/registry/memory" 21 "github.com/btccom/go-micro/v2/server" 22 gsrv "github.com/btccom/go-micro/v2/server/grpc" 23 pb "github.com/btccom/go-micro/v2/server/grpc/proto" 24 ) 25 26 // server is used to implement helloworld.GreeterServer. 27 type testServer struct { 28 msgCount int 29 } 30 31 // TestHello implements helloworld.GreeterServer 32 func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error { 33 rsp.Msg = "Hello " + req.Uuid 34 return nil 35 } 36 37 // TestHello implements helloworld.GreeterServer 38 func (s *testServer) CallPcre(ctx context.Context, req *pb.Request, rsp *pb.Response) error { 39 rsp.Msg = "Hello " + req.Uuid 40 return nil 41 } 42 43 // TestHello implements helloworld.GreeterServer 44 func (s *testServer) CallPcreInvalid(ctx context.Context, req *pb.Request, rsp *pb.Response) error { 45 rsp.Msg = "Hello " + req.Uuid 46 return nil 47 } 48 49 func initial(t *testing.T) (server.Server, client.Client) { 50 r := rmemory.NewRegistry() 51 52 // create a new client 53 s := gsrv.NewServer( 54 server.Name("foo"), 55 server.Registry(r), 56 ) 57 58 // create a new server 59 c := gcli.NewClient( 60 client.Registry(r), 61 ) 62 63 h := &testServer{} 64 pb.RegisterTestHandler(s, h) 65 66 if err := s.Start(); err != nil { 67 t.Fatalf("failed to start: %v", err) 68 } 69 70 return s, c 71 } 72 73 func check(t *testing.T, addr string, path string, expected string) { 74 req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), nil) 75 if err != nil { 76 t.Fatalf("Failed to created http.Request: %v", err) 77 } 78 req.Header.Set("Content-Type", "application/json") 79 rsp, err := (&http.Client{}).Do(req) 80 if err != nil { 81 t.Fatalf("Failed to created http.Request: %v", err) 82 } 83 defer rsp.Body.Close() 84 85 buf, err := ioutil.ReadAll(rsp.Body) 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 jsonMsg := expected 91 if string(buf) != jsonMsg { 92 t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg) 93 } 94 } 95 96 func TestRouterRegistryPcre(t *testing.T) { 97 s, c := initial(t) 98 defer s.Stop() 99 100 router := rregistry.NewRouter( 101 router.WithHandler(rpc.Handler), 102 router.WithRegistry(s.Options().Registry), 103 ) 104 hrpc := rpc.NewHandler( 105 handler.WithClient(c), 106 handler.WithRouter(router), 107 ) 108 hsrv := &http.Server{ 109 Handler: hrpc, 110 Addr: "127.0.0.1:6543", 111 WriteTimeout: 15 * time.Second, 112 ReadTimeout: 15 * time.Second, 113 IdleTimeout: 20 * time.Second, 114 MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb 115 } 116 117 go func() { 118 log.Println(hsrv.ListenAndServe()) 119 }() 120 121 defer hsrv.Close() 122 time.Sleep(1 * time.Second) 123 check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`) 124 } 125 126 func TestRouterStaticPcre(t *testing.T) { 127 s, c := initial(t) 128 defer s.Stop() 129 130 router := rstatic.NewRouter( 131 router.WithHandler(rpc.Handler), 132 router.WithRegistry(s.Options().Registry), 133 ) 134 135 err := router.Register(&api.Endpoint{ 136 Name: "foo.Test.Call", 137 Method: []string{"POST"}, 138 Path: []string{"^/api/v0/test/call/?$"}, 139 Handler: "rpc", 140 }) 141 if err != nil { 142 t.Fatal(err) 143 } 144 145 hrpc := rpc.NewHandler( 146 handler.WithClient(c), 147 handler.WithRouter(router), 148 ) 149 hsrv := &http.Server{ 150 Handler: hrpc, 151 Addr: "127.0.0.1:6543", 152 WriteTimeout: 15 * time.Second, 153 ReadTimeout: 15 * time.Second, 154 IdleTimeout: 20 * time.Second, 155 MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb 156 } 157 158 go func() { 159 log.Println(hsrv.ListenAndServe()) 160 }() 161 defer hsrv.Close() 162 163 time.Sleep(1 * time.Second) 164 check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`) 165 } 166 167 func TestRouterStaticGpath(t *testing.T) { 168 s, c := initial(t) 169 defer s.Stop() 170 171 router := rstatic.NewRouter( 172 router.WithHandler(rpc.Handler), 173 router.WithRegistry(s.Options().Registry), 174 ) 175 176 err := router.Register(&api.Endpoint{ 177 Name: "foo.Test.Call", 178 Method: []string{"POST"}, 179 Path: []string{"/api/v0/test/call/{uuid}"}, 180 Handler: "rpc", 181 }) 182 if err != nil { 183 t.Fatal(err) 184 } 185 186 hrpc := rpc.NewHandler( 187 handler.WithClient(c), 188 handler.WithRouter(router), 189 ) 190 hsrv := &http.Server{ 191 Handler: hrpc, 192 Addr: "127.0.0.1:6543", 193 WriteTimeout: 15 * time.Second, 194 ReadTimeout: 15 * time.Second, 195 IdleTimeout: 20 * time.Second, 196 MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb 197 } 198 199 go func() { 200 log.Println(hsrv.ListenAndServe()) 201 }() 202 defer hsrv.Close() 203 204 time.Sleep(1 * time.Second) 205 check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`) 206 } 207 208 func TestRouterStaticPcreInvalid(t *testing.T) { 209 var ep *api.Endpoint 210 var err error 211 212 s, c := initial(t) 213 defer s.Stop() 214 215 router := rstatic.NewRouter( 216 router.WithHandler(rpc.Handler), 217 router.WithRegistry(s.Options().Registry), 218 ) 219 220 ep = &api.Endpoint{ 221 Name: "foo.Test.Call", 222 Method: []string{"POST"}, 223 Path: []string{"^/api/v0/test/call/?"}, 224 Handler: "rpc", 225 } 226 227 err = router.Register(ep) 228 if err == nil { 229 t.Fatalf("invalid endpoint %v", ep) 230 } 231 232 ep = &api.Endpoint{ 233 Name: "foo.Test.Call", 234 Method: []string{"POST"}, 235 Path: []string{"/api/v0/test/call/?$"}, 236 Handler: "rpc", 237 } 238 239 err = router.Register(ep) 240 if err == nil { 241 t.Fatalf("invalid endpoint %v", ep) 242 } 243 244 _ = c 245 }