github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/api/router/router_test.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/api/router/router_test.go 16 package router_test 17 18 import ( 19 "context" 20 "fmt" 21 "io/ioutil" 22 "log" 23 "net/http" 24 "testing" 25 "time" 26 27 "github.com/tickoalcantara12/micro/v3/service/api/handler" 28 "github.com/tickoalcantara12/micro/v3/service/api/handler/rpc" 29 "github.com/tickoalcantara12/micro/v3/service/api/router" 30 rregistry "github.com/tickoalcantara12/micro/v3/service/api/router/registry" 31 "github.com/tickoalcantara12/micro/v3/service/client" 32 gcli "github.com/tickoalcantara12/micro/v3/service/client/grpc" 33 rmemory "github.com/tickoalcantara12/micro/v3/service/registry/memory" 34 rt "github.com/tickoalcantara12/micro/v3/service/router" 35 regRouter "github.com/tickoalcantara12/micro/v3/service/router/registry" 36 "github.com/tickoalcantara12/micro/v3/service/server" 37 gsrv "github.com/tickoalcantara12/micro/v3/service/server/grpc" 38 pb "github.com/tickoalcantara12/micro/v3/service/server/grpc/proto" 39 ) 40 41 // server is used to implement helloworld.GreeterServer. 42 type testServer struct { 43 msgCount int 44 } 45 46 // TestHello implements helloworld.GreeterServer 47 func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error { 48 rsp.Msg = "Hello " + req.Uuid 49 return nil 50 } 51 52 // TestHello implements helloworld.GreeterServer 53 func (s *testServer) CallPcre(ctx context.Context, req *pb.Request, rsp *pb.Response) error { 54 rsp.Msg = "Hello " + req.Uuid 55 return nil 56 } 57 58 // TestHello implements helloworld.GreeterServer 59 func (s *testServer) CallPcreInvalid(ctx context.Context, req *pb.Request, rsp *pb.Response) error { 60 rsp.Msg = "Hello " + req.Uuid 61 return nil 62 } 63 64 func initial(t *testing.T) (server.Server, client.Client) { 65 r := rmemory.NewRegistry() 66 67 // create a new client 68 s := gsrv.NewServer( 69 server.Name("foo"), 70 server.Registry(r), 71 ) 72 73 rtr := regRouter.NewRouter( 74 rt.Registry(r), 75 ) 76 77 // create a new server 78 c := gcli.NewClient( 79 client.Router(rtr), 80 ) 81 82 h := &testServer{} 83 pb.RegisterTestHandler(s, h) 84 85 if err := s.Start(); err != nil { 86 t.Fatalf("failed to start: %v", err) 87 } 88 89 return s, c 90 } 91 92 func check(t *testing.T, addr string, path string, expected string) { 93 req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), nil) 94 if err != nil { 95 t.Fatalf("Failed to created http.Request: %v", err) 96 } 97 req.Header.Set("Content-Type", "application/json") 98 rsp, err := (&http.Client{}).Do(req) 99 if err != nil { 100 t.Fatalf("Failed to created http.Request: %v", err) 101 } 102 defer rsp.Body.Close() 103 104 buf, err := ioutil.ReadAll(rsp.Body) 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 jsonMsg := expected 110 if string(buf) != jsonMsg { 111 t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg) 112 } 113 } 114 115 func TestRouterRegistryPcre(t *testing.T) { 116 s, c := initial(t) 117 defer s.Stop() 118 119 router := rregistry.NewRouter( 120 router.WithHandler(rpc.Handler), 121 router.WithRegistry(s.Options().Registry), 122 ) 123 hrpc := rpc.NewHandler( 124 handler.WithClient(c), 125 handler.WithRouter(router), 126 ) 127 hsrv := &http.Server{ 128 Handler: hrpc, 129 Addr: "127.0.0.1:6543", 130 WriteTimeout: 15 * time.Second, 131 ReadTimeout: 15 * time.Second, 132 IdleTimeout: 20 * time.Second, 133 MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb 134 } 135 136 go func() { 137 log.Println(hsrv.ListenAndServe()) 138 }() 139 140 defer hsrv.Close() 141 time.Sleep(1 * time.Second) 142 check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`) 143 }