o
    f8                     @   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   $ 