@@ -15,6 +15,7 @@ import (
1515 "strings"
1616 "sync"
1717 "testing"
18+ "time"
1819
1920 "github.com/golang/protobuf/jsonpb"
2021 "github.com/golang/protobuf/proto"
@@ -263,6 +264,7 @@ func TestABE(t *testing.T) {
263264 testABECreate (t , 8080 )
264265 testABECreateBody (t , 8080 )
265266 testABEBulkCreate (t , 8080 )
267+ testABEBulkCreateWithError (t , 8080 )
266268 testABELookup (t , 8080 )
267269 testABELookupNotFound (t , 8080 )
268270 testABEList (t , 8080 )
@@ -549,6 +551,65 @@ func testABEBulkCreate(t *testing.T, port int) {
549551 }
550552}
551553
554+ func testABEBulkCreateWithError (t * testing.T , port int ) {
555+ count := 0
556+ r , w := io .Pipe ()
557+ go func (w io.WriteCloser ) {
558+ defer func () {
559+ if cerr := w .Close (); cerr != nil {
560+ t .Errorf ("w.Close() failed with %v; want success" , cerr )
561+ }
562+ }()
563+ for _ , val := range []string {
564+ "foo" , "bar" , "baz" , "qux" , "quux" ,
565+ } {
566+ time .Sleep (1 * time .Millisecond )
567+
568+ want := gw.ABitOfEverything {
569+ StringValue : fmt .Sprintf ("strprefix/%s" , val ),
570+ }
571+ var m jsonpb.Marshaler
572+ if err := m .Marshal (w , & want ); err != nil {
573+ t .Fatalf ("m.Marshal(%#v, w) failed with %v; want success" , want , err )
574+ }
575+ if _ , err := io .WriteString (w , "\n " ); err != nil {
576+ t .Errorf ("w.Write(%q) failed with %v; want success" , "\n " , err )
577+ return
578+ }
579+ count ++
580+ }
581+ }(w )
582+
583+ apiURL := fmt .Sprintf ("http://localhost:%d/v1/example/a_bit_of_everything/bulk" , port )
584+ request , err := http .NewRequest ("POST" , apiURL , r )
585+ if err != nil {
586+ t .Fatalf ("http.NewRequest(%q, %q, nil) failed with %v; want success" , "POST" , apiURL , err )
587+ }
588+ request .Header .Add ("Grpc-Metadata-error" , "some error" )
589+
590+ resp , err := http .DefaultClient .Do (request )
591+ if err != nil {
592+ t .Errorf ("http.Post(%q) failed with %v; want success" , apiURL , err )
593+ return
594+ }
595+ defer resp .Body .Close ()
596+ buf , err := ioutil .ReadAll (resp .Body )
597+ if err != nil {
598+ t .Errorf ("ioutil.ReadAll(resp.Body) failed with %v; want success" , err )
599+ return
600+ }
601+
602+ if got , want := resp .StatusCode , http .StatusBadRequest ; got != want {
603+ t .Errorf ("resp.StatusCode = %d; want %d" , got , want )
604+ t .Logf ("%s" , buf )
605+ }
606+
607+ var msg errorBody
608+ if err := json .Unmarshal (buf , & msg ); err != nil {
609+ t .Fatalf ("json.Unmarshal(%s, &msg) failed with %v; want success" , buf , err )
610+ }
611+ }
612+
552613func testABELookup (t * testing.T , port int ) {
553614 apiURL := fmt .Sprintf ("http://localhost:%d/v1/example/a_bit_of_everything" , port )
554615 cresp , err := http .Post (apiURL , "application/json" , strings .NewReader (`
0 commit comments