o
    Óó·ihw  ã                   @   s|  d Z ddl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mZ ddlmZmZ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mZmZm Z m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* zedƒ W n e+y…   dZ,Y nw dZ,G dd„ dej-ƒZ.G dd„ de/ƒZ0G dd„ de1ƒZ2G dd„ de%ƒZ3G dd„ de%ƒZ4G dd„ de%ƒZ5G dd „ d e3ƒZ6G d!d"„ d"e!j7ƒZ8G d#d$„ d$e!j&ƒZ9G d%d&„ d&e!j&ƒZ:G d'd(„ d(ej-ƒZ;G d)d*„ d*e;ƒZ<G d+d,„ d,ej-ƒZ=G d-d.„ d.ƒZ>G d/d0„ d0e>ej-ƒZ?G d1d2„ d2e>ej-ƒZ@G d3d4„ d4e;ƒZAG d5d6„ d6e;ƒZBG d7d8„ d8ej-ƒZCG d9d:„ d:ej-ƒZDG d;d<„ d<ej-ƒZEdS )=z6
Tests for  XML-RPC support in L{twisted.web.xmlrpc}.
é    N)ÚBytesIOÚStringIO)ÚskipIf)ÚdeferÚreactor)ÚConnectionDone)ÚEventLoggingObserverÚMemoryReactor)ÚFilteringLogObserverÚLogLevelÚLogLevelFilterPredicateÚglobalLogPublisher)Úfailure)ÚnativeStringÚnetworkString)ÚnamedModule)Úunittest)ÚclientÚhttpÚserverÚstaticÚxmlrpc)ÚDummyRequest)ÚXMLRPCÚQueryFactoryÚaddIntrospectionÚpayloadTemplateÚwithRequestÚ	xmlrpclibztwisted.internet.sslTFc                   @   ó(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚAsyncXMLRPCTestsz5
    Tests for L{XMLRPC}'s support of Deferreds.
    c                    s\   t dgƒ| _d| j_ttdt d¡f ƒ| j_t 	¡  ‰ | _
G ‡ fdd„dtƒ}|ƒ | _d S )NÚ ÚPOSTÚasync© c                       s   e Zd Z‡ fdd„ZdS )z-AsyncXMLRPCTests.setUp.<locals>.AsyncResourcec                    s   ˆ S ©Nr$   ©Úself©Úresultr$   úbC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/web/test/test_xmlrpc.pyÚxmlrpc_async=   ó   z:AsyncXMLRPCTests.setUp.<locals>.AsyncResource.xmlrpc_asyncN)Ú__name__Ú
__module__Ú__qualname__r+   r$   r(   r$   r*   ÚAsyncResource<   s    r0   )r   ÚrequestÚmethodr   r   r   ÚdumpsÚcontentr   ZDeferredr)   r   Úresource)r'   r0   r$   r(   r*   ÚsetUp3   s   ÿzAsyncXMLRPCTests.setUpc                 C   s^   | j  | j¡ |  | jjg ¡ | j d¡ t d 	| jj¡¡}|  |d¡ |  | jj
d¡ dS )zž
        If an L{XMLRPC} C{xmlrpc_*} method returns a L{defer.Deferred}, the
        response to the request is the result of that L{defer.Deferred}.
        r)   ó    )r(   Né   N)r5   Úrenderr1   ÚassertEqualÚwrittenr)   Úcallbackr   ÚloadsÚjoinÚfinished)r'   Úrespr$   r$   r*   Útest_deferredResponseB   s   z&AsyncXMLRPCTests.test_deferredResponsec                 C   sT   | j  | j¡ | j t tdƒ¡¡ | j d¡ |  	| jj
g ¡ |  	| jjd¡ dS )a   
        While waiting for the L{Deferred} returned by an L{XMLRPC} C{xmlrpc_*}
        method to fire, the connection the request was issued over may close.
        If this happens, neither C{write} nor C{finish} is called on the
        request.
        Z	Simulatedr)   r   N)r5   r9   r1   ZprocessingFailedr   ÚFailurer   r)   r<   r:   r;   r?   r&   r$   r$   r*   Ú test_interruptedDeferredResponseP   s
   z1AsyncXMLRPCTests.test_interruptedDeferredResponseN)r-   r.   r/   Ú__doc__r6   rA   rC   r$   r$   r$   r*   r    .   s
    r    c                   @   ó   e Zd ZdS )ÚTestRuntimeErrorN©r-   r.   r/   r$   r$   r$   r*   rF   ^   ó    rF   c                   @   rE   )ÚTestValueErrorNrG   r$   r$   r$   r*   rI   b   rH   rI   c                   @   s¨   e Zd ZdZdZdZdd„ Zdd„ Zg d¢g d	¢ge_d
d„ Z	g d¢ge	_dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zde_edd„ ƒZd d!„ Zd"S )#ÚTestéš  é   é*   c                 C   ó   |S r%   r$   )r'   Úargr$   r$   r*   Úxmlrpc_echon   r,   zTest.xmlrpc_echoc                 C   s   || S )z0
        This function add two numbers.
        Nr$   )r'   ÚaÚbr$   r$   r*   Ú
xmlrpc_addr   ó   zTest.xmlrpc_add©ÚintrV   rV   ©ÚdoublerX   rX   c                 C   s   ||gS )zC
        This function puts the two arguments in an array.
        Nr$   )r'   ÚstringÚnumr$   r$   r*   Úxmlrpc_pair~   rT   zTest.xmlrpc_pair©ÚarrayrY   rV   c                 C   s
   t  |¡S )úHelp for defer.N)r   Zsucceed©r'   Úxr$   r$   r*   Úxmlrpc_defer‡   s   
zTest.xmlrpc_deferc                 C   s   t  tƒ ¡S r%   )r   ÚfailrI   r&   r$   r$   r*   Úxmlrpc_deferFail‹   ó   zTest.xmlrpc_deferFailc                 C   s   t ‚r%   )rF   r&   r$   r$   r*   Úxmlrpc_fail�   r,   zTest.xmlrpc_failc                 C   s   t  dd¡S )Né   Úhello©r   ÚFaultr&   r$   r$   r*   Úxmlrpc_fault’   rd   zTest.xmlrpc_faultc                 C   s   t  t dd¡¡S )Né   Úhi)r   rb   r   ri   r&   r$   r$   r*   Úxmlrpc_deferFault•   ó   zTest.xmlrpc_deferFaultc                 C   s   d}||krt  dd¡S |S )z8
        Used to test that we can pass Unicode.
        õ   â˜ƒé   zPayload not unicode snowmanNrh   )r'   ÚpayloadÚsnowmanr$   r$   r*   Úxmlrpc_snowman˜   s   zTest.xmlrpc_snowmanc                 C   s   dddg gddœS )NrR   Úcrf   Úfoo©rQ   ÚDr$   r&   r$   r$   r*   Úxmlrpc_complex¡   rn   zTest.xmlrpc_complexc                 C   s   || S r%   r$   )r'   ÚmapÚkeyr$   r$   r*   Úxmlrpc_dict¤   ó   zTest.xmlrpc_dictúHelp for dict.c                 C   s   |j d | S )z©
        A method decorated with L{withRequest} which can be called by
        a test to verify that the request object really is passed as
        an argument.
        ú N)r2   )r'   r1   Úotherr$   r$   r*   Úxmlrpc_withRequest©   s   	ýüþzTest.xmlrpc_withRequestc                 C   s:   zt  | |¡W S  tjy   | d¡rt | jd¡‚‚ w )NZSESSIONzSession non-existent/expired.)r   ÚlookupProcedurer   ÚNoSuchFunctionÚ
startswithri   ÚSESSION_EXPIRED)r'   ZprocedurePathr$   r$   r*   r�   ¹   s   
ÿúzTest.lookupProcedureN)r-   r.   r/   ÚFAILUREÚ	NOT_FOUNDr„   rP   rS   Ú	signaturer[   ra   rc   re   rj   rm   rs   rx   r{   Úhelpr   r€   r�   r$   r$   r$   r*   rJ   f   s.    þ	
rJ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTestLookupProcedurez}
    This is a resource which customizes procedure lookup to be used by the tests
    of support for this customization.
    c                 C   rN   r%   r$   r_   r$   r$   r*   ÚechoË   r,   zTestLookupProcedure.echoc                 C   s$   |dkr| j S t | jd|› d�¡‚)zs
        Lookup a procedure from a fixed set of choices, either I{echo} or
        I{system.listeMethods}.
        rŠ   z
procedure z
 not foundN)rŠ   r   r‚   r†   )r'   ZprocedureNamer$   r$   r*   r�   Î   s
   ÿz#TestLookupProcedure.lookupProcedureN)r-   r.   r/   rD   rŠ   r�   r$   r$   r$   r*   r‰   Å   s    r‰   c                   @   ó   e Zd ZdZdd„ ZdS )ÚTestListProceduresz‚
    This is a resource which customizes procedure enumeration to be used by the
    tests of support for this customization.
    c                 C   s   dgS )zW
        Return a list of a single method this resource will claim to support.
        ru   Nr$   r&   r$   r$   r*   ÚlistProceduresà   s   z!TestListProcedures.listProceduresN)r-   r.   r/   rD   r�   r$   r$   r$   r*   rŒ   Ú   ó    rŒ   c                   @   r   )	ÚTestAuthHeaderzU
    This is used to get the header info so that we can test
    authentication.
    c                 C   s   t  | ¡ d | _d S r%   )rJ   Ú__init__r1   r&   r$   r$   r*   r�   í   s   

zTestAuthHeader.__init__c                 C   s   || _ t | |¡S r%   )r1   rJ   r9   )r'   r1   r$   r$   r*   r9   ñ   s   zTestAuthHeader.renderc                 C   s   | j  ¡ | j  ¡ fS r%   )r1   ZgetUserZgetPasswordr&   r$   r$   r*   Úxmlrpc_authinfoõ   ó   zTestAuthHeader.xmlrpc_authinfoN)r-   r.   r/   rD   r�   r9   r‘   r$   r$   r$   r*   r�   ç   s
    r�   c                   @   r   )	ÚTestQueryProtocolz_
    QueryProtocol for tests that saves headers received and sent,
    inside the factory.
    c                 C   s   | j | j_ tj | ¡ d S r%   )Ú	transportÚfactoryr   ÚQueryProtocolÚconnectionMader&   r$   r$   r*   r—   ÿ   s   
z TestQueryProtocol.connectionMadec                 C   s   || j j| ¡ < d S r%   )r•   ÚheadersÚlower©r'   rz   Úvalr$   r$   r*   ÚhandleHeader  r’   zTestQueryProtocol.handleHeaderc                 C   s$   || j j| ¡ < tj | ||¡ dS )zA
        Keep sent headers so we can inspect them later.
        N)r•   Úsent_headersr™   r   r–   Ú
sendHeaderrš   r$   r$   r*   rž     s   zTestQueryProtocol.sendHeaderN)r-   r.   r/   rD   r—   rœ   rž   r$   r$   r$   r*   r“   ù   s
    r“   c                   @   s   e Zd ZdZeZdd„ ZdS )ÚTestQueryFactoryzE
    QueryFactory using L{TestQueryProtocol} for saving headers.
    c                 O   s*   i | _ i | _tjj| g|¢R i |¤Ž d S r%   )r˜   r�   r   r   r�   )r'   ÚargsÚkwargsr$   r$   r*   r�     s   zTestQueryFactory.__init__N)r-   r.   r/   rD   r“   Úprotocolr�   r$   r$   r$   r*   rŸ     s    rŸ   c                   @   r‹   )ÚTestQueryFactoryCancelz{
    QueryFactory that saves a reference to the
    L{twisted.internet.interfaces.IConnector} to test connection lost.
    c                 C   s
   || _ d S r%   )Ú	connector)r'   r¤   r$   r$   r*   ÚstartedConnecting!  ó   
z(TestQueryFactoryCancel.startedConnectingN)r-   r.   r/   rD   r¥   r$   r$   r$   r*   r£     rŽ   r£   c                   @   s�   e Zd Zdd„ Zdd„ Zdd„ Zd!dd	„Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeedƒdd „ ƒZdS )"ÚXMLRPCTestsc                 C   ó2   t jdt tƒ ¡dd�| _| j ¡ j| _g | _d S ©Nr   ú	127.0.0.1©Z	interface)	r   Ú	listenTCPr   ÚSiterJ   ÚpÚgetHostÚportÚ	factoriesr&   r$   r$   r*   r6   &  s   
zXMLRPCTests.setUpc                 C   s   g | _ | j ¡ S r%   )r±   r®   ÚstopListeningr&   r$   r$   r*   ÚtearDown+  s   
zXMLRPCTests.tearDownc                 O   s   t |i |¤Ž}| j |¡ |S )zw
        Specific queryFactory for proxy that uses our custom
        L{TestQueryFactory}, and save factories.
        N)rŸ   r±   Úappend)r'   r    r¡   r•   r$   r$   r*   ÚqueryFactory/  s   zXMLRPCTests.queryFactoryNc                 C   s2   t  td| j ƒ¡}|du r| j|_|S ||_|S )z¾
        Return a new xmlrpc.Proxy for the test site created in
        setUp(), using the given factory as the queryFactory, or
        self.queryFactory if no factory is provided.
        úhttp://127.0.0.1:%d/N©r   ÚProxyr   r°   rµ   ©r'   r•   r®   r$   r$   r*   Úproxy8  s   ÿzXMLRPCTests.proxyc                 C   sŒ   dddddidfdfddddgfdd	d
dddg gddœfg}g }|D ]\}}}|   ¡ j|g|¢R Ž }| | j|¡ | |¡ q"tj|dd�S )N)Úadd)é   é   é   )r   )rQ   rQ   ÚdictrQ   r8   Úpair)rQ   r8   )rr   ro   ro   Úcomplexr$   rR   rt   rf   ru   rv   T©ZfireOnOneErrback©rº   Ú
callRemoteÚaddCallbackr:   r´   r   ÚDeferredList)r'   ZinputOutputÚdlÚmethr    ZoutpÚdr$   r$   r*   Útest_resultsE  s   ú	zXMLRPCTests.test_resultsc                    s@   ˆ   ¡  dd¡}‡ fdd„}‡ fdd„}| |¡ | |¡ |S )zƒ
        Verify that headers sent from the client side and the ones we
        get back from the server side are correct.

        rr   ro   c                    s4   ˆ   ˆ jd jd d¡ ˆ   ˆ jd jd d¡ d S )Nr   ó   content-typeó   text/xml; charset=utf-8ó   content-lengths   129)r:   r±   r˜   )Zingr&   r$   r*   Úcheck_server_headers^  s   ÿz6XMLRPCTests.test_headers.<locals>.check_server_headersc                    sL   ˆ   ˆ jd jd d¡ ˆ   ˆ jd jd d¡ ˆ   ˆ jd jd d¡ d S )Nr   s
   user-agents   Twisted/XMLRPClibrË   rÌ   rÍ   s   155)r:   r±   r�   )Úignr&   r$   r*   Úcheck_client_headersd  s   ÿþz6XMLRPCTests.test_headers.<locals>.check_client_headersN©rº   rÄ   rÅ   )r'   rÉ   rÎ   rÐ   r$   r&   r*   Útest_headersV  s   


zXMLRPCTests.test_headersc                    s®   t ƒ ‰tˆttjd�gƒ‰ t ˆ ¡ ˆ ‡ fdd„¡ g }dD ]"\}}ˆ ¡  	|¡}ˆ 
|tj¡}| |f‡fdd„	¡ | |¡ qtj|dd�}‡‡fdd	„}| |¡ |S )zø
        Verify that for each way a method exposed via XML-RPC can fail, the
        correct 'Content-type' header is set in the response and that the
        client-side Deferred is errbacked with an appropriate C{Fault}
        instance.
        ©ZdefaultLogLevelc                      ó
   t  ˆ ¡S r%   ©r   ZremoveObserverr$   ©Úfilteredr$   r*   Ú<lambda>~  ó   
 z)XMLRPCTests.test_errors.<locals>.<lambda>))rK   rb   )rK   Ú	deferFail)rf   Úfault)rL   ZnoSuchMethod)rk   Ú
deferFault)rM   ZSESSION_TESTc                    s   ˆ   | j|¡S r%   )r:   Ú	faultCode)ÚexcÚcoder&   r$   r*   rØ   Š  s    TrÂ   c                    sŠ   ˆj D ]}ˆ |jd d¡ qˆ dtˆ ƒ¡ ˆ d d j}ˆ d d j}t|tƒr1ˆ |t	¡ nˆ |t	¡ ˆ |t¡ ˆ 
t	t¡ d S )NrË   rÌ   r¼   r   Úlog_failurer8   )r±   r:   r˜   ÚassertEqualsÚlenÚvalueÚ
isinstancerI   ÚassertIsInstancerF   ÚflushLoggedErrors)rÏ   r•   Úf1Úf2©ÚlogObserverr'   r$   r*   ÚcbŽ  s   

ÿ
z#XMLRPCTests.test_errors.<locals>.cbN)r   r
   r   r   Úcriticalr   ÚaddObserverÚ
addCleanuprº   rÄ   ÚassertFailurer   ri   rÅ   r´   r   rÆ   )r'   rÇ   rß   Ú
methodNamerÉ   rë   r$   ©r×   rê   r'   r*   Útest_errorsr  s    ÿ

zXMLRPCTests.test_errorsc                    s^   ‡ fdd„‰ |   ˆ ¡ ddd¡}|  ˆ jjjd¡ | ¡  |  ˆ jjjd¡ |  |t	j
¡}|S )z‚
        A deferred from the Proxy can be cancelled, disconnecting
        the L{twisted.internet.interfaces.IConnector}.
        c                     s   t | i |¤Žˆ _ˆ jS r%   )r£   Úf)r    Úkw©r•   r$   r*   r•   ¨  s   z(XMLRPCTests.test_cancel.<locals>.factoryr»   r¼   r½   ÚdisconnectedN)rº   rÄ   ZassertNotEqualró   r¤   ÚstateÚcancelr:   rï   r   ÚCancelledError©r'   rÉ   r$   rõ   r*   Útest_cancel¢  s   zXMLRPCTests.test_cancelc                    s<   t  t¡}| dtdˆ jf ƒ¡}‡ fdd„}| |¡ |S )zN
        A classic GET on the xml server should return a NOT_ALLOWED.
        s   GETr¶   c                    s   ˆ   | jtj¡ d S r%   )r:   rß   r   ÚNOT_ALLOWED)Úresponser&   r$   r*   ÚcheckResponseº  r’   z0XMLRPCTests.test_errorGet.<locals>.checkResponseN)r   ÚAgentr   r1   r   r°   rÅ   )r'   ÚagentrÉ   rþ   r$   r&   r*   Útest_errorGet³  s
   

zXMLRPCTests.test_errorGetc                    sV   t  t¡}|jtdˆ jf ƒdt  tdƒ¡d�}| t j	¡ ‡ fdd„}| |¡ |S )zL
        Test that an invalid XML input returns an L{xmlrpc.Fault}.
        r¶   s   POSTs   foo)Úurir2   ZbodyProducerc                    s   ˆ   tjtj| ¡ d S r%   )ÚassertRaisesr   ri   r   r=   r(   r&   r$   r*   rë   Ì  s   z,XMLRPCTests.test_errorXMLContent.<locals>.cbN)
r   rÿ   r   r1   r   r°   ZFileBodyProducerr   rÅ   ZreadBody)r'   r   rÉ   rë   r$   r&   r*   Útest_errorXMLContentÀ  s   
ý
z XMLRPCTests.test_errorXMLContentc                 C   s*   t  ¡ }|  ¡  d|¡}| | j|¡ |S )zÈ
        If an L{xmlrpclib.DateTime} is passed as an argument to an XML-RPC
        call and then returned by the server unmodified, the result should
        be equal to the original object.
        rŠ   N)r   ÚDateTimerº   rÄ   rÅ   r:   )r'   ÚwhenrÉ   r$   r$   r*   Útest_datetimeRoundtripÒ  s   z"XMLRPCTests.test_datetimeRoundtripc                    s„   t ƒ ‰tˆttjd�gƒ‰ t ˆ ¡ ˆ ‡ fdd„¡ ˆ ¡  	dd¡}dd„ }ˆ 
td|¡ ˆ |t¡}‡‡fd	d
„}| |¡ |S )a.  
        If it is not possible to encode a response to the request (for example,
        because L{xmlrpclib.dumps} raises an exception when encoding a
        L{Fault}) the exception which prevents the response from being
        generated is logged and the request object is finished anyway.
        rÓ   c                      rÔ   r%   rÕ   r$   rÖ   r$   r*   rØ   é  rÙ   z6XMLRPCTests.test_doubleEncodingError.<locals>.<lambda>rŠ   r!   c                  _   s   t dƒ‚)NzCannot encode anything at all!)ÚRuntimeError)r    r¡   r$   r$   r*   Ú	fakeDumpsí  r|   z7XMLRPCTests.test_doubleEncodingError.<locals>.fakeDumpsr3   c                    s@   ˆ  dtˆ ƒ¡ ˆ ˆ d d jt¡ ˆ tˆ t¡ƒd¡ d S )Nr8   r   rà   )rá   râ   rå   rã   r  r:   ræ   )Úignoredré   r$   r*   ÚcbFailed÷  s   z6XMLRPCTests.test_doubleEncodingError.<locals>.cbFailedN)r   r
   r   r   rì   r   rí   rî   rº   rÄ   Úpatchr   rï   Ú	ExceptionrÅ   )r'   rÉ   r	  r  r$   rñ   r*   Útest_doubleEncodingErrorÝ  s   ÿ

z$XMLRPCTests.test_doubleEncodingErrorc                    s&   ˆ   ¡  dd¡}‡ fdd„}| |¡S )zV
        The connection to the web server is closed when the request is done.
        rŠ   r!   c                    s(   ˆ j \}ˆ  |jj¡ ˆ  |jj¡ d S r%   )r±   ZassertFalser”   Ú	connectedÚ
assertTruerö   )r
  r•   r&   r$   r*   ÚresponseDone  s   zBXMLRPCTests.test_closeConnectionAfterRequest.<locals>.responseDoneNrÑ   )r'   rÉ   r  r$   r&   r*   Ú test_closeConnectionAfterRequest   s   
z,XMLRPCTests.test_closeConnectionAfterRequestc                 C   ó:   t ƒ }tjdd|d�}| d¡ |  |jd d d¡ dS )zÍ
        For I{HTTP} URIs, L{xmlrpc.Proxy.callRemote} passes the value it
        received for the C{connectTimeout} parameter as the C{timeout} argument
        to the underlying connectTCP call.
        s   http://127.0.0.1:69g       @©ZconnectTimeoutr   Ú
someMethodr   r½   N)r	   r   r¸   rÄ   r:   Z
tcpClients©r'   r   rº   r$   r$   r*   Útest_tcpTimeout  s   ÿ
zXMLRPCTests.test_tcpTimeoutzOpenSSL not presentc                 C   r  )zÎ
        For I{HTTPS} URIs, L{xmlrpc.Proxy.callRemote} passes the value it
        received for the C{connectTimeout} parameter as the C{timeout} argument
        to the underlying connectSSL call.
        s   https://127.0.0.1:69g      @r  r  r   é   N)r	   r   r¸   rÄ   r:   Z
sslClientsr  r$   r$   r*   Útest_sslTimeout  s   ÿ
zXMLRPCTests.test_sslTimeoutr%   )r-   r.   r/   r6   r³   rµ   rº   rÊ   rÒ   rò   rû   r  r  r  r  r  r  r   ÚsslSkipr  r$   r$   r$   r*   r§   %  s"    
	0#r§   c                   @   s   e Zd ZdZddd„ZdS )ÚXMLRPCProxyWithoutSlashTestsz3
    Test with proxy that doesn't add a slash.
    Nc                 C   s2   t  td| j ƒ¡}|d u r| j|_|S ||_|S )Núhttp://127.0.0.1:%dr·   r¹   r$   r$   r*   rº   .  s   ÿz"XMLRPCProxyWithoutSlashTests.proxyr%   )r-   r.   r/   rD   rº   r$   r$   r$   r*   r  )  s    r  c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú XMLRPCPublicLookupProcedureTestszr
    Tests for L{XMLRPC}'s support of subclasses which override
    C{lookupProcedure} and C{listProcedures}.
    c                 C   sN   t jdt |¡dd�| _|  | jj¡ | j ¡ j| _t	 
td| j ƒ¡| _d S )Nr   rª   r«   r  )r   r¬   r   r­   r®   rî   r²   r¯   r°   r   r¸   r   rº   )r'   r5   r$   r$   r*   ÚcreateServer=  s   z-XMLRPCPublicLookupProcedureTests.createServerc                 C   s0   |   tƒ ¡ d}| j d|¡}| | j|¡ |S )z¢
        A subclass of L{XMLRPC} can override C{lookupProcedure} to find
        procedures that are not defined using a C{xmlrpc_}-prefixed method name.
        rg   rŠ   N)r  r‰   rº   rÄ   rÅ   r:   )r'   ÚwhatrÉ   r$   r$   r*   Útest_lookupProcedureC  s
   z5XMLRPCPublicLookupProcedureTests.test_lookupProcedurec                 C   s,   |   tƒ ¡ | j dd¡}|  |tj¡}|S )zá
        A subclass of L{XMLRPC} can override C{lookupProcedure} to raise
        L{NoSuchFunction} to indicate that a requested method is not available
        to be called, signalling a fault to the XML-RPC client.
        Zxxxxrg   N)r  r‰   rº   rÄ   rï   r   ri   rú   r$   r$   r*   rò   N  s   z,XMLRPCPublicLookupProcedureTests.test_errorsc                    s>   t ƒ }t|ƒ ˆ  |¡ ˆ j d¡}‡ fdd„}| |¡ |S )z¬
        A subclass of L{XMLRPC} can override C{listProcedures} to define
        Overriding listProcedures should prevent introspection from being
        broken.
        úsystem.listMethodsc                    s   ˆ   d| ¡ d S )Nru   )ZassertIn)Z
proceduresr&   r$   r*   Úlistedd  s   zAXMLRPCPublicLookupProcedureTests.test_listMethods.<locals>.listedN)rŒ   r   r  rº   rÄ   rÅ   )r'   r5   rÉ   r"  r$   r&   r*   Útest_listMethodsY  s   

z1XMLRPCPublicLookupProcedureTests.test_listMethodsN)r-   r.   r/   rD   r  r   rò   r#  r$   r$   r$   r*   r  7  s    r  c                   @   r   )	ÚSerializationConfigMixinas  
    Mixin which defines a couple tests which should pass when a particular flag
    is passed to L{XMLRPC}.

    These are not meant to be exhaustive serialization tests, since L{xmlrpclib}
    does all of the actual serialization work.  They are just meant to exercise
    a few codepaths to make sure we are calling into xmlrpclib correctly.

    @ivar flagName: A C{str} giving the name of the flag which must be passed to
        L{XMLRPC} to allow the tests to pass.  Subclasses should set this.

    @ivar value: A value which the specified flag will allow the serialization
        of.  Subclasses should set this.
    c                 C   sl   | j di}tjdt tdi |¤Ž¡dd�| _|  | jj¡ | j 	¡ j
| _
tjtd| j
f ƒfi |¤Ž| _dS )zO
        Create a new XML-RPC server with C{allowNone} set to C{True}.
        Tr   rª   r«   r¶   Nr$   )ÚflagNamer   r¬   r   r­   rJ   r®   rî   r²   r¯   r°   r   r¸   r   rº   )r'   r¡   r$   r$   r*   r6   ~  s   
ÿÿÿzSerializationConfigMixin.setUpc                 C   s$   | j  d| j¡}| | j| j¡ |S )zY
        C{self.value} can be round-tripped over an XMLRPC method call/response.
        r   N©rº   rÄ   rã   rÅ   r:   rú   r$   r$   r*   Útest_roundtripValueŒ  s   z,SerializationConfigMixin.test_roundtripValuec                 C   s,   | j  dd| ji¡}| | jd| ji¡ |S )zz
        A C{dict} which contains C{self.value} can be round-tripped over an
        XMLRPC method call/response.
        r   rQ   Nr&  rú   r$   r$   r*   Útest_roundtripNestedValue”  s   z2SerializationConfigMixin.test_roundtripNestedValueN)r-   r.   r/   rD   r6   r'  r(  r$   r$   r$   r*   r$  n  s
    r$  c                   @   s   e Zd ZdZdZdZdS )ÚXMLRPCAllowNoneTestszF
    Tests for passing L{None} when the C{allowNone} flag is set.
    Ú	allowNoneN)r-   r.   r/   rD   r%  rã   r$   r$   r$   r*   r)  ž  s    r)  c                   @   s(   e Zd ZdZdZe dddddd¡Zd	S )
ÚXMLRPCUseDateTimeTestszd
    Tests for passing a C{datetime.datetime} instance when the C{useDateTime}
    flag is set.
    ZuseDateTimeiÐ  rf   é   r½   é-   é;   N)r-   r.   r/   rD   r%  Údatetimerã   r$   r$   r$   r*   r+  §  s    r+  c                   @   s@   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dS )ÚXMLRPCAuthenticatedTestsz]
    Test with authenticated proxy. We run this with the same input/output as
    above.
    s   usernames   asecretc                 C   r¨   r©   )	r   r¬   r   r­   r�   r®   r¯   r°   r±   r&   r$   r$   r*   r6   º  s
   ÿ
zXMLRPCAuthenticatedTests.setUpc                 C   sN   dt | jƒt | jƒ| jf }t t|ƒ¡}| d¡}| | j	| j| jg¡ |S )Nzhttp://%s:%s@127.0.0.1:%d/Úauthinfo)
r   ÚuserÚpasswordr°   r   r¸   r   rÄ   rÅ   r:   )r'   Úurlr®   rÉ   r$   r$   r*   Útest_authInfoInURLÁ  s   ý
z+XMLRPCAuthenticatedTests.test_authInfoInURLc                 C   óB   t  td| jf ƒ| j| j¡}| d¡}| | j| j| jg¡ |S )Nr¶   r1  ©	r   r¸   r   r°   r2  r3  rÄ   rÅ   r:   ©r'   r®   rÉ   r$   r$   r*   Útest_explicitAuthInfoÌ  ó   ý
z.XMLRPCAuthenticatedTests.test_explicitAuthInfoc                 C   sH   | j d }t td| jf ƒ| j|¡}| d¡}| | j| j|g¡ |S )zÔ
        C{QueryProtocol} uses the C{base64.b64encode} function to encode user
        name and password in the I{Authorization} header, so that it doesn't
        embed new lines when using long inputs.
        é(   r¶   r1  N)	r3  r   r¸   r   r°   r2  rÄ   rÅ   r:   )r'   ZlongPasswordr®   rÉ   r$   r$   r*   Útest_longPasswordÖ  s   
ý
z*XMLRPCAuthenticatedTests.test_longPasswordc                 C   r6  )Nzhttp://wrong:info@127.0.0.1:%d/r1  r7  r8  r$   r$   r*   Útest_explicitAuthInfoOverrideæ  r:  z6XMLRPCAuthenticatedTests.test_explicitAuthInfoOverrideN)r-   r.   r/   rD   r2  r3  r6   r5  r9  r<  r=  r$   r$   r$   r*   r0  ±  s    
r0  c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚXMLRPCIntrospectionTestsc                 C   s>   t ƒ }t|ƒ tjdt |¡dd�| _| j ¡ j| _g | _	d S r©   )
rJ   r   r   r¬   r   r­   r®   r¯   r°   r±   )r'   r   r$   r$   r*   r6   ò  s
   
zXMLRPCIntrospectionTests.setUpc                    s(   ‡ fdd„}ˆ   ¡  d¡}| |¡ |S )Nc                    s   |   ¡  ˆ  | g d¢¡ d S )N)r»   rÁ   r   rÚ   rÜ   r¿   rŠ   rb   rÛ   rÀ   rr   r!  úsystem.methodHelpúsystem.methodSignaturer   )Úsortr:   )Zmethsr&   r$   r*   Ú	cbMethodsú  s
   þz<XMLRPCIntrospectionTests.test_listMethods.<locals>.cbMethodsr!  rÑ   )r'   rB  rÉ   r$   r&   r*   r#  ù  s   
z)XMLRPCIntrospectionTests.test_listMethodsc                 C   sP   g d¢}g }|D ]\}}|   ¡  d|¡}| | j|¡ | |¡ qtj|dd�S )N))r   r^   )rb   r!   )r¿   r}   r?  TrÂ   rÃ   ©r'   ZinputOutputsrÇ   rÈ   ÚexpectedrÉ   r$   r$   r*   Útest_methodHelp  s   z(XMLRPCIntrospectionTests.test_methodHelpc                 C   sl   ddg d¢g d¢gfdg d¢gfg}g }|D ]\}}|   ¡  d|¡}| | j|¡ | |¡ qtj|dd	�S )
N)r   r!   r»   rU   rW   rÀ   r\   r@  TrÂ   rÃ   rC  r$   r$   r*   Útest_methodSignature#  s   ýz-XMLRPCIntrospectionTests.test_methodSignatureN)r-   r.   r/   r6   r#  rE  rF  r$   r$   r$   r*   r>  ñ  s
    r>  c                   @   r   )	ÚXMLRPCClientErrorHandlingTestsz3
    Test error handling on the xmlrpc client.
    c                 C   s4   t  dd¡| _d| j_tjdt | j¡dd�| _d S )Ns*   This text is not a valid XML-RPC response.s
   text/plainTr   rª   r«   )	r   ZDatar5   ZisLeafr   r¬   r   r­   r°   r&   r$   r$   r*   r6   7  s   ÿÿz$XMLRPCClientErrorHandlingTests.setUpc                 C   s
   | j  ¡ S r%   )r°   r²   r&   r$   r$   r*   r³   A  r¦   z'XMLRPCClientErrorHandlingTests.tearDownc                 C   s.   t  td| j ¡ jf ƒ¡}|  | d¡t¡S )zj
        Test that calling the xmlrpc client on a static http server raises
        an exception.
        r¶   r  N)r   r¸   r   r°   r¯   rï   rÄ   Ú
ValueError)r'   rº   r$   r$   r*   Útest_erroneousResponseD  s   ÿz5XMLRPCClientErrorHandlingTests.test_erroneousResponseN)r-   r.   r/   rD   r6   r³   rI  r$   r$   r$   r*   rG  2  s
    
rG  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚQueryFactoryParseResponseTestsz>
    Test the behaviour of L{QueryFactory.parseResponse}.
    c              	   C   s<   t d d dd d ddd�| _t d¡| _d| _t tƒ ¡| _	d S )Nr"   Fr$   )ÚpathÚhostr2   r2  r3  r*  r    )r!   zinvalid xml)
r   rµ   r   r3   ÚgoodContentsÚbadContentsr   rB   r   Úreasonr&   r$   r$   r*   r6   T  s   ù
z$QueryFactoryParseResponseTests.setUpc                 C   ó,   | j j}| | j j| j¡ | j  | j¡ |S )z�
        We can safely call L{QueryFactory.clientConnectionLost} as a callback
        of L{QueryFactory.parseResponse}.
        N)rµ   ÚdeferredrÅ   ÚclientConnectionLostrO  ÚparseResponserM  rú   r$   r$   r*   Ú test_parseResponseCallbackSafetyh  ó   z?QueryFactoryParseResponseTests.test_parseResponseCallbackSafetyc                 C   rP  )z�
        We can safely call L{QueryFactory.clientConnectionLost} as an errback
        of L{QueryFactory.parseResponse}.
        N)rµ   rQ  Ú
addErrbackrR  rO  rS  rN  rú   r$   r$   r*   Útest_parseResponseErrbackSafetyu  rU  z>QueryFactoryParseResponseTests.test_parseResponseErrbackSafetyc                 C   s,   | j j}| | j j| j¡ | j  dd¡ |S )z}
        We can safely call L{QueryFactory.clientConnectionLost} as an errback
        of L{QueryFactory.badStatus}.
        ÚstatusÚmessageN)rµ   rQ  rV  rR  rO  Z	badStatusrú   r$   r$   r*   Útest_badStatusErrbackSafety‚  rU  z:QueryFactoryParseResponseTests.test_badStatusErrbackSafetyc                 C   s$   d}| j j}| j  |¡ |  |t¡S )z¥
        Some server can send a response without any data:
        L{QueryFactory.parseResponse} should catch the error and call the
        result errback.
        zM
<methodResponse>
 <params>
  <param>
  </param>
 </params>
</methodResponse>N)rµ   rQ  rS  rï   Ú
IndexError)r'   r4   rÉ   r$   r$   r*   Útest_parseResponseWithoutData�  s   z<QueryFactoryParseResponseTests.test_parseResponseWithoutDataN)	r-   r.   r/   rD   r6   rT  rW  rZ  r\  r$   r$   r$   r*   rJ  O  s    rJ  c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚXMLRPCWithRequestTestsc                 C   s   t ƒ | _d S r%   )rJ   r5   r&   r$   r$   r*   r6   ¢  rd   zXMLRPCWithRequestTests.setUpc                    sL   t dƒ}d|_tt dd¡ƒ|_‡ fdd„}| ¡  ||¡}ˆ j 	|¡ |S )z¯
        When an XML-RPC method is called and the implementation is
        decorated with L{withRequest}, the request object is passed as
        the first argument.
        z/RPC2r"   )ru   r   c                    s    t  |jd ¡}ˆ  |d¡ d S )Nr   ))zPOST fooN)r   r=   r;   r:   )Únr1   Údatar&   r$   r*   Úvalid¯  s   z6XMLRPCWithRequestTests.test_withRequest.<locals>.validN)
r   r2   r   r   r3   r4   ZnotifyFinishrÅ   r5   Zrender_POST)r'   r1   r`  rÉ   r$   r&   r*   Útest_withRequest¥  s   z'XMLRPCWithRequestTests.test_withRequestN)r-   r.   r/   r6   ra  r$   r$   r$   r*   r]  ¡  s    r]  )FrD   r/  Úior   r   r   r   Ztwisted.internetr   r   Ztwisted.internet.errorr   Ztwisted.internet.testingr   r	   Ztwisted.loggerr
   r   r   r   Ztwisted.pythonr   Ztwisted.python.compatr   r   Ztwisted.python.reflectr   Ztwisted.trialZtwisted.webr   r   r   r   r   Ztwisted.web.test.test_webr   Ztwisted.web.xmlrpcr   r   r   r   r   r   ÚImportErrorr  ZTestCaser    r  rF   rH  rI   rJ   r‰   rŒ   r�   r–   r“   rŸ   r£   r§   r  r  r$  r)  r+  r0  r>  rG  rJ  r]  r$   r$   r$   r*   Ú<module>   sX    	ÿ0_
  70	
@AR