func WithGateway(r *router.Router) gogogrpc.ServerOption { return gogogrpc.WithServiceDescWrapper(func(desc *grpc.ServiceDesc, srv interface{}) { gatewayDesc := lookupGatewayDesc(desc.HandlerType) if gatewayDesc == nil { return } // ensure the ServiceDesc doesn't get modified by later options desc = gogogrpc.CloneServiceDesc(desc) for _, route := range gatewayDesc.Routes { r.Addf(route.Method, route.Pattern, wrapServiceWithGateway(desc, srv, route.Handler)) } }) }
func TestGreeting(t *testing.T) { var ( svc = testService{} router router.Router ) RegisterTestServiceGateway(&router, &svc) err := router.Compile() if err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err := router.ServeHTTP(context.Background(), w, r) if err != nil { t.Fatal(err) } })) defer server.Close() resp, err := http.Post(server.URL+"/greet", "application/json", strings.NewReader(`{"name":"Simon"}`)) if err != nil { t.Fatal(err) } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } data = bytes.TrimSpace(data) expected := []byte(`{"text":"Hello Simon!"}`) if !bytes.Equal(data, expected) { t.Errorf("expected: %s\nactual: %s", expected, data) } }
func TestListWithParam(t *testing.T) { var ( svc = testService{} router router.Router ) RegisterTestServiceGateway(&router, &svc) err := router.Compile() if err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err := router.ServeHTTP(context.Background(), w, r) if err != nil { t.Fatal(err) } })) defer server.Close() resp, err := http.Get(server.URL + "/list?after=128") if err != nil { t.Fatal(err) } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } data = bytes.TrimSpace(data) expected := []byte(`{"items":[{"index":"128"},{"index":"129"},{"index":"130"},{"index":"131"},{"index":"132"},{"index":"133"},{"index":"134"},{"index":"135"},{"index":"136"},{"index":"137"},{"index":"138"},{"index":"139"},{"index":"140"},{"index":"141"},{"index":"142"},{"index":"143"},{"index":"144"},{"index":"145"},{"index":"146"},{"index":"147"},{"index":"148"},{"index":"149"},{"index":"150"},{"index":"151"},{"index":"152"}]}`) if !bytes.Equal(data, expected) { t.Errorf("expected: %s\nactual: %s", expected, data) } }
func TestList(t *testing.T) { var ( svc = testService{} router router.Router ) RegisterTestServiceGateway(&router, &svc) err := router.Compile() if err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err := router.ServeHTTP(context.Background(), w, r) if err != nil { t.Fatal(err) } })) defer server.Close() resp, err := http.Get(server.URL + "/list") if err != nil { t.Fatal(err) } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } data = bytes.TrimSpace(data) expected := []byte(`{"items":[{"index":"0"},{"index":"1"},{"index":"2"},{"index":"3"},{"index":"4"},{"index":"5"},{"index":"6"},{"index":"7"},{"index":"8"},{"index":"9"},{"index":"10"},{"index":"11"},{"index":"12"},{"index":"13"},{"index":"14"},{"index":"15"},{"index":"16"},{"index":"17"},{"index":"18"},{"index":"19"},{"index":"20"},{"index":"21"},{"index":"22"},{"index":"23"},{"index":"24"}]}`) if !bytes.Equal(data, expected) { t.Errorf("expected: %s\nactual: %s", expected, data) } }
func TestFetchPerson(t *testing.T) { var ( svc = testService{} router router.Router ) RegisterTestServiceGateway(&router, &svc) err := router.Compile() if err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err := router.ServeHTTP(context.Background(), w, r) if err != nil { t.Fatal(err) } })) defer server.Close() resp, err := http.Get(server.URL + "/person/25") if err != nil { t.Fatal(err) } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } data = bytes.TrimSpace(data) expected := []byte(`{"name":"Person 25"}`) if !bytes.Equal(data, expected) { t.Errorf("expected: %s\nactual: %s", expected, data) } }
func RegisterTestServiceGateway(router *github_com_limbo_services_core_runtime_router.Router, ss TestServiceServer) { h := &testServiceHandler{ss: ss} router.Addf("POST", "/greet", h._http_TestService_Greet) router.Addf("GET", "/list", h._http_TestService_List) router.Addf("GET", "/person/{id}", h._http_TestService_FetchPerson) }