ó
ç'ÊUc           @   s–   d  d l  m Z d  d l m Z d  d l m Z m Z d  d l m Z d  d l	 m
 Z
 d  d l m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d S(   iÿÿÿÿ(   t   TestCase(   t   Failure(   t   Requestt   Response(   t   Spider(   t   DownloaderMiddlewareManager(   t   get_crawlert   ManagerTestCasec           B   s,   e  Z d Z d  „  Z d „  Z d d „ Z RS(   c         C   sh   t  t |  j ƒ |  _ |  j j d ƒ |  _ t j |  j ƒ |  _ |  j j	 j
 |  j ƒ |  j j
 |  j ƒ S(   Nt   foo(   R   R   t   settings_dictt   crawlert   _create_spidert   spiderR   t   from_crawlert   mwmant   statst   open_spider(   t   self(    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt   setUp   s
    c         C   s,   |  j  j j |  j d ƒ |  j j |  j ƒ S(   Nt    (   R
   R   t   close_spiderR   R   (   R   (    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt   tearDown   s    c            s   ˆ  s t  | j ƒ ‰  n  ‡  f d †  } |  j j | | |  j ƒ } g  } | j | j ƒ |  j | ƒ | d } t | t	 ƒ r‹ | j
 ƒ  n  | S(   s   Executes downloader mw manager's download method and returns
        the result (Request or Response) or raise exception in case of
        failure.
        c             s   ˆ  S(   N(    (   t   kwargs(   t   response(    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt   download_func"   s    i    (   R   t   urlR   t   downloadR   t   addBotht   appendt   _waitt
   isinstanceR   t   raiseException(   R   t   requestR   R   t   dfdt   resultst   ret(    (   R   sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt	   _download   s    
N(   t   __name__t
   __module__t   NoneR	   R   R   R$   (    (    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyR   
   s   		t   DefaultsTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s,   Tests default behavior with default settingsc         C   sP   t  d ƒ } t | j d d ƒ} |  j | | ƒ } |  j t | t ƒ d ƒ d  S(   Ns   http://example.com/index.htmlt   statusiÈ   s   Non-response returned(   R   R   R   R$   t
   assertTrueR   (   R   t   reqt   respR#   (    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt   test_request_response3   s    c         C   s¶   t  d ƒ } d } t | j d d d | d i t t | ƒ ƒ d 6d d	 6d
 d 6d d 6ƒ} |  j d | d | ƒ } |  j t | t  ƒ d j | ƒ ƒ |  j	 | j | j
 d d ƒ d S(   s¯  Regression test for a failure when redirecting a compressed
        request.

        This happens when httpcompression middleware is executed before redirect
        middleware and attempts to decompress a non-compressed body.
        In particular when some website returns a 30x response with header
        'Content-Encoding: gzip' giving as result the error below:

            exceptions.IOError: Not a gzipped file

        s   http://example.coms   <p>You are being redirected</p>R)   i.  t   bodyt   headerss   Content-Lengths	   text/htmls   Content-Typet   gzips   Content-Encodings   http://example.com/logint   LocationR    R   s   Not redirected: {0!r}s!   Not redirected to location headerN(   R   R   R   t   strt   lenR$   R*   R   t   formatt   assertEqualR/   (   R   R+   R.   R,   R#   (    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt/   test_3xx_and_invalid_gzipped_body_must_redirect9   s    c         C   s~   t  d ƒ } d } t | j d d d | d i t t | ƒ ƒ d 6d d	 6d
 d 6d d 6ƒ} |  j t |  j d | d | ƒd  S(   Ns   http://example.coms   <p>You are being redirected</p>R)   iÈ   R.   R/   s   Content-Lengths	   text/htmls   Content-TypeR0   s   Content-Encodings   http://example.com/loginR1   R    R   (   R   R   R   R2   R3   t   assertRaisest   IOErrorR$   (   R   R+   R.   R,   (    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt+   test_200_and_invalid_gzipped_body_must_failS   s    (   R%   R&   t   __doc__R-   R6   R9   (    (    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyR(   0   s   		N(   t   twisted.trial.unittestR    t   twisted.python.failureR   t   scrapy.httpR   R   t   scrapy.spidersR   t!   scrapy.core.downloader.middlewareR   t   scrapy.utils.testR   R   R(   (    (    (    sC   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware.pyt   <module>   s   &