11import glob
22import os
33import time
4- import traceback
54from typing import Optional
65
76import pytest
87
98from httpdbg import httprecord
9+ from httpdbg .export import generate_html
1010
1111httpdbg_record_filename = pytest .StashKey [str ]()
1212
@@ -88,20 +88,6 @@ def pytest_addoption(parser):
8888 help = "save HTTP(S) traces into the allure report" ,
8989 )
9090
91- reporting_group .addoption (
92- "--httpdbg-no-headers" ,
93- action = "store_true" ,
94- default = False ,
95- help = "do not save the HTTP headers" ,
96- )
97-
98- reporting_group .addoption (
99- "--httpdbg-no-binary" ,
100- action = "store_true" ,
101- default = False ,
102- help = "do not save the HTTP payload if it's a binary content" ,
103- )
104-
10591 reporting_group .addoption (
10692 "--httpdbg-only-on-failure" ,
10793 action = "store_true" ,
@@ -183,52 +169,6 @@ def pytest_sessionfinish(session, exitstatus):
183169 session .httpdbg_recorder .__exit__ (None , None , None )
184170
185171
186- def get_allure_attachment_type_from_content_type (content_type : str ):
187- try :
188- import allure
189-
190- content_type = content_type .split (";" , 1 )[0 ].strip ()
191-
192- for attachment_type in allure .attachment_type :
193- if attachment_type .mime_type .lower () == content_type .lower ():
194- return attachment_type
195- except ImportError :
196- pass
197- return None
198-
199-
200- def req_resp_steps (label , req , save_headers , save_binary_payload ):
201- try :
202- import allure
203-
204- # we generate the payload first because we do not want to add a step
205- # if there is no headers and no payload to save
206- content = req .preview
207- payload = None
208- if content .get ("text" ):
209- payload = content .get ("text" )
210- elif save_binary_payload :
211- payload = req .content
212-
213- if save_headers or payload :
214- with allure .step (label ):
215- if save_headers :
216- allure .attach (
217- req .rawheaders .decode ("utf-8" ),
218- name = "headers" ,
219- attachment_type = allure .attachment_type .TEXT ,
220- )
221- if payload :
222- attachment_type = get_allure_attachment_type_from_content_type (
223- content .get ("content_type" , "" )
224- )
225- allure .attach (
226- payload , name = "payload" , attachment_type = attachment_type
227- )
228- except ImportError :
229- pass
230-
231-
232172@pytest .hookimpl (hookwrapper = True )
233173def pytest_runtest_makereport (item , call ):
234174
@@ -246,73 +186,13 @@ def pytest_runtest_makereport(item, call):
246186
247187 with allure .step ("httpdbg" ):
248188
249- records = item .session .httpdbg_records
250-
251- for record in records :
252-
253- label = ""
254-
255- if record .response .status_code :
256- label += f"{ record .response .status_code } "
257-
258- if record .request .method :
259- label += f"{ record .request .method } "
260-
261- if record .request .uri :
262- url = record .request .uri
263- else :
264- url = record .url
265- if len (url ) > 200 :
266- url = url [:100 ] + "..." + url [- 97 :]
267- ex = (
268- (str (type (record .exception )) + " " )
269- if record .exception is not None
270- else ""
271- )
272- label += f"{ ex } { url } "
273-
274- if record .tag :
275- label += f" (from { record .tag } )"
276-
277- with allure .step (label ):
278- details = record .url
279- details += f"\n \n status: { record .response .status_code } { record .response .message } "
280- details += f"\n \n start: { record .tbegin .isoformat ()} "
281- details += f"\n end: { record .last_update .isoformat ()} "
282-
283- if record .initiator_id in records .initiators :
284- details += f"\n \n { records .initiators [record .initiator_id ].short_stack } "
285-
286- if record .exception is not None :
287- details += (
288- f"\n \n Exception: { type (record .exception )} \n "
289- )
290- details += "" .join (
291- traceback .format_exception (
292- type (record .exception ),
293- record .exception ,
294- record .exception .__traceback__ ,
295- )
296- )
297-
298- allure .attach (
299- details ,
300- name = "details" ,
301- attachment_type = allure .attachment_type .TEXT ,
302- )
303-
304- req_resp_steps (
305- "request" ,
306- record .request ,
307- not item .config .option .httpdbg_no_headers ,
308- not item .config .option .httpdbg_no_binary ,
309- )
310- req_resp_steps (
311- "response" ,
312- record .response ,
313- not item .config .option .httpdbg_no_headers ,
314- not item .config .option .httpdbg_no_binary ,
315- )
189+ allure .attach (
190+ generate_html (
191+ item .session .httpdbg_records , for_export = True
192+ ),
193+ name = "http traces" ,
194+ attachment_type = allure .attachment_type .HTML ,
195+ )
316196 except ImportError :
317197 pass
318198
0 commit comments