mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-18 08:26:38 +02:00
88 lines
1.3 KiB
Go
88 lines
1.3 KiB
Go
|
package http
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/micro/go-micro/registry"
|
||
|
"github.com/micro/go-micro/registry/memory"
|
||
|
)
|
||
|
|
||
|
func TestRoundTripper(t *testing.T) {
|
||
|
m := memory.NewRegistry()
|
||
|
|
||
|
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)
|
||
|
|
||
|
host, p, _ := net.SplitHostPort(l.Addr().String())
|
||
|
port, _ := strconv.Atoi(p)
|
||
|
|
||
|
m.Register(®istry.Service{
|
||
|
Name: "example.com",
|
||
|
Nodes: []*registry.Node{
|
||
|
{
|
||
|
Id: "1",
|
||
|
Address: host,
|
||
|
Port: port,
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
|
||
|
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 := ioutil.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 = ioutil.ReadAll(rsp.Body)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
rsp.Body.Close()
|
||
|
|
||
|
if string(b) != "hello world" {
|
||
|
t.Fatal("response is", string(b))
|
||
|
}
|
||
|
|
||
|
}
|