From e6acf99ce0629b4970e77378adf3d6eafc9249b2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 30 Apr 2021 00:44:22 +0300 Subject: [PATCH] server/http: add metadata wrapper test Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 7 ++++--- server/http/http_test.go | 23 +++++++++++++++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 6462a7c..1afbfa0 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,7 @@ require ( github.com/unistack-org/micro-proto v0.0.2-0.20210227213711-77c7563bd01e github.com/unistack-org/micro-router-register/v3 v3.2.2 github.com/unistack-org/micro-server-grpc/v3 v3.3.6 - github.com/unistack-org/micro-server-http/v3 v3.3.14 + github.com/unistack-org/micro-server-http/v3 v3.3.15 github.com/unistack-org/micro-server-tcp/v3 v3.3.2 github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.2.0 github.com/unistack-org/micro/v3 v3.3.17 diff --git a/go.sum b/go.sum index 7b75c92..f4e22c9 100644 --- a/go.sum +++ b/go.sum @@ -484,8 +484,8 @@ github.com/unistack-org/micro-router-register/v3 v3.2.2 h1:lYCymDHkJfhZWYQ4+Sb7F github.com/unistack-org/micro-router-register/v3 v3.2.2/go.mod h1:Y9Qtlg4NHqq5rR6X6Jm+04LoSJMi7/OOCm2mRueZYTE= github.com/unistack-org/micro-server-grpc/v3 v3.3.6 h1:z4Koe7K4tVVs4XAo6C34XAvg+HH4/yW6pf/RFyTjkq0= github.com/unistack-org/micro-server-grpc/v3 v3.3.6/go.mod h1:nmh12MeWm8bTWwkf6pSxrcO+jlcV5VgpN3QAtcNm9C8= -github.com/unistack-org/micro-server-http/v3 v3.3.14 h1:fGRRUHq+XWNqB9ENyFVVq4cLLLarZ6Beh57J1/jcTG0= -github.com/unistack-org/micro-server-http/v3 v3.3.14/go.mod h1:dToX+lswxUMd6d4NJSxC16ZjsFrw3F3qhu+mw9r0fhs= +github.com/unistack-org/micro-server-http/v3 v3.3.15 h1:ZzkPnZGugtM10MjCG1aBz4oqU7AwZSyQS9WFGz/yY8k= +github.com/unistack-org/micro-server-http/v3 v3.3.15/go.mod h1:LRST6u2eH35P/II+5OjvtlLdyWN+1FelISIXNnIKtQo= github.com/unistack-org/micro-server-tcp/v3 v3.3.2 h1:x5FjxOuI61Jy6OzgCWElCRCWP9NffGNWajz/WJPEzdQ= github.com/unistack-org/micro-server-tcp/v3 v3.3.2/go.mod h1:981+kGQT3Js2LJrNaI2AyP/NcbnL7GU8WTnlRxj07Ao= github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.2.0 h1:PvemkpeCVUWfCoKwt1XmJ8uGK9My/7T29qOVxtYJohw= @@ -592,8 +592,9 @@ golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLd golang.org/x/net v0.0.0-20210331060903-cb1fcc7394e5/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= -golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 h1:0PC75Fz/kyMGhL0e1QnypqK2kQMqKt9csD1GnMJR+Zk= golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= diff --git a/server/http/http_test.go b/server/http/http_test.go index 9717826..fa918b4 100644 --- a/server/http/http_test.go +++ b/server/http/http_test.go @@ -130,10 +130,14 @@ func TestMultipart(t *testing.T) { } } -func NewServerHandlerWrapper() server.HandlerWrapper { +func NewServerHandlerWrapper(t *testing.T) server.HandlerWrapper { return func(fn server.HandlerFunc) server.HandlerFunc { return func(ctx context.Context, req server.Request, rsp interface{}) error { - //fmt.Printf("wrap ctx: %s\n", req.Service()) + md, ok := metadata.FromIncomingContext(ctx) + if !ok { + t.Fatal("metadata empty") + } + t.Logf("md: %v", md) return fn(ctx, req, rsp) } } @@ -202,7 +206,7 @@ func TestNativeFormUrlencoded(t *testing.T) { server.Register(reg), server.Codec("application/json", jsoncodec.NewCodec()), server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()), - //server.WrapHandler(NewServerHandlerWrapper()), + //server.WrapHandler(NewServerHandlerWrapper()), ) if err := srv.Init(); err != nil { @@ -323,7 +327,7 @@ func TestNativeClientServer(t *testing.T) { server.WrapHandler(mwrapper.NewHandlerWrapper(mwrapper.Meter(m))), server.WrapHandler(lwrapper.NewServerHandlerWrapper(lwrapper.WithEnabled(false), lwrapper.WithLevel(logger.ErrorLevel))), httpsrv.Middleware(mwf), - server.WrapHandler(NewServerHandlerWrapper()), + server.WrapHandler(NewServerHandlerWrapper(t)), ) h := &Handler{t: t} @@ -459,7 +463,7 @@ func TestNativeServer(t *testing.T) { server.Register(reg), server.Codec("application/json", jsoncodec.NewCodec()), server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()), - //server.WrapHandler(NewServerHandlerWrapper()), + server.WrapHandler(NewServerHandlerWrapper(t)), ) h := &Handler{t: t} @@ -485,10 +489,17 @@ func TestNativeServer(t *testing.T) { } // make request - rsp, err := http.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), "application/json", nil) + 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) if err != nil { t.Fatal(err) } + req.Header.Set("Authorization", "test") + req.Header.Set("Content-Type", "application/json") + rsp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + if rsp.StatusCode != http.StatusCreated { t.Fatalf("invalid status received: %#+v\n", rsp) }