@@ -43,8 +43,12 @@ function fakeRetryRequest() {
43
43
return ( retryRequestOverride || retryRequest ) . apply ( null , arguments ) ;
44
44
}
45
45
46
+ var grpcMetadataOverride ;
46
47
var grpcLoadOverride ;
47
48
var fakeGrpc = {
49
+ Metadata : function ( ) {
50
+ return new ( grpcMetadataOverride || grpc . Metadata ) ( ) ;
51
+ } ,
48
52
load : function ( ) {
49
53
return ( grpcLoadOverride || grpc . load ) . apply ( null , arguments ) ;
50
54
} ,
@@ -84,7 +88,10 @@ describe('GrpcService', function() {
84
88
var CONFIG = {
85
89
proto : { } ,
86
90
service : 'Service' ,
87
- apiVersion : 'v1'
91
+ apiVersion : 'v1' ,
92
+ grpcMetadata : {
93
+ property : 'value'
94
+ }
88
95
} ;
89
96
90
97
var OPTIONS = {
@@ -111,6 +118,7 @@ describe('GrpcService', function() {
111
118
} ) ;
112
119
113
120
beforeEach ( function ( ) {
121
+ grpcMetadataOverride = null ;
114
122
retryRequestOverride = null ;
115
123
116
124
googleProtoFilesOverride = function ( ) {
@@ -232,6 +240,30 @@ describe('GrpcService', function() {
232
240
assert . strictEqual ( calledWith [ 1 ] , OPTIONS ) ;
233
241
} ) ;
234
242
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
+
235
267
it ( 'should localize maxRetries' , function ( ) {
236
268
assert . strictEqual ( grpcService . maxRetries , OPTIONS . maxRetries ) ;
237
269
} ) ;
@@ -738,7 +770,7 @@ describe('GrpcService', function() {
738
770
grpcService . protos . Service = {
739
771
service : function ( ) {
740
772
return {
741
- method : function ( reqOpts , grpcOpts , callback ) {
773
+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
742
774
callback ( grpcError500 ) ;
743
775
}
744
776
} ;
@@ -763,7 +795,7 @@ describe('GrpcService', function() {
763
795
grpcService . protos . Service = {
764
796
service : function ( ) {
765
797
return {
766
- method : function ( reqOpts , grpcOpts , callback ) {
798
+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
767
799
callback ( grpcError500 ) ;
768
800
}
769
801
} ;
@@ -787,7 +819,7 @@ describe('GrpcService', function() {
787
819
grpcService . protos . Service = {
788
820
service : function ( ) {
789
821
return {
790
- method : function ( reqOpts , grpcOpts , callback ) {
822
+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
791
823
callback ( unknownError , null ) ;
792
824
}
793
825
} ;
@@ -821,6 +853,21 @@ describe('GrpcService', function() {
821
853
grpcService . request ( PROTO_OPTS , REQ_OPTS , assert . ifError ) ;
822
854
} ) ;
823
855
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
+
824
871
it ( 'should set a deadline if a timeout is provided' , function ( done ) {
825
872
var expectedDeadlineRange = [
826
873
Date . now ( ) + PROTO_OPTS . timeout - 250 ,
@@ -830,7 +877,7 @@ describe('GrpcService', function() {
830
877
grpcService . protos . Service = {
831
878
service : function ( ) {
832
879
return {
833
- method : function ( reqOpts , grpcOpts ) {
880
+ method : function ( reqOpts , metadata , grpcOpts ) {
834
881
assert ( is . date ( grpcOpts . deadline ) ) ;
835
882
836
883
assert ( grpcOpts . deadline . getTime ( ) > expectedDeadlineRange [ 0 ] ) ;
@@ -874,7 +921,7 @@ describe('GrpcService', function() {
874
921
grpcService . protos . Service = {
875
922
service : function ( ) {
876
923
return {
877
- method : function ( reqOpts , grpcOpts , callback ) {
924
+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
878
925
callback ( grpcError ) ;
879
926
}
880
927
} ;
@@ -896,7 +943,7 @@ describe('GrpcService', function() {
896
943
grpcService . protos . Service = {
897
944
service : function ( ) {
898
945
return {
899
- method : function ( reqOpts , grpcOpts , callback ) {
946
+ method : function ( reqOpts , metadata , grpcOpts , callback ) {
900
947
callback ( null , RESPONSE ) ;
901
948
}
902
949
} ;
@@ -976,7 +1023,7 @@ describe('GrpcService', function() {
976
1023
return fakeDeadline ;
977
1024
} ;
978
1025
979
- ProtoService . prototype . method = function ( reqOpts , grpcOpts ) {
1026
+ ProtoService . prototype . method = function ( reqOpts , metadata , grpcOpts ) {
980
1027
assert . strictEqual ( grpcOpts . deadline , fakeDeadline ) ;
981
1028
982
1029
GrpcService . createDeadline_ = createDeadline ;
@@ -992,6 +1039,20 @@ describe('GrpcService', function() {
992
1039
grpcService . requestStream ( PROTO_OPTS , REQ_OPTS ) ;
993
1040
} ) ;
994
1041
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
+
995
1056
describe ( 'getting gRPC credentials' , function ( ) {
996
1057
beforeEach ( function ( ) {
997
1058
delete grpcService . grpcCredentials ;
0 commit comments