2020-10-30 23:27:33 +03:00
|
|
|
package http_test
|
|
|
|
|
|
|
|
import (
|
2021-03-29 18:46:50 +03:00
|
|
|
"bytes"
|
2020-11-03 02:24:07 +03:00
|
|
|
"context"
|
2020-10-30 23:27:33 +03:00
|
|
|
"fmt"
|
2021-03-25 14:46:32 +03:00
|
|
|
"io"
|
2020-10-30 23:27:33 +03:00
|
|
|
"io/ioutil"
|
2021-04-13 11:35:55 +03:00
|
|
|
"mime/multipart"
|
2020-10-30 23:27:33 +03:00
|
|
|
"net/http"
|
2021-04-10 01:32:25 +03:00
|
|
|
"net/url"
|
2021-03-25 14:46:32 +03:00
|
|
|
"strings"
|
2020-10-30 23:27:33 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
httpcli "github.com/unistack-org/micro-client-http/v3"
|
|
|
|
jsoncodec "github.com/unistack-org/micro-codec-json/v3"
|
|
|
|
jsonpbcodec "github.com/unistack-org/micro-codec-jsonpb/v3"
|
2021-04-18 15:52:26 +03:00
|
|
|
urlencodecodec "github.com/unistack-org/micro-codec-urlencode/v3"
|
2021-03-25 14:46:32 +03:00
|
|
|
vmeter "github.com/unistack-org/micro-meter-victoriametrics/v3"
|
2021-01-16 02:49:58 +03:00
|
|
|
httpsrv "github.com/unistack-org/micro-server-http/v3"
|
2021-02-06 19:08:01 +03:00
|
|
|
pb "github.com/unistack-org/micro-tests/server/http/proto"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
2021-03-06 23:30:04 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
lwrapper "github.com/unistack-org/micro/v3/logger/wrapper"
|
2021-02-06 19:08:01 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2021-03-25 14:46:32 +03:00
|
|
|
handler "github.com/unistack-org/micro/v3/meter/handler"
|
|
|
|
mwrapper "github.com/unistack-org/micro/v3/meter/wrapper"
|
2021-02-12 20:50:02 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-10-30 23:27:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2021-03-25 14:46:32 +03:00
|
|
|
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
|
2020-10-30 23:27:33 +03:00
|
|
|
)
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
type Handler struct {
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:35:55 +03:00
|
|
|
func multipartHandler(w http.ResponseWriter, r *http.Request) {
|
2021-04-29 22:51:46 +03:00
|
|
|
//fmt.Printf("%#+v\n", r)
|
2021-04-13 11:35:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func upload(client *http.Client, url string, values map[string]io.Reader) error {
|
|
|
|
var err error
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
w := multipart.NewWriter(b)
|
|
|
|
for key, r := range values {
|
|
|
|
var fw io.Writer
|
|
|
|
if x, ok := r.(io.Closer); ok {
|
|
|
|
defer x.Close()
|
|
|
|
}
|
|
|
|
if fw, err = w.CreateFormFile(key, key); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(fw, r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// Don't forget to close the multipart writer.
|
|
|
|
// If you don't close it, your request will be missing the terminating boundary.
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
// Now that you have a form, you can submit it to your handler.
|
|
|
|
req, err := http.NewRequest("POST", url, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Don't forget to set the content type, this will contain the boundary.
|
|
|
|
req.Header.Set("Content-Type", w.FormDataContentType())
|
|
|
|
|
|
|
|
// Submit the request
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the response
|
|
|
|
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
|
|
|
|
err = fmt.Errorf("bad status: %s", res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipart(t *testing.T) {
|
|
|
|
reg := register.NewRegister()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// create server
|
|
|
|
srv := httpsrv.NewServer(
|
|
|
|
server.Name("helloworld"),
|
|
|
|
server.Register(reg),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2021-04-18 15:52:26 +03:00
|
|
|
server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
|
2021-04-13 11:35:55 +03:00
|
|
|
httpsrv.PathHandler("/upload", multipartHandler),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := srv.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := &Handler{t: t}
|
|
|
|
pb.RegisterTestServer(srv, h)
|
|
|
|
|
|
|
|
// start server
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup server
|
|
|
|
service, err := reg.LookupService(ctx, "helloworld")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("test multipart upload")
|
|
|
|
values := make(map[string]io.Reader, 2)
|
|
|
|
values["first.txt"] = bytes.NewReader([]byte("first content"))
|
|
|
|
values["second.txt"] = bytes.NewReader([]byte("second content"))
|
|
|
|
err = upload(http.DefaultClient, "http://"+service[0].Nodes[0].Address+"/upload", values)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 00:44:22 +03:00
|
|
|
func NewServerHandlerWrapper(t *testing.T) server.HandlerWrapper {
|
2021-02-06 19:08:01 +03:00
|
|
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
2021-04-30 00:44:22 +03:00
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("metadata empty")
|
|
|
|
}
|
2021-05-01 01:25:38 +03:00
|
|
|
if v, ok := md.Get("Authorization"); ok && v == "test" {
|
|
|
|
type err struct {
|
|
|
|
detail string
|
|
|
|
}
|
|
|
|
nmd := metadata.New(1)
|
|
|
|
nmd.Set("my-key", "my-val")
|
|
|
|
metadata.SetOutgoingContext(ctx, nmd)
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusUnauthorized)
|
|
|
|
return httpsrv.SetError(&pb.CallRsp{Rsp: "name_my_name"})
|
|
|
|
}
|
2021-02-06 19:08:01 +03:00
|
|
|
return fn(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:25:27 +03:00
|
|
|
func (h *Handler) CallDouble(ctx context.Context, req *pb.CallReq, rsp *pb.CallRsp) error {
|
|
|
|
rsp.Rsp = "name_double"
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusCreated)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:36:58 +03:00
|
|
|
func (h *Handler) CallRepeatedString(ctx context.Context, req *pb.CallReq, rsp *pb.CallRsp) error {
|
|
|
|
if len(req.StringIds) != 2 || req.StringIds[0] != "123" {
|
|
|
|
h.t.Fatalf("invalid reflect merging, strings_ids invalid: %v", req.StringIds)
|
|
|
|
}
|
|
|
|
rsp.Rsp = "name_my_name"
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusCreated)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (h *Handler) CallRepeatedInt64(ctx context.Context, req *pb.CallReq, rsp *pb.CallRsp) error {
|
|
|
|
if len(req.Int64Ids) != 2 || req.Int64Ids[0] != 123 {
|
|
|
|
h.t.Fatalf("invalid reflect merging, int64_ids invalid: %v", req.Int64Ids)
|
2021-04-19 02:16:00 +03:00
|
|
|
}
|
|
|
|
rsp.Rsp = "name_my_name"
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusCreated)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
func (h *Handler) Call(ctx context.Context, req *pb.CallReq, rsp *pb.CallRsp) error {
|
|
|
|
if req.Nested == nil {
|
|
|
|
h.t.Fatalf("invalid reflect merging")
|
|
|
|
}
|
|
|
|
if len(req.Nested.Uint64Args) != 3 || req.Nested.Uint64Args[2].Value != 3 {
|
|
|
|
h.t.Fatalf("invalid reflect merging")
|
|
|
|
}
|
2021-02-27 13:51:39 +03:00
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
2021-02-06 19:08:01 +03:00
|
|
|
if !ok {
|
|
|
|
h.t.Fatalf("context without metadata")
|
|
|
|
}
|
|
|
|
if _, ok := md.Get("User-Agent"); !ok {
|
|
|
|
h.t.Fatalf("context metadata does not have User-Agent header")
|
|
|
|
}
|
|
|
|
if req.Name != "my_name" {
|
|
|
|
h.t.Fatalf("invalid req received: %#+v", req)
|
|
|
|
}
|
|
|
|
rsp.Rsp = "name_my_name"
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusCreated)
|
2021-03-06 22:24:49 +03:00
|
|
|
md = metadata.New(1)
|
|
|
|
md.Set("my-key", "my-val")
|
|
|
|
metadata.SetOutgoingContext(ctx, md)
|
2021-02-06 19:08:01 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) CallError(ctx context.Context, req *pb.CallReq1, rsp *pb.CallRsp1) error {
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
2021-02-27 13:51:39 +03:00
|
|
|
return httpsrv.SetError(&pb.Error{Msg: "my_error"})
|
2021-02-06 19:08:01 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:32:25 +03:00
|
|
|
func TestNativeFormUrlencoded(t *testing.T) {
|
|
|
|
reg := register.NewRegister()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// create server
|
|
|
|
srv := httpsrv.NewServer(
|
|
|
|
server.Name("helloworld"),
|
|
|
|
server.Register(reg),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2021-04-18 15:52:26 +03:00
|
|
|
server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
|
2021-04-30 00:44:22 +03:00
|
|
|
//server.WrapHandler(NewServerHandlerWrapper()),
|
2021-04-10 01:32:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := srv.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h := &Handler{t: t}
|
|
|
|
pb.RegisterTestServer(srv, h)
|
|
|
|
|
|
|
|
// start server
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup server
|
|
|
|
service, err := reg.LookupService(ctx, "helloworld")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("test net/http client with application/x-www-form-urlencoded")
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("req", "fookey")
|
|
|
|
data.Set("arg1", "arg1val")
|
|
|
|
data.Add("nested.uint64_args", "1")
|
|
|
|
data.Add("nested.uint64_args", "2")
|
|
|
|
data.Add("nested.uint64_args", "3")
|
|
|
|
// make request
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/v1/test/call/my_name", service[0].Nodes[0].Address), strings.NewReader(data.Encode())) // URL-encoded payload
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
//req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
|
|
|
|
|
|
|
rsp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(rsp.Body)
|
|
|
|
rsp.Body.Close()
|
|
|
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.StatusCode != http.StatusCreated {
|
|
|
|
t.Fatalf("invalid status received: %#+v\n%s\n", rsp, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(b); s != `{"rsp":"name_my_name"}` {
|
|
|
|
t.Fatalf("Expected response %s, got %s", `{"rsp":"name_my_name"}`, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := rsp.Header.Get("My-Key"); v != "my-val" {
|
|
|
|
t.Fatalf("empty response header: %#+v", rsp.Header)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("test native client with application/x-www-form-urlencoded")
|
2021-04-18 15:52:26 +03:00
|
|
|
cli := client.NewClientCallOptions(
|
|
|
|
httpcli.NewClient(
|
|
|
|
client.ContentType("application/x-www-form-urlencoded"),
|
|
|
|
client.Codec("application/json", jsonpbcodec.NewCodec()),
|
|
|
|
client.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
|
|
|
|
),
|
|
|
|
client.WithAddress(fmt.Sprintf("http://%s", service[0].Nodes[0].Address)))
|
2021-04-10 01:32:25 +03:00
|
|
|
|
|
|
|
svc1 := pb.NewTestClient("helloworld", cli)
|
|
|
|
nrsp, err := svc1.Call(ctx, &pb.CallReq{
|
|
|
|
Name: "my_name",
|
|
|
|
Arg1: "arg1val",
|
|
|
|
Nested: &pb.Nested{Uint64Args: []*wrapperspb.UInt64Value{
|
|
|
|
&wrapperspb.UInt64Value{Value: 1},
|
|
|
|
&wrapperspb.UInt64Value{Value: 2},
|
|
|
|
&wrapperspb.UInt64Value{Value: 3},
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nrsp.Rsp != "name_my_name" {
|
|
|
|
t.Fatalf("invalid response: %#+v\n", nrsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop server
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
func TestNativeClientServer(t *testing.T) {
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
2021-02-06 19:08:01 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-02-07 13:31:39 +03:00
|
|
|
var mwfOk bool
|
|
|
|
mwf := func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
mwfOk = true
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:46:32 +03:00
|
|
|
m := vmeter.NewMeter()
|
2021-02-06 19:08:01 +03:00
|
|
|
// create server
|
|
|
|
srv := httpsrv.NewServer(
|
2021-03-25 14:46:32 +03:00
|
|
|
server.Meter(m),
|
2021-02-06 19:08:01 +03:00
|
|
|
server.Name("helloworld"),
|
|
|
|
server.Register(reg),
|
|
|
|
server.Codec("application/json", jsonpbcodec.NewCodec()),
|
2021-04-18 15:52:26 +03:00
|
|
|
server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
|
2021-03-25 14:46:32 +03:00
|
|
|
server.WrapHandler(mwrapper.NewHandlerWrapper(mwrapper.Meter(m))),
|
2021-04-29 22:51:46 +03:00
|
|
|
server.WrapHandler(lwrapper.NewServerHandlerWrapper(lwrapper.WithEnabled(false), lwrapper.WithLevel(logger.ErrorLevel))),
|
2021-02-07 13:31:39 +03:00
|
|
|
httpsrv.Middleware(mwf),
|
2021-04-30 00:44:22 +03:00
|
|
|
server.WrapHandler(NewServerHandlerWrapper(t)),
|
2021-02-06 19:08:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
h := &Handler{t: t}
|
2021-03-09 14:25:27 +03:00
|
|
|
|
|
|
|
// init server
|
|
|
|
if err := srv.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pb.RegisterTestServer(srv, h); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := pb.RegisterTestDoubleServer(srv, h); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-03-29 18:46:50 +03:00
|
|
|
if err := handler.RegisterMeterServer(srv, handler.NewHandler(handler.Meter(srv.Options().Meter))); err != nil {
|
2021-03-25 14:46:32 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-06 19:08:01 +03:00
|
|
|
// start server
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup server
|
|
|
|
service, err := reg.LookupService(ctx, "helloworld")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := client.NewClientCallOptions(httpcli.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsonpbcodec.NewCodec())), client.WithAddress(fmt.Sprintf("http://%s", service[0].Nodes[0].Address)))
|
|
|
|
|
2021-03-09 14:25:27 +03:00
|
|
|
svc1 := pb.NewTestClient("helloworld", cli)
|
|
|
|
rsp, err := svc1.Call(ctx, &pb.CallReq{
|
2021-02-06 19:08:01 +03:00
|
|
|
Name: "my_name",
|
2021-03-25 14:46:32 +03:00
|
|
|
Nested: &pb.Nested{Uint64Args: []*wrapperspb.UInt64Value{
|
|
|
|
&wrapperspb.UInt64Value{Value: 1},
|
|
|
|
&wrapperspb.UInt64Value{Value: 2},
|
|
|
|
&wrapperspb.UInt64Value{Value: 3},
|
2021-02-06 19:08:01 +03:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.Rsp != "name_my_name" {
|
|
|
|
t.Fatalf("invalid response: %#+v\n", rsp)
|
|
|
|
}
|
2021-02-07 13:31:39 +03:00
|
|
|
|
|
|
|
if !mwfOk {
|
|
|
|
t.Fatalf("http middleware not works")
|
|
|
|
}
|
2021-03-09 14:25:27 +03:00
|
|
|
|
2021-03-29 18:46:50 +03:00
|
|
|
hb, err := jsonpbcodec.NewCodec().Marshal(&pb.CallReq{
|
|
|
|
Nested: &pb.Nested{Uint64Args: []*wrapperspb.UInt64Value{
|
|
|
|
&wrapperspb.UInt64Value{Value: 1},
|
|
|
|
&wrapperspb.UInt64Value{Value: 2},
|
|
|
|
&wrapperspb.UInt64Value{Value: 3},
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("test rsp code from net/http client to native micro http server")
|
|
|
|
hr, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("http://%s/v1/test/call/my_name", service[0].Nodes[0].Address), bytes.NewReader(hb))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
hr.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
hrsp, err := http.DefaultClient.Do(hr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer hrsp.Body.Close()
|
|
|
|
buf, err := io.ReadAll(hrsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hrsp.StatusCode != 201 {
|
|
|
|
t.Fatalf("invalid rsp code %#+v", hrsp)
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:25:27 +03:00
|
|
|
t.Logf("test second server")
|
|
|
|
svc2 := pb.NewTestDoubleClient("helloworld", cli)
|
|
|
|
rsp, err = svc2.CallDouble(ctx, &pb.CallReq{
|
|
|
|
Name: "my_name",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.Rsp != "name_double" {
|
|
|
|
t.Fatalf("invalid response: %#+v\n", rsp)
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:46:50 +03:00
|
|
|
hrsp, err = http.Get(fmt.Sprintf("http://%s/metrics", service[0].Nodes[0].Address))
|
2021-03-25 14:46:32 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:46:50 +03:00
|
|
|
buf, err = io.ReadAll(hrsp.Body)
|
2021-03-25 14:46:32 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-04-29 22:51:46 +03:00
|
|
|
if !strings.Contains(string(buf), `micro_server_request_total`) {
|
2021-03-25 14:46:32 +03:00
|
|
|
t.Fatalf("rsp not contains metrics: %s", buf)
|
|
|
|
}
|
2021-02-06 19:08:01 +03:00
|
|
|
// stop server
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNativeServer(t *testing.T) {
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
2021-02-06 19:08:01 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// create server
|
|
|
|
srv := httpsrv.NewServer(
|
|
|
|
server.Name("helloworld"),
|
|
|
|
server.Register(reg),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2021-04-18 15:52:26 +03:00
|
|
|
server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
|
2021-04-30 00:44:22 +03:00
|
|
|
server.WrapHandler(NewServerHandlerWrapper(t)),
|
2021-02-06 19:08:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
h := &Handler{t: t}
|
2021-02-27 13:51:39 +03:00
|
|
|
pb.RegisterTestServer(srv, h)
|
2021-02-06 19:08:01 +03:00
|
|
|
|
|
|
|
// start server
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup server
|
|
|
|
service, err := reg.LookupService(ctx, "helloworld")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make request
|
2021-04-30 00:44:22 +03:00
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/v1/test/call/my_name?req=key&arg1=arg1&arg2=12345&nested.string_args=str1&nested.string_args=str2&nested.uint64_args=1&nested.uint64_args=2&nested.uint64_args=3", service[0].Nodes[0].Address), nil)
|
2021-02-06 19:08:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-04-30 00:44:22 +03:00
|
|
|
req.Header.Set("Authorization", "test")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
rsp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-05-01 01:25:38 +03:00
|
|
|
if rsp.StatusCode != http.StatusUnauthorized {
|
2021-02-06 19:08:01 +03:00
|
|
|
t.Fatalf("invalid status received: %#+v\n", rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(rsp.Body)
|
|
|
|
rsp.Body.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(b); s != `{"rsp":"name_my_name"}` {
|
|
|
|
t.Fatalf("Expected response %s, got %s", `{"rsp":"name_my_name"}`, s)
|
|
|
|
}
|
|
|
|
|
2021-03-06 22:24:49 +03:00
|
|
|
if v := rsp.Header.Get("My-Key"); v != "my-val" {
|
|
|
|
t.Fatalf("empty response header: %#+v", rsp.Header)
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
// make request with error
|
|
|
|
rsp, err = http.Post(fmt.Sprintf("http://%s/v1/test/callerror/my_name", service[0].Nodes[0].Address), "application/json", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if rsp.StatusCode != http.StatusBadRequest {
|
|
|
|
t.Fatalf("invalid status received: %#+v\n", rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = ioutil.ReadAll(rsp.Body)
|
|
|
|
rsp.Body.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(b); s != `{"msg":"my_error"}` {
|
|
|
|
t.Fatalf("Expected response %s, got %s", `{"msg":"my_error"}`, s)
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:36:58 +03:00
|
|
|
rsp, err = http.Post(fmt.Sprintf("http://%s/v1/test/call_repeated_string/?string_ids=123&string_ids=321", service[0].Nodes[0].Address), "application/json", nil)
|
2021-04-19 02:16:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if rsp.StatusCode != http.StatusCreated {
|
|
|
|
buf, err := io.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid status received: %#+v err: %v\n", rsp, err)
|
|
|
|
}
|
|
|
|
t.Fatalf("invalid status received: %#+v buf: %s\n", rsp, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = ioutil.ReadAll(rsp.Body)
|
|
|
|
rsp.Body.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(b); s != `{"rsp":"name_my_name"}` {
|
|
|
|
t.Fatalf("Expected response %s, got %s", `{"rsp":"name_my_name"}`, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := client.NewClientCallOptions(httpcli.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec())), client.WithAddress("http://"+service[0].Nodes[0].Address))
|
|
|
|
pbc := pb.NewTestClient("test", c)
|
|
|
|
|
2021-04-19 11:36:58 +03:00
|
|
|
t.Logf("test with string_ids")
|
|
|
|
prsp, err := pbc.CallRepeatedString(context.TODO(), &pb.CallReq{StringIds: []string{"123", "321"}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prsp.Rsp != "name_my_name" {
|
|
|
|
t.Fatalf("invalid rsp received: %#+v\n", rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("test with int64_ids")
|
|
|
|
prsp, err = pbc.CallRepeatedInt64(context.TODO(), &pb.CallReq{Int64Ids: []int64{123, 321}})
|
2021-04-19 02:16:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prsp.Rsp != "name_my_name" {
|
|
|
|
t.Fatalf("invalid rsp received: %#+v\n", rsp)
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
// stop server
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:59:40 +03:00
|
|
|
func TestHTTPHandler(t *testing.T) {
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
2020-11-03 02:24:07 +03:00
|
|
|
ctx := context.Background()
|
2020-10-30 23:27:33 +03:00
|
|
|
|
|
|
|
// create server
|
2021-03-09 23:59:40 +03:00
|
|
|
srv := httpsrv.NewServer(
|
|
|
|
server.Register(reg),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2021-04-18 15:52:26 +03:00
|
|
|
server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
|
2021-03-09 23:59:40 +03:00
|
|
|
)
|
2020-10-30 23:27:33 +03:00
|
|
|
|
|
|
|
// create server mux
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(`hello world`))
|
|
|
|
})
|
|
|
|
|
|
|
|
// create handler
|
|
|
|
hd := srv.NewHandler(mux)
|
|
|
|
|
|
|
|
// register handler
|
|
|
|
if err := srv.Handle(hd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// start server
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup server
|
2021-01-29 16:47:26 +03:00
|
|
|
service, err := reg.LookupService(ctx, server.DefaultName)
|
2020-10-30 23:27:33 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make request
|
|
|
|
rsp, err := http.Get(fmt.Sprintf("http://%s", service[0].Nodes[0].Address))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(b); s != "hello world" {
|
|
|
|
t.Fatalf("Expected response %s, got %s", "hello world", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop server
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 23:59:40 +03:00
|
|
|
|
|
|
|
func TestHTTPServer(t *testing.T) {
|
|
|
|
reg := register.NewRegister()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// create server mux
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(`hello world`))
|
|
|
|
})
|
|
|
|
|
|
|
|
// create server
|
|
|
|
srv := httpsrv.NewServer(
|
|
|
|
server.Register(reg),
|
|
|
|
httpsrv.Server(&http.Server{Handler: mux}),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2021-04-18 15:52:26 +03:00
|
|
|
server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
|
2021-03-09 23:59:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := srv.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// start server
|
|
|
|
if err := srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup server
|
|
|
|
service, err := reg.LookupService(ctx, server.DefaultName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make request
|
|
|
|
rsp, err := http.Get(fmt.Sprintf("http://%s", service[0].Nodes[0].Address))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(b); s != "hello world" {
|
|
|
|
t.Fatalf("Expected response %s, got %s", "hello world", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop server
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|