o
    d`                     @   s   d Z dZddlmZ ddlZddlmZ ddlmZ G dd deZ	d	d
 Z
dd Zdd ZG dd dejjejjZG dd deZdS )z$Fake urllib HTTP connection objects.zMark Roach (mrroach@google.com)    )pformatN)BytesIO)urllibc                   @   s   e Zd ZdZdS )Errorz$Base exception type for this module.N)__name__
__module____qualname____doc__ r
   r
   y/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/pyactiveresource/testing/http_fake.pyr      s    r   c                  C   s   t jt} t j|  dS )zAInstall TestHandler as the only active handler for http requests.N)r   requestbuild_openerTestHandlerinstall_opener)openerr
   r
   r   
initialize   s   r   c              	      sh   t j|}t j|j t j fddt  D }t| t j	|j
|j|j||jft|fS )a  Create the response key for a request.

    Args:
        method: The http method (e.g. 'get', 'put', etc.)
        url: The path being requested including site.
        request_headers: Dictionary of headers passed along with the request.
    Returns:
        The key as a string.
    c                    s   g | ]}| | fqS r
   r
   .0kqsr
   r   
<listcomp>$   s    z'create_response_key.<locals>.<listcomp>)r   parseurlsplitparse_qsquery	urlencodesortedkeysstr
urlunsplitschemenetlocpathfragmentdictionary_to_canonical_str)methodurlrequest_headersparsedr   r
   r   r   create_response_key   s   
"r*   c                    s   t  fddt  D S )zCreate canonical string from a dictionary.

    Args:
        dictionary: The dictionary to convert.
    Returns:
        A string of the dictionary in canonical form.
    c                    s   g | ]
}|   | fqS r
   )
capitalizer   
dictionaryr
   r   r   4   s    z/dictionary_to_canonical_str.<locals>.<listcomp>)r   r   r   r,   r
   r,   r   r%   ,   s   
r%   c                   @   sN   e Zd ZdZdZi ZdZdZdddZe	dd Z
e		dd
dZdd ZdS )r   z<A urllib handler object which returns a predefined response.N r   c                 K   s"   || _ |d| _|d| _d S )Ncontextcheck_hostname)_debuglevelget_context_check_hostname)self
debuglevelkwargsr
   r
   r   __init__@   s   zTestHandler.__init__c                 C   s   i | _ || _dS )zSet a static response to be returned for all requests.

        Args:
            response: A FakeResponse object to be returned.
        N)_response_map	_response)clsresponser
   r
   r   set_responseE   s   
zTestHandler.set_response   c           	      C   s4   t |tj| j||}|||f}|| jt|< dS )a  Build a response object to be used for a specific request.

        Args:
            method: The http method (e.g. 'get', 'put' etc.)
            path: The path being requested (e.g. '/collection/id.json')
            request_headers: Dictionary of headers passed along with the request
            body: The string that should be returned for a matching request
            code: The http response code to return
            response_headers: Dictionary of headers to return
        Returns:
            None
        N)r*   r   r   urljoinsiter9   r   )	r;   r&   r#   r(   bodycoderesponse_headerskeyvaluer
   r
   r   
respond_toO   s
   
zTestHandler.respond_toc              	   K   s   || j _| jr@t| | |j}t|| jv r)| jt| \}}}t|||S t	d| | t|t
t| j f t| jtrI| j| jS )ar  Return the response object for the given request.

        Overrides the HTTPHandler method of the same name to return a
        FakeResponse instead of creating any network connections.

        Args:
            http_class: The http protocol being used.
            request: A urllib.request.Request object.
        Returns:
            A FakeResponse object.
        z0Unknown request %s %s
request:%s
response_map:%s)	__class__r   r9   r*   
get_methodget_full_urlheadersr   FakeResponser   r   listr   
isinstancer:   	Exception)r5   
http_classr   http_conn_argsrD   rB   rA   rC   r
   r
   r   do_openc   s    zTestHandler.do_open)r   )r>   N)r   r   r   r	   r:   r9   r   r@   r8   classmethodr=   rF   rQ   r
   r
   r
   r   r   8   s    

	r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )rK   z'A fake HTTPResponse object for testing.Nc                    sT   | _ t| _|d u ri }| _ fdd _t|tjr#|d}t	| _
d S )Nc                      s    j S N)rJ   r
   r5   r
   r   <lambda>   s    z'FakeResponse.__init__.<locals>.<lambda>zutf-8)rB   r   msgrJ   inforM   six	text_typeencoder   	body_file)r5   rB   rA   rJ   r
   rT   r   r8      s   

zFakeResponse.__init__c                 C   
   | j  S )zRead the entire response body.)r[   readrT   r
   r
   r   r]         
zFakeResponse.readc                 C   r\   )z*Read a single line from the response body.)r[   readlinerT   r
   r
   r   r_      r^   zFakeResponse.readlinec                 C   s   dS )zClose the connection.Nr
   rT   r
   r
   r   close   s   zFakeResponse.closerS   )r   r   r   r	   r8   r]   r_   r`   r
   r
   r
   r   rK      s    
rK   )r	   
__author__pprintr   rX   r   	six.movesr   rN   r   r   r*   r%   r   HTTPHandlerHTTPSHandlerr   objectrK   r
   r
   r
   r   <module>   s   I