
    @OOf	                     l    d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	  G d d      Z
y)	    N)command)eventsequence)hooks)core)scriptc                   B    e Zd ZdZddddZd Zd Zd Zd	 Zd
 Z	d Z
y)contextz
    A context for testing addons, which sets up the mitmproxy.ctx module so
    handlers can run as they would within mitmproxy. The context also
    provides a number of helper methods for common testing scenarios.
    NT)optionsloadcorec                   	 t        j                         }|xs t        j
                  j                         }t        j                  j                  ||      | _        | j                  j
                  | _        |r7| j                  j                  j                  t        j                                |D ]'  }| j                  j                  j                  |       ) y # t        $ r t        j                         }Y w xY w)N)
event_loop)asyncioget_running_loopRuntimeErrornew_event_loop	mitmproxyr
   OptionsmasterMasteraddonsaddr   Core)selfr
   r   r   loopas         W/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/test/taddons.py__init__zcontext.__init__   s    	,++-D 8Y..668&&--g$-G{{**KK""499;/ 	&AKK""1%	&  	,))+D	,s   C C?>C?c                     | S )N )r   s    r   	__enter__zcontext.__enter__#   s        c                      y)NFr   )r   exc_type	exc_value	tracebacks       r   __exit__zcontext.__exit__&   s    r!   c                    K   t        j                  |      D ]>  }| j                  j                  j	                  ||       d{    |j
                  s> y y7 w)zl
        Cycles the flow through the events for the flow. Stops if the flow
        is intercepted.
        N)r   iterater   r   invoke_addonintercepted)r   addonfevts       r   cyclezcontext.cycle)   sO     
 !((+ 	C++$$11%===}}	=s   AAAAAc                    || j                   j                  vr%| j                   j                  j                  |       | j                  j	                  |j                         d      5  |r | j                  j                  di | nA| j                   j                  j                  |t        j                  t                            ddd       y# 1 sw Y   yxY w)z
        A helper for testing configure methods. Modifies the registered
        Options object with the given keyword arguments, then calls the
        configure method on the addon with the updated value.
        T)reraiseNr   )r   r   registerr
   rollbackkeysupdateinvoke_addon_syncr   ConfigureHookset)r   r+   kwargss      r   	configurezcontext.configure3   s     ***KK''.\\""6;;=$"? 	X###-f-""44UE<O<OPSPU<VW		X 	X 	Xs   )A!CCc                 h    t        j                  |d      }|j                  r|j                  d   S dS )zK
        Loads a script from path, and returns the enclosed addon.
        Fr   N)r   Scriptr   )r   pathscs      r   r   zcontext.scriptA   s-     ]]4'!yyryy|2d2r!   c                 z    t        j                  | j                  j                  d|      }|j	                  |      S )z
        Invoke a command function with a list of string arguments within a command context, mimicking the actual command environment.
        ztest.command)r   Commandr   commandscall)r   funcargscmds       r   r   zcontext.commandH   s.     oodkk22NDIxx~r!   )__name__
__module____qualname____doc__r   r    r&   r.   r9   r   r   r   r!   r   r	   r	      s2     )-t & X3r!   r	   )r   mitmproxy.masterr   mitmproxy.optionsr   r   r   mitmproxy.addonsr   r   r	   r   r!   r   <module>rL      s)        #  ! #A Ar!   