o
    ¯blw  ã                   @   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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 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)ÚFilteringLogObserverÚLogLevelÚLogLevelFilterPredicateÚglobalLogPublisher)Úfailure)ÚnativeStringÚnetworkString)ÚnamedModule)ÚEventLoggingObserverÚMemoryReactor)Ú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$   ú>/usr/lib/python3/dist-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   Ú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)r6   Ú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.
        Ú	Simulatedr)   r   N)r6   r:   r1   ÚprocessingFailedr   ÚFailurer   r)   r=   r;   r<   r@   r&   r$   r$   r*   Ú test_interruptedDeferredResponseP   s
   z1AsyncXMLRPCTests.test_interruptedDeferredResponseN)r-   r.   r/   Ú__doc__r7   rB   rF   r$   r$   r$   r*   r    .   s
    r    c                   @   ó   e Zd ZdS )ÚTestRuntimeErrorN©r-   r.   r/   r$   r$   r$   r*   rI   ^   ó    rI   c                   @   rH   )ÚTestValueErrorNrJ   r$   r$   r$   r*   rL   b   rK   rL   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_echoo   r,   zTest.xmlrpc_echoc                 C   s   || S )z0
        This function add two numbers.
        r$   )r'   ÚaÚbr$   r$   r*   Ú
xmlrpc_adds   ó   zTest.xmlrpc_add©ÚintrY   rY   ©Údoubler[   r[   c                 C   s   ||gS )zC
        This function puts the two arguments in an array.
        r$   )r'   ÚstringÚnumr$   r$   r*   Úxmlrpc_pair   rW   zTest.xmlrpc_pair©Úarrayr\   rY   c                 C   s
   t  |¡S )úHelp for defer.)r   Úsucceed©r'   Úxr$   r$   r*   Úxmlrpc_deferˆ   s   
zTest.xmlrpc_deferc                 C   s   t  tƒ ¡S r%   )r   ÚfailrL   r&   r$   r$   r*   Úxmlrpc_deferFailŒ   ó   zTest.xmlrpc_deferFailc                 C   s   t ‚r%   )rI   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“   rh   zTest.xmlrpc_faultc                 C   s   t  t dd¡¡S )Né   Úhi)r   rf   r   rm   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 snowmanrl   )r'   ÚpayloadÚsnowmanr$   r$   r*   Úxmlrpc_snowman™   s   zTest.xmlrpc_snowmanc                 C   s   dddg gddœS )NrU   Úcrj   Úfoo©rT   ÚDr$   r&   r$   r$   r*   Úxmlrpc_complex¢   rr   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.
        ú )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 )NÚSESSIONzSession non-existent/expired.)r   ÚlookupProcedurer   ÚNoSuchFunctionÚ
startswithrm   ÚSESSION_EXPIRED)r'   ÚprocedurePathr$   r$   r*   r†   º   s   
ÿúzTest.lookupProcedureN)r-   r.   r/   ÚFAILUREÚ	NOT_FOUNDr‰   rS   rV   Ú	signaturer^   re   rg   ri   rn   rq   rw   r|   r   Úhelpr   r„   r†   r$   r$   r$   r*   rM   f   s.    þ	
rM   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   rQ   r%   r$   rc   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 found)r�   r   r‡   rŒ   )r'   ÚprocedureNamer$   r$   r*   r†   Ï   s
   ÿz#TestLookupProcedure.lookupProcedureN)r-   r.   r/   rG   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.
        ry   r$   r&   r$   r$   r*   ÚlistProceduresá   s   z!TestListProcedures.listProceduresN)r-   r.   r/   rG   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%   )rM   Ú__init__r1   r&   r$   r$   r*   r—   î   s   

zTestAuthHeader.__init__c                 C   s   || _ t | |¡S r%   )r1   rM   r:   )r'   r1   r$   r$   r*   r:   ò   s   zTestAuthHeader.renderc                 C   s   | j  ¡ | j  ¡ fS r%   )r1   ÚgetUserÚgetPasswordr&   r$   r$   r*   Úxmlrpc_authinfoö   ó   zTestAuthHeader.xmlrpc_authinfoN)r-   r.   r/   rG   r—   r:   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'   r~   Ú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/   rG   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/   rG   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/   rG   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©Ú	interface)	r   Ú	listenTCPr   ÚSiterM   ÚpÚgetHostÚportÚ	factoriesr&   r$   r$   r*   r7   '  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.
        )r¨   r»   Úappend)r'   r©   rª   rž   r$   r$   r*   ÚqueryFactory0  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*   Úproxy9  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   )rT   rT   ÚdictrT   r9   Úpair)rT   r9   )rv   rs   rs   Úcomplexr$   rU   rx   rj   ry   rz   T©ÚfireOnOneErrback©rÄ   Ú
callRemoteÚaddCallbackr;   r¾   r   ÚDeferredList)r'   ÚinputOutputÚdlÚmethr©   ÚoutpÚdr$   r$   r*   Útest_resultsF  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.

        rv   rs   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¡   )Ú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_headerse  s   ÿþz6XMLRPCTests.test_headers.<locals>.check_client_headers©rÄ   rÏ   rÐ   )r'   rÖ   rÜ   rÞ   r$   r&   r*   Útest_headersW  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.
        ©ÚdefaultLogLevelc                      ó
   t  ˆ ¡S r%   ©r   ÚremoveObserverr$   ©Úfilteredr$   r*   Ú<lambda>  ó   
 z)XMLRPCTests.test_errors.<locals>.<lambda>))rN   rf   )rN   Ú	deferFail)rj   Úfault)rO   ÚnoSuchMethod)ro   Ú
deferFault)rP   Ú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_failurer9   )r»   r;   r¡   ÚassertEqualsÚlenÚvalueÚ
isinstancerL   ÚassertIsInstancerI   ÚflushLoggedErrors)rÝ   rž   Úf1Úf2©ÚlogObserverr'   r$   r*   Úcb�  s   

ÿ
z#XMLRPCTests.test_errors.<locals>.cb)r   r   r
   r	   Úcriticalr   ÚaddObserverÚ
addCleanuprÄ   rÏ   ÚassertFailurer   rm   rÐ   r¾   r   rÑ   )r'   rÓ   rñ   Ú
methodNamerÖ   rý   r$   ©rç   rü   r'   r*   Útest_errorss  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Ç   Údisconnected)rÄ   rÏ   Ú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>.checkResponse)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   ÚbodyProducerc                    s   ˆ   tjtj| ¡ d S r%   )ÚassertRaisesr   rm   r   r>   r(   r&   r$   r*   rý   Í  s   z,XMLRPCTests.test_errorXMLContent.<locals>.cb)
r   r  r   r1   r   rº   ÚFileBodyProducerr   rÐ   Ú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�   )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 )Nr9   r   rò   )ró   rô   r÷   rõ   r  r;   rø   )Úignoredrû   r$   r*   ÚcbFailedù  s   z6XMLRPCTests.test_doubleEncodingError.<locals>.cbFailed)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»   ÚassertFalser�   Ú	connectedÚ
assertTruer  )r   rž   r&   r$   r*   ÚresponseDone  s   zBXMLRPCTests.test_closeConnectionAfterRequest.<locals>.responseDonerß   )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       @©ÚconnectTimeoutr   Ú
someMethodr   rÇ   N)r   r   rÂ   rÏ   r;   Ú
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;   Ú
sslClientsr/  r$   r$   r*   Útest_sslTimeout  s   ÿ
zXMLRPCTests.test_sslTimeoutr%   )r-   r.   r/   r7   r½   r¿   rÄ   r×   rà   r  r  r  r  r  r$  r)  r0  r   ÚsslSkipr3  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Ä   0  s   ÿz"XMLRPCProxyWithoutSlashTests.proxyr%   )r-   r.   r/   rG   rÄ   r$   r$   r$   r*   r5  +  s    r5  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´   r6  )r   r¶   r   r·   r¸   r   r¼   r¹   rº   r   rÂ   r   rÄ   )r'   r6   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.
        rk   r�   )r8  r�   rÄ   rÏ   rÐ   r;   )r'   ÚwhatrÖ   r$   r$   r*   Útest_lookupProcedureE  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.
        Úxxxxrk   )r8  r�   rÄ   rÏ   r  r   rm   r  r$   r$   r*   r  P  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 )Nry   )ÚassertIn)Ú
proceduresr&   r$   r*   Úlistedf  s   zAXMLRPCPublicLookupProcedureTests.test_listMethods.<locals>.listed)r“   r   r8  rÄ   rÏ   rÐ   )r'   r6   rÖ   r?  r$   r&   r*   Útest_listMethods[  s   

z1XMLRPCPublicLookupProcedureTests.test_listMethodsN)r-   r.   r/   rG   r8  r:  r  r@  r$   r$   r$   r*   r7  9  s    r7  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·   rM   r¸   r   r¼   r¹   rº   r   rÂ   r   rÄ   )r'   rª   r$   r$   r*   r7   €  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   ©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   rT   rC  r  r$   r$   r*   Útest_roundtripNestedValue–  s   z2SerializationConfigMixin.test_roundtripNestedValueN)r-   r.   r/   rG   r7   rD  rE  r$   r$   r$   r*   rA  p  s
    rA  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/   rG   rB  rõ   r$   r$   r$   r*   rF     s    rF  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.
    ÚuseDateTimeiÐ  rj   é   rÇ   é-   é;   N)r-   r.   r/   rG   rB  Údatetimerõ   r$   r$   r$   r*   rH  ©  s    rH  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*   r7   ¼  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À   rO  ©	r   rÂ   r   rº   rP  rQ  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À   rO  )	rQ  r   rÂ   r   rº   rP  rÏ   rÐ   r;   )r'   ÚlongPasswordr¸   rÖ   r$   r$   r*   Útest_longPasswordØ  s   
ý
z*XMLRPCAuthenticatedTests.test_longPasswordc                 C   rT  )Nzhttp://wrong:info@127.0.0.1:%d/rO  rU  rV  r$   r$   r*   Útest_explicitAuthInfoOverrideè  rX  z6XMLRPCAuthenticatedTests.test_explicitAuthInfoOverrideN)r-   r.   r/   rG   rP  rQ  r7   rS  rW  r[  r\  r$   r$   r$   r*   rN  ³  s    
rN  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²   )
rM   r   r   r¶   r   r·   r¸   r¹   rº   r»   )r'   r   r$   r$   r*   r7   ô  s
   
zXMLRPCIntrospectionTests.setUpc                    s(   ‡ fdd„}ˆ   ¡  d¡}| |¡ |S )Nc                    s   |   ¡  ˆ  | g d¢¡ d S )N)rÅ   rË   r   rê   rí   rÉ   r�   rf   rë   rÊ   rv   r<  úsystem.methodHelpúsystem.methodSignaturer   )Úsortr;   )Ú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   ra   )rf   r!   )rÉ   r�   r^  TrÌ   rÎ   ©r'   Ú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Å   rX   rZ   rÊ   r_   r_  TrÌ   rÎ   rc  r$   r$   r*   Útest_methodSignature%  s   ýz-XMLRPCIntrospectionTests.test_methodSignatureN)r-   r.   r/   r7   r@  rf  rg  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   ÚDatar6   ÚisLeafr   r¶   r   r·   rº   r&   r$   r$   r*   r7   9  s   ÿÿz$XMLRPCClientErrorHandlingTests.setUpc                 C   s
   | j  ¡ S r%   )rº   r¼   r&   r$   r$   r*   r½   C  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-  )r   rÂ   r   rº   r¹   r  rÏ   Ú
ValueError)r'   rÄ   r$   r$   r*   Útest_erroneousResponseF  s   ÿz5XMLRPCClientErrorHandlingTests.test_erroneousResponseN)r-   r.   r/   rG   r7   r½   rl  r$   r$   r$   r*   rh  4  s
    
rh  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   rP  rQ  rG  r©   )r!   zinvalid xml)
r   r¿   r   r3   ÚgoodContentsÚbadContentsr   rE   r   Úreasonr&   r$   r$   r*   r7   V  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}.
        )r¿   ÚdeferredrÐ   ÚclientConnectionLostrr  ÚparseResponserp  r  r$   r$   r*   Ú test_parseResponseCallbackSafetyj  ó   z?QueryFactoryParseResponseTests.test_parseResponseCallbackSafetyc                 C   rs  )z�
        We can safely call L{QueryFactory.clientConnectionLost} as an errback
        of L{QueryFactory.parseResponse}.
        )r¿   rt  Ú
addErrbackru  rr  rv  rq  r  r$   r$   r*   Útest_parseResponseErrbackSafetyw  rx  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Úmessage)r¿   rt  ry  ru  rr  Ú	badStatusr  r$   r$   r*   Útest_badStatusErrbackSafety„  rx  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>)r¿   rt  rv  r  Ú
IndexError)r'   r4   rÖ   r$   r$   r*   Útest_parseResponseWithoutData‘  s   z<QueryFactoryParseResponseTests.test_parseResponseWithoutDataN)	r-   r.   r/   rG   r7   rw  rz  r~  r€  r$   r$   r$   r*   rm  Q  s    rm  c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚXMLRPCWithRequestTestsc                 C   s   t ƒ | _d S r%   )rM   r6   r&   r$   r$   r*   r7   ¤  rh   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"   )ry   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>.valid)
r   r2   r   r   r3   r4   ÚnotifyFinishrÐ   r6   Úrender_POST)r'   r1   r„  rÖ   r$   r&   r*   Útest_withRequest§  s   z'XMLRPCWithRequestTests.test_withRequestN)r-   r.   r/   r7   r‡  r$   r$   r$   r*   r�  £  s    r�  )FrG   rM  Úior   r   r   r   Útwisted.internetr   r   Útwisted.internet.errorr   Útwisted.loggerr   r	   r
   r   Útwisted.pythonr   Útwisted.python.compatr   r   Útwisted.python.reflectr   Útwisted.test.proto_helpersr   r   Útwisted.trialÚtwisted.webr   r   r   r   r   Útwisted.web.test.test_webr   Útwisted.web.xmlrpcr   r   r   r   r   r   ÚImportErrorr4  ÚTestCaser    r  rI   rk  rL   rM   r�   r“   r–   rŸ   rœ   r¨   r¬   r°   r5  r7  rA  rF  rH  rN  r]  rh  rm  r�  r$   r$   r$   r*   Ú<module>   sX    	ÿ0`
  70	
@AR