o
    Óó·iø  ã                   @   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   úaC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/web/test/test_vhost.pyÚ<lambda>-   s    z1HostResourceTests.test_getChild.<locals>.<lambda>ó    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      ó    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                    ó   ˆ  ˆ jt¡ d S ©N©r+   ZresponseCoder   ©Úignored©r!   r   r   r   Ú
cbRenderedR   ó   zHNameVirtualHostTests.test_renderWithoutHostNoDefault.<locals>.cbRenderedN)r
   r	   r   ÚaddCallback©r   r.   Údr6   r   r5   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)r4   r!   ©r   r   r   r6   e   ó   z<NameVirtualHostTests.test_renderWithHost.<locals>.cbRendereds   example.org:8000c                    r>   r?   r@   )r4   ÚrequestWithPortrC   r   r   r6   o   rD   N)	r
   r   r   r	   ÚrequestHeadersÚaddRawHeaderr   r8   r   )r   r.   r!   r:   r6   rE   Z	dWithPortr   rC   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   r=   ó   example.comc                    s   ˆ  d ˆ j¡d¡ d S )Nr   rI   r@   r3   r5   r   r   r6   ‚   rD   zCNameVirtualHostTests.test_renderWithUnknownHost.<locals>.cbRenderedN)r
   r   r   r	   rF   rG   r   r8   r9   r   r5   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   r=   rJ   c                    r0   r1   r2   r3   r5   r   r   r6   “   r7   zLNameVirtualHostTests.test_renderWithUnknownHostNoDefault.<locals>.cbRenderedN)r
   r	   rF   rG   r   r8   r9   r   r5   r   Ú#test_renderWithUnknownHostNoDefaultˆ   s   


z8NameVirtualHostTests.test_renderWithUnknownHostNoDefaultc                 Ã   sH   �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   r=   s   <b>example</b>.comNs   <b>)r
   r	   rF   rG   r   ZassertNotInrA   rB   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.orgr=   r   N)r
   r   ZisLeafr   r	   rF   rG   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/   r;   rH   rK   rL   rM   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   ZassertFalser   Ú
assertTrue)r   Zvhmr!   r   r   r   r#   É   s   z'VHostMonsterResourceTests.test_getChildNr$   r   r   r   r   rN   Ä   r)   rN   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*   rN   r   r   r   r   Ú<module>   s   $ 