U
    2ÜŸjø  ã                   @   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                   @   s   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ƒ}d	d
dg|_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 datas   baz.coms   baró   httpc                   S   s   dS )NF© r   r   r   ú?/tmp/pip-unpacked-wheel-4b529toj/twisted/web/test/test_vhost.pyÚ<lambda>-   ó    z1HostResourceTests.test_getChild.<locals>.<lambda>r   N)r   ZputChildr   r
   ÚdefaultÚaddHostr	   Úprepathr   ÚsiteÚisSecureÚhostÚgetChildÚassertIsInstancer   )ÚselfZbazrootZbazuriZbaztestÚhrÚrootÚrequestÚstepr   r   r   Útest_getChild   s&    




zHostResourceTests.test_getChildN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r   r   r   r   r      s   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   r   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                    s   ˆ  ˆ jt¡ d S ©N©r(   ZresponseCoder   ©Úignored©r   r   r   r   Ú
cbRenderedR   s    zHNameVirtualHostTests.test_renderWithoutHostNoDefault.<locals>.cbRendered)r
   r	   r   ÚaddCallback©r   r+   Údr2   r   r1   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                    s   ˆ   d |j¡d¡ d S ©Nr   r7   ©r(   ÚjoinÚwritten)r0   r   ©r   r   r   r2   e   s    z<NameVirtualHostTests.test_renderWithHost.<locals>.cbRendereds   example.org:8000c                    s   ˆ   d |j¡d¡ d S r9   r:   )r0   ÚrequestWithPortr=   r   r   r2   o   s    )	r
   r   r   r	   ÚrequestHeadersÚaddRawHeaderr   r3   r   )r   r+   r   r5   r2   r>   Z	dWithPortr   r=   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   r8   ó   example.comc                    s   ˆ  d ˆ j¡d¡ d S )Nr   rB   r:   r/   r1   r   r   r2   ‚   s    zCNameVirtualHostTests.test_renderWithUnknownHost.<locals>.cbRendered)r
   r   r   r	   r?   r@   r   r3   r4   r   r1   r   Útest_renderWithUnknownHostv   s    


z/NameVirtualHostTests.test_renderWithUnknownHostc                    sD   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   r8   rC   c                    s   ˆ  ˆ jt¡ d S r-   r.   r/   r1   r   r   r2   “   s    zLNameVirtualHostTests.test_renderWithUnknownHostNoDefault.<locals>.cbRendered)r
   r	   r?   r@   r   r3   r4   r   r1   r   Ú#test_renderWithUnknownHostNoDefaultˆ   s    


z8NameVirtualHostTests.test_renderWithUnknownHostNoDefaultc                 Ã   sF   t ƒ }tdgƒ}|j dd¡ t||ƒI dH  |  dd |j¡¡ dS )zs
        L{NameVirtualHost.render} doesn't echo unescaped HTML when present in
        the I{Host} header.
        r   r8   s   <b>example</b>.comNs   <b>)r
   r	   r?   r@   r   ZassertNotInr;   r<   r*   r   r   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.orgr8   r   N)r
   r   ZisLeafr   r	   r?   r@   r   r   r   r   r(   Zpostpath)r   r+   ZleafResourceZnormResourcer   r   r   r   r!   ¦   s$    

z"NameVirtualHostTests.test_getChildN)r#   r$   r%   r&   r,   r6   rA   rD   rE   rF   r!   r   r   r   r   r'   8   s   r'   c                   @   s   e Zd ZdZdd„ ZdS )Ú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   ZassertFalser   Ú
assertTrue)r   Zvhmr   r   r   r   r!   É   s    z'VHostMonsterResourceTests.test_getChildNr"   r   r   r   r   rG   Ä   s   rG   N)r&   Ztwisted.internet.deferr   Ztwisted.trial.unittestr   Ztwisted.web.httpr   Ztwisted.web.resourcer   Ztwisted.web.serverr   Ztwisted.web.staticr   Ztwisted.web.test._utilr   Ztwisted.web.test.test_webr	   Ztwisted.web.vhostr
   r   r   r   r'   rG   r   r   r   r   Ú<module>   s   $ 