11package main
22
33import (
4- "bytes"
54 "encoding/json"
65 "fmt"
76 "io"
@@ -13,13 +12,13 @@ import (
1312 "testing"
1413 "time"
1514
16- "golang.org/x/net/context"
17-
1815 gw "github.com/gengo/grpc-gateway/examples/examplepb"
1916 server "github.com/gengo/grpc-gateway/examples/server"
2017 sub "github.com/gengo/grpc-gateway/examples/sub"
2118 "github.com/gengo/grpc-gateway/runtime"
19+ "github.com/golang/protobuf/jsonpb"
2220 "github.com/golang/protobuf/proto"
21+ "golang.org/x/net/context"
2322 "google.golang.org/grpc/codes"
2423)
2524
@@ -97,8 +96,8 @@ func testEcho(t *testing.T, port int, contentType string) {
9796 }
9897
9998 var msg gw.SimpleMessage
100- if err := json . Unmarshal ( buf , & msg ); err != nil {
101- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success" , buf , err )
99+ if err := jsonpb . UnmarshalString ( string ( buf ) , & msg ); err != nil {
100+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success" , buf , err )
102101 return
103102 }
104103 if got , want := msg .Id , "myid" ; got != want {
@@ -112,19 +111,20 @@ func testEcho(t *testing.T, port int, contentType string) {
112111
113112func testEchoBody (t * testing.T ) {
114113 sent := gw.SimpleMessage {Id : "example" }
115- buf , err := json .Marshal (sent )
114+ var m jsonpb.Marshaler
115+ payload , err := m .MarshalToString (& sent )
116116 if err != nil {
117- t .Fatalf ("json.Marshal (%#v) failed with %v; want success" , sent , err )
117+ t .Fatalf ("m.MarshalToString (%#v) failed with %v; want success" , payload , err )
118118 }
119119
120120 url := "http://localhost:8080/v1/example/echo_body"
121- resp , err := http .Post (url , "" , bytes .NewReader (buf ))
121+ resp , err := http .Post (url , "" , strings .NewReader (payload ))
122122 if err != nil {
123123 t .Errorf ("http.Post(%q) failed with %v; want success" , url , err )
124124 return
125125 }
126126 defer resp .Body .Close ()
127- buf , err = ioutil .ReadAll (resp .Body )
127+ buf , err : = ioutil .ReadAll (resp .Body )
128128 if err != nil {
129129 t .Errorf ("iotuil.ReadAll(resp.Body) failed with %v; want success" , err )
130130 return
@@ -136,8 +136,8 @@ func testEchoBody(t *testing.T) {
136136 }
137137
138138 var received gw.SimpleMessage
139- if err := json . Unmarshal ( buf , & received ); err != nil {
140- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success" , buf , err )
139+ if err := jsonpb . UnmarshalString ( string ( buf ) , & received ); err != nil {
140+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success" , buf , err )
141141 return
142142 }
143143 if got , want := received , sent ; ! reflect .DeepEqual (got , want ) {
@@ -197,8 +197,8 @@ func testABECreate(t *testing.T) {
197197 }
198198
199199 var msg gw.ABitOfEverything
200- if err := json . Unmarshal ( buf , & msg ); err != nil {
201- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success" , buf , err )
200+ if err := jsonpb . UnmarshalString ( string ( buf ) , & msg ); err != nil {
201+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success" , buf , err )
202202 return
203203 }
204204 if msg .Uuid == "" {
@@ -240,18 +240,19 @@ func testABECreateBody(t *testing.T) {
240240 },
241241 }
242242 url := "http://localhost:8080/v1/example/a_bit_of_everything"
243- buf , err := json .Marshal (want )
243+ var m jsonpb.Marshaler
244+ payload , err := m .MarshalToString (& want )
244245 if err != nil {
245- t .Fatalf ("json.Marshal (%#v) failed with %v; want success" , want , err )
246+ t .Fatalf ("m.MarshalToString (%#v) failed with %v; want success" , want , err )
246247 }
247248
248- resp , err := http .Post (url , "application/json" , bytes .NewReader (buf ))
249+ resp , err := http .Post (url , "application/json" , strings .NewReader (payload ))
249250 if err != nil {
250251 t .Errorf ("http.Post(%q) failed with %v; want success" , url , err )
251252 return
252253 }
253254 defer resp .Body .Close ()
254- buf , err = ioutil .ReadAll (resp .Body )
255+ buf , err : = ioutil .ReadAll (resp .Body )
255256 if err != nil {
256257 t .Errorf ("iotuil.ReadAll(resp.Body) failed with %v; want success" , err )
257258 return
@@ -263,8 +264,8 @@ func testABECreateBody(t *testing.T) {
263264 }
264265
265266 var msg gw.ABitOfEverything
266- if err := json . Unmarshal ( buf , & msg ); err != nil {
267- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success" , buf , err )
267+ if err := jsonpb . UnmarshalString ( string ( buf ) , & msg ); err != nil {
268+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success" , buf , err )
268269 return
269270 }
270271 if msg .Uuid == "" {
@@ -316,16 +317,12 @@ func testABEBulkCreate(t *testing.T) {
316317 },
317318 },
318319 }
319- buf , err := json .Marshal (want )
320- if err != nil {
321- t .Fatalf ("json.Marshal(%#v) failed with %v; want success" , want , err )
322- }
323- if _ , err := w .Write (buf ); err != nil {
324- t .Errorf ("w.Write(%s) failed with %v; want success" , buf , err )
325- return
320+ var m jsonpb.Marshaler
321+ if err := m .Marshal (w , & want ); err != nil {
322+ t .Fatalf ("m.Marshal(%#v, w) failed with %v; want success" , want , err )
326323 }
327324 if _ , err := io .WriteString (w , "\n " ); err != nil {
328- t .Errorf ("w.Write(%s ) failed with %v; want success" , buf , err )
325+ t .Errorf ("w.Write(%q ) failed with %v; want success" , " \n " , err )
329326 return
330327 }
331328 count ++
@@ -350,8 +347,8 @@ func testABEBulkCreate(t *testing.T) {
350347 }
351348
352349 var msg gw.EmptyMessage
353- if err := json . Unmarshal ( buf , & msg ); err != nil {
354- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success" , buf , err )
350+ if err := jsonpb . UnmarshalString ( string ( buf ) , & msg ); err != nil {
351+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success" , buf , err )
355352 return
356353 }
357354
@@ -389,8 +386,8 @@ func testABELookup(t *testing.T) {
389386 }
390387
391388 var want gw.ABitOfEverything
392- if err := json . Unmarshal ( buf , & want ); err != nil {
393- t .Errorf ("json.Unmarshal (%s, &want) failed with %v; want success" , buf , err )
389+ if err := jsonpb . UnmarshalString ( string ( buf ) , & want ); err != nil {
390+ t .Errorf ("jsonpb.UnmarshalString (%s, &want) failed with %v; want success" , buf , err )
394391 return
395392 }
396393
@@ -409,8 +406,8 @@ func testABELookup(t *testing.T) {
409406 }
410407
411408 var msg gw.ABitOfEverything
412- if err := json . Unmarshal ( buf , & msg ); err != nil {
413- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success" , buf , err )
409+ if err := jsonpb . UnmarshalString ( string ( buf ) , & msg ); err != nil {
410+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success" , buf , err )
414411 return
415412 }
416413 if got := msg ; ! reflect .DeepEqual (got , want ) {
@@ -447,7 +444,7 @@ func testABELookupNotFound(t *testing.T) {
447444
448445 var msg errorBody
449446 if err := json .Unmarshal (buf , & msg ); err != nil {
450- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success" , buf , err )
447+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success" , buf , err )
451448 return
452449 }
453450
@@ -473,13 +470,24 @@ func testABEList(t *testing.T) {
473470 dec := json .NewDecoder (resp .Body )
474471 var i int
475472 for i = 0 ; ; i ++ {
476- var msg gw.ABitOfEverything
477- err := dec .Decode (& msg )
473+ var item struct {
474+ Result json.RawMessage `json:"result"`
475+ Error map [string ]interface {} `json:"error"`
476+ }
477+ err := dec .Decode (& item )
478478 if err == io .EOF {
479479 break
480480 }
481481 if err != nil {
482- t .Errorf ("dec.Decode(&msg) failed with %v; want success; i = %d" , err , i )
482+ t .Errorf ("dec.Decode(&item) failed with %v; want success; i = %d" , err , i )
483+ }
484+ if len (item .Error ) != 0 {
485+ t .Errorf ("item.Error = %#v; want empty; i = %d" , item .Error , i )
486+ continue
487+ }
488+ var msg gw.ABitOfEverything
489+ if err := jsonpb .UnmarshalString (string (item .Result ), & msg ); err != nil {
490+ t .Errorf ("jsonpb.UnmarshalString(%s, &msg) failed with %v; want success" , item .Result , err )
483491 }
484492 }
485493 if i <= 0 {
@@ -548,8 +556,8 @@ func testAdditionalBindings(t *testing.T) {
548556 }
549557
550558 var msg sub.StringMessage
551- if err := json . Unmarshal ( buf , & msg ); err != nil {
552- t .Errorf ("json.Unmarshal (%s, &msg) failed with %v; want success; %d" , buf , err , i )
559+ if err := jsonpb . UnmarshalString ( string ( buf ) , & msg ); err != nil {
560+ t .Errorf ("jsonpb.UnmarshalString (%s, &msg) failed with %v; want success; %d" , buf , err , i )
553561 return
554562 }
555563 if got , want := msg .GetValue (), "hello" ; got != want {
0 commit comments