micro/transport/http_transport_test.go

112 lines
2.0 KiB
Go
Raw Normal View History

2016-03-02 15:33:42 +00:00
package transport_test
import (
"strings"
"testing"
"github.com/micro/go-micro/transport"
)
2016-03-14 17:50:18 +00:00
func expectedPort(t *testing.T, expected string, lsn transport.Listener) {
parts := strings.Split(lsn.Addr(), ":")
port := parts[len(parts)-1]
if port != expected {
lsn.Close()
t.Errorf("Expected address to be `%s`, got `%s`", expected, port)
}
}
func TestHTTPTransportPortRange(t *testing.T) {
2016-03-15 22:25:32 +00:00
tp := transport.NewTransport()
2016-03-02 15:33:42 +00:00
lsn1, err := tp.Listen(":44444-44448")
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
expectedPort(t, "44444", lsn1)
lsn2, err := tp.Listen(":44444-44448")
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
expectedPort(t, "44445", lsn2)
lsn, err := tp.Listen(":0")
if err != nil {
t.Errorf("Did not expect an error, got %s", err)
}
lsn.Close()
lsn1.Close()
lsn2.Close()
}
2016-03-14 17:50:18 +00:00
func TestHTTPTransportCommunication(t *testing.T) {
2016-03-15 22:25:32 +00:00
tr := transport.NewTransport()
2016-03-02 15:33:42 +00:00
2016-03-14 17:50:18 +00:00
l, err := tr.Listen(":0")
if err != nil {
t.Errorf("Unexpected listen err: %v", err)
}
defer l.Close()
fn := func(sock transport.Socket) {
defer sock.Close()
2016-03-14 17:50:18 +00:00
for {
var m transport.Message
if err := sock.Recv(&m); err != nil {
return
}
t.Logf("Successfully received %+v", m)
if err := sock.Send(&m); err != nil {
return
}
}
}
done := make(chan bool)
2016-03-14 17:50:18 +00:00
go func() {
if err := l.Accept(fn); err != nil {
select {
case <-done:
default:
t.Errorf("Unexpected accept err: %v", err)
}
2016-03-14 17:50:18 +00:00
}
}()
c, err := tr.Dial(l.Addr())
if err != nil {
t.Errorf("Unexpected dial err: %v", err)
}
defer c.Close()
m := transport.Message{
Header: map[string]string{
"Content-Type": "application/json",
},
Body: []byte(`{"message": "Hello World"}`),
}
if err := c.Send(&m); err != nil {
t.Errorf("Unexpected send err: %v", err)
}
var rm transport.Message
if err := c.Recv(&rm); err != nil {
t.Errorf("Unexpected recv err: %v", err)
}
if string(rm.Body) != string(m.Body) {
t.Errorf("Expected %v, got %v", m.Body, rm.Body)
2016-03-02 15:33:42 +00:00
}
close(done)
2016-03-02 15:33:42 +00:00
}