U
    �Acjhw  ã                   @   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+k
�r   dZ,Y nX 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                   @   s(   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#   ú@/tmp/pip-unpacked-wheel-nrvc_22y/twisted/web/test/test_xmlrpc.pyÚxmlrpc_async=   s    z:AsyncXMLRPCTests.setUp.<locals>.AsyncResource.xmlrpc_asyncN)Ú__name__Ú
__module__Ú__qualname__r*   r#   r'   r#   r)   ÚAsyncResource<   s   r.   )r   ÚrequestÚmethodr   r   r   ÚdumpsÚcontentr   ZDeferredr(   r   Úresource)r&   r.   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)r3   Úrenderr/   Ú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)r3   r7   r/   ZprocessingFailedr   ÚFailurer   r(   r:   r8   r9   r=   r%   r#   r#   r)   Ú test_interruptedDeferredResponseP   s
    z1AsyncXMLRPCTests.test_interruptedDeferredResponseN)r+   r,   r-   Ú__doc__r4   r?   rA   r#   r#   r#   r)   r   .   s   r   c                   @   s   e Zd ZdS )ÚTestRuntimeErrorN©r+   r,   r-   r#   r#   r#   r)   rC   ^   s   rC   c                   @   s   e Zd ZdS )ÚTestValueErrorNrD   r#   r#   r#   r)   rE   b   s   rE   c                   @   s®   e Zd ZdZdZdZdd„ Zdd„ Zdddgd	d	d	gge_d
d„ Z	dddg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   |S r$   r#   )r&   Úargr#   r#   r)   Úxmlrpc_echon   s    zTest.xmlrpc_echoc                 C   s   || S )z0
        This function add two numbers.
        r#   )r&   ÚaÚbr#   r#   r)   Ú
xmlrpc_addr   s    zTest.xmlrpc_addÚintÚdoublec                 C   s   ||gS )zC
        This function puts the two arguments in an array.
        r#   )r&   ÚstringÚnumr#   r#   r)   Úxmlrpc_pair~   s    zTest.xmlrpc_pairÚarrayrQ   c                 C   s
   t  |¡S )úHelp for defer.)r   Zsucceed©r&   Úxr#   r#   r)   Úxmlrpc_defer‡   s    zTest.xmlrpc_deferc                 C   s   t  tƒ ¡S r$   )r   ÚfailrE   r%   r#   r#   r)   Úxmlrpc_deferFail‹   s    zTest.xmlrpc_deferFailc                 C   s   t ‚d S r$   )rC   r%   r#   r#   r)   Úxmlrpc_fail�   s    zTest.xmlrpc_failc                 C   s   t  dd¡S )Né   Úhello©r   ÚFaultr%   r#   r#   r)   Úxmlrpc_fault’   s    zTest.xmlrpc_faultc                 C   s   t  t dd¡¡S )Né   Úhi)r   rY   r   r_   r%   r#   r#   r)   Úxmlrpc_deferFault•   s    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 snowmanr^   )r&   ÚpayloadÚsnowmanr#   r#   r)   Úxmlrpc_snowman˜   s    zTest.xmlrpc_snowmanc                 C   s   dddg gddœS )NrM   Úcr\   Úfoo©rL   ÚDr#   r%   r#   r#   r)   Úxmlrpc_complex¡   s    zTest.xmlrpc_complexc                 C   s   || S r$   r#   )r&   ÚmapÚkeyr#   r#   r)   Úxmlrpc_dict¤   s    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.
        ú )r0   )r&   r/   Úotherr#   r#   r)   Úxmlrpc_withRequest©   s    	ýüþzTest.xmlrpc_withRequestc                 C   sF   zt  | |¡W S  tjk
r@   | d¡r:t | jd¡‚n‚ Y nX d S )NZSESSIONzSession non-existent/expired.)r   ÚlookupProcedurer   ÚNoSuchFunctionÚ
startswithr_   ÚSESSION_EXPIRED)r&   ZprocedurePathr#   r#   r)   ru   ¹   s    
 ÿzTest.lookupProcedureN)r+   r,   r-   ÚFAILUREÚ	NOT_FOUNDrx   rK   rN   Ú	signaturerS   rX   rZ   r[   r`   rc   rh   rm   rp   Úhelpr   rt   ru   r#   r#   r#   r)   rF   f   s,   þ	
rF   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   s   |S r$   r#   rV   r#   r#   r)   ÚechoË   s    zTestLookupProcedure.echoc                 C   s(   |dkr| j S t | jd|› d�¡‚dS )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   rv   rz   )r&   ZprocedureNamer#   r#   r)   ru   Î   s     
ÿz#TestLookupProcedure.lookupProcedureN)r+   r,   r-   rB   r~   ru   r#   r#   r#   r)   r}   Å   s   r}   c                   @   s   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.
        rj   r#   r%   r#   r#   r)   ÚlistProceduresà   s    z!TestListProcedures.listProceduresN)r+   r,   r-   rB   r€   r#   r#   r#   r)   r   Ú   s   r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTestAuthHeaderzU
    This is used to get the header info so that we can test
    authentication.
    c                 C   s   t  | ¡ d | _d S r$   )rF   Ú__init__r/   r%   r#   r#   r)   r‚   í   s    
zTestAuthHeader.__init__c                 C   s   || _ t | |¡S r$   )r/   rF   r7   )r&   r/   r#   r#   r)   r7   ñ   s    zTestAuthHeader.renderc                 C   s   | j  ¡ | j  ¡ fS r$   )r/   ZgetUserZgetPasswordr%   r#   r#   r)   Úxmlrpc_authinfoõ   s    zTestAuthHeader.xmlrpc_authinfoN)r+   r,   r-   rB   r‚   r7   rƒ   r#   r#   r#   r)   r�   ç   s   r�   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú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&   ro   Úvalr#   r#   r)   ÚhandleHeader  s    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-   rB   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| f|ž|Ž d S r$   )r‰   rŽ   r   r   r‚   )r&   ÚargsÚkwargsr#   r#   r)   r‚     s    zTestQueryFactory.__init__N)r+   r,   r-   rB   r„   Úprotocolr‚   r#   r#   r#   r)   r�     s   r�   c                   @   s   e Zd ZdZdd„ ZdS )Ú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!  s    z(TestQueryFactoryCancel.startedConnectingN)r+   r,   r-   rB   r–   r#   r#   r#   r)   r”     s   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   s2   t jdt tƒ ¡dd�| _| j ¡ j| _g | _d S ©Nr   ú	127.0.0.1©Z	interface)	r   Ú	listenTCPr   ÚSiterF   ÚpÚgetHostÚportÚ	factoriesr%   r#   r#   r)   r4   &  s    zXMLRPCTests.setUpc                 C   s   g | _ | j ¡ S r$   )r    r�   ÚstopListeningr%   r#   r#   r)   ÚtearDown+  s    zXMLRPCTests.tearDownc                 O   s   t ||Ž}| 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)   ÚqueryFactory/  s    
zXMLRPCTests.queryFactoryNc                 C   s0   t  td| j ƒ¡}|dkr&| j|_n||_|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 ]6\}}}|   ¡ j|f|žŽ }| | j|¡ | |¡ qDtj|dd�S )N)Úadd)é   é   é   )r   )rL   rL   ÚdictrL   r6   Úpair)rL   r6   )rg   rd   rd   Úcomplexr#   rM   ri   r\   rj   rk   T©ZfireOnOneErrback©r©   Ú
callRemoteÚaddCallbackr8   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.

        rg   rd   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)r8   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)r8   r    rŽ   )Úignr%   r#   r)   Úcheck_client_headersd  s     ÿþz6XMLRPCTests.test_headers.<locals>.check_client_headers©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 ]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                      s
   t  ˆ ¡S r$   ©r   ZremoveObserverr#   ©Úfilteredr#   r)   Ú<lambda>~  r5   z)XMLRPCTests.test_errors.<locals>.<lambda>))rG   rY   )rG   Ú	deferFail)r\   Úfault)rH   ZnoSuchMethod)ra   Ú
deferFault)rI   ZSESSION_TESTc                    s   ˆ   | j|¡S r$   )r8   Ú	faultCode)ÚexcÚcoder%   r#   r)   rÆ   Š  r5   Tr±   c                    sŠ   ˆj D ]}ˆ |jd d¡ qˆ dtˆ ƒ¡ ˆ d d j}ˆ d d j}t|tƒrbˆ |t	¡ nˆ |t	¡ ˆ |t¡ ˆ 
t	t¡ d S )Nrº   r»   r«   r   Úlog_failurer6   )r    r8   r‰   ÚassertEqualsÚlenÚvalueÚ
isinstancerE   ÚassertIsInstancerC   Ú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   r_   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 | |Žˆ _ˆ jS r$   )r”   Úf)r‘   Úkw©r†   r#   r)   r†   ¨  s    z(XMLRPCTests.test_cancel.<locals>.factoryrª   r«   r¬   Údisconnected)r©   r³   ZassertNotEqualrà   r•   ÚstateÚcancelr8   rÜ   r   Z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$   )r8   rÌ   r   ÚNOT_ALLOWED)Úresponser%   r#   r)   ÚcheckResponseº  s    z0XMLRPCTests.test_errorGet.<locals>.checkResponse)r   ÚAgentr   r/   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)Úurir0   ZbodyProducerc                    s   ˆ   tjtj| ¡ d S r$   )ÚassertRaisesr   r_   r   r;   r'   r%   r#   r)   rØ   Ì  s    z,XMLRPCTests.test_errorXMLContent.<locals>.cb)
r   rë   r   r/   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~   )r   ÚDateTimer©   r³   r´   r8   )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                      s
   t  ˆ ¡S r$   rÃ   r#   rÄ   r#   r)   rÆ   é  r5   z6XMLRPCTests.test_doubleEncodingError.<locals>.<lambda>r~   r    c                  _   s   t dƒ‚d S )NzCannot encode anything at all!)ÚRuntimeError)r‘   r’   r#   r#   r)   Ú	fakeDumpsí  s    z7XMLRPCTests.test_doubleEncodingError.<locals>.fakeDumpsr1   c                    s@   ˆ  dtˆ ƒ¡ ˆ ˆ d d jt¡ ˆ tˆ t¡ƒd¡ d S )Nr6   r   rÍ   )rÎ   rÏ   rÒ   rÐ   rô   r8   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    Z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   s:   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³   r8   Z
tcpClients©r&   r   r©   r#   r#   r)   Útest_tcpTimeout  s      ÿ
zXMLRPCTests.test_tcpTimeoutzOpenSSL not presentc                 C   s:   t ƒ }tjdd|d�}| d¡ |  |jd d d¡ dS )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³   r8   Z
sslClientsr  r#   r#   r)   Útest_sslTimeout  s      ÿ
zXMLRPCTests.test_sslTimeout)N)r+   r,   r-   r4   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   s0   t  td| j ƒ¡}|d kr&| j|_n||_|S )Núhttp://127.0.0.1:%dr¦   r¨   r#   r#   r)   r©   .  s
    
z"XMLRPCProxyWithoutSlashTests.proxy)N)r+   r,   r-   rB   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&   r3   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.
        r]   r~   )r	  r}   r©   r³   r´   r8   )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xxxxr]   )r	  r}   r©   r³   rÜ   r   r_   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 )Nrj   )ZassertIn)Z
proceduresr%   r#   r)   Úlistedd  s    zAXMLRPCPublicLookupProcedureTests.test_listMethods.<locals>.listed)r   r   r	  r©   r³   r´   )r&   r3   r¸   r  r#   r%   r)   Útest_listMethodsY  s    

z1XMLRPCPublicLookupProcedureTests.test_listMethodsN)r+   r,   r-   rB   r	  r  rß   r  r#   r#   r#   r)   r  7  s
   r  c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú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   sd   | j di}tjdt tf |Ž¡dd�| _|  | jj¡ | j 	¡ j
| _
tjtd| j
f ƒf|Ž| _dS )zO
        Create a new XML-RPC server with C{allowNone} set to C{True}.
        Tr   r™   rš   r¥   N)ÚflagNamer   r›   r   rœ   rF   r�   rÛ   r¡   rž   rŸ   r   r§   r   r©   )r&   r’   r#   r#   r)   r4   ~  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´   r8   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   rL   r  ræ   r#   r#   r)   Útest_roundtripNestedValue”  s    z2SerializationConfigMixin.test_roundtripNestedValueN)r+   r,   r-   rB   r4   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-   rB   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Ð  r\   é   r¬   é-   é;   N)r+   r,   r-   rB   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   s2   t jdt tƒ ¡dd�| _| j ¡ j| _g | _d S r˜   )	r   r›   r   rœ   r�   r�   rž   rŸ   r    r%   r#   r#   r)   r4   º  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´   r8   )r&   Úurlr�   r¸   r#   r#   r)   Útest_authInfoInURLÁ  s    ý
z+XMLRPCAuthenticatedTests.test_authInfoInURLc                 C   sB   t  td| jf ƒ| j| j¡}| d¡}| | j| j| jg¡ |S )Nr¥   r  ©	r   r§   r   rŸ   r  r  r³   r´   r8   ©r&   r�   r¸   r#   r#   r)   Útest_explicitAuthInfoÌ  s    ý
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¥   r  )	r  r   r§   r   rŸ   r  r³   r´   r8   )r&   ZlongPasswordr�   r¸   r#   r#   r)   Útest_longPasswordÖ  s    
ý
z*XMLRPCAuthenticatedTests.test_longPasswordc                 C   sB   t  td| jf ƒ| j| j¡}| d¡}| | j| j| jg¡ |S )Nzhttp://wrong:info@127.0.0.1:%d/r  r!  r"  r#   r#   r)   Útest_explicitAuthInfoOverrideæ  s    ý
z6XMLRPCAuthenticatedTests.test_explicitAuthInfoOverrideN)r+   r,   r-   rB   r  r  r4   r   r#  r%  r&  r#   r#   r#   r)   r  ±  s   
r  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˜   )
rF   r   r   r›   r   rœ   r�   rž   rŸ   r    )r&   r   r#   r#   r)   r4   ò  s
    zXMLRPCIntrospectionTests.setUpc                    s(   ‡ fdd„}ˆ   ¡  d¡}| |¡ |S )Nc                    s6   |   ¡  ˆ  | ddddddddd	d
dddddg¡ d S )Nrª   r°   r   rÇ   rÉ   r®   r~   rY   rÈ   r¯   rg   r  úsystem.methodHelpúsystem.methodSignaturer   )Úsortr8   )Zmethsr%   r#   r)   Ú	cbMethodsú  s(    ñþz<XMLRPCIntrospectionTests.test_listMethods.<locals>.cbMethodsr  rÀ   )r&   r+  r¸   r#   r%   r)   r  ù  s    
z)XMLRPCIntrospectionTests.test_listMethodsc                 C   sR   dddg}g }|D ]0\}}|   ¡  d|¡}| | j|¡ | |¡ qtj|dd�S )N)r   rU   )rY   r    )r®   rq   r(  Tr±   r²   ©r&   ZinputOutputsr¶   r·   Úexpectedr¸   r#   r#   r)   Útest_methodHelp  s    ýz(XMLRPCIntrospectionTests.test_methodHelpc                 C   sr   dddddgdddggfddddggfg}g }|D ]0\}}|   ¡  d|¡}| | j|¡ | |¡ q2tj|d	d
�S )N)r   r    rª   rO   rP   r¯   rT   rQ   r)  Tr±   r²   r,  r#   r#   r)   Útest_methodSignature#  s    ýz-XMLRPCIntrospectionTests.test_methodSignatureN)r+   r,   r-   r4   r  r.  r/  r#   r#   r#   r)   r'  ñ  s   r'  c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú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   ÚDatar3   ZisLeafr   r›   r   rœ   rŸ   r%   r#   r#   r)   r4   7  s     ÿ 
 ÿz$XMLRPCClientErrorHandlingTests.setUpc                 C   s
   | j  ¡ S r$   )rŸ   r¡   r%   r#   r#   r)   r¢   A  s    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_erroneousResponseD  s    ÿz5XMLRPCClientErrorHandlingTests.test_erroneousResponseN)r+   r,   r-   rB   r4   r¢   r3  r#   r#   r#   r)   r0  2  s   
r0  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Úhostr0   r  r  r  r‘   )r    zinvalid xml)
r   r¤   r   r1   ÚgoodContentsÚbadContentsr   r@   r   Úreasonr%   r#   r#   r)   r4   T  s    ù
z$QueryFactoryParseResponseTests.setUpc                 C   s,   | 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´   ÚclientConnectionLostr9  ÚparseResponser7  ræ   r#   r#   r)   Ú test_parseResponseCallbackSafetyh  s    z?QueryFactoryParseResponseTests.test_parseResponseCallbackSafetyc                 C   s,   | j j}| | j j| j¡ | j  | j¡ |S )z�
        We can safely call L{QueryFactory.clientConnectionLost} as an errback
        of L{QueryFactory.parseResponse}.
        )r¤   r:  Ú
addErrbackr;  r9  r<  r8  ræ   r#   r#   r)   Útest_parseResponseErrbackSafetyu  s    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¤   r:  r>  r;  r9  Z	badStatusræ   r#   r#   r)   Útest_badStatusErrbackSafety‚  s    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¤   r:  r<  rÜ   Ú
IndexError)r&   r2   r¸   r#   r#   r)   Útest_parseResponseWithoutData�  s    z<QueryFactoryParseResponseTests.test_parseResponseWithoutDataN)	r+   r,   r-   rB   r4   r=  r?  rB  rD  r#   r#   r#   r)   r4  O  s   r4  c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚXMLRPCWithRequestTestsc                 C   s   t ƒ | _d S r$   )rF   r3   r%   r#   r#   r)   r4   ¢  s    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!   )rj   r   c                    s    t  |jd ¡}ˆ  |d¡ d S )Nr   ))zPOST fooN)r   r;   r9   r8   )Únr/   Údatar%   r#   r)   Úvalid¯  s    z6XMLRPCWithRequestTests.test_withRequest.<locals>.valid)
r   r0   r   r   r1   r2   ZnotifyFinishr´   r3   Zrender_POST)r&   r/   rH  r¸   r#   r%   r)   Útest_withRequest¥  s    z'XMLRPCWithRequestTests.test_withRequestN)r+   r,   r-   r4   rI  r#   r#   r#   r)   rE  ¡  s   rE  )FrB   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ô   rC   r2  rE   rF   r}   r   r�   r‡   r„   r�   r”   r—   r  r  r  r  r  r  r'  r0  r4  rE  r#   r#   r#   r)   Ú<module>   sT    	
0_
  70	
@AR