o
    Æb.  ć                   @   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   Ś_urljoin©Śself© r   śA/usr/lib/python3/dist-packages/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ŚuriHostŚ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(   é  )Ś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:5144r0   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   óD   |   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“   ó   
śzURITests.test_pathc                 C   r4   )zL
        The path of a I{URI} that has no path is the empty string.
        r(   r5   r)   r   r7   Nr8   r:   r   r   r   Śtest_noPathÄ   r=   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/r5   r)   ó   /r7   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;paramr5   r)   r6   ó   param)r   r    r   r!   r"   r#   Nr8   r:   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=2r5   r)   r6   rB   ó   a=1&b=2)r   r    r   r!   r"   r#   r$   Nr8   r:   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#fragr5   r)   r6   rB   rD   s   frag)r   r    r   r!   r"   r#   r$   r%   Nr8   r:   r   r   r   Śtest_fragment  s   
÷zURITests.test_fragmentc                 C   ó$   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	   Ś
originFormr.   r   r   r   Śtest_originForm  ó   zURITests.test_originFormc                 C   rG   )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=1NrH   r.   r   r   r   Śtest_originFormComplex   s   ’zURITests.test_originFormComplexc                 C   rG   )zp
        L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
        specifies no path.
        r(   r@   NrH   r.   r   r   r   Śtest_originFormNoPath+  rK   zURITests.test_originFormNoPathc                 C   rG   )zv
        L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
        specifies an empty path.
        r?   r@   NrH   r.   r   r   r   Śtest_originFormEmptyPath3  rK   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   Ś	goodInputŚbadInputr&   r   r   r   Ś test_externalUnicodeInterference;  s   

z)URITests.test_externalUnicodeInterferenceN)r   r   r   )r   r   r   r   r   r'   r/   r2   r3   r<   r>   rA   rC   rE   rF   rJ   rL   rM   rN   rS   r   r   r   r   r   :   s&    
’0r   c                   @   ó   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   rU   J  ó    rU   c                   @   rT   )ŚURITestsForIPv4zG
    Tests for L{twisted.web.client.URI} with IPv4 host addresses.
    s   192.168.1.67NrV   r   r   r   r   rX   R  rW   rX   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    r9   r.   r   r   r   Ś"test_hostBracketIPv6AddressLiterale  s   z2URITestsForIPv6.test_hostBracketIPv6AddressLiteralN)r   r   r   r   r   r   rZ   r   r   r   r   rY   Z  s
    rY   N)r   Śurllib.parser   Śtwisted.trialr   Śtwisted.webr   ŚTestCaser   r   rU   rX   rY   r   r   r   r   Ś<module>   s   +  