24
24
#
25
25
###########################################################################
26
26
#
27
- import pytest
28
27
import json
29
28
import logging
30
29
import os
@@ -199,15 +198,15 @@ def check_response(self, http_status: Optional[int] = 200,
199
198
if self .with_stats :
200
199
for idx , x in enumerate (self .stats ):
201
200
assert 'http_code' in x , \
202
- f'response #{ idx } reports no http_code\n { self .dump_logs ( )} '
201
+ f'response #{ idx } reports no http_code\n { self .dump_stat ( x )} '
203
202
assert x ['http_code' ] == http_status , \
204
203
f'response #{ idx } http_code: expected { http_status } , ' \
205
- f'got { x ["http_code" ]} \n { self .dump_logs ( )} '
204
+ f'got { x ["http_code" ]} \n { self .dump_stat ( x )} '
206
205
else :
207
206
for idx , x in enumerate (self .responses ):
208
207
assert x ['status' ] == http_status , \
209
208
f'response #{ idx } status: expected { http_status } ,' \
210
- f'got { x ["status" ]} \n { self .dump_logs ( )} '
209
+ f'got { x ["status" ]} \n { self .dump_stat ( x )} '
211
210
if protocol is not None :
212
211
if self .with_stats :
213
212
http_version = None
@@ -221,7 +220,7 @@ def check_response(self, http_status: Optional[int] = 200,
221
220
for idx , x in enumerate (self .stats ):
222
221
assert x ['http_version' ] == http_version , \
223
222
f'response #{ idx } protocol: expected http/{ http_version } ,' \
224
- f'got version { x ["http_version" ]} \n { self .dump_logs ( )} '
223
+ f'got version { x ["http_version" ]} \n { self .dump_stat ( x )} '
225
224
else :
226
225
for idx , x in enumerate (self .responses ):
227
226
assert x ['protocol' ] == protocol , \
@@ -241,26 +240,44 @@ def check_stats(self, count: int, http_status: Optional[int] = None,
241
240
if http_status is not None :
242
241
for idx , x in enumerate (self .stats ):
243
242
assert 'http_code' in x , \
244
- f'status #{ idx } reports no http_code\n { self .dump_logs ( )} '
243
+ f'status #{ idx } reports no http_code\n { self .dump_stat ( x )} '
245
244
assert x ['http_code' ] == http_status , \
246
245
f'status #{ idx } http_code: expected { http_status } , ' \
247
- f'got { x ["http_code" ]} \n { self .dump_logs ( )} '
246
+ f'got { x ["http_code" ]} \n { self .dump_stat ( x )} '
248
247
if exitcode is not None :
249
248
for idx , x in enumerate (self .stats ):
250
249
if 'exitcode' in x :
251
250
assert x ['exitcode' ] == 0 , \
252
251
f'status #{ idx } exitcode: expected { exitcode } , ' \
253
- f'got { x ["exitcode" ]} \n { self .dump_logs ( )} '
252
+ f'got { x ["exitcode" ]} \n { self .dump_stat ( x )} '
254
253
255
254
def dump_logs (self ):
256
- lines = []
257
- lines .append ('>>--stdout ----------------------------------------------\n ' )
255
+ lines = ['>>--stdout ----------------------------------------------\n ' ]
258
256
lines .extend (self ._stdout )
259
257
lines .append ('>>--stderr ----------------------------------------------\n ' )
260
258
lines .extend (self ._stderr )
261
259
lines .append ('<<-------------------------------------------------------\n ' )
262
260
return '' .join (lines )
263
261
262
+ def dump_stat (self , x ):
263
+ lines = [
264
+ 'json stat from curl:' ,
265
+ json .JSONEncoder (indent = 2 ).encode (x ),
266
+ ]
267
+ if 'xfer_id' in x :
268
+ xfer_id = x ['xfer_id' ]
269
+ lines .append (f'>>--xfer { xfer_id } trace:\n ' )
270
+ lines .extend (self .xfer_trace_for (xfer_id ))
271
+ else :
272
+ lines .append ('>>--full trace-------------------------------------------\n ' )
273
+ lines .extend (self ._stderr )
274
+ lines .append ('<<-------------------------------------------------------\n ' )
275
+ return '' .join (lines )
276
+
277
+ def xfer_trace_for (self , xfer_id ) -> List [str ]:
278
+ pat = re .compile (f'^[^[]* \\ [{ xfer_id } -.*$' )
279
+ return [line for line in self ._stderr if pat .match (line )]
280
+
264
281
265
282
class CurlClient :
266
283
0 commit comments