1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-06 08:16:03 +02:00
go-micro/util/http/http_test.go
Benjamin 5d5aee1f08
replace ioutil with io and os (#2327)
set the go version to 1.16 in pr.yml and tests.yml, so as to be consistent with the version in go.mod.
2021-10-30 19:24:40 +01:00

82 lines
1.2 KiB
Go

package http
import (
"io"
"net"
"net/http"
"testing"
"go-micro.dev/v4/registry"
)
func TestRoundTripper(t *testing.T) {
m := registry.NewMemoryRegistry()
rt := NewRoundTripper(
WithRegistry(m),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`hello world`))
})
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go http.Serve(l, nil)
m.Register(&registry.Service{
Name: "example.com",
Nodes: []*registry.Node{
{
Id: "1",
Address: l.Addr().String(),
},
},
})
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatal(err)
}
w, err := rt.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
b, err := io.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
w.Body.Close()
if string(b) != "hello world" {
t.Fatal("response is", string(b))
}
// test http request
c := &http.Client{
Transport: rt,
}
rsp, err := c.Get("http://example.com")
if err != nil {
t.Fatal(err)
}
b, err = io.ReadAll(rsp.Body)
if err != nil {
t.Fatal(err)
}
rsp.Body.Close()
if string(b) != "hello world" {
t.Fatal("response is", string(b))
}
}