
    tf                         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e          Z G d de          Z G 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 ZdS )HostResourceTestsz%
    Tests for L{_HostResource}.
    c                    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|          }|                     |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                      dS )NF r       Z/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/web/test/test_vhost.py<lambda>z1HostResourceTests.test_getChild.<locals>.<lambda>-   s    5 r   r   N)r   putChildr   r   defaultaddHostr
   prepathr   siteisSecurehostgetChildassertIsInstancer   )selfbazrootbazuribaztesthrrootrequeststeps           r   test_getChildzHostResourceTests.test_getChild   s;    |R((k2&&|R(()))(((__  OR00Z)))011777Dzz(={{:w//dD)))011}}VW--dJ/////r   N__name__
__module____qualname____doc__r*   r   r   r   r   r      s-         0 0 0 0 0r   r   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )
NameVirtualHostTestsz'
    Tests for L{NameVirtualHost}.
    c                     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(   s      r   test_renderWithoutHostz+NameVirtualHostTests.test_renderWithoutHost=   s_     .//&*+<b&A&A#u%%,33G<<>OPPPPPr   c                      t                      }t          dg          t          |          } f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                 H                         j        t                     d S Nr3   responseCoder   ignoredr(   r"   s    r   
cbRenderedzHNameVirtualHostTests.test_renderWithoutHostNoDefault.<locals>.cbRenderedR   "    W19=====r   )r   r
   r	   addCallbackr"   r6   dr?   r(   s   `   @r   test_renderWithoutHostNoDefaultz4NameVirtualHostTests.test_renderWithoutHostNoDefaultH   sh     .//u%%'11	> 	> 	> 	> 	> 	> 	
j!!!r   c                     t                      }|                    dt          dd                     t          dg          }|j                            dd           t          ||          } fd}|                    ||           t          dg          }|j                            dd           t          ||          } f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                         d                    |j                  d           d S Nr   rF   r3   joinwrittenr=   s     r   r?   z<NameVirtualHostTests.test_renderWithHost.<locals>.cbRenderede   s-    SXXgo66	BBBBBr   s   example.org:8000c                 d                         d                    |j                  d           d S rI   rJ   )r>   requestWithPortr"   s     r   r?   z<NameVirtualHostTests.test_renderWithHost.<locals>.cbRenderedo   s.    SXXo&=>>	JJJJJr   )	r   r   r   r
   requestHeadersaddRawHeaderr	   rA   r   )r"   r6   r(   rC   r?   rN   	dWithPorts   `      r   test_renderWithHostz(NameVirtualHostTests.test_renderWithHostX   s     .//##NDB4G4GHHHu%%++G^DDD'11	C 	C 	C 	C 	C 	
j'*** 'u--&33G=PQQQ/AA		K 	K 	K 	K 	K 	j/:::a^,,,r   c                      t                      }t          dd          |_        t          dg          j                            dd           t          |          } f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   rG      example.comc                 d                         d                    j                  d           d S )Nr   rT   rJ   r=   s    r   r?   zCNameVirtualHostTests.test_renderWithUnknownHost.<locals>.cbRendered   s-    SXXgo66HHHHHr   )r   r   r   r
   rO   rP   r	   rA   rB   s   `   @r   test_renderWithUnknownHostz/NameVirtualHostTests.test_renderWithUnknownHostv   s     .//&*?B&?&?#u%%++G^DDD'11	I 	I 	I 	I 	I 	I 	
j!!!r   c                      t                      }t          dg          j                            dd           t	          |          } f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   rG   rU   c                 H                         j        t                     d S r:   r;   r=   s    r   r?   zLNameVirtualHostTests.test_renderWithUnknownHostNoDefault.<locals>.cbRendered   r@   r   )r   r
   rO   rP   r	   rA   rB   s   `   @r   #test_renderWithUnknownHostNoDefaultz8NameVirtualHostTests.test_renderWithUnknownHostNoDefault   s     .//u%%++G^DDD'11	> 	> 	> 	> 	> 	> 	
j!!!r   c                   K   t                      }t          dg          }|j                            dd           t	          ||           d{V  |                     dd                    |j                             dS )zs
        L{NameVirtualHost.render} doesn't echo unescaped HTML when present in
        the I{Host} header.
        r   rG   s   <b>example</b>.comNs   <b>)r   r
   rO   rP   r	   assertNotInrK   rL   r5   s      r   test_renderWithHTMLHostz,NameVirtualHostTests.test_renderWithHTMLHost   s      
 .//u%%++G5JKKK)7333333333'/!:!:;;;;;r   c                 (   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.orgrG   r   N)r   r   isLeafr   r
   rO   rP   r   r!   r    r   r3   postpath)r"   r6   leafResourcenormResourcer(   s        r   r*   z"NameVirtualHostTests.test_getChild   s~    .//L"--"L"--##$7FFF##$7FFFr""++G5HIII%1::3HH*UUU3%000)2...r""++G5HIII%1::3HH$OOO"---)C511111r   N)r,   r-   r.   r/   r7   rD   rR   rW   rZ   r]   r*   r   r   r   r1   r1   8   s         	Q 	Q 	Q   - - -<  $  "< < <2 2 2 2 2r   r1   c                       e Zd ZdZd ZdS )VHostMonsterResourceTestsz,
    Tests for L{VHostMonsterResource}.
    c                    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(   s      r   r*   z'VHostMonsterResourceTests.test_getChild   s    
 #$$r""cll7G<<mLLL))++,,,r""cll8W==}MMM((**+++++r   Nr+   r   r   r   rd   rd      s-         , , , , ,r   rd   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   r1   rd   r   r   r   <module>rr      sQ   
 1 0 0 0 0 0 + + + + + + & & & & & & + + + + + + # # # # # # # # # # # # * * * * * * 2 2 2 2 2 2 R R R R R R R R R R!0 !0 !0 !0 !0 !0 !0 !0HI2 I2 I2 I2 I28 I2 I2 I2X, , , , , , , , , ,r   