U
    2ÜŸj.  ã                   @   sˆ   d Z ddlmZ ddlmZ ddlmZ G dd„ dejƒZG dd„ dƒZ	G d	d
„ d
e	ejƒZ
G dd„ de	ejƒZG dd„ de	ejƒZdS )z)
Tests L{twisted.web.client} helper APIs
é    )Úurlparse)Úunittest)Úclientc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚURLJoinTestsz'
    Tests for L{client._urljoin}.
    c                 C   s@   |   t dd¡d¡ |   t dd¡d¡ |   t dd¡d¡ dS )z´
        L{client._urljoin} does not include a fragment identifier in the
        resulting URL if neither the base nor the new path include a fragment
        identifier.
        ó   http://foo.com/baró   /quuxs   http://foo.com/quuxs   http://foo.com/bar#s   /quux#N©ÚassertEqualr   Z_urljoin©Úself© r   úC/tmp/pip-unpacked-wheel-4b529toj/twisted/web/test/test_webclient.pyÚtest_noFragments   s    
 ÿ
 ÿ
 ÿzURLJoinTests.test_noFragmentsc                 C   s@   |   t dd¡d¡ |   t dd¡d¡ |   t dd¡d¡ dS )a  
        L{client._urljoin} preserves the fragment identifier from either the
        new path or the base URL respectively, as specified in the HTTP 1.1 bis
        draft.

        @see: U{https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#section-7.1.2}
        s   http://foo.com/bar#fragr   s   http://foo.com/quux#fragr   s   /quux#frag2s   http://foo.com/quux#frag2Nr   r
   r   r   r   Útest_preserveFragments$   s    
þ
þ
þz#URLJoinTests.test_preserveFragmentsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   r   c                   @   s’   e Zd Z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dd„ Zdd „ Zd!d"„ Zd#S )%ÚURITestsa  
    Abstract tests for L{twisted.web.client.URI}.

    Subclass this and L{unittest.TestCase}. Then provide a value for
    C{host} and C{uriHost}.

    @ivar host: A host specification for use in tests, must be L{bytes}.

    @ivar uriHost: The host specification in URI form, must be a L{bytes}. In
        most cases this is identical with C{host}. IPv6 address literals are an
        exception, according to RFC 3986 section 3.2.2, as they need to be
        enclosed in brackets. In this case this variable is different.
    c                 C   sB   |   | jt¡ |   | jt¡ |   |t¡ |  d|¡ | d| j¡S )a±  
        Replace the string "HOST" in C{template} with this test's host.

        Byte strings Python between (and including) versions 3.0 and 3.4
        cannot be formatted using C{%} or C{format} so this does a simple
        replace.

        @type template: L{bytes}
        @param template: A string containing "HOST".

        @rtype: L{bytes}
        @return: A string where "HOST" has been replaced by C{self.host}.
        s   HOST)ÚassertIsInstanceÚhostÚbytesÚuriHostZassertInÚreplace)r   Útemplater   r   r   ÚmakeURIStringI   s
    zURITests.makeURIStringó    c
           
      C   s@   |   ||||||||	f|j|j|j|j|j|j|j|jf¡ dS )aU  
        Assert that all of a L{client.URI}'s components match the expected
        values.

        @param uri: U{client.URI} instance whose attributes will be checked
            for equality.

        @type scheme: L{bytes}
        @param scheme: URI scheme specifier.

        @type netloc: L{bytes}
        @param netloc: Network location component.

        @type host: L{bytes}
        @param host: Host name.

        @type port: L{int}
        @param port: Port number.

        @type path: L{bytes}
        @param path: Hierarchical path.

        @type params: L{bytes}
        @param params: Parameters for last path segment, defaults to C{b''}.

        @type query: L{bytes}
        @param query: Query string, defaults to C{b''}.

        @type fragment: L{bytes}
        @param fragment: Fragment identifier, defaults to C{b''}.
        N)	r	   ÚschemeÚnetlocr   ÚportÚpathÚparamsÚqueryÚfragment)
r   Úurir   r   r   r   r    r!   r"   r#   r   r   r   ÚassertURIEquals]   s    "øþzURITests.assertURIEqualsc                 C   sd   t j |  d¡¡}|  d|j¡ t j |  d¡¡}|  d|j¡ t j |  d¡¡}|  d|j¡ dS )z„
        L{client.URI.fromBytes} by default assumes port 80 for the I{http}
        scheme and 443 for the I{https} scheme.
        ó   http://HOSTéP   s   http://HOST:ó   https://HOSTi»  N©r   ÚURIÚ	fromBytesr   r	   r   ©r   r$   r   r   r   Útest_parseDefaultPort�   s    zURITests.test_parseDefaultPortc                 C   sL   t jj|  d¡dd�}|  d|j¡ t jj|  d¡dd�}|  d|j¡ dS )z‚
        L{client.URI.fromBytes} accepts a C{defaultPort} parameter that
        overrides the normal default port logic.
        r&   é  )ZdefaultPortr(   Nr)   r,   r   r   r   Útest_parseCustomDefaultPortš   s     ÿz$URITests.test_parseCustomDefaultPortc                 C   sj   t j |  d¡¡}|  d|j¡ |  | j|j¡ |  | jd |j¡ t j |  d¡¡}|  | j|j¡ dS )zj
        Parsing a I{URI} splits the network location component into I{host} and
        I{port}.
        s   http://HOST:5144r.   s   :5144s   http://HOST N)	r   r*   r+   r   r	   r   r   r   r   r,   r   r   r   Útest_netlocHostPort¦   s    zURITests.test_netlocHostPortc                 C   sD   |   d¡}tj |¡}| j|d| j| jddd� |  || ¡ ¡ dS )z/
        Parse the path from a I{URI}.
        s   http://HOST/foo/baró   httpr'   ó   /foo/bar©r   r   r   r   r    N©	r   r   r*   r+   r%   r   r   r	   ÚtoBytes©r   r$   Úparsedr   r   r   Ú	test_path´   s    
úzURITests.test_pathc                 C   sD   |   d¡}tj |¡}| j|d| j| jddd� |  || ¡ ¡ dS )zL
        The path of a I{URI} that has no path is the empty string.
        r&   r1   r'   r   r3   Nr4   r6   r   r   r   Útest_noPathÄ   s    
úzURITests.test_noPathc                 C   s0   |   d¡}| jtj |¡d| j| jddd� dS )zE
        The path of a I{URI} with an empty path is C{b'/'}.
        ó   http://HOST/r1   r'   ó   /r3   N)r   r%   r   r*   r+   r   r   r,   r   r   r   Útest_emptyPathÔ   s    

úzURITests.test_emptyPathc              	   C   sF   |   d¡}tj |¡}| j|d| j| jdddd� |  || ¡ ¡ dS )z8
        Parse I{URI} parameters from a I{URI}.
        s   http://HOST/foo/bar;paramr1   r'   r2   ó   param)r   r   r   r   r    r!   Nr4   r6   r   r   r   Ú
test_paramâ   s    
ù	zURITests.test_paramc              
   C   sH   |   d¡}tj |¡}| j|d| j| jddddd� |  || ¡ ¡ dS )	z7
        Parse the query string from a I{URI}.
        s!   http://HOST/foo/bar;param?a=1&b=2r1   r'   r2   r=   ó   a=1&b=2)r   r   r   r   r    r!   r"   Nr4   r6   r   r   r   Ú
test_queryó   s    
ø
zURITests.test_queryc                 C   sJ   |   d¡}tj |¡}| j|d| j| jdddddd�	 |  || ¡ ¡ d	S )
z>
        Parse the fragment identifier from a I{URI}.
        s&   http://HOST/foo/bar;param?a=1&b=2#fragr1   r'   r2   r=   r?   s   frag)r   r   r   r   r    r!   r"   r#   Nr4   r6   r   r   r   Útest_fragment  s    
÷zURITests.test_fragmentc                 C   s$   t j |  d¡¡}|  d|j¡ dS )zn
        L{client.URI.originForm} produces an absolute I{URI} path including
        the I{URI} path.
        s   http://HOST/foos   /fooN©r   r*   r+   r   r	   Z
originFormr,   r   r   r   Útest_originForm  s    zURITests.test_originFormc                 C   s$   t j |  d¡¡}|  d|j¡ dS )z¸
        L{client.URI.originForm} produces an absolute I{URI} path including
        the I{URI} path, parameters and query string but excludes the fragment
        identifier.
        s   http://HOST/foo;param?a=1#frags   /foo;param?a=1NrB   r,   r   r   r   Útest_originFormComplex   s    ÿzURITests.test_originFormComplexc                 C   s$   t j |  d¡¡}|  d|j¡ dS )zp
        L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
        specifies no path.
        r&   r;   NrB   r,   r   r   r   Útest_originFormNoPath+  s    zURITests.test_originFormNoPathc                 C   s$   t j |  d¡¡}|  d|j¡ dS )zv
        L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
        specifies an empty path.
        r:   r;   NrB   r,   r   r   r   Útest_originFormEmptyPath3  s    z!URITests.test_originFormEmptyPathc                 C   sV   |   d¡}| d¡}t|ƒ tj |¡}|  |jt¡ |  |j	t¡ |  |j
t¡ dS )zÓ
        L{client.URI.fromBytes} parses the scheme, host, and path elements
        into L{bytes}, even when passed an URL which has previously been passed
        to L{urlparse} as a L{unicode} string.
        s   http://HOST/pathÚasciiN)r   Údecoder   r   r*   r+   r   r   r   r   r    )r   Z	goodInputZbadInputr$   r   r   r   Ú test_externalUnicodeInterference;  s    

z)URITests.test_externalUnicodeInterferenceN)r   r   r   )r   r   r   r   r   r%   r-   r/   r0   r8   r9   r<   r>   r@   rA   rC   rD   rE   rF   rI   r   r   r   r   r   :   s(        ÿ
0r   c                   @   s   e Zd ZdZd ZZdS )ÚURITestsForHostnamez>
    Tests for L{twisted.web.client.URI} with host names.
    s   example.comN©r   r   r   r   r   r   r   r   r   r   rJ   J  s   rJ   c                   @   s   e Zd ZdZd ZZdS )ÚURITestsForIPv4zG
    Tests for L{twisted.web.client.URI} with IPv4 host addresses.
    s   192.168.1.67NrK   r   r   r   r   rL   R  s   rL   c                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚURITestsForIPv6z·
    Tests for L{twisted.web.client.URI} with IPv6 host addresses.

    IPv6 addresses must always be surrounded by square braces in URIs. No
    attempt is made to test without.
    s   fe80::20c:29ff:fea4:c60s   [fe80::20c:29ff:fea4:c60]c                 C   s<   t j d¡}|  |jd¡ |  |jd¡ |  | ¡ d¡ dS )z¶
        Brackets around IPv6 addresses are stripped in the host field. The host
        field is then exported with brackets in the output of
        L{client.URI.toBytes}.
        s   http://[::1]:80/index.htmls   ::1s   [::1]:80N)r   r*   r+   r	   r   r   r5   r,   r   r   r   Ú"test_hostBracketIPv6AddressLiterale  s    z2URITestsForIPv6.test_hostBracketIPv6AddressLiteralN)r   r   r   r   r   r   rN   r   r   r   r   rM   Z  s   rM   N)r   Úurllib.parser   Ztwisted.trialr   Ztwisted.webr   ZTestCaser   r   rJ   rL   rM   r   r   r   r   Ú<module>   s   +  