ó
ç'ÊUc           @   só   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 d  d l m Z m Z m Z d  d l m Z d  d l m Z d  d	 l m Z e e d
 ƒ Z i d d 6d d 6d d 6d d 6Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   BytesIO(   t   TestCase(   t   joint   abspatht   dirname(   t   GzipFile(   t   Spider(   t   Responset   Requestt   HtmlResponse(   t   HttpCompressionMiddleware(   t   tests_datadir(   t   resolve_encodingt
   compresseds   html-gzip.bint   gzips   x-gzips   html-rawdeflate.bint   deflatet
   rawdeflates   html-zlibdeflate.bint   zlibdeflatet   HttpCompressionTestc           B   sk   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   c         C   s   t  d ƒ |  _ t ƒ  |  _ d  S(   Nt   foo(   R   t   spiderR
   t   mw(   t   self(    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt   setUp   s    c         C   s¼   | t  k r t ƒ  ‚ n  t  | \ } } t t t | ƒ d ƒ  } | j ƒ  } Wd  QXi d d 6d d 6t | ƒ d 6d d 6| d	 6} t d
 d | d | ƒ} t d d i d d 6ƒ| _	 | S(   Nt   rbs    Yaws/1.49 Yet Another Web Servert   Servers   Sun, 08 Mar 2009 00:41:03 GMTt   Dates   Content-Lengths	   text/htmls   Content-Types   Content-Encodings   http://scrapytest.org/t   bodyt   headerss   http://scrapytest.orgs   gzip,deflates   Accept-Encoding(
   t   FORMATt
   ValueErrort   openR   t	   SAMPLEDIRt   readt   lenR   R   t   request(   R   t   codingt
   samplefilet   contentencodingt   sampleR   R   t   response(    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt   _getresponse   s    
c         C   sW   t  d ƒ } d | j k s! t ‚ |  j j | |  j ƒ |  j | j j d ƒ d ƒ d  S(   Ns   http://scrapytest.orgs   Accept-Encodings   gzip,deflate(   R   R   t   AssertionErrorR   t   process_requestR   t   assertEqualt   get(   R   R#   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt   test_process_request1   s    c         C   s   |  j  d ƒ } | j } |  j | j d d ƒ |  j j | | |  j ƒ } | | k	 s\ t ‚ | j j	 d ƒ st t ‚ d | j k s‰ t ‚ d  S(   NR   s   Content-Encodings	   <!DOCTYPE(
   R)   R#   R,   R   R   t   process_responseR   R*   R   t
   startswith(   R   R(   R#   t   newresponse(    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt   test_process_response_gzip7   s    	c         C   s   |  j  d ƒ } | j } |  j | j d d ƒ |  j j | | |  j ƒ } | | k	 s\ t ‚ | j j	 d ƒ st t ‚ d | j k s‰ t ‚ d  S(   NR   s   Content-EncodingR   s	   <!DOCTYPE(
   R)   R#   R,   R   R   R/   R   R*   R   R0   (   R   R(   R#   R1   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt    test_process_response_rawdeflateA   s    	c         C   s   |  j  d ƒ } | j } |  j | j d d ƒ |  j j | | |  j ƒ } | | k	 s\ t ‚ | j j	 d ƒ st t ‚ d | j k s‰ t ‚ d  S(   NR   s   Content-EncodingR   s	   <!DOCTYPE(
   R)   R#   R,   R   R   R/   R   R*   R   R0   (   R   R(   R#   R1   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt    test_process_response_zlibdelateK   s    	c         C   s€   t  d d d ƒ} t d ƒ } | j j d ƒ s7 t ‚ |  j j | | |  j ƒ } | | k sd t ‚ | j j	 d ƒ s| t ‚ d  S(   Ns   http://scrapytest.orgR   s   <!DOCTYPE...s   Content-Encodings	   <!DOCTYPE(
   R   R   R   R-   R*   R   R/   R   R   R0   (   R   R(   R#   R1   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt   test_process_response_plainU   s    c         C   s{   |  j  d ƒ } d d g | j d <| j } |  j j | | |  j ƒ } | | k	 sX t ‚ |  j | j j d ƒ d g ƒ d  S(   NR   t   uuencodes   Content-Encoding(	   R)   R   R#   R   R/   R   R*   R,   t   getlist(   R   R(   R#   R1   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt   test_multipleencodings^   s    	c         C   sÙ   i d d 6d d 6} t  ƒ  } d } t d | d d ƒ } | j | ƒ | j ƒ  t d	 d
 | d | j ƒ  ƒ} t d ƒ } |  j j | | |  j	 ƒ } t
 | t ƒ s© t ‚ |  j | j | ƒ |  j | j t d ƒ ƒ d  S(   Ns	   text/htmls   Content-TypeR   s   Content-Encodingsh   <html><head><title>Some page</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312">t   fileobjt   modet   wbs   http;//www.example.com/R   R   s   http://www.example.com/t   gb2312(   R    R   t   writet   closeR   t   getvalueR   R   R/   R   t
   isinstanceR	   R*   R,   R   t   encodingR   (   R   R   t   ft	   plainbodyt   zfR(   R#   R1   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt*   test_process_response_encoding_inside_bodyf   s    
	
c         C   sÙ   i d d 6d d 6} t  ƒ  } d } t d | d d ƒ } | j | ƒ | j ƒ  t d	 d
 | d | j ƒ  ƒ} t d ƒ } |  j j | | |  j	 ƒ } t
 | t ƒ s© t ‚ |  j | j | ƒ |  j | j t d ƒ ƒ d  S(   Ns	   text/htmls   Content-TypeR   s   Content-Encodingsh   <html><head><title>Some page</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312">R9   R:   R;   s    http;//www.example.com/page.htmlR   R   s   http://www.example.com/R<   (   R    R   R=   R>   R	   R?   R   R   R/   R   R@   R*   R,   R   RA   R   (   R   R   RB   RC   RD   R(   R#   R1   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt0   test_process_response_force_recalculate_encodingx   s    
	
c         C   s‚   |  j  d ƒ } d | j d <| j } |  j j | | |  j ƒ } |  j | | ƒ |  j | j d d ƒ |  j | j d d ƒ d  S(   NR   s   application/gzips   Content-Types   Content-Encoding(   R)   R   R#   R   R/   R   t   assertIsR,   (   R   R(   R#   R1   (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt)   test_process_response_gzipped_contenttypeŠ   s    	(   t   __name__t
   __module__R   R)   R.   R2   R3   R4   R5   R8   RE   RF   RH   (    (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyR      s   				
	
	
					N(   s   html-gzip.bins   gzip(   s   html-gzip.bins   gzip(   s   html-rawdeflate.bins   deflate(   s   html-zlibdeflate.bins   deflate(   t   ioR    t   unittestR   t   os.pathR   R   R   R   R   t   scrapy.spidersR   t   scrapy.httpR   R   R	   t,   scrapy.downloadermiddlewares.httpcompressionR
   t   testsR   t   w3lib.encodingR   R    R   R   (    (    (    sS   /home/travis/build/scrapy/scrapy/tests/test_downloadermiddleware_httpcompression.pyt   <module>   s   
