o
    pj8  ã                   @   s¬   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z!
Tests for L{twisted.web.vhost}.
é    )ÚgatherResults)ÚTestCase)Ú	NOT_FOUND)Ú
NoResource)ÚSite)ÚData)Ú_render)ÚDummyRequest)ÚNameVirtualHostÚVHostMonsterResourceÚ_HostResourcec                   @   ó   e Zd ZdZdd„ ZdS )ÚHostResourceTestsz%
    Tests for L{_HostResource}.
    c                 C   sÊ   t ddƒ}t ddƒ}t ddƒ}| d|¡ | d|¡ tƒ }tƒ }t ddƒ|_| d|¡ tddgƒ}g d	¢|_t|ƒ|_	d
d„ |_
d|_| d|¡}|  |t ¡ tddgƒ}| d|¡}|  |t¡ dS )zØ
        L{_HostResource.getChild} returns the proper I{Resource} for the vhost
        embedded in the URL.  Verify that returning the proper I{Resource}
        required changing the I{Host} in the header.
        s	   root dataÚ s   uri datas	   test datas   tests   uris   default dataó   baz.com)s   baró   httpr   c                   S   s   dS )NF© r   r   r   ú=/usr/lib/python3/dist-packages/twisted/web/test/test_vhost.pyÚ<lambda>-   s    z1HostResourceTests.test_getChild.<locals>.<lambda>ó    N)r   ÚputChildr   r
   ÚdefaultÚaddHostr	   Úprepathr   ÚsiteÚisSecureÚhostÚgetChildÚassertIsInstancer   )ÚselfÚbazrootÚbazuriÚbaztestÚhrÚrootÚrequestÚstepr   r   r   Útest_getChild   s&   





zHostResourceTests.test_getChildN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r   r   r   r   r      ó    r   c                   @   sH   e Zd Z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 )ÚNameVirtualHostTestsz'
    Tests for L{NameVirtualHost}.
    c                 C   s2   t ƒ }tddƒ|_tdgƒ}|  | |¡d¡ dS )z¹
        L{NameVirtualHost.render} returns the result of rendering the
        instance's C{default} if it is not L{None} and there is no I{Host}
        header in the request.
        s   correct resultr   N)r
   r   r   r	   ÚassertEqualÚrender)r   ÚvirtualHostResourcer%   r   r   r   Útest_renderWithoutHost=   s   
z+NameVirtualHostTests.test_renderWithoutHostc                    s6   t ƒ }tdgƒ‰ t|ˆ ƒ}‡ ‡fdd„}| |¡ |S )zÃ
        L{NameVirtualHost.render} returns a response with a status of I{NOT
        FOUND} if the instance's C{default} is L{None} and there is no I{Host}
        header in the request.
        r   c                    ó   ˆ  ˆ jt¡ d S ©N©r/   ÚresponseCoder   ©Úignored©r%   r   r   r   Ú
cbRenderedR   ó   zHNameVirtualHostTests.test_renderWithoutHostNoDefault.<locals>.cbRendered)r
   r	   r   ÚaddCallback©r   r1   Údr:   r   r9   r   Útest_renderWithoutHostNoDefaultH   s   


z4NameVirtualHostTests.test_renderWithoutHostNoDefaultc                    s˜   t ƒ }| dtddƒ¡ tdgƒ}|j dd¡ t||ƒ}‡ fdd„}| ||¡ tdgƒ}|j dd¡ t||ƒ}‡ fd	d„}| ||¡ t||gƒS )
zö
        L{NameVirtualHost.render} returns the result of rendering the resource
        which is the value in the instance's C{host} dictionary corresponding
        to the key indicated by the value of the I{Host} header in the request.
        s   example.orgó   winnerr   r   ó   hostc                    ó   ˆ   d |j¡d¡ d S ©Nr   r@   ©r/   ÚjoinÚwritten)r8   r%   ©r   r   r   r:   e   ó   z<NameVirtualHostTests.test_renderWithHost.<locals>.cbRendereds   example.org:8000c                    rB   rC   rD   )r8   ÚrequestWithPortrG   r   r   r:   o   rH   )	r
   r   r   r	   ÚrequestHeadersÚaddRawHeaderr   r<   r   )r   r1   r%   r>   r:   rI   Ú	dWithPortr   rG   r   Útest_renderWithHostX   s   



z(NameVirtualHostTests.test_renderWithHostc                    sP   t ƒ }tddƒ|_tdgƒ‰ ˆ j dd¡ t|ˆ ƒ}‡ ‡fdd„}| |¡ |S )zØ
        L{NameVirtualHost.render} returns the result of rendering the
        instance's C{default} if it is not L{None} and there is no host
        matching the value of the I{Host} header in the request.
        ó   correct datar   r   rA   ó   example.comc                    s   ˆ  d ˆ j¡d¡ d S )Nr   rN   rD   r7   r9   r   r   r:   ‚   rH   zCNameVirtualHostTests.test_renderWithUnknownHost.<locals>.cbRendered)r
   r   r   r	   rJ   rK   r   r<   r=   r   r9   r   Útest_renderWithUnknownHostv   s   


z/NameVirtualHostTests.test_renderWithUnknownHostc                    óD   t ƒ }tdgƒ‰ ˆ j dd¡ t|ˆ ƒ}‡ ‡fdd„}| |¡ |S )zâ
        L{NameVirtualHost.render} returns a response with a status of I{NOT
        FOUND} if the instance's C{default} is L{None} and there is no host
        matching the value of the I{Host} header in the request.
        r   rA   rO   c                    r3   r4   r5   r7   r9   r   r   r:   “   r;   zLNameVirtualHostTests.test_renderWithUnknownHostNoDefault.<locals>.cbRendered©r
   r	   rJ   rK   r   r<   r=   r   r9   r   Ú#test_renderWithUnknownHostNoDefaultˆ   s   


z8NameVirtualHostTests.test_renderWithUnknownHostNoDefaultc                    rQ   )zs
        L{NameVirtualHost.render} doesn't echo unescaped HTML when present in
        the I{Host} header.
        r   rA   s   <b>example</b>.comc                    s   ˆ  dˆ j¡ d S )Nz<b>)ÚassertNotInrF   r7   r9   r   r   r:   ¢   r;   z@NameVirtualHostTests.test_renderWithHTMLHost.<locals>.cbRenderedrR   r=   r   r9   r   Útest_renderWithHTMLHost™   s   


z,NameVirtualHostTests.test_renderWithHTMLHostc                 C   sÜ   t ƒ }tddƒ}d|_tddƒ}| d|¡ | d|¡ tg ƒ}|j dd¡ dg|_|  | 	d|¡t
¡ |  |jdg¡ |  |jg ¡ tg ƒ}|j dd¡ dg|_|  | 	d|¡t¡ |  |jg ¡ |  |jdg¡ d	S )
zµ
        L{NameVirtualHost.getChild} returns correct I{Resource} based off
        the header and modifies I{Request} to ensure proper prepath and
        postpath are set.
        s	   leaf datar   Ts	   norm datas   leaf.example.orgs   norm.example.orgrA   r   N)r
   r   ÚisLeafr   r	   rJ   rK   r   r   r   r   r/   Úpostpath)r   r1   ÚleafResourceÚnormResourcer%   r   r   r   r'   ¨   s$   

z"NameVirtualHostTests.test_getChildN)r)   r*   r+   r,   r2   r?   rM   rP   rS   rU   r'   r   r   r   r   r.   8   s    r.   c                   @   r   )ÚVHostMonsterResourceTestsz,
    Tests for L{VHostMonsterResource}.
    c                 C   s^   t ƒ }tg ƒ}|  | d|¡t¡ |  | ¡ ¡ tg ƒ}|  | d|¡t¡ |  | ¡ ¡ dS )z�
        L{VHostMonsterResource.getChild} returns I{_HostResource} and modifies
        I{Request} with correct L{Request.isSecure}.
        r   s   httpsN)r   r	   r   r   r   ÚassertFalser   Ú
assertTrue)r   Úvhmr%   r   r   r   r'   Ë   s   z'VHostMonsterResourceTests.test_getChildNr(   r   r   r   r   rZ   Æ   r-   rZ   N)r,   Útwisted.internet.deferr   Útwisted.trial.unittestr   Útwisted.web.httpr   Útwisted.web.resourcer   Útwisted.web.serverr   Útwisted.web.staticr   Útwisted.web.test._utilr   Útwisted.web.test.test_webr	   Útwisted.web.vhostr
   r   r   r   r.   rZ   r   r   r   r   Ú<module>   s   $ 