update tests for http client

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-01-19 04:15:57 +03:00
parent 16301438fd
commit 48558b66ff
10 changed files with 784 additions and 16 deletions

3
client/http/generate.go Normal file
View File

@@ -0,0 +1,3 @@
package http
//go:generate protoc -I./proto -I. -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0 -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/v2@v2.1.0/third_party/googleapis --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --micro_out=components=micro|http,debug=true,paths=source_relative:./proto proto/github.proto

View File

@@ -15,6 +15,7 @@ import (
jsoncodec "github.com/unistack-org/micro-codec-json/v3"
rmemory "github.com/unistack-org/micro-registry-memory/v3"
rrouter "github.com/unistack-org/micro-router-registry/v3"
pb "github.com/unistack-org/micro-tests/client/http/proto"
"github.com/unistack-org/micro/v3/client"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/registry"
@@ -46,7 +47,7 @@ func (e *GithubRspError) Error() string {
return string(b)
}
func TestNativeError(t *testing.T) {
func TestError(t *testing.T) {
c := client.NewClientCallOptions(mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec())), client.WithAddress("https://api.github.com"))
req := c.NewRequest("github", "/dddd", nil)
rsp := &GithubRsp{}
@@ -62,14 +63,15 @@ func TestNativeError(t *testing.T) {
func TestNative(t *testing.T) {
c := client.NewClientCallOptions(mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec())), client.WithAddress("https://api.github.com"))
req := c.NewRequest("github", "/users/vtolstov", nil)
rsp := &GithubRsp{}
err := c.Call(context.TODO(), req, rsp, mhttp.Method(http.MethodGet))
gh := pb.NewGithubService("github", c)
rsp, err := gh.LookupUser(context.TODO(), &pb.LookupUserReq{Username: "vtolstov"})
if err != nil {
t.Fatal(err)
}
if rsp.Name != "Vasiliy Tolstov" {
t.Fatalf("invlid rsp received: %#+v\n", rsp)
t.Fatalf("invalid rsp received: %#+v\n", rsp)
}
}

View File

@@ -0,0 +1,293 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0-devel
// protoc v3.6.1
// source: github.proto
package pb
import (
proto "github.com/golang/protobuf/proto"
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options"
_ "google.golang.org/genproto/googleapis/api/annotations"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4
type LookupUserReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"`
}
func (x *LookupUserReq) Reset() {
*x = LookupUserReq{}
if protoimpl.UnsafeEnabled {
mi := &file_github_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *LookupUserReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*LookupUserReq) ProtoMessage() {}
func (x *LookupUserReq) ProtoReflect() protoreflect.Message {
mi := &file_github_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use LookupUserReq.ProtoReflect.Descriptor instead.
func (*LookupUserReq) Descriptor() ([]byte, []int) {
return file_github_proto_rawDescGZIP(), []int{0}
}
func (x *LookupUserReq) GetUsername() string {
if x != nil {
return x.Username
}
return ""
}
type LookupUserRsp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
}
func (x *LookupUserRsp) Reset() {
*x = LookupUserRsp{}
if protoimpl.UnsafeEnabled {
mi := &file_github_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *LookupUserRsp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*LookupUserRsp) ProtoMessage() {}
func (x *LookupUserRsp) ProtoReflect() protoreflect.Message {
mi := &file_github_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use LookupUserRsp.ProtoReflect.Descriptor instead.
func (*LookupUserRsp) Descriptor() ([]byte, []int) {
return file_github_proto_rawDescGZIP(), []int{1}
}
func (x *LookupUserRsp) GetName() string {
if x != nil {
return x.Name
}
return ""
}
type Error struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
}
func (x *Error) Reset() {
*x = Error{}
if protoimpl.UnsafeEnabled {
mi := &file_github_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Error) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Error) ProtoMessage() {}
func (x *Error) ProtoReflect() protoreflect.Message {
mi := &file_github_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Error.ProtoReflect.Descriptor instead.
func (*Error) Descriptor() ([]byte, []int) {
return file_github_proto_rawDescGZIP(), []int{2}
}
func (x *Error) GetMessage() string {
if x != nil {
return x.Message
}
return ""
}
var File_github_proto protoreflect.FileDescriptor
var file_github_proto_rawDesc = []byte{
0x0a, 0x0c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06,
0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61,
0x70, 0x69, 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e,
0x2d, 0x6f, 0x70, 0x65, 0x6e, 0x61, 0x70, 0x69, 0x76, 0x32, 0x2f, 0x6f, 0x70, 0x74, 0x69, 0x6f,
0x6e, 0x73, 0x2f, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2b, 0x0a, 0x0d, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73,
0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d,
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d,
0x65, 0x22, 0x23, 0x0a, 0x0d, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72, 0x52,
0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x21, 0x0a, 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x12,
0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x9f, 0x01, 0x0a, 0x06, 0x47, 0x69,
0x74, 0x68, 0x75, 0x62, 0x12, 0x94, 0x01, 0x0a, 0x0a, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55,
0x73, 0x65, 0x72, 0x12, 0x15, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x4c, 0x6f, 0x6f,
0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x15, 0x2e, 0x67, 0x69, 0x74,
0x68, 0x75, 0x62, 0x2e, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73, 0x65, 0x72, 0x52, 0x73,
0x70, 0x22, 0x58, 0x92, 0x41, 0x3c, 0x2a, 0x0a, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x55, 0x73,
0x65, 0x72, 0x4a, 0x2e, 0x0a, 0x07, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x12, 0x23, 0x0a,
0x0e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12,
0x11, 0x0a, 0x0f, 0x1a, 0x0d, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x45, 0x72, 0x72,
0x6f, 0x72, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x73,
0x2f, 0x7b, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x7d, 0x42, 0x3b, 0x5a, 0x39, 0x67,
0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x2f, 0x75, 0x6e, 0x69, 0x73, 0x74,
0x61, 0x63, 0x6b, 0x2d, 0x6f, 0x72, 0x67, 0x2f, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2d, 0x74, 0x65,
0x73, 0x74, 0x73, 0x2f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2f, 0x68, 0x74, 0x74, 0x70, 0x2f,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_github_proto_rawDescOnce sync.Once
file_github_proto_rawDescData = file_github_proto_rawDesc
)
func file_github_proto_rawDescGZIP() []byte {
file_github_proto_rawDescOnce.Do(func() {
file_github_proto_rawDescData = protoimpl.X.CompressGZIP(file_github_proto_rawDescData)
})
return file_github_proto_rawDescData
}
var file_github_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_github_proto_goTypes = []interface{}{
(*LookupUserReq)(nil), // 0: github.LookupUserReq
(*LookupUserRsp)(nil), // 1: github.LookupUserRsp
(*Error)(nil), // 2: github.Error
}
var file_github_proto_depIdxs = []int32{
0, // 0: github.Github.LookupUser:input_type -> github.LookupUserReq
1, // 1: github.Github.LookupUser:output_type -> github.LookupUserRsp
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_github_proto_init() }
func file_github_proto_init() {
if File_github_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_github_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*LookupUserReq); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_github_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*LookupUserRsp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_github_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Error); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_github_proto_rawDesc,
NumEnums: 0,
NumMessages: 3,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_github_proto_goTypes,
DependencyIndexes: file_github_proto_depIdxs,
MessageInfos: file_github_proto_msgTypes,
}.Build()
File_github_proto = out.File
file_github_proto_rawDesc = nil
file_github_proto_goTypes = nil
file_github_proto_depIdxs = nil
}

View File

@@ -0,0 +1,41 @@
syntax = "proto3";
package github;
option go_package = "github.com//unistack-org/micro-tests/client/http/proto;pb";
import "google/api/annotations.proto";
import "protoc-gen-openapiv2/options/annotations.proto";
service Github {
rpc LookupUser(LookupUserReq) returns (LookupUserRsp) {
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
operation_id: "LookupUser";
responses: {
key: "default";
value: {
description: "Error response";
schema: {
json_schema: {
ref: ".github.Error";
}
}
}
}
};
option (google.api.http) = {
get: "/users/{username}";
};
};
};
message LookupUserReq {
string username = 1;
};
message LookupUserRsp {
string name = 1;
};
message Error {
string message = 1;
};

View File

@@ -0,0 +1,90 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
package pb
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion6
// GithubClient is the client API for Github service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type GithubClient interface {
LookupUser(ctx context.Context, in *LookupUserReq, opts ...grpc.CallOption) (*LookupUserRsp, error)
}
type githubClient struct {
cc grpc.ClientConnInterface
}
func NewGithubClient(cc grpc.ClientConnInterface) GithubClient {
return &githubClient{cc}
}
func (c *githubClient) LookupUser(ctx context.Context, in *LookupUserReq, opts ...grpc.CallOption) (*LookupUserRsp, error) {
out := new(LookupUserRsp)
err := c.cc.Invoke(ctx, "/github.Github/LookupUser", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// GithubServer is the server API for Github service.
// All implementations must embed UnimplementedGithubServer
// for forward compatibility
type GithubServer interface {
LookupUser(context.Context, *LookupUserReq) (*LookupUserRsp, error)
mustEmbedUnimplementedGithubServer()
}
// UnimplementedGithubServer must be embedded to have forward compatible implementations.
type UnimplementedGithubServer struct {
}
func (*UnimplementedGithubServer) LookupUser(context.Context, *LookupUserReq) (*LookupUserRsp, error) {
return nil, status.Errorf(codes.Unimplemented, "method LookupUser not implemented")
}
func (*UnimplementedGithubServer) mustEmbedUnimplementedGithubServer() {}
func RegisterGithubServer(s *grpc.Server, srv GithubServer) {
s.RegisterService(&_Github_serviceDesc, srv)
}
func _Github_LookupUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(LookupUserReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GithubServer).LookupUser(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/github.Github/LookupUser",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GithubServer).LookupUser(ctx, req.(*LookupUserReq))
}
return interceptor(ctx, in, info, handler)
}
var _Github_serviceDesc = grpc.ServiceDesc{
ServiceName: "github.Github",
HandlerType: (*GithubServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "LookupUser",
Handler: _Github_LookupUser_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "github.proto",
}

View File

@@ -0,0 +1,51 @@
// Code generated by protoc-gen-micro
// source: github.proto
package pb
import (
"context"
micro_api "github.com/unistack-org/micro/v3/api"
micro_client "github.com/unistack-org/micro/v3/client"
micro_server "github.com/unistack-org/micro/v3/server"
)
// NewGithubEndpoints provides api endpoints metdata for Github service
func NewGithubEndpoints() []*micro_api.Endpoint {
var endpoints []*micro_api.Endpoint
endpoint := &micro_api.Endpoint{
Name: "Github.LookupUser",
Path: []string{"/users/{username}"},
Method: []string{"GET"},
Handler: "rpc",
}
endpoints = append(endpoints, endpoint)
return endpoints
}
// GithubService interface
type GithubService interface {
LookupUser(context.Context, *LookupUserReq, ...micro_client.CallOption) (*LookupUserRsp, error)
}
// Micro server stuff
// GithubHandler server handler
type GithubHandler interface {
LookupUser(context.Context, *LookupUserReq, *LookupUserRsp) error
}
// RegisterGithubHandler registers server handler
func RegisterGithubHandler(s micro_server.Server, sh GithubHandler, opts ...micro_server.HandlerOption) error {
type github interface {
LookupUser(context.Context, *LookupUserReq, *LookupUserRsp) error
}
type Github struct {
github
}
h := &githubHandler{sh}
for _, endpoint := range NewGithubEndpoints() {
opts = append(opts, micro_api.WithEndpoint(endpoint))
}
return s.Handle(s.NewHandler(&Github{h}, opts...))
}

View File

@@ -0,0 +1,94 @@
// Code generated by protoc-gen-micro
// source: github.proto
package pb
import (
"context"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/gorilla/mux"
micro_client_http "github.com/unistack-org/micro-client-http/v3"
micro_api "github.com/unistack-org/micro/v3/api"
micro_client "github.com/unistack-org/micro/v3/client"
micro_server "github.com/unistack-org/micro/v3/server"
)
var (
_ micro_server.Option
_ micro_client.Option
)
type githubService struct {
c micro_client.Client
name string
}
// Micro client stuff
// NewGithubService create new service client
func NewGithubService(name string, c micro_client.Client) GithubService {
return &githubService{c: c, name: name}
}
func (c *githubService) LookupUser(ctx context.Context, req *LookupUserReq, opts ...micro_client.CallOption) (*LookupUserRsp, error) {
errmap := make(map[string]interface{}, 1)
errmap["default"] = &Error{}
nopts := append(opts,
micro_client_http.Method("GET"),
micro_client_http.Path("/users/{username}"),
micro_client_http.Body(""),
micro_client_http.ErrorMap(errmap),
)
rsp := &LookupUserRsp{}
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Github.LookupUser", req), rsp, nopts...)
if err != nil {
return nil, err
}
return rsp, nil
}
// Error method to satisfy error interface
func (e *Error) Error() string {
return fmt.Sprintf("%#v", e)
}
// Micro server stuff
type githubHandler struct {
GithubHandler
}
func (h *githubHandler) LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error {
return h.GithubHandler.LookupUser(ctx, req, rsp)
}
func Register(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {
v := reflect.ValueOf(h)
methods := v.NumMethod()
if methods < 1 {
return fmt.Errorf("invalid handler specified: %#+v", h)
}
for _, ep := range eps {
idx := strings.Index(ep.Name, ".")
if idx < 1 || len(ep.Name) <= idx {
return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)
}
name := ep.Name[idx+1:]
m := v.MethodByName(name)
if !m.IsValid() || m.IsZero() {
return fmt.Errorf("invalid handler, method %s not found", name)
}
rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))
if !ok {
return fmt.Errorf("invalid handler: %#+v", m.Interface())
}
r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)
}
return nil
}

7
client/http/tools.go Normal file
View File

@@ -0,0 +1,7 @@
// +build tools
package main
import (
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
)