| 
									
										
										
										
											2024-06-24 16:14:59 +08:00
										 |  |  | import json | 
					
						
							|  |  |  | from typing import Literal | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import httpx | 
					
						
							|  |  |  | import pytest | 
					
						
							|  |  |  | from _pytest.monkeypatch import MonkeyPatch | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-01 08:49:43 +02:00
										 |  |  | from core.helper import ssrf_proxy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 16:14:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MockedHttp: | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |     @staticmethod | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |     def httpx_request( | 
					
						
							|  |  |  |         method: Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"], url: str, **kwargs | 
					
						
							|  |  |  |     ) -> httpx.Response: | 
					
						
							| 
									
										
										
										
											2024-06-24 16:14:59 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Mocked httpx.request | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         request = httpx.Request( | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |             method, url, params=kwargs.get("params"), headers=kwargs.get("headers"), cookies=kwargs.get("cookies") | 
					
						
							| 
									
										
										
										
											2024-06-24 16:14:59 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-09-29 00:29:59 +08:00
										 |  |  |         data = kwargs.get("data") | 
					
						
							| 
									
										
										
										
											2024-08-23 23:52:25 +08:00
										 |  |  |         resp = json.dumps(data).encode("utf-8") if data else b"OK" | 
					
						
							| 
									
										
										
										
											2024-06-24 16:14:59 +08:00
										 |  |  |         response = httpx.Response( | 
					
						
							|  |  |  |             status_code=200, | 
					
						
							|  |  |  |             request=request, | 
					
						
							|  |  |  |             content=resp, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         return response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.fixture | 
					
						
							|  |  |  | def setup_http_mock(request, monkeypatch: MonkeyPatch): | 
					
						
							| 
									
										
										
										
											2025-05-01 08:49:43 +02:00
										 |  |  |     monkeypatch.setattr(ssrf_proxy, "make_request", MockedHttp.httpx_request) | 
					
						
							| 
									
										
										
										
											2024-06-24 16:14:59 +08:00
										 |  |  |     yield | 
					
						
							|  |  |  |     monkeypatch.undo() |