@@ -31,6 +31,9 @@ def _make_one(self, *args, **kw):
3131 return self ._get_target_class ()(* args , ** kw )
3232
3333 def test__rpc (self ):
34+ from google .cloud import _http as connection_module
35+ from google .cloud .datastore ._http import _CLIENT_INFO
36+
3437 class ReqPB (object ):
3538
3639 def SerializeToString (self ):
@@ -56,17 +59,24 @@ def FromString(cls, pb):
5659 self .assertIsInstance (response , RspPB )
5760 self .assertEqual (response ._pb , 'CONTENT' )
5861 called_with = http ._called_with
62+ self .assertEqual (len (called_with ), 4 )
5963 self .assertEqual (called_with ['uri' ], URI )
6064 self .assertEqual (called_with ['method' ], 'POST' )
61- self .assertEqual (called_with ['headers' ]['Content-Type' ],
62- 'application/x-protobuf' )
63- self .assertEqual (called_with ['headers' ]['User-Agent' ],
64- conn .USER_AGENT )
65+ expected_headers = {
66+ 'Content-Type' : 'application/x-protobuf' ,
67+ 'User-Agent' : conn .USER_AGENT ,
68+ 'Content-Length' : '5' ,
69+ connection_module .CLIENT_INFO_HEADER : _CLIENT_INFO ,
70+ }
71+ self .assertEqual (called_with ['headers' ], expected_headers )
6572 self .assertEqual (called_with ['body' ], REQPB )
6673 self .assertEqual (conn .build_kwargs ,
6774 [{'method' : METHOD , 'project' : PROJECT }])
6875
6976 def test__request_w_200 (self ):
77+ from google .cloud import _http as connection_module
78+ from google .cloud .datastore ._http import _CLIENT_INFO
79+
7080 PROJECT = 'PROJECT'
7181 METHOD = 'METHOD'
7282 DATA = b'DATA'
@@ -77,12 +87,16 @@ def test__request_w_200(self):
7787 self .assertEqual (datastore_api ._request (PROJECT , METHOD , DATA ),
7888 'CONTENT' )
7989 called_with = http ._called_with
90+ self .assertEqual (len (called_with ), 4 )
8091 self .assertEqual (called_with ['uri' ], URI )
8192 self .assertEqual (called_with ['method' ], 'POST' )
82- self .assertEqual (called_with ['headers' ]['Content-Type' ],
83- 'application/x-protobuf' )
84- self .assertEqual (called_with ['headers' ]['User-Agent' ],
85- conn .USER_AGENT )
93+ expected_headers = {
94+ 'Content-Type' : 'application/x-protobuf' ,
95+ 'User-Agent' : conn .USER_AGENT ,
96+ 'Content-Length' : '4' ,
97+ connection_module .CLIENT_INFO_HEADER : _CLIENT_INFO ,
98+ }
99+ self .assertEqual (called_with ['headers' ], expected_headers )
86100 self .assertEqual (called_with ['body' ], DATA )
87101 self .assertEqual (conn .build_kwargs ,
88102 [{'method' : METHOD , 'project' : PROJECT }])
@@ -386,12 +400,18 @@ def _make_one(self, client, use_grpc=False):
386400 return self ._get_target_class ()(client )
387401
388402 def _verifyProtobufCall (self , called_with , URI , conn ):
403+ from google .cloud import _http as connection_module
404+ from google .cloud .datastore ._http import _CLIENT_INFO
405+
389406 self .assertEqual (called_with ['uri' ], URI )
390407 self .assertEqual (called_with ['method' ], 'POST' )
391- self .assertEqual (called_with ['headers' ]['Content-Type' ],
392- 'application/x-protobuf' )
393- self .assertEqual (called_with ['headers' ]['User-Agent' ],
394- conn .USER_AGENT )
408+ expected_headers = {
409+ 'Content-Type' : 'application/x-protobuf' ,
410+ 'User-Agent' : conn .USER_AGENT ,
411+ 'Content-Length' : str (len (called_with ['body' ])),
412+ connection_module .CLIENT_INFO_HEADER : _CLIENT_INFO ,
413+ }
414+ self .assertEqual (called_with ['headers' ], expected_headers )
395415
396416 def test_default_url (self ):
397417 klass = self ._get_target_class ()
@@ -681,6 +701,9 @@ def test_lookup_multiple_keys_w_missing(self):
681701 def test_lookup_multiple_keys_w_deferred (self ):
682702 from google .cloud .grpc .datastore .v1 import datastore_pb2
683703
704+ from google .cloud import _http as connection_module
705+ from google .cloud .datastore ._http import _CLIENT_INFO
706+
684707 PROJECT = 'PROJECT'
685708 key_pb1 = self ._make_key_pb (PROJECT )
686709 key_pb2 = self ._make_key_pb (PROJECT , id_ = 2345 )
@@ -704,9 +727,13 @@ def test_lookup_multiple_keys_w_deferred(self):
704727 self ._verifyProtobufCall (cw , URI , conn )
705728 self .assertEqual (cw ['uri' ], URI )
706729 self .assertEqual (cw ['method' ], 'POST' )
707- self .assertEqual (cw ['headers' ]['Content-Type' ],
708- 'application/x-protobuf' )
709- self .assertEqual (cw ['headers' ]['User-Agent' ], conn .USER_AGENT )
730+ expected_headers = {
731+ 'Content-Type' : 'application/x-protobuf' ,
732+ 'User-Agent' : conn .USER_AGENT ,
733+ 'Content-Length' : '48' ,
734+ connection_module .CLIENT_INFO_HEADER : _CLIENT_INFO ,
735+ }
736+ self .assertEqual (cw ['headers' ], expected_headers )
710737 rq_class = datastore_pb2 .LookupRequest
711738 request = rq_class ()
712739 request .ParseFromString (cw ['body' ])
0 commit comments