U
    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-j5h_c8_q/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   +  