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