Skip to content

Commit

Permalink
test Header metadata for streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
kazegusuri committed Feb 17, 2016
1 parent a5bd340 commit d202328
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 111 deletions.
138 changes: 32 additions & 106 deletions examples/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
"testing"
"time"
Expand All @@ -22,11 +23,6 @@ import (
"google.golang.org/grpc/codes"
)

type aBitOfEverything struct {
Result gw.ABitOfEverything `json:"result,omitempty"`
Error *runtime.ResponseStreamError `json:"error,omitempty"`
}

func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip()
Expand All @@ -40,21 +36,7 @@ func TestIntegration(t *testing.T) {
}
}()
go func() {
if err := Run(
":8080",
runtime.WithForwardResponseOption(
func(ctx context.Context, w http.ResponseWriter, _ proto.Message) error {
if md, ok := runtime.ServerMetadataFromContext(ctx); ok {
for k, vs := range md.TrailerMD {
for i := range vs {
w.Header().Add(fmt.Sprintf("Grpc-Trailer-%s", k), vs[i])
}
}
}
return nil
},
),
); err != nil {
if err := Run(":8080"); err != nil {
t.Errorf("gw.Run() failed with %v; want success", err)
return
}
Expand All @@ -69,7 +51,6 @@ func TestIntegration(t *testing.T) {
testABELookup(t)
testABELookupNotFound(t)
testABEList(t)
testABEListError(t)
testAdditionalBindings(t)

go func() {
Expand Down Expand Up @@ -158,20 +139,18 @@ func testEchoBody(t *testing.T) {
t.Errorf("msg.Id = %q; want %q", got, want)
}

if value := resp.Header.Get("Grpc-Metadata-foo"); value != "foo1" {
t.Errorf("Grpc-Header-foo was %s, wanted %s", value, "foo1")
if got, want := resp.Header.Get("Grpc-Metadata-Foo"), "foo1"; got != want {
t.Errorf("Grpc-Header-Foo was %q, wanted %q", got, want)
}

if value := resp.Header.Get("Grpc-Metadata-bar"); value != "bar1" {
t.Errorf("Grpc-Header-bar was %s, wanted %s", value, "bar1")
if got, want := resp.Header.Get("Grpc-Metadata-Bar"), "bar1"; got != want {
t.Errorf("Grpc-Header-Bar was %q, wanted %q", got, want)
}

if value := resp.Header.Get("Grpc-Trailer-foo"); value != "foo2" {
t.Errorf("Grpc-Trailer-foo was %s, wanted %s", value, "foo2")
if got, want := resp.Trailer.Get("Grpc-Trailer-Foo"), "foo2"; got != want {
t.Errorf("Grpc-Trailer-Foo was %q, wanted %q", got, want)
}

if value := resp.Header.Get("Grpc-Trailer-bar"); value != "bar2" {
t.Errorf("Grpc-Trailer-bar was %s, wanted %s", value, "bar2")
if got, want := resp.Trailer.Get("Grpc-Trailer-Bar"), "bar2"; got != want {
t.Errorf("Grpc-Trailer-Bar was %q, wanted %q", got, want)
}
}

Expand Down Expand Up @@ -291,6 +270,7 @@ func testABECreateBody(t *testing.T) {
}

func testABEBulkCreate(t *testing.T) {
count := 0
r, w := io.Pipe()
go func(w io.WriteCloser) {
defer func() {
Expand Down Expand Up @@ -340,6 +320,7 @@ func testABEBulkCreate(t *testing.T) {
t.Errorf("w.Write(%s) failed with %v; want success", buf, err)
return
}
count++
}
}(w)
url := "http://localhost:8080/v1/example/a_bit_of_everything/bulk"
Expand All @@ -366,12 +347,15 @@ func testABEBulkCreate(t *testing.T) {
return
}

if value := resp.Header.Get("Grpc-Trailer-foo"); value != "foo2" {
t.Errorf("Grpc-Trailer-foo was %q, wanted %q", value, "foo2")
if got, want := resp.Header.Get("Grpc-Metadata-Count"), fmt.Sprintf("%d", count); got != want {
t.Errorf("Grpc-Header-Count was %q, wanted %q", got, want)
}

if value := resp.Header.Get("Grpc-Trailer-bar"); value != "bar2" {
t.Errorf("Grpc-Trailer-bar was %q, wanted %q", value, "bar2")
if got, want := resp.Trailer.Get("Grpc-Trailer-Foo"), "foo2"; got != want {
t.Errorf("Grpc-Trailer-Foo was %q, wanted %q", got, want)
}
if got, want := resp.Trailer.Get("Grpc-Trailer-Bar"), "bar2"; got != want {
t.Errorf("Grpc-Trailer-Bar was %q, wanted %q", got, want)
}
}

Expand Down Expand Up @@ -425,8 +409,8 @@ func testABELookup(t *testing.T) {
t.Errorf("msg= %v; want %v", &got, &want)
}

if got, want := resp.Header.Get("Grpc-Metadata-uuid"), want.Uuid; got != want {
t.Errorf("Grpc-Metadata-foo was %s, wanted %s", got, want)
if got, want := resp.Header.Get("Grpc-Metadata-Uuid"), want.Uuid; got != want {
t.Errorf("Grpc-Metadata-Uuid was %s, wanted %s", got, want)
}
}

Expand Down Expand Up @@ -464,22 +448,8 @@ func testABELookupNotFound(t *testing.T) {
return
}

if got, want := resp.Header.Get("Grpc-Metadata-uuid"), uuid; got != want {
t.Errorf("Grpc-Metadata-foo was %s, wanted %s", got, want)
}

md := msg.Trailer
if md.Len() == 0 {
t.Errorf("no trailer is set")
return
}

if got, want := md["foo"], []string{"foo2"}; !reflect.DeepEqual(got, want) {
t.Errorf("msg.Trailer[%q] = %v; want %v", "foo", got, want)
}

if got, want := md["bar"], []string{"bar2"}; !reflect.DeepEqual(got, want) {
t.Errorf("msg.Trailer[%q] = %v; want %v", "bar", got, want)
if got, want := resp.Header.Get("Grpc-Metadata-Uuid"), uuid; got != want {
t.Errorf("Grpc-Metadata-Uuid was %s, wanted %s", got, want)
}
}

Expand All @@ -495,7 +465,7 @@ func testABEList(t *testing.T) {
dec := json.NewDecoder(resp.Body)
var i int
for i = 0; ; i++ {
var msg aBitOfEverything
var msg gw.ABitOfEverything
err := dec.Decode(&msg)
if err == io.EOF {
break
Expand All @@ -507,63 +477,19 @@ func testABEList(t *testing.T) {
if i <= 0 {
t.Errorf("i == %d; want > 0", i)
}
}

func testABEListError(t *testing.T) {
url := "http://localhost:8080/v1/example/a_bit_of_everything"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Errorf("http.NewReuest(%q) failed with %v; want success", url, err)
return
value := resp.Header.Get("Grpc-Metadata-Count")
if value == "" {
t.Errorf("Grpc-Header-Count should not be empty")
}
req.Header.Set("Grpc-Metadata-error", "foo")

client := new(http.Client)
resp, err := client.Do(req)
count, err := strconv.Atoi(value)
if err != nil {
t.Errorf("client.Do failed with %v; want success", err)
return
t.Errorf("failed to Atoi %q: %v", value, err)
}
defer resp.Body.Close()

dec := json.NewDecoder(resp.Body)
var i int
var lastMsg aBitOfEverything
for i = 0; ; i++ {
var msg aBitOfEverything
err := dec.Decode(&msg)
if err == io.EOF {
break
}
if err != nil {
t.Errorf("dec.Decode(&msg) failed with %v; want success; i = %d", err, i)
}
lastMsg = msg
}
if i <= 0 {
t.Errorf("i == %d; want > 0", i)
}

if got, want := lastMsg.Error.HTTPCode, http.StatusBadRequest; got != want {
t.Errorf("lastMsg.Error.HTTPCode = %d; want %d", got, want)
return
}

md := lastMsg.Error.Trailer
v, ok := md["foo"]
if !ok || len(v) == 0 {
t.Errorf("Trailer doesn't contain %q", "foo")
}
if !ok && v[0] != "foo1" {
t.Errorf("Trailer %q = %q; want %q", "foo", v[0], "foo2")
}

v, ok = md["bar"]
if !ok || len(v) == 0 {
t.Errorf("Trailer doesn't contain %q", "bar")
}
if !ok && v[0] != "bar1" {
t.Errorf("Trailer %q = %q; want %q", "bar", v[0], "bar2")
if count <= 0 {
t.Errorf("count == %d; want > 0", count)
}
}

Expand Down Expand Up @@ -615,7 +541,7 @@ func testAdditionalBindings(t *testing.T) {

var msg sub.StringMessage
if err := json.Unmarshal(buf, &msg); err != nil {
t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success; %i", buf, err, i)
t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success; %d", buf, err, i)
return
}
if got, want := msg.GetValue(), "hello"; got != want {
Expand Down
25 changes: 20 additions & 5 deletions examples/server/a_bit_of_everything.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *_ABitOfEverythingServer) CreateBody(ctx context.Context, msg *examples.
}

func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingService_BulkCreateServer) error {

count := 0
ctx := stream.Context()
for {
msg, err := stream.Recv()
Expand All @@ -63,16 +63,20 @@ func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingSer
if err != nil {
return err
}
count++
glog.Error(msg)
if _, err = s.Create(ctx, msg); err != nil {
return err
}
}

stream.SendHeader(metadata.New(map[string]string{
"foo": "foo1",
"bar": "bar1",
err := stream.SendHeader(metadata.New(map[string]string{
"count": fmt.Sprintf("%d", count),
}))
if err != nil {
return nil
}

stream.SetTrailer(metadata.New(map[string]string{
"foo": "foo2",
"bar": "bar2",
Expand All @@ -85,9 +89,12 @@ func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *examples.IdMe
defer s.m.Unlock()
glog.Info(msg)

grpc.SendHeader(ctx, metadata.New(map[string]string{
err := grpc.SendHeader(ctx, metadata.New(map[string]string{
"uuid": msg.Uuid,
}))
if err != nil {
return nil, err
}

if a, ok := s.v[msg.Uuid]; ok {
return a, nil
Expand All @@ -103,6 +110,14 @@ func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *examples.IdMe
func (s *_ABitOfEverythingServer) List(_ *examples.EmptyMessage, stream examples.ABitOfEverythingService_ListServer) error {
s.m.Lock()
defer s.m.Unlock()

err := stream.SendHeader(metadata.New(map[string]string{
"count": fmt.Sprintf("%d", len(s.v)),
}))
if err != nil {
return nil
}

for _, msg := range s.v {
if err := stream.Send(msg); err != nil {
return err
Expand Down

0 comments on commit d202328

Please sign in to comment.