client/mock: add new tests
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
66
client/mock/mock_test.go
Normal file
66
client/mock/mock_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro-client-mock/v3"
|
||||
jsoncodec "go.unistack.org/micro-codec-json/v3"
|
||||
pb "go.unistack.org/micro-tests/client/mock/proto"
|
||||
"go.unistack.org/micro/v3/client"
|
||||
"go.unistack.org/micro/v3/errors"
|
||||
)
|
||||
|
||||
func TestCallWithoutError(t *testing.T) {
|
||||
c := mock.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec()))
|
||||
|
||||
if err := c.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reqbuf := []byte(`{"username": "vtolstov"}`)
|
||||
rspbuf := []byte(`{"name": "Vasiliy Tolstov"}`)
|
||||
er := c.ExpectRequest(c.NewRequest("github", "Github.LookupUser", reqbuf))
|
||||
er.WillReturnResponse(rspbuf)
|
||||
er.WillDelayFor(10 * time.Millisecond)
|
||||
|
||||
gh := pb.NewGithubClient("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("invalid rsp received: %#+v\n", rsp)
|
||||
}
|
||||
|
||||
if err := c.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallWithtError(t *testing.T) {
|
||||
c := mock.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec()))
|
||||
|
||||
if err := c.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reqbuf := []byte(`{"username": "vtolstov"}`)
|
||||
rspbuf := []byte(`{"name": "Vasiliy Tolstov"}`)
|
||||
er := c.ExpectRequest(c.NewRequest("github", "Github.LookupUser", reqbuf))
|
||||
er.WillReturnResponse(rspbuf)
|
||||
er.WillDelayFor(10 * time.Millisecond)
|
||||
er.WillReturnError(errors.InternalServerError("test", "internal server error"))
|
||||
|
||||
gh := pb.NewGithubClient("github", c)
|
||||
|
||||
rsp, err := gh.LookupUser(context.TODO(), &pb.LookupUserReq{Username: "vtolstov"})
|
||||
if err == nil || rsp != nil {
|
||||
t.Fatal("call must return error")
|
||||
}
|
||||
|
||||
if err := c.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
19
client/mock/proto/github.pb.go
Normal file
19
client/mock/proto/github.pb.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package pb
|
||||
|
||||
import (
|
||||
_ "go.unistack.org/micro-proto/v3/api"
|
||||
_ "go.unistack.org/micro-proto/v3/openapiv3"
|
||||
)
|
||||
|
||||
type LookupUserReq struct {
|
||||
Username string `json:"username,omitempty"`
|
||||
}
|
||||
|
||||
type LookupUserRsp struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
DocumentationUrl string `json:"documentation_url,omitempty"`
|
||||
}
|
28
client/mock/proto/github_micro.pb.go
Normal file
28
client/mock/proto/github_micro.pb.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package pb
|
||||
|
||||
import (
|
||||
context "context"
|
||||
|
||||
v3 "go.unistack.org/micro-server-http/v3"
|
||||
client "go.unistack.org/micro/v3/client"
|
||||
)
|
||||
|
||||
var GithubName = "Github"
|
||||
|
||||
var GithubServerEndpoints = []v3.EndpointMetadata{
|
||||
{
|
||||
Name: "Github.LookupUser",
|
||||
Path: "/users/{username}",
|
||||
Method: "GET",
|
||||
Body: "",
|
||||
Stream: false,
|
||||
},
|
||||
}
|
||||
|
||||
type GithubClient interface {
|
||||
LookupUser(ctx context.Context, req *LookupUserReq, opts ...client.CallOption) (*LookupUserRsp, error)
|
||||
}
|
||||
|
||||
type GithubServer interface {
|
||||
LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error
|
||||
}
|
59
client/mock/proto/github_micro_http.pb.go
Normal file
59
client/mock/proto/github_micro_http.pb.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package pb
|
||||
|
||||
import (
|
||||
context "context"
|
||||
http "net/http"
|
||||
|
||||
v3 "go.unistack.org/micro-client-http/v3"
|
||||
v31 "go.unistack.org/micro-server-http/v3"
|
||||
client "go.unistack.org/micro/v3/client"
|
||||
server "go.unistack.org/micro/v3/server"
|
||||
)
|
||||
|
||||
type githubClient struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewGithubClient(name string, c client.Client) GithubClient {
|
||||
return &githubClient{c: c, name: name}
|
||||
}
|
||||
|
||||
func (c *githubClient) LookupUser(ctx context.Context, req *LookupUserReq, opts ...client.CallOption) (*LookupUserRsp, error) {
|
||||
errmap := make(map[string]interface{}, 1)
|
||||
errmap["default"] = &Error{}
|
||||
opts = append(opts,
|
||||
v3.ErrorMap(errmap),
|
||||
)
|
||||
opts = append(opts,
|
||||
v3.Method(http.MethodGet),
|
||||
v3.Path("/users/{username}"),
|
||||
)
|
||||
rsp := &LookupUserRsp{}
|
||||
err := c.c.Call(ctx, c.c.NewRequest(c.name, "Github.LookupUser", req), rsp, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
type githubServer struct {
|
||||
GithubServer
|
||||
}
|
||||
|
||||
func (h *githubServer) LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error {
|
||||
return h.GithubServer.LookupUser(ctx, req, rsp)
|
||||
}
|
||||
|
||||
func RegisterGithubServer(s server.Server, sh GithubServer, opts ...server.HandlerOption) error {
|
||||
type github interface {
|
||||
LookupUser(ctx context.Context, req *LookupUserReq, rsp *LookupUserRsp) error
|
||||
}
|
||||
type Github struct {
|
||||
github
|
||||
}
|
||||
h := &githubServer{sh}
|
||||
var nopts []server.HandlerOption
|
||||
nopts = append(nopts, v31.HandlerEndpoints(GithubServerEndpoints))
|
||||
return s.Handle(s.NewHandler(&Github{h}, append(nopts, opts...)...))
|
||||
}
|
Reference in New Issue
Block a user