server/http: add test for wrapped router

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-11-22 10:16:17 +03:00
parent 7411f14ae5
commit 7149f1fda8

View File

@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"sync"
"testing" "testing"
httpcli "go.unistack.org/micro-client-http/v3" httpcli "go.unistack.org/micro-client-http/v3"
@ -704,23 +705,39 @@ func TestHTTPHandler(t *testing.T) {
} }
} }
type handlerSwapper struct {
mu sync.RWMutex
handler http.Handler
}
func (h *handlerSwapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mu.RLock()
handler := h.handler
h.mu.RUnlock()
handler.ServeHTTP(w, r)
}
func TestHTTPServer(t *testing.T) { func TestHTTPServer(t *testing.T) {
reg := register.NewRegister() reg := register.NewRegister()
ctx := context.Background() ctx := context.Background()
// create server mux // create server mux
mux := http.NewServeMux() mux1 := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux1.HandleFunc("/first", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`hello world`))
})
mux2 := http.NewServeMux()
mux2.HandleFunc("/second", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`hello world`)) _, _ = w.Write([]byte(`hello world`))
}) })
h := &handlerSwapper{handler: mux1}
// create server // create server
srv := httpsrv.NewServer( srv := httpsrv.NewServer(
server.Address("127.0.0.1:0"), server.Address("127.0.0.1:0"),
server.Register(reg), server.Register(reg),
httpsrv.Server(&http.Server{Handler: mux}), httpsrv.Server(&http.Server{Handler: h}),
server.Codec("application/json", jsoncodec.NewCodec()), server.Codec("application/json", jsoncodec.NewCodec()),
server.Codec("application/x-www-form-urlencoded", urlencodecodec.NewCodec()),
) )
if err := srv.Init(); err != nil { if err := srv.Init(); err != nil {
@ -747,7 +764,7 @@ func TestHTTPServer(t *testing.T) {
} }
// make request // make request
rsp, err := http.Get(fmt.Sprintf("http://%s", service[0].Nodes[0].Address)) rsp, err := http.Get(fmt.Sprintf("http://%s/first", service[0].Nodes[0].Address))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -762,6 +779,41 @@ func TestHTTPServer(t *testing.T) {
t.Fatalf("Expected response %s, got %s", "hello world", s) t.Fatalf("Expected response %s, got %s", "hello world", s)
} }
rsp, err = http.Get(fmt.Sprintf("http://%s/second", service[0].Nodes[0].Address))
if err != nil {
t.Fatal(err)
}
if rsp.StatusCode != 404 {
t.Fatal("second route must not exists")
}
h.mu.Lock()
h.handler = mux2
h.mu.Unlock()
rsp, err = http.Get(fmt.Sprintf("http://%s/first", service[0].Nodes[0].Address))
if err != nil {
t.Fatal(err)
}
defer rsp.Body.Close()
if rsp.StatusCode != 404 {
t.Fatal("first route must not exists")
}
rsp, err = http.Get(fmt.Sprintf("http://%s/second", 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 // stop server
if err := srv.Stop(); err != nil { if err := srv.Stop(); err != nil {
t.Fatal(err) t.Fatal(err)