ó
ç'ÊUc           @   s½   d  d l  Z  d  d l m Z 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 e  j f d „  ƒ  YZ d	 e  j f d
 „  ƒ  YZ e d k r¹ e  j ƒ  n  d S(   iÿÿÿÿN(   t   RedirectMiddlewaret   MetaRefreshMiddleware(   t   Spider(   t   IgnoreRequest(   t   Requestt   Responset   HtmlResponse(   t   get_crawlert   RedirectMiddlewareTestc           B   sY   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   c         C   s4   t  t ƒ } | j d ƒ |  _ t j | ƒ |  _ d  S(   Nt   foo(   R   R   t   _create_spidert   spiderR    t   from_crawlert   mw(   t   selft   crawler(    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   setUp   s    c         C   sb   t  d ƒ } t d d i d d 6d d ƒ} |  j j | | |  j ƒ } | j | j k s^ t ‚ d  S(   Ns   http://a.comt   headerss   http://a.com/redirectedt   Locationt   statusi-  (   R   R   R   t   process_responseR   t   priorityt   AssertionError(   R   t   reqt   rspt   req2(    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_priority_adjust   s    c            s1   ‡  f d †  } | d ƒ | d ƒ | d ƒ d  S(   Nc            sÈ   d } d } t  | d |  ƒ} t | d i | d 6d d ƒ} ˆ  j j | | ˆ  j ƒ } t | t  ƒ sm t ‚ ˆ  j | j | ƒ ˆ  j | j	 |  ƒ | j
 d =ˆ  j j | | ˆ  j ƒ | k sÄ t ‚ d  S(   Ns   http://www.example.com/301s!   http://www.example.com/redirectedt   methodR   R   R   i-  (   R   R   R   R   R   t
   isinstanceR   t   assertEqualt   urlR   R   (   R   R   t   url2R   R   R   (   R   (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   _test   s    
t   GETt   POSTt   HEAD(    (   R   R    (    (   R   sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_redirect_301   s    

c         C   s÷   d } d } t  | d i t d 6ƒ} t | d i | d 6d d ƒ} |  j j | | |  j ƒ } t | t ƒ st t ‚ | | k s† t ‚ t  | d i t d 6ƒ} t | d d	 ƒ} |  j j | | |  j ƒ } t | t ƒ sá t ‚ | | k só t ‚ d  S(
   Ns   http://www.example.com/301s!   http://www.example.com/redirectedt   metat   dont_redirectR   R   R   i-  iÈ   (	   R   t   TrueR   R   R   R   R   R   t   False(   R   R   R   R   R   t   r(    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_dont_redirect+   s    c      
   C   s5  d } d } t  | d d d d d i d d	 6d
 d 6ƒ} t | d i | d 6d d ƒ} |  j j | | |  j ƒ } t | t  ƒ s‡ t ‚ |  j | j | ƒ |  j | j	 d ƒ d	 | j
 k sÈ t d ƒ ‚ d | j
 k sã t d ƒ ‚ | j s t d | j ƒ ‚ | j
 d =|  j j | | |  j ƒ | k s1t ‚ d  S(   Ns   http://www.example.com/302s"   http://www.example.com/redirected2R   R"   t   bodyt   testR   s
   text/plains   Content-Typet   4s   Content-lengthR   R   i.  R!   s=   Content-Type header must not be present in redirected requests   Content-Lengths?   Content-Length header must not be present in redirected requests'   Redirected body must be empty, not '%s'(   R   R   R   R   R   R   R   R   R   R   R   R+   (   R   R   R   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_redirect_302>   s"    		
c         C   sÈ   d } d } t  | d d ƒ} t | d i | d 6d d ƒ} |  j j | | |  j ƒ } t | t  ƒ sm t ‚ |  j | j | ƒ |  j | j	 d ƒ | j
 d =|  j j | | |  j ƒ | k sÄ t ‚ d  S(	   Ns   http://www.example.com/302s"   http://www.example.com/redirected2R   R#   R   R   R   i.  (   R   R   R   R   R   R   R   R   R   R   R   (   R   R   R   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_redirect_302_headT   s    
c         C   s¹   d |  j  _ t d ƒ } t d d i d d 6d d ƒ} |  j  j | | |  j ƒ } t | t ƒ sg t ‚ d | j k s| t ‚ |  j	 | j d d ƒ |  j
 t |  j  j | | |  j ƒ d  S(	   Ni   s   http://scrapytest.org/302R   s   /redirectedR   R   i.  t   redirect_times(   R   t   max_redirect_timesR   R   R   R   R   R   R%   R   t   assertRaisesR   (   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_max_redirect_timesd   s    c         C   sš   d |  j  _ t d d i d d 6ƒ} t d d i d d	 6d
 d ƒ} |  j  j | | |  j ƒ } t | t ƒ st t ‚ |  j t	 |  j  j | | |  j ƒ d  S(   Nid   s   http://scrapytest.org/302R%   i   t   redirect_ttls   http://www.scrapytest.org/302R   s   /redirectedR   R   i.  (
   R   R1   R   R   R   R   R   R   R2   R   (   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_ttlo   s    c         C   sá   t  d ƒ } t d d i d d 6d d ƒ} |  j j | | |  j ƒ } t d d i d d 6d d ƒ} |  j j | | |  j ƒ } |  j | j d ƒ |  j | j d	 d g ƒ |  j | j d
 ƒ |  j | j d	 d d g ƒ d  S(   Ns   http://scrapytest.org/firstR   s   /redirectedR   R   i.  s    http://scrapytest.org/redirecteds   /redirected2t   redirect_urlss!   http://scrapytest.org/redirected2(   R   R   R   R   R   R   R   R%   (   R   t   req1t   rsp1R   t   rsp2t   req3(    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_redirect_urlsx   s    (   t   __name__t
   __module__R   R   R$   R*   R.   R/   R3   R5   R;   (    (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyR   
   s   									t   MetaRefreshMiddlewareTestc           B   s_   e  Z d  „  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z	 d
 „  Z
 RS(   c         C   s4   t  t ƒ } | j d ƒ |  _ t j | ƒ |  _ d  S(   NR	   (   R   R   R
   R   R   R   R   (   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyR   †   s    i   s   http://example.org/newpagec         C   s   d j  | | ƒ S(   NsL   <html><head><meta http-equiv="refresh" content="{0};url={1}"/></head></html>(   t   format(   R   t   intervalR   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   _body‹   s    c         C   s^   t  d ƒ } t | j d |  j ƒ  ƒ} |  j j | | |  j ƒ } | j | j k sZ t ‚ d  S(   Ns   http://a.comR+   (	   R   R   R   RA   R   R   R   R   R   (   R   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyR      s    c         C   sq   t  d d ƒ } t | j d |  j ƒ  ƒ} |  j j | | |  j ƒ } t | t  ƒ sZ t ‚ |  j	 | j d ƒ d  S(   NR   s   http://example.orgR+   s   http://example.org/newpage(
   R   R   R   RA   R   R   R   R   R   R   (   R   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   test_meta_refresh•   s
    c         C   sa   t  d d ƒ } t d d d |  j d d ƒ ƒ } |  j j | | |  j ƒ } | | k s] t ‚ d  S(   NR   s   http://example.orgR+   R@   iè  (   R   R   RA   R   R   R   R   (   R   R   R   R9   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt$   test_meta_refresh_with_high_intervalœ   s    !c         C   s÷   t  d d d d d d d i d d	 6d
 d 6ƒ } t | j d |  j ƒ  ƒ} |  j j | | |  j ƒ } t | t  ƒ sz t ‚ |  j	 | j d ƒ |  j	 | j
 d ƒ d	 | j k s» t d ƒ ‚ d | j k sÖ t d ƒ ‚ | j só t d | j ƒ ‚ d  S(   NR   s   http://example.orgR   R"   R+   R,   R   s
   text/plains   Content-TypeR-   s   Content-lengths   http://example.org/newpageR!   s=   Content-Type header must not be present in redirected requests   Content-Lengths?   Content-Length header must not be present in redirected requests'   Redirected body must be empty, not '%s'(   R   R   R   RA   R   R   R   R   R   R   R   R   R+   (   R   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt'   test_meta_refresh_trough_posted_request£   s    		c         C   sµ   d |  j  _ t d ƒ } t | j d |  j ƒ  ƒ} |  j  j | | |  j ƒ } t | t ƒ sc t	 ‚ d | j
 k sx t	 ‚ |  j | j
 d d ƒ |  j t |  j  j | | |  j ƒ d  S(   Ni   s   http://scrapytest.org/maxR+   R0   (   R   R1   R   R   R   RA   R   R   R   R   R%   R   R2   R   (   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyR3   ³   s    c         C   s–   d |  j  _ t d d i d d 6ƒ} t | j d |  j ƒ  ƒ} |  j  j | | |  j ƒ } t | t ƒ sp t	 ‚ |  j
 t |  j  j | | |  j ƒ d  S(   Nid   s   http://scrapytest.org/302R%   i   R4   R+   (   R   R1   R   R   R   RA   R   R   R   R   R2   R   (   R   R   R   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyR5   ¾   s    c         C   s  t  d ƒ } t | j d |  j d d ƒ ƒ} |  j j | | |  j ƒ } t | t  ƒ sc t | ƒ ‚ t | j d |  j d d ƒ ƒ} |  j j | | |  j ƒ } t | t  ƒ sº t | ƒ ‚ |  j	 | j d ƒ |  j	 | j
 d d g ƒ |  j	 | j d ƒ |  j	 | j
 d d d g ƒ d  S(	   Ns   http://scrapytest.org/firstR+   R   s   /redirecteds   /redirected2s    http://scrapytest.org/redirectedR6   s!   http://scrapytest.org/redirected2(   R   R   R   RA   R   R   R   R   R   R   R%   (   R   R7   R8   R   R9   R:   (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyR;   Ç   s    !!(   R<   R=   R   RA   R   RB   RC   RD   R3   R5   R;   (    (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyR>   „   s   								t   __main__(   t   unittestt%   scrapy.downloadermiddlewares.redirectR    R   t   scrapy.spidersR   t   scrapy.exceptionsR   t   scrapy.httpR   R   R   t   scrapy.utils.testR   t   TestCaseR   R>   R<   t   main(    (    (    sL   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_redirect.pyt   <module>   s   zP