U
    
W[^v                     @   s  d Z ddlmZmZ ddlmZmZmZ ddlm	Z	 ddl
Z
ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZmZ dd
lmZmZmZmZ ddlmZmZ ddlmZ ddl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, ze"d W n e-k
r"   dZ.Y nX dZ.G dd dej/Z0G dd de1Z2G dd de3Z4G dd deZ5G dd deZ6G dd deZ7G d d! d!e5Z8G d"d# d#ej9Z:G d$d% d%ejZ;G 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 d0d1 d1e@ej/ZAG d2d3 d3e@ej/ZBG d4d5 d5e=ZCG d6d7 d7e=ZDG d8d9 d9ej/ZEG d:d; d;ej/ZFG d<d= d=ej/ZGdS )>z6
Tests for  XML-RPC support in L{twisted.web.xmlrpc}.
    )divisionabsolute_import)nativeStringnetworkStringNativeStringIO)BytesION)unittest)xmlrpc)XMLRPCpayloadTemplateaddIntrospection)_QueryFactorywithRequest	xmlrpclib)serverclienthttpstatic)reactordefer)ConnectionDone)failure)namedModule)MemoryReactorEventLoggingObserver)DummyRequest)globalLogPublisherFilteringLogObserverLogLevelFilterPredicateLogLevelztwisted.internet.sslzOpenSSL not presentc                   @   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$   >/usr/lib/python3/dist-packages/twisted/web/test/test_xmlrpc.pyxmlrpc_async1   s    z:AsyncXMLRPCTests.setUp.<locals>.AsyncResource.xmlrpc_asyncN)__name__
__module____qualname__r+   r$   r(   r$   r*   AsyncResource0   s   r/   )r   requestmethodr   r   r   dumpscontentr   ZDeferredr)   r
   resource)r'   r/   r$   r(   r*   setUp)   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)r4   renderr0   assertEqualwrittenr)   callbackr   loadsjoinfinished)r'   Zrespr$   r$   r*   test_deferredResponse7   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)r4   r8   r0   ZprocessingFailedr   Failurer   r)   r;   r9   r:   r>   r&   r$   r$   r*    test_interruptedDeferredResponseF   s    z1AsyncXMLRPCTests.test_interruptedDeferredResponseN)r,   r-   r.   __doc__r5   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   V   s   rC   c                   @   s   e Zd ZdS )TestValueErrorNrD   r$   r$   r$   r*   rE   [   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_echoi   s    zTest.xmlrpc_echoc                 C   s   || S )z0
        This function add two numbers.
        r$   )r'   abr$   r$   r*   
xmlrpc_addm   s    zTest.xmlrpc_addintdoublec                 C   s   ||gS )zC
        This function puts the two arguments in an array.
        r$   )r'   stringZnumr$   r$   r*   xmlrpc_pairw   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   rX   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'   Z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.
         )r1   )r'   r0   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*   rs      s    
zTest.lookupProcedureN)r,   r-   r.   FAILURE	NOT_FOUNDrv   rK   rN   Z	signaturerR   rW   rY   rZ   r_   rb   rf   rk   rn   helpr   rr   rs   r$   r$   r$   r*   rF   `   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$   rU   r$   r$   r*   echo   s    zTestLookupProcedure.echoc                 C   s&   |dkr| j S t| jd|f dS )zs
        Lookup a procedure from a fixed set of choices, either I{echo} or
        I{system.listeMethods}.
        r{   zprocedure %s not foundN)r{   r	   rt   rx   )r'   ZprocedureNamer$   r$   r*   rs      s     z#TestLookupProcedure.lookupProcedureN)r,   r-   r.   rB   r{   rs   r$   r$   r$   r*   rz      s   rz   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.
        rh   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__r0   r&   r$   r$   r*   r      s    
zTestAuthHeader.__init__c                 C   s   || _ t| |S r%   )r0   rF   r8   )r'   r0   r$   r$   r*   r8      s    zTestAuthHeader.renderc                 C   s   | j  | j  fS r%   )r0   ZgetUserZgetPasswordr&   r$   r$   r*   xmlrpc_authinfo   s    zTestAuthHeader.xmlrpc_authinfoN)r,   r-   r.   rB   r   r8   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'   rm   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   Z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dd Zee_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*   r5     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*   proxy0  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   r7   pair)rL   r7   )re   rc   rc   complexr$   rM   rg   r[   rh   ri   TZfireOnOneErrbackr   
callRemoteaddCallbackr9   r   r   DeferredList)r'   ZinputOutputdlmethr   Zoutpdr$   r$   r*   test_results=  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.

        re   rc   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)r9   r   r   )Zingr&   r$   r*   check_server_headersV  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)r9   r   r   )ignr&   r$   r*   check_client_headers]  s     z6XMLRPCTests.test_headers.<locals>.check_client_headersr   r   r   )r'   r   r   r   r$   r&   r*   test_headersN  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>y  r6   z)XMLRPCTests.test_errors.<locals>.<lambda>))rG   rX   )rG   	deferFail)r[   fault)rH   ZnoSuchMethod)r`   
deferFault)rI   ZSESSION_TESTc                    s     | j|S r%   )r9   Z	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rb|t	 n|t	 |t 
t	t d S )Nr   r   r   r   log_failurer7   )r   r9   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   Z
methodNamer   r   r$   r   r   r'   r*   test_errorsl  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   stateZcancelr9   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%   )r9   r   r   ZNOT_ALLOWED)Zresponser&   r$   r*   checkResponse  s    z0XMLRPCTests.test_errorGet.<locals>.checkResponse)r   Agentr   r0   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)Zurir1   ZbodyProducerc                    s     tjtj|  d S r%   )ZassertRaisesr	   r^   r   r<   r(   r&   r$   r*   r     s    z,XMLRPCTests.test_errorXMLContent.<locals>.cb)
r   r   r   r0   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   ZDateTimer   r   r   r9   )r'   Z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     r6   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>.fakeDumpsr2   c                    s@    dt   d d jt ttd d S )Nr7   r   r   )r   r   r   r   r   r9   r   )ignoredr   r$   r*   cbFailed  s    z6XMLRPCTests.test_doubleEncodingError.<locals>.cbFailed)r   r   r   r   r   r   r   r   r   r   Z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   Z	connectedZ
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   r9   Z
tcpClientsr'   r   r   r$   r$   r*   test_tcpTimeout  s    
zXMLRPCTests.test_tcpTimeoutc                 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   r9   Z
sslClientsr   r$   r$   r*   test_sslTimeout  s    
zXMLRPCTests.test_sslTimeout)N)r,   r-   r.   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   r   sslSkipskipr$   r$   r$   r*   r     s    	
+&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'   r4   r$   r$   r*   createServer2  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   rz   r   r   r   r9   )r'   Zwhatr   r$   r$   r*   test_lookupProcedure;  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   rz   r   r   r   r	   r^   r   r$   r$   r*   r   G  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 )Nrh   )ZassertIn)Z
proceduresr&   r$   r*   listed]  s    zAXMLRPCPublicLookupProcedureTests.test_listMethods.<locals>.listed)r|   r   r   r   r   r   )r'   r4   r   r   r$   r&   r*   test_listMethodsS  s    

z1XMLRPCPublicLookupProcedureTests.test_listMethodsN)r,   r-   r.   rB   r   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 )	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*   r5   v  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   r9   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   r5   r  r  r$   r$   r$   r*   r   g  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*   r5     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   r9   )r'   Z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   r9   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   r9   )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  r5   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*   r5     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{   rX   r   r   re   r   system.methodHelpsystem.methodSignaturer   )sortr9   )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   rT   )rX   r!   )r   ro   r  Tr   r   r'   ZinputOutputsr   r   Z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   rS   rQ   r  Tr   r   r  r$   r$   r*   test_methodSignature  s    
z-XMLRPCIntrospectionTests.test_methodSignatureN)r,   r-   r.   r5   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   ZDatar4   ZisLeafr   r   r   r   r   r&   r$   r$   r*   r5     s    z$XMLRPCClientErrorHandlingTests.setUpc                 C   s
   | j  S r%   )r   r   r&   r$   r$   r*   r      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_erroneousResponse#  s    z5XMLRPCClientErrorHandlingTests.test_erroneousResponseN)r,   r-   r.   rB   r5   r   r   r$   r$   r$   r*   r    s   	r  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$   )pathZhostr1   r  r  r  r   )r!   zinvalid xml)
r   r   r   r2   goodContentsbadContentsr   r@   r   reasonr&   r$   r$   r*   r5   3  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   clientConnectionLostr%  parseResponser#  r   r$   r$   r*    test_parseResponseCallbackSafetyB  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'  r%  r(  r$  r   r$   r$   r*   test_parseResponseErrbackSafetyP  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}.
        Zstatusmessage)r   r&  r*  r'  r%  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'   r3   r   r$   r$   r*   test_parseResponseWithoutDatak  s    z<QueryFactoryParseResponseTests.test_parseResponseWithoutDataN)	r,   r-   r.   rB   r5   r)  r+  r-  r/  r$   r$   r$   r*   r!  .  s   r!  c                   @   s   e Zd Zdd Zdd ZdS )XMLRPCWithRequestTestsc                 C   s   t  | _d S r%   )rF   r4   r&   r$   r$   r*   r5     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"   )rh   r   c                    s    t |jd } |d d S )Nr   ))zPOST fooN)r   r<   r:   r9   )nr0   datar&   r$   r*   valid  s    z6XMLRPCWithRequestTests.test_withRequest.<locals>.valid)
r   r1   r   r   r2   r3   ZnotifyFinishr   r4   Zrender_POST)r'   r0   r3  r   r$   r&   r*   test_withRequest  s     z'XMLRPCWithRequestTests.test_withRequestN)r,   r-   r.   r5   r4  r$   r$   r$   r*   r0  ~  s   r0  )HrB   Z
__future__r   r   Ztwisted.python.compatr   r   r   ior   r
  Ztwisted.trialr   Ztwisted.webr	   Ztwisted.web.xmlrpcr
   r   r   r   r   r   r   r   r   r   Ztwisted.internetr   r   Ztwisted.internet.errorr   Ztwisted.pythonr   Ztwisted.python.reflectr   Ztwisted.test.proto_helpersr   r   Ztwisted.web.test.test_webr   Ztwisted.loggerr   r   r   r   ImportErrorr   ZTestCaser    r   rC   r  rE   rF   rz   r|   r~   r   r   r   r   r   r   r   r   r  r  r  r  r  r!  r0  r$   r$   r$   r*   <module>   sX   
1[
  ;0	65P