Ë
    ÑÕ¯i  ã                   óÞ   — 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e«      Z	d	„ Z
d
„ Zd„ Z G d„ dej                  j                  ej                  j                  «      Z G d„ de«      Zy)z$Fake urllib HTTP connection objects.zMark Roach (mrroach@google.com)é    )ÚpformatN)ÚBytesIO)Úurllibc                   ó   — e Zd ZdZy)ÚErrorz$Base exception type for this module.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úU/var/www/html/venv/lib/python3.12/site-packages/pyactiveresource/testing/http_fake.pyr   r      s   „ Ú.r   r   c                  óˆ   — t         j                  j                  t        «      } t         j                  j	                  | «       y)zAInstall TestHandler as the only active handler for http requests.N)r   ÚrequestÚbuild_openerÚTestHandlerÚinstall_opener)Úopeners    r   Ú
initializer      s(   € ä�^‰^×(Ñ(¬Ó5€FÜ
‡N�N×!Ñ! &Õ)r   c           
      óð  — t         j                  j                  |«      }t         j                  j                  |j                  «      }t         j                  j                  t        |j                  «       «      D �cg c]	  }|||   f‘Œ c}«      }t        | t         j                  j                  |j                  |j                  |j                  ||j                  f«      t        |«      f«      S c c}w )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.
    )r   ÚparseÚurlsplitÚparse_qsÚqueryÚ	urlencodeÚsortedÚkeysÚstrÚ
urlunsplitÚschemeÚnetlocÚpathÚfragmentÚdictionary_to_canonical_str)ÚmethodÚurlÚrequest_headersÚparsedÚqsÚkr   s          r   Úcreate_response_keyr+      s¿   € ô �\‰\×"Ñ" 3Ó'€FÜ	�‰×	Ñ	˜vŸ|™|Ó	,€BÜ�L‰L×"Ñ"¼¸r¿w¹w»yÔ8IÓ#JÑ8I°1 Q¨¨1©¢JÐ8IÑ#JÓK€EÜØÜ�‰×ÑØ�M‰M˜6Ÿ=™=¨&¯+©+°u¸f¿o¹oð!Oó 	Pä# OÓ4ð	6ó 7ð 7ùò $Ks   Á=C3c                 ó’   — t        t        | j                  «       «      D �cg c]  }|j                  «       | |   f‘Œ c}«      S c c}w )z¬Create canonical string from a dictionary.

    Args:
        dictionary: The dictionary to convert.
    Returns:
        A string of the dictionary in canonical form.
    )r   r   r   Ú
capitalize)Ú
dictionaryr*   s     r   r$   r$   ,   sO   € ô ¼Ø�‰Óô:ó ñ :°A�—‘“ ¨A¡Ò/ð :ñ ó ð ùò s   ¡Ac                   óR   — e Zd ZdZdZi ZdZdZdd„Ze	d„ «       Z
e		 d	d„«       Zd„ Zy)
r   z<A urllib handler object which returns a predefined response.NÚ c                 ój   — || _         |j                  d«      | _        |j                  d«      | _        y )NÚcontextÚcheck_hostname)Ú_debuglevelÚgetÚ_contextÚ_check_hostname)ÚselfÚ
debuglevelÚkwargss      r   Ú__init__zTestHandler.__init__@   s,   € Ø%ˆÔØŸ
™
 9Ó-ˆŒØ%Ÿz™zÐ*:Ó;ˆÕr   c                 ó    — i | _         || _        y)zŠSet a static response to be returned for all requests.

        Args:
            response: A FakeResponse object to be returned.
        N)Ú_response_mapÚ	_response)ÚclsÚresponses     r   Úset_responsezTestHandler.set_responseE   s   € ð ˆÔØ ˆ�r   c                 ó¨   — t        |t        j                  j                  | j                  |«      |«      }|||f}|| j
                  t        |«      <   y)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Úsiter=   r   )	r?   r%   r"   r'   ÚbodyÚcodeÚresponse_headersÚkeyÚvalues	            r   Ú
respond_tozTestHandler.respond_toO   sK   € ô " &¬&¯,©,×*>Ñ*>¸s¿x¹xÈÓ*NØ"1ó3ˆà�tÐ-Ð.ˆØ&+ˆ×Ñœ#˜c›(Ò#r   c                 óR  — || j                   _        | j                  rÙt        |j	                  «       |j                  «       |j                  «      }t        |«      | j                  v r)| j                  t        |«         \  }}}t        |||«      S t        d|j	                  «       ›d|j                  «       ›dt        |«      ›dt        t        | j                  j                  «       «      «      ›�«      ‚t        | j                  t        «      r| 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.
        zUnknown request Ú z	
request:z
response_map:)Ú	__class__r   r=   r+   Ú
get_methodÚget_full_urlÚheadersr   ÚFakeResponser   r   Úlistr   Ú
isinstancer>   Ú	Exception)r8   Ú
http_classr   Úhttp_conn_argsrH   rF   rE   rG   s           r   Údo_openzTestHandler.do_openc   sñ   € ð ")ˆ�‰ÔØ×ÒÜ%Ø×"Ñ"Ó$ g×&:Ñ&:Ó&<¸g¿o¹oóOˆCä�3‹x˜4×-Ñ-Ñ-Ø15×1CÑ1CÄCÈÃHÑ1MÑ.��tÐ-Ü# D¨$Ð0@ÓAÐAåà#×.Ñ.Õ0°'×2FÑ2FÕ2HÜ �H¤g¬d°4×3EÑ3E×3JÑ3JÓ3LÓ.MÔ&NðPó Qð Qô ˜Ÿ™¬	Ô2Ø—.‘.Ð!à—>‘>Ð!r   )r   )éÈ   N)r   r	   r
   r   r>   r=   r   rD   r;   ÚclassmethodrA   rJ   rW   r   r   r   r   r   8   sN   „ ÙFà€IØ€MØ€GØ€Dó<ð
 ñ!ó ð!ð àEIò,ó ð,ó&"r   r   c                   ó*   — e Zd ZdZdd„Zd„ Zd„ Zd„ Zy)rQ   z'A fake HTTPResponse object for testing.Nc                 óÔ   ‡ — |‰ _         t        |«      ‰ _        |€i }|‰ _        ˆ fd„‰ _        t        |t        j                  «      r|j                  d«      }t        |«      ‰ _
        y )Nc                  ó   •— ‰ j                   S ©N)rP   ©r8   s   €r   Ú<lambda>z'FakeResponse.__init__.<locals>.<lambda>Š   s	   ø€ ˜DŸLšLr   zutf-8)rF   r   ÚmsgrP   ÚinforS   ÚsixÚ	text_typeÚencoder   Ú	body_file)r8   rF   rE   rP   s   `   r   r;   zFakeResponse.__init__„   sT   ø€ ØˆŒ	Ü�t“9ˆŒØˆ?ØˆGØˆŒÛ(ˆŒ	Ü�dœCŸM™MÔ*Ø—;‘;˜wÓ'ˆDÜ  ›ˆ�r   c                 ó6   — | j                   j                  «       S )zRead the entire response body.)re   Úreadr^   s    r   rg   zFakeResponse.read�   s   € à�~‰~×"Ñ"Ó$Ð$r   c                 ó6   — | j                   j                  «       S )z*Read a single line from the response body.)re   Úreadliner^   s    r   ri   zFakeResponse.readline“   s   € à�~‰~×&Ñ&Ó(Ð(r   c                  ó   — y)zClose the connection.Nr   r^   s    r   ÚclosezFakeResponse.close—   s   € àr   r]   )r   r	   r
   r   r;   rg   ri   rk   r   r   r   rQ   rQ   �   s   „ Ù1ó	'ò%ò)ór   rQ   )r   Ú
__author__Úpprintr   rb   r   Ú	six.movesr   rT   r   r   r+   r$   r   ÚHTTPHandlerÚHTTPSHandlerr   ÚobjectrQ   r   r   r   Ú<module>rr      si   ðñ +à.€
õ Û 
Ý Ý ô/ˆIô /ò*ò7ò(	ôF"�&—.‘.×,Ñ,¨f¯n©n×.IÑ.Iô F"ôR�6õ r   