2020-08-10 15:58:39 +01:00
|
|
|
package http
|
2016-03-02 15:33:42 +00:00
|
|
|
|
|
|
|
import (
|
2016-05-10 11:23:30 +01:00
|
|
|
"io"
|
2019-07-17 10:38:50 +03:00
|
|
|
"net"
|
2016-03-02 15:33:42 +00:00
|
|
|
"testing"
|
2016-07-29 15:30:51 +01:00
|
|
|
"time"
|
2020-08-10 15:58:39 +01:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/v3/transport"
|
2016-03-02 15:33:42 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
func expectedPort(t *testing.T, expected string, lsn transport.Listener) {
|
2019-07-17 10:38:50 +03:00
|
|
|
_, port, err := net.SplitHostPort(lsn.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected address to be `%s`, got error: %v", expected, err)
|
|
|
|
}
|
2016-03-14 17:50:18 +00:00
|
|
|
|
|
|
|
if port != expected {
|
|
|
|
lsn.Close()
|
|
|
|
t.Errorf("Expected address to be `%s`, got `%s`", expected, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPTransportPortRange(t *testing.T) {
|
2016-05-06 15:44:32 +01:00
|
|
|
tp := 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)
|
|
|
|
|
2017-11-03 17:30:16 +00:00
|
|
|
lsn, err := tp.Listen("127.0.0.1:0")
|
2016-03-02 15:33:42 +00:00
|
|
|
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-05-06 15:44:32 +01:00
|
|
|
tr := NewTransport()
|
2016-03-02 15:33:42 +00:00
|
|
|
|
2017-11-03 17:30:16 +00:00
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
2016-03-14 17:50:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
fn := func(sock transport.Socket) {
|
2016-03-14 18:20:38 +00:00
|
|
|
defer sock.Close()
|
|
|
|
|
2016-03-14 17:50:18 +00:00
|
|
|
for {
|
2020-08-10 15:58:39 +01:00
|
|
|
var m transport.Message
|
2016-03-14 17:50:18 +00:00
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sock.Send(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:20:38 +00:00
|
|
|
done := make(chan bool)
|
|
|
|
|
2016-03-14 17:50:18 +00:00
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
2016-03-14 18:20:38 +00:00
|
|
|
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()
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
m := transport.Message{
|
2016-03-14 17:50:18 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
var rm transport.Message
|
2016-03-14 17:50:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-03-14 18:20:38 +00:00
|
|
|
|
|
|
|
close(done)
|
2016-03-02 15:33:42 +00:00
|
|
|
}
|
2016-05-06 15:44:32 +01:00
|
|
|
|
|
|
|
func TestHTTPTransportError(t *testing.T) {
|
|
|
|
tr := NewTransport()
|
|
|
|
|
2017-11-03 17:30:16 +00:00
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
2016-05-06 15:44:32 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
fn := func(sock transport.Socket) {
|
2016-05-06 15:44:32 +01:00
|
|
|
defer sock.Close()
|
|
|
|
|
|
|
|
for {
|
2020-08-10 15:58:39 +01:00
|
|
|
var m transport.Message
|
2016-05-06 15:44:32 +01:00
|
|
|
if err := sock.Recv(&m); err != nil {
|
2016-05-10 11:23:30 +01:00
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
2016-05-06 15:44:32 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
sock.(*httpTransportSocket).error(&transport.Message{
|
2016-05-06 15:44:32 +01:00
|
|
|
Body: []byte(`an error occurred`),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
m := transport.Message{
|
2016-05-06 15:44:32 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
var rm transport.Message
|
2016-05-06 15:44:32 +01:00
|
|
|
|
|
|
|
err = c.Recv(&rm)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error but got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != "500 Internal Server Error: an error occurred" {
|
|
|
|
t.Fatalf("Did not receive expected error, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
}
|
2016-07-29 15:30:51 +01:00
|
|
|
|
2016-08-01 16:31:27 +01:00
|
|
|
func TestHTTPTransportTimeout(t *testing.T) {
|
2020-08-10 15:58:39 +01:00
|
|
|
tr := NewTransport(transport.Timeout(time.Millisecond * 100))
|
2016-07-29 15:30:51 +01:00
|
|
|
|
2017-11-03 17:30:16 +00:00
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
2016-07-29 15:30:51 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
fn := func(sock transport.Socket) {
|
2016-07-29 15:30:51 +01:00
|
|
|
defer func() {
|
|
|
|
sock.Close()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("deadline not executed")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2020-08-10 15:58:39 +01:00
|
|
|
var m transport.Message
|
2016-07-29 15:30:51 +01:00
|
|
|
|
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2020-08-10 15:58:39 +01:00
|
|
|
m := transport.Message{
|
2016-07-29 15:30:51 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|