From afea6b0a8e659183e88a4626e1bdf8e7c3a19a84 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 9 Mar 2021 23:59:40 +0300 Subject: [PATCH] server/http: improve tests Signed-off-by: Vasiliy Tolstov --- go.mod | 3 +- go.sum | 4 +-- server/http/http_test.go | 69 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index e1bc6aa..df7d167 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.2.4 - github.com/unistack-org/micro-server-http/v3 v3.2.10 + github.com/unistack-org/micro-server-http/v3 v3.2.11 github.com/unistack-org/micro-server-tcp/v3 v3.2.2 github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.2.0 github.com/unistack-org/micro/v3 v3.2.20 @@ -40,6 +40,7 @@ require ( //replace github.com/unistack-org/micro-client-grpc/v3 => ../micro-client-grpc //replace github.com/unistack-org/micro-server-grpc/v3 => ../micro-server-grpc //replace github.com/unistack-org/micro-server-http/v3 => ../micro-server-http + //replace github.com/unistack-org/micro-client-http/v3 => ../micro-client-http //replace github.com/unistack-org/micro/v3 => ../micro //replace github.com/unistack-org/micro-proto => ../micro-proto diff --git a/go.sum b/go.sum index 17b54f0..733ae4a 100644 --- a/go.sum +++ b/go.sum @@ -473,8 +473,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.2.4 h1:C/vkzkZ0rpzLIYiaIK53kk5KIfBox2JCbqLvMwz0lWI= github.com/unistack-org/micro-server-grpc/v3 v3.2.4/go.mod h1:ZtCwgb0E7vS4C9GBqpLCNpyQs8TrHQSh4Q0RuNiWE7Y= -github.com/unistack-org/micro-server-http/v3 v3.2.10 h1:YYzwaa9Lcbm5TSU+94PbAGHNK0FQ5CSNuAg55rHtgFw= -github.com/unistack-org/micro-server-http/v3 v3.2.10/go.mod h1:HRA02Jwe6DoSoOyLidu8RJfRa4aItckGIwyn7pCVaqw= +github.com/unistack-org/micro-server-http/v3 v3.2.11 h1:oGjhU7OHbcSMNRSckwmzRULvfDed8T0u7OhgV3W2Z+I= +github.com/unistack-org/micro-server-http/v3 v3.2.11/go.mod h1:HRA02Jwe6DoSoOyLidu8RJfRa4aItckGIwyn7pCVaqw= github.com/unistack-org/micro-server-tcp/v3 v3.2.2 h1:2/Xn+4+dnzY/tpD3MgLO1wg3ect9Jx5CLSDfPBjdjT4= github.com/unistack-org/micro-server-tcp/v3 v3.2.2/go.mod h1:TQDIck2+RdEAGIRnwv+2a0OVBUTkL6OM7YUY4AjFFmY= github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.2.0 h1:PvemkpeCVUWfCoKwt1XmJ8uGK9My/7T29qOVxtYJohw= diff --git a/server/http/http_test.go b/server/http/http_test.go index d71b9d9..fc4c54f 100644 --- a/server/http/http_test.go +++ b/server/http/http_test.go @@ -253,12 +253,15 @@ func TestNativeServer(t *testing.T) { } -func TestHTTPServer(t *testing.T) { +func TestHTTPHandler(t *testing.T) { reg := register.NewRegister() ctx := context.Background() // create server - srv := httpsrv.NewServer(server.Register(reg)) + srv := httpsrv.NewServer( + server.Register(reg), + server.Codec("application/json", jsoncodec.NewCodec()), + ) // create server mux mux := http.NewServeMux() @@ -314,3 +317,65 @@ func TestHTTPServer(t *testing.T) { t.Fatal(err) } } + +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()), + ) + + 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) + } +}