@@ -43,8 +43,12 @@ function fakeRetryRequest() {
4343 return ( retryRequestOverride || retryRequest ) . apply ( null , arguments ) ;
4444}
4545
46+ var grpcMetadataOverride ;
4647var grpcLoadOverride ;
4748var fakeGrpc = {
49+ Metadata : function ( ) {
50+ return new ( grpcMetadataOverride || grpc . Metadata ) ( ) ;
51+ } ,
4852 load : function ( ) {
4953 return ( grpcLoadOverride || grpc . load ) . apply ( null , arguments ) ;
5054 } ,
@@ -84,7 +88,10 @@ describe('GrpcService', function() {
8488 var CONFIG = {
8589 proto : { } ,
8690 service : 'Service' ,
87- apiVersion : 'v1'
91+ apiVersion : 'v1' ,
92+ grpcMetadata : {
93+ property : 'value'
94+ }
8895 } ;
8996
9097 var OPTIONS = {
@@ -111,6 +118,7 @@ describe('GrpcService', function() {
111118 } ) ;
112119
113120 beforeEach ( function ( ) {
121+ grpcMetadataOverride = null ;
114122 retryRequestOverride = null ;
115123
116124 googleProtoFilesOverride = function ( ) {
@@ -232,6 +240,30 @@ describe('GrpcService', function() {
232240 assert . strictEqual ( calledWith [ 1 ] , OPTIONS ) ;
233241 } ) ;
234242
243+ it ( 'should default grpcMetadata to null' , function ( ) {
244+ var config = extend ( { } , CONFIG ) ;
245+ delete config . grpcMetadata ;
246+
247+ var grpcService = new GrpcService ( config , OPTIONS ) ;
248+ assert . strictEqual ( grpcService . grpcMetadata , null ) ;
249+ } ) ;
250+
251+ it ( 'should create and localize grpcMetadata' , function ( ) {
252+ var fakeGrpcMetadata = {
253+ add : function ( prop , value ) {
254+ assert . strictEqual ( prop , Object . keys ( CONFIG . grpcMetadata ) [ 0 ] ) ;
255+ assert . strictEqual ( value , CONFIG . grpcMetadata [ prop ] ) ;
256+ }
257+ } ;
258+
259+ grpcMetadataOverride = function ( ) {
260+ return fakeGrpcMetadata ;
261+ } ;
262+
263+ var grpcService = new GrpcService ( CONFIG , OPTIONS ) ;
264+ assert . strictEqual ( grpcService . grpcMetadata , fakeGrpcMetadata ) ;
265+ } ) ;
266+
235267 it ( 'should localize maxRetries' , function ( ) {
236268 assert . strictEqual ( grpcService . maxRetries , OPTIONS . maxRetries ) ;
237269 } ) ;
@@ -738,7 +770,7 @@ describe('GrpcService', function() {
738770 grpcService . protos . Service = {
739771 service : function ( ) {
740772 return {
741- method : function ( reqOpts , grpcOpts , callback ) {
773+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
742774 callback ( grpcError500 ) ;
743775 }
744776 } ;
@@ -763,7 +795,7 @@ describe('GrpcService', function() {
763795 grpcService . protos . Service = {
764796 service : function ( ) {
765797 return {
766- method : function ( reqOpts , grpcOpts , callback ) {
798+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
767799 callback ( grpcError500 ) ;
768800 }
769801 } ;
@@ -787,7 +819,7 @@ describe('GrpcService', function() {
787819 grpcService . protos . Service = {
788820 service : function ( ) {
789821 return {
790- method : function ( reqOpts , grpcOpts , callback ) {
822+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
791823 callback ( unknownError , null ) ;
792824 }
793825 } ;
@@ -821,6 +853,21 @@ describe('GrpcService', function() {
821853 grpcService . request ( PROTO_OPTS , REQ_OPTS , assert . ifError ) ;
822854 } ) ;
823855
856+ it ( 'should pass the grpc metadata with the request' , function ( done ) {
857+ grpcService . protos . Service = {
858+ service : function ( ) {
859+ return {
860+ method : function ( reqOpts , metadata ) {
861+ assert . strictEqual ( metadata , grpcService . grpcMetadata ) ;
862+ done ( ) ;
863+ }
864+ } ;
865+ }
866+ } ;
867+
868+ grpcService . request ( PROTO_OPTS , REQ_OPTS , assert . ifError ) ;
869+ } ) ;
870+
824871 it ( 'should set a deadline if a timeout is provided' , function ( done ) {
825872 var expectedDeadlineRange = [
826873 Date . now ( ) + PROTO_OPTS . timeout - 250 ,
@@ -830,7 +877,7 @@ describe('GrpcService', function() {
830877 grpcService . protos . Service = {
831878 service : function ( ) {
832879 return {
833- method : function ( reqOpts , grpcOpts ) {
880+ method : function ( reqOpts , metadata , grpcOpts ) {
834881 assert ( is . date ( grpcOpts . deadline ) ) ;
835882
836883 assert ( grpcOpts . deadline . getTime ( ) > expectedDeadlineRange [ 0 ] ) ;
@@ -874,7 +921,7 @@ describe('GrpcService', function() {
874921 grpcService . protos . Service = {
875922 service : function ( ) {
876923 return {
877- method : function ( reqOpts , grpcOpts , callback ) {
924+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
878925 callback ( grpcError ) ;
879926 }
880927 } ;
@@ -896,7 +943,7 @@ describe('GrpcService', function() {
896943 grpcService . protos . Service = {
897944 service : function ( ) {
898945 return {
899- method : function ( reqOpts , grpcOpts , callback ) {
946+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
900947 callback ( null , RESPONSE ) ;
901948 }
902949 } ;
@@ -976,7 +1023,7 @@ describe('GrpcService', function() {
9761023 return fakeDeadline ;
9771024 } ;
9781025
979- ProtoService . prototype . method = function ( reqOpts , grpcOpts ) {
1026+ ProtoService . prototype . method = function ( reqOpts , metadata , grpcOpts ) {
9801027 assert . strictEqual ( grpcOpts . deadline , fakeDeadline ) ;
9811028
9821029 GrpcService . createDeadline_ = createDeadline ;
@@ -992,6 +1039,20 @@ describe('GrpcService', function() {
9921039 grpcService . requestStream ( PROTO_OPTS , REQ_OPTS ) ;
9931040 } ) ;
9941041
1042+ it ( 'should pass the grpc metadata with the request' , function ( done ) {
1043+ ProtoService . prototype . method = function ( reqOpts , metadata ) {
1044+ assert . strictEqual ( metadata , grpcService . grpcMetadata ) ;
1045+ setImmediate ( done ) ;
1046+ return through . obj ( ) ;
1047+ } ;
1048+
1049+ retryRequestOverride = function ( _ , retryOpts ) {
1050+ return retryOpts . request ( ) ;
1051+ } ;
1052+
1053+ grpcService . requestStream ( PROTO_OPTS , REQ_OPTS ) ;
1054+ } ) ;
1055+
9951056 describe ( 'getting gRPC credentials' , function ( ) {
9961057 beforeEach ( function ( ) {
9971058 delete grpcService . grpcCredentials ;
0 commit comments