Since we send 200 response from the Listener, we should expect 200 response from the Client

This commit is contained in:
Asim 2016-05-06 15:44:32 +01:00
parent 8b76e277bc
commit 07687b9595
2 changed files with 79 additions and 12 deletions

View File

@ -174,6 +174,10 @@ func (h *httpTransportClient) Recv(m *Message) error {
return err return err
} }
if rsp.StatusCode != 200 {
return errors.New(rsp.Status + ": " + string(b))
}
mr := &Message{ mr := &Message{
Header: make(map[string]string), Header: make(map[string]string),
Body: b, Body: b,

View File

@ -1,13 +1,11 @@
package transport_test package transport
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/micro/go-micro/transport"
) )
func expectedPort(t *testing.T, expected string, lsn transport.Listener) { func expectedPort(t *testing.T, expected string, lsn Listener) {
parts := strings.Split(lsn.Addr(), ":") parts := strings.Split(lsn.Addr(), ":")
port := parts[len(parts)-1] port := parts[len(parts)-1]
@ -18,7 +16,7 @@ func expectedPort(t *testing.T, expected string, lsn transport.Listener) {
} }
func TestHTTPTransportPortRange(t *testing.T) { func TestHTTPTransportPortRange(t *testing.T) {
tp := transport.NewTransport() tp := NewTransport()
lsn1, err := tp.Listen(":44444-44448") lsn1, err := tp.Listen(":44444-44448")
if err != nil { if err != nil {
@ -43,7 +41,7 @@ func TestHTTPTransportPortRange(t *testing.T) {
} }
func TestHTTPTransportCommunication(t *testing.T) { func TestHTTPTransportCommunication(t *testing.T) {
tr := transport.NewTransport() tr := NewTransport()
l, err := tr.Listen(":0") l, err := tr.Listen(":0")
if err != nil { if err != nil {
@ -51,17 +49,15 @@ func TestHTTPTransportCommunication(t *testing.T) {
} }
defer l.Close() defer l.Close()
fn := func(sock transport.Socket) { fn := func(sock Socket) {
defer sock.Close() defer sock.Close()
for { for {
var m transport.Message var m Message
if err := sock.Recv(&m); err != nil { if err := sock.Recv(&m); err != nil {
return return
} }
t.Logf("Successfully received %+v", m)
if err := sock.Send(&m); err != nil { if err := sock.Send(&m); err != nil {
return return
} }
@ -86,7 +82,7 @@ func TestHTTPTransportCommunication(t *testing.T) {
} }
defer c.Close() defer c.Close()
m := transport.Message{ m := Message{
Header: map[string]string{ Header: map[string]string{
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
@ -97,7 +93,7 @@ func TestHTTPTransportCommunication(t *testing.T) {
t.Errorf("Unexpected send err: %v", err) t.Errorf("Unexpected send err: %v", err)
} }
var rm transport.Message var rm Message
if err := c.Recv(&rm); err != nil { if err := c.Recv(&rm); err != nil {
t.Errorf("Unexpected recv err: %v", err) t.Errorf("Unexpected recv err: %v", err)
@ -109,3 +105,70 @@ func TestHTTPTransportCommunication(t *testing.T) {
close(done) close(done)
} }
func TestHTTPTransportError(t *testing.T) {
tr := NewTransport()
l, err := tr.Listen(":0")
if err != nil {
t.Errorf("Unexpected listen err: %v", err)
}
defer l.Close()
fn := func(sock Socket) {
defer sock.Close()
for {
var m Message
if err := sock.Recv(&m); err != nil {
t.Fatal(err)
}
sock.(*httpTransportSocket).error(&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)
}
}
}()
c, err := tr.Dial(l.Addr())
if err != nil {
t.Errorf("Unexpected dial err: %v", err)
}
defer c.Close()
m := 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 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)
}