server/http: improve tests

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-03-09 23:59:40 +03:00
parent a1b7e6cc71
commit afea6b0a8e
3 changed files with 71 additions and 5 deletions

3
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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)
}
}