
    ]j*                     F   d dl Z d dlZd dlZd dlmZ d dlmZm	Z	 d dlm
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ  eg d	      Z eg d
      ZdZ e       Z G d de
      Zd Z G d de      Zd Z G d de j:                        Zd Z G d de      Z  G d de      Z!y)    N)HTTPAdapter)MockRequestMockResponse)RequestsCookieJar)merge_cookiescookiejar_from_dict)get_encoding_from_headers)HTTPResponse)
exceptions)rawbodycontenttextjson)status_codereasonheaderscookiesjson_encoder   c                        e Zd Z fdZ xZS )	CookieJarc                 .    t        t        | 
  ||fi |S )az  Add a cookie to the Jar.

        :param str name: cookie name/key.
        :param str value: cookie value.
        :param int version: Integer or None. Netscape cookies have version 0.
            RFC 2965 and RFC 2109 cookies have a version cookie-attribute of 1.
            However, note that cookielib may 'downgrade' RFC 2109 cookies to
            Netscape cookies, in which case version is 0.
        :param str port: String representing a port or a set of ports
            (eg. '80', or '80,8080'),
        :param str domain: The domain the cookie should apply to.
        :param str path: Cookie path (a string, eg. '/acme/rocket_launchers').
        :param bool secure: True if cookie should only be returned over a
            secure connection.
        :param int expires: Integer expiry date in seconds since epoch or None.
        :param bool discard: True if this is a session cookie.
        :param str comment: String comment from the server explaining the
            function of this cookie.
        :param str comment_url: URL linking to a comment from the server
            explaining the function of this cookie.
        )superr   set)selfnamevaluekwargs	__class__s       @/root/env/lib/python3.12/site-packages/requests_mock/response.pyr   zCookieJar.set)   s    . Y)$@@@    )__name__
__module____qualname__r   __classcell__)r    s   @r!   r   r   '   s    A Ar"   r   c                  (   t         D cg c]  }| j                  |d       | }}t        |      dkD  rt        ddj	                  |      z        | D cg c]  }|t
        vs| }}|rt        ddj	                  |      z        y c c}w c c}w )N   z5You may only supply one body element. You supplied %sz, z5Too many arguments provided. Unexpected arguments %s.)
_BODY_ARGSpoplenRuntimeErrorjoin
_HTTP_ARGS	TypeError)r   xprovidedextras       r!   _check_body_argumentsr3   C   s    %IaAt)<)HIHI
8}q )+/99X+>? @ 	@ 61!:"5Q6E6 (*.))E*:; < 	<  J 7s   B
B
B$Bc                       e Zd ZdZd Zd Zy)_FakeConnectionzBAn object that can mock the necessary parts of a socket interface.c                 0    d}t        j                  |      )NzwThis response was created without a connection. You are therefore unable to make a request directly on that connection.)r   InvalidRequest)r   requestr   msgs       r!   sendz_FakeConnection.sendU   s    P'',,r"   c                      y N )r   s    r!   closez_FakeConnection.closeZ   s    r"   N)r#   r$   r%   __doc__r:   r>   r=   r"   r!   r5   r5   R   s    L-
r"   r5   c                     |j                   j                  t        |j                  j                        t        |              |rt        |j                   |       yy)zAdd cookies to the response.

    Cookies in requests are extracted from the headers in the original_response
    httplib.HTTPMessage which we don't create so we have to do this step
    manually.
    N)r   extract_cookiesr   r   r   r   r   )r8   responser   s      r!   _extract_cookiesrC   ^   sK     $$\(,,2F2F%G%0%9;
 h&&0 r"   c                       e Zd ZdZd Zy)	_IOReadera  A reader that makes a BytesIO look like a HTTPResponse.

    A HTTPResponse will return an empty string when you read from it after
    the socket has been closed. A BytesIO will raise a ValueError. For
    compatibility we want to do the same thing a HTTPResponse does.
    c                     | j                   ryt        |      dkD  r	|d   dk(  ryt        j                  j                  | g|i |}|dk(  r| j                          |S )Nr"   r   )closedr+   ioBytesIOreadr>   )r   argsr   results       r!   rJ   z_IOReader.readx   sY    ;; t9q=T!W\777
 S=JJLr"   N)r#   r$   r%   r?   rJ   r=   r"   r!   rE   rE   p   s    r"   rE   c           
         |j                  dt                     }t        di | |j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  di       }d}	|t        |t              st        d	      |t        |t              st        d
      |;|j                  dd      xs t        j                  }
t        j                  ||
      }| t        |      xs d}	|j                  |	      }|t        |      }|so|j                  dt              }|j                  dt        j                   j"                  j                  |            }t%        ||||xs t        d      dddd      }t&        j)                  | |      }||_        |	r|j,                  s|	|_        t/        | ||j                  d             |S )a  
    :param int status_code: The status code to return upon a successful
        match. Defaults to 200.
    :param HTTPResponse raw: A HTTPResponse object to return upon a
        successful match.
    :param io.IOBase body: An IO object with a read() method that can
        return a body on successful match.
    :param bytes content: A byte string to return upon a successful match.
    :param unicode text: A text string to return upon a successful match.
    :param object json: A python object to be converted to a JSON string
        and returned upon a successful match.
    :param class json_encoder: Encoder object to use for JOSON.
    :param dict headers: A dictionary object containing headers that are
        returned upon a successful match.
    :param CookieJar cookies: A cookie jar with cookies to set on the
        response.

    :returns requests.Response: A response object that can
        be returned to requests.
    
connectionr   Nr   r   r   r   r   zContent should be binary datazText should be string datar   )clszutf-8r   r   r"   F)statusr   r   r   decode_contentenforce_content_lengthpreload_contentoriginal_responser   r=   )r*   r5   r3   
isinstancebytesr/   str	jsonutilsJSONEncoderdumpsr	   encoderE   get_DEFAULT_STATUShttpclient	responsesr
   _http_adapterbuild_responserN   encodingrC   )r8   r   rN   r   r   r   r   r   r   rc   encoderrP   r   rB   s                 r!   create_responsere      s   * L/*;<J#F#
**UD
!C::fd#DjjD)G::fd#D::fd#DjjB'GH:gu#=788
4 5455**^T2Ki6K6Kt1,W5@++h'!M?;Hdkk&;&;&?&?&GH&"(#* $ 6	#*/27+0-13 ++GS9H$H))$Wh

9(=>Or"   c                       e Zd ZdZd Zy)_Contextz:Stores the data being used to process a current URL match.c                 <    || _         || _        || _        || _        y r<   )r   r   r   r   )r   r   r   r   r   s        r!   __init__z_Context.__init__   s    &r"   N)r#   r$   r%   r?   ri   r=   r"   r!   rg   rg      s
    Dr"   rg   c                       e Zd Zd Zd Zy)_MatcherResponsec                    |j                  dd       | _        | j                  r|rt        d      t        di | || _        | j                  j                  d      }| j                  j                  d      }|&t        |      st        |t              st        d      |(t        |      st        |t              st        d      y y y )Nexcz(Cannot provide other arguments with exc.r   r   z+Content should be a callback or binary dataz(Text should be a callback or string datar=   )
r*   _excr/   r3   _paramsr\   callablerU   rV   rW   )r   r   r   r   s       r!   ri   z_MatcherResponse.__init__   s    JJud+	
 99FGG''
 ,,""9-||'(9(27E(BIJJXd^%/c%:FGG &; &4r"   c                    | j                   r| j                   | j                  j                  dt                     }t	        |t
              rt        |t                     }t        | j                  j                  di       j                         | j                  j                  dt              | j                  j                  d      |      fd}t         || j                  j                  d             || j                  j                  d             || j                  j                  d             || j                  j                  d	             || j                  j                  d
            | j                  j                  d      j                  j                  j                  j                        S )Nr   r   r   r   c                 8    t        |       r | g|i |S | S r<   )rp   )frK   r   contextr8   s      r!   _callz,_MatcherResponse.get_response.<locals>._call	  s%    ;CA;1Wg777MAMr"   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )rn   ro   r\   r   rU   dictr   rg   copyr]   re   r   r   r   r   )r   r8   r   ru   rt   s    `  @r!   get_responsez_MatcherResponse.get_response   sN   99))O ,,""9ik:gt$)'9;?G4<<++Ir:??A<<++M?K<<++H5"$	N w$)$,,*:*:6*B$C$)$,,*:*:6*B$C',T\\-=-=i-H'I$)$,,*:*:6*B$C#()9)9%)@#A,0LL,<,<^,L+2+>+>&-nn'.'.
8 
	8r"   N)r#   r$   r%   ri   rx   r=   r"   r!   rk   rk      s    H48r"   rk   )"rH   http.clientr^   r   rX   requests.adaptersr   requests.cookiesr   r   r   r   r   requests.utilsr	   urllib3.responser
   requests_mockr   	frozensetr)   r.   r]   ra   r   r3   objectr5   rC   rI   rE   re   rg   rk   r=   r"   r!   <module>r      s    
   ) 6 . ? 4 ) $AB
  
 A! A8<	f 	1$

 8CLv :8v :8r"   