U
    s@g                     @   s   d Z ddlmZ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 dd
lmZ ddl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}.
    )absolute_importdivision)gatherResults)TestCase)	NOT_FOUND)
NoResource)Data)Site)_HostResourceNameVirtualHostVHostMonsterResource)DummyRequest)_renderc                   @   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   =/usr/lib/python3/dist-packages/twisted/web/test/test_vhost.py<lambda>/       z1HostResourceTests.test_getChild.<locals>.<lambda>r   N)r   ZputChildr
   r   defaultaddHostr   prepathr	   ZsiteisSecureZhostgetChildassertIsInstancer   )selfZbazrootZbazuriZbaztestZ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   N)r   r   r   r   assertEqualZ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   
cbRenderedU   s    zHNameVirtualHostTests.test_renderWithoutHostNoDefault.<locals>.cbRendered)r   r   r   addCallbackr   r(   dr/   r   r.   r   test_renderWithoutHostNoDefaultL   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   r4   r'   joinwritten)r-   r   r   r   r   r/   g   s    z<NameVirtualHostTests.test_renderWithHost.<locals>.cbRendereds   example.org:8000c                    s     d|jd d S r6   r7   )r-   requestWithPortr:   r   r   r/   o   s    )	r   r   r   r   requestHeadersaddRawHeaderr   r0   r   )r   r(   r   r2   r/   r;   Z	dWithPortr   r:   r   test_renderWithHost[   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   r5      example.comc                    s    d jd d S )Nr   r?   r7   r,   r.   r   r   r/      s    zCNameVirtualHostTests.test_renderWithUnknownHost.<locals>.cbRendered)r   r   r   r   r<   r=   r   r0   r1   r   r.   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   r5   r@   c                    s     jt d S r*   r+   r,   r.   r   r   r/      s    zLNameVirtualHostTests.test_renderWithUnknownHostNoDefault.<locals>.cbRenderedr   r   r<   r=   r   r0   r1   r   r.   r   #test_renderWithUnknownHostNoDefault   s    


z8NameVirtualHostTests.test_renderWithUnknownHostNoDefaultc                    sD   t  }tdg  jdd t| } fdd}|| |S )zs
        L{NameVirtualHost.render} doesn't echo unescaped HTML when present in
        the I{Host} header.
        r   r5   s   <b>example</b>.comc                    s    d j d S )Nz<b>)ZassertNotInr9   r,   r.   r   r   r/      s    z@NameVirtualHostTests.test_renderWithHTMLHost.<locals>.cbRenderedrB   r1   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.orgr5   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)   r3   r>   rA   rC   rD   r    r   r   r   r   r&   ;   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   Z
assertTrue)r   Zvhmr   r   r   r   r       s    z'VHostMonsterResourceTests.test_getChildNr!   r   r   r   r   rE      s   rE   N)r%   Z
__future__r   r   Ztwisted.internet.deferr   Ztwisted.trial.unittestr   Ztwisted.web.httpr   Ztwisted.web.resourcer   Ztwisted.web.staticr   Ztwisted.web.serverr	   Ztwisted.web.vhostr
   r   r   Ztwisted.web.test.test_webr   Ztwisted.web.test._utilr   r   r&   rE   r   r   r   r   <module>   s   $ 