2020-08-23 20:37:22 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-01-29 18:57:29 +03:00
|
|
|
"context"
|
2020-08-23 20:37:22 +03:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-28 01:00:16 +03:00
|
|
|
"go.unistack.org/micro/v3/network/transport"
|
2020-08-23 20:37:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func expectedPort(t *testing.T, expected string, lsn transport.Listener) {
|
|
|
|
_, port, err := net.SplitHostPort(lsn.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected address to be `%s`, got error: %v", expected, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if port != expected {
|
|
|
|
lsn.Close()
|
|
|
|
t.Errorf("Expected address to be `%s`, got `%s`", expected, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPTransportPortRange(t *testing.T) {
|
|
|
|
tp := NewTransport()
|
2021-01-29 18:57:29 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
lsn1, err := tp.Listen(ctx, ":44444-44448")
|
2020-08-23 20:37:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error, got %s", err)
|
|
|
|
}
|
|
|
|
expectedPort(t, "44444", lsn1)
|
|
|
|
|
2021-01-29 18:57:29 +03:00
|
|
|
lsn2, err := tp.Listen(ctx, ":44444-44448")
|
2020-08-23 20:37:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error, got %s", err)
|
|
|
|
}
|
|
|
|
expectedPort(t, "44445", lsn2)
|
|
|
|
|
2021-01-29 18:57:29 +03:00
|
|
|
lsn, err := tp.Listen(ctx, "127.0.0.1:0")
|
2020-08-23 20:37:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lsn.Close()
|
|
|
|
lsn1.Close()
|
|
|
|
lsn2.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPTransportCommunication(t *testing.T) {
|
|
|
|
tr := NewTransport()
|
2021-01-29 18:57:29 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
l, err := tr.Listen(ctx, "127.0.0.1:0")
|
2020-08-23 20:37:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
fn := func(sock transport.Socket) {
|
|
|
|
defer sock.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m transport.Message
|
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sock.Send(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-29 18:57:29 +03:00
|
|
|
c, err := tr.Dial(ctx, l.Addr())
|
2020-08-23 20:37:22 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPTransportError(t *testing.T) {
|
|
|
|
tr := NewTransport()
|
2021-01-29 18:57:29 +03:00
|
|
|
ctx := context.Background()
|
2020-08-23 20:37:22 +03:00
|
|
|
|
2021-01-29 18:57:29 +03:00
|
|
|
l, err := tr.Listen(ctx, "127.0.0.1:0")
|
2020-08-23 20:37:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
fn := func(sock transport.Socket) {
|
|
|
|
defer sock.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m transport.Message
|
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sock.(*httpTransportSocket).error(&transport.Message{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-29 18:57:29 +03:00
|
|
|
c, err := tr.Dial(ctx, l.Addr())
|
2020-08-23 20:37:22 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPTransportTimeout(t *testing.T) {
|
|
|
|
tr := NewTransport(transport.Timeout(time.Millisecond * 100))
|
2021-01-29 18:57:29 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
l, err := tr.Listen(ctx, "127.0.0.1:0")
|
2020-08-23 20:37:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
fn := func(sock transport.Socket) {
|
|
|
|
defer func() {
|
|
|
|
sock.Close()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("deadline not executed")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m transport.Message
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-29 18:57:29 +03:00
|
|
|
c, err := tr.Dial(ctx, l.Addr())
|
2020-08-23 20:37:22 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|