ó
ç'ÊUc           @   sI  d  Z  d d l Z d d l m Z d d l m Z d d l m Z m Z m	 Z	 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 d
 l m Z d d l m Z m Z d d „ Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d d l m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% d e j f d „  ƒ  YZ& d S(   sV   
from twisted.internet import defer
Tests borrowed from the twisted.web.client tests.
iÿÿÿÿN(   t   urlparse(   t   unittest(   t   servert   statict   errort   util(   t   reactort   defer(   t   StringTransport(   t   FilePath(   t   WrappingFactory(   t	   webclient(   t   Requestt   Headersc         O   s5   d „  } d d l  m } | |  | d | | | Žj S(   s-   Adapted version of twisted.web.client.getPagec          _   sG   | j  d d ƒ } t j t |  | Ž  d | ƒ} | j j d „  ƒ | S(   Nt   timeouti    c         S   s   |  j  S(   N(   t   body(   t   r(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   <lambda>   s    (   t   popt   clientt   ScrapyHTTPClientFactoryR   t   deferredt   addCallback(   t   argst   kwargsR   t   f(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   _clientfactory   s    iÿÿÿÿ(   t   _makeGetterFactoryt   contextFactory(   t   twisted.web.clientR   R   (   t   urlR   R   R   R   R   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   getPage   s    	t   ParseUrlTestCasec           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s.   Test URL parsing facility and defaults values.c         C   s7   t  j t | ƒ ƒ } | j | j | j | j | j f S(   N(   R   R   R   t   schemet   netloct   hostt   portt   path(   t   selfR   R   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   _parse#   s    c         C   sã  d } d d | | d d f f d d | | d d f f d d | | d d f f d	 d | d
 | d d f f d d | d
 | d d f f d d | d
 | d d f f d d | | d d f f d d | | d d f f d d | | d d f f d d | | d d f f d d | | d d f f d d | d | d d f f d d) f d d* f d d | | d  d f f d! d | | d  d f f d" d | d | d d f f d# d+ f d% d, f f } x0 | D]( \ } } |  j  t j | ƒ | | ƒ q³Wd  S(-   Ns	   127.0.0.1s#   http://127.0.0.1?c=v&c2=v2#fragmentt   httpiP   s   /?c=v&c2=v2s$   http://127.0.0.1/?c=v&c2=v2#fragments#   http://127.0.0.1/foo?c=v&c2=v2#frags   /foo?c=v&c2=v2s'   http://127.0.0.1:100?c=v&c2=v2#fragments   :100id   s$   http://127.0.0.1:100/?c=v&c2=v2#frags'   http://127.0.0.1:100/foo?c=v&c2=v2#frags   http://127.0.0.1t   /s   http://127.0.0.1/s   http://127.0.0.1/foos   /foos   http://127.0.0.1?param=values   /?param=values   http://127.0.0.1/?param=values   http://127.0.0.1:12345/foos   :12345i90  s   http://spam:12345/foos
   spam:12345t   spams   http://spam.test.org/foos   spam.test.orgs   https://127.0.0.1/foot   httpsi»  s   https://127.0.0.1/?param=values   https://127.0.0.1:12345/s   http://scrapytest.org/foo s   scrapytest.orgs   http://egg:7890 s   egg:7890t   eggiÒ  (   s   https
   spam:12345s   spami90  s   /foo(   s   https   spam.test.orgs   spam.test.orgiP   s   /foo(   s   https   scrapytest.orgs   scrapytest.orgiP   s   /foo(   s   https   egg:7890s   eggiÒ  R)   (   t   assertEqualsR   R'   (   R&   t   lipt   testsR   t   test(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt	   testParse'   s,    			c         C   s¯   d } | j  d ƒ } t | ƒ |  j | ƒ \ } } } } } |  j t | t ƒ ƒ |  j t | t ƒ ƒ |  j t | t ƒ ƒ |  j t | t ƒ ƒ |  j t | t ƒ ƒ d S(   sä   
        L{client._parse} should return C{str} for the scheme, host, and path
        elements of its return tuple, even when passed an URL which has
        previously been passed to L{urlparse} as a C{unicode} string.
        u   http://example.com/patht   asciiN(   t   encodeR    R'   t
   assertTruet
   isinstancet   strt   int(   R&   t   badInputt	   goodInputR!   R"   R#   R$   R%   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt    test_externalUnicodeInterferenceE   s    
(   t   __name__t
   __module__t   __doc__R'   R1   R:   (    (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyR        s   		t   ScrapyHTTPPageGetterTestsc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sy  t  j t d d d d d i d d 6d d	 6d
 d 6d d 6d d 6ƒ ƒ } |  j | d ƒ t  j t d ƒ ƒ } |  j | d ƒ t  j t d d d d d d d i d d 6ƒ ƒ } |  j | d ƒ t  j t d d d d ƒ ƒ } |  j | d ƒ t  j t d d d i d d 6d d g d 6ƒ ƒ } |  j | d ƒ t  j t d d d t i d d 6d d g d 6ƒ ƒ ƒ } |  j | d ƒ d  S(   NR   s   http://foo/barR   s	   some datat   headerss   example.nett   Hostt   foobles
   User-Agents	   blah blaht   Cookiet   12981s   Content-Lengtht   valuet   Usefuls   GET /bar HTTP/1.0
Content-Length: 9
Useful: value
Connection: close
User-Agent: fooble
Host: example.net
Cookie: blah blah

some datas    GET /bar HTTP/1.0
Host: foo

t   methodt   POSTs
   name=values!   application/x-www-form-urlencodeds   Content-Typesƒ   POST /bar HTTP/1.0
Host: foo
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 10

name=values4   POST /bar HTTP/1.0
Host: foo
Content-Length: 0

t   singles   X-Meta-Singlet   value1t   value2s   X-Meta-Multivaluedso   GET /bar HTTP/1.0
Host: foo
X-Meta-Multivalued: value1
X-Meta-Multivalued: value2
X-Meta-Single: single

(   R   R   R   t   _testR   (   R&   t   factory(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   test_earlyHeadersY   sP    							c         C   s]   t  ƒ  } t j ƒ  } | | _ | j | ƒ |  j t | j ƒ  j ƒ  ƒ t | j ƒ  ƒ ƒ | S(   N(	   R   R   t   ScrapyHTTPPageGetterRL   t   makeConnectiont   assertEqualt   setRD   t
   splitlines(   R&   RL   t	   testvaluet	   transportt   protocol(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyRK   ³   s    		c         C   sž   t  j t d d ƒ ƒ } t  j ƒ  } | | _ t ƒ  | _ | j d ƒ | j d ƒ | j d ƒ | j d ƒ |  j | j t i d g d 6d	 g d
 6ƒ ƒ d  S(   NR   s   http://foo/bars   HTTP/1.0 200 OK
s   Hello: World
s	   Foo: Bar
s   
t   Worldt   Hellot   Bart   Foo(	   R   R   R   RN   RL   R   R?   t   dataReceivedRP   (   R&   RL   RU   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   test_non_standard_line_endings½   s    	(   R;   R<   RM   RK   R[   (    (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyR>   W   s   	Z	
(   t   ForeverTakingResourcet   ErrorResourcet   NoLengthResourcet   HostHeaderResourcet   PayloadResourcet   BrokenDownloadResourcet   WebClientTestCasec           B   s˜   e  Z d  „  Z d „  Z d „  Z d „  Z d „  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  j d | d d ƒS(   Ni    t	   interfaces	   127.0.0.1(   R   t	   listenTCP(   R&   t   site(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   _listenÑ   s    c         C   s'  |  j  ƒ  } t j | ƒ t | ƒ j d ƒ j d ƒ t j | ƒ } | j d t	 j
 d ƒ ƒ | j d t ƒ  ƒ | j d t ƒ  ƒ | j d t ƒ  ƒ | j d t ƒ  ƒ | j d	 t ƒ  ƒ | j d
 t ƒ  ƒ t j | d d  ƒ|  _ t |  j ƒ |  _ |  j |  j ƒ |  _ |  j j ƒ  j |  _ d  S(   Nt   filet
   0123456789t   redirects   /filet   waitR   t   nolengthR#   t   payloadt   brokenR   (   t   mktempt   ost   mkdirR	   t   childt
   setContentR   t   Filet   putChildR   t   RedirectR\   R]   R^   R_   R`   Ra   R   t   Sitet   NoneRe   R
   t   wrapperRf   R$   t   getHostt   portno(   R&   t   nameR   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   setUpÔ   s    c         C   s   |  j  j ƒ  S(   N(   R$   t   stopListening(   R&   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   tearDownå   s    c         C   s   d |  j  | f S(   Ns   http://127.0.0.1:%d/%s(   Rz   (   R&   R%   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   getURLè   s    c         C   s2   d d } t  |  j d ƒ d | ƒj |  j | ƒ S(   NRh   i
   Rl   R   (   R   R   R   R-   (   R&   t   s(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   testPayloadë   s    
c         C   sc   t  j t |  j d ƒ ƒ j |  j d |  j ƒ t |  j d ƒ d i d d 6ƒj |  j d ƒ g ƒ S(   NR#   s   127.0.0.1:%dR?   s   www.example.comR@   (   R   t   gatherResultsR   R   R   R-   Rz   (   R&   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   testHostHeaderï   s    (c         C   s,   t  |  j d ƒ ƒ } | j |  j d ƒ | S(   sš   
        L{client.getPage} returns a L{Deferred} which is called back with
        the body of the response if the default method B{GET} is used.
        Rg   Rh   (   R   R   R   R-   (   R&   t   d(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   test_getPage÷   s    c            sL   ‡  f d †  } t  j | d ƒ j ˆ  j d ƒ | d ƒ j ˆ  j d ƒ g ƒ S(   s¶   
        L{client.getPage} returns a L{Deferred} which is called back with
        the empty string if the method is C{HEAD} and there is a successful
        response code.
        c            s   t  ˆ  j d ƒ d |  ƒS(   NRg   RF   (   R   R   (   RF   (   R&   (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   _getPage  s    t   headt    t   HEAD(   R   R‚   R   RP   (   R&   R†   (    (   R&   s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   test_getPageHead  s    c         C   s9   t  |  j d ƒ d d ƒ} | j |  j d |  j ƒ | S(   sÌ   
        When a non-zero timeout is passed to L{getPage} and the page is
        retrieved before the timeout period elapses, the L{Deferred} is
        called back with the contents of the page.
        R#   R   id   s   127.0.0.1:%d(   R   R   R   R-   Rz   (   R&   R„   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   test_timeoutNotTriggering  s    c            sJ   ˆ  j  t ˆ  j d ƒ d d ƒt j ƒ } ‡  f d †  } | j | ƒ | S(   sÔ   
        When a non-zero timeout is passed to L{getPage} and that many
        seconds elapse before the server responds to the request. the
        L{Deferred} is errbacked with a L{error.TimeoutError}.
        Rj   R   gíµ ÷Æ°>c            s0   ˆ  j  j j ƒ  } | r, | d j j ƒ  n  |  S(   Ni    (   Rx   t	   protocolst   keysRT   t   loseConnection(   t   passthrought	   connected(   R&   (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   cleanup"  s    (   t   assertFailureR   R   R   t   TimeoutErrort   addBoth(   R&   t   finishedR‘   (    (   R&   s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   test_timeoutTriggering  s    	c         C   s   t  |  j d ƒ ƒ j |  j ƒ S(   Nt   notsuchfile(   R   R   R   t   _cbNoSuchFile(   R&   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   testNotFound.  s    c         C   s   |  j  d | k ƒ d  S(   Ns   404 - No Such Resource(   t   assert_(   R&   t   pageData(    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyR˜   1  s    c         C   sk   |  j  d ƒ } t j | ƒ \ } } } } } t j t | ƒ ƒ } t j | | | ƒ | j j |  j	 | ƒ S(   NRg   (
   R   R   R'   R   R   R   t
   connectTCPR   R   t   _cbFactoryInfo(   R&   R   R!   R"   R#   R$   R%   RL   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   testFactoryInfo4  s
    c         C   sZ   |  j  | j d ƒ |  j | j j d ƒ ƒ |  j  | j d ƒ |  j  | j d d ƒ d  S(   Nt   200s   HTTP/t   OKs   content-lengtht   10(   R-   t   statusRš   t   versiont
   startswitht   messaget   response_headers(   R&   t   ignoredResultRL   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyR   ;  s    c         C   s   t  |  j d ƒ ƒ j |  j ƒ S(   NRi   (   R   R   R   t   _cbRedirect(   R&   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   testRedirectA  s    c         C   s   |  j  | d ƒ d  S(   Ns¼   
<html>
    <head>
        <meta http-equiv="refresh" content="0;URL=/file">
    </head>
    <body bgcolor="#FFFFFF" text="#000000">
    <a href="/file">click here</a>
    </body>
</html>
(   R-   (   R&   R›   (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyR¨   D  s    	(   R;   R<   Rf   R|   R~   R   R   Rƒ   R…   RŠ   R‹   R–   R™   R˜   Rž   R   R©   R¨   (    (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyRb   Ð   s    							
								('   R=   Ro   t   six.moves.urllib.parseR    t   twisted.trialR   t   twisted.webR   R   R   R   t   twisted.internetR   R   t   twisted.test.proto_helpersR   t   twisted.python.filepathR	   t   twisted.protocols.policiesR
   t   scrapy.core.downloaderR   R   t   scrapy.httpR   R   Rw   R   t   TestCaseR    R>   t   twisted.web.test.test_webclientR\   R]   R^   R_   R`   Ra   Rb   (    (    (    s8   /home/travis/build/scrapy/scrapy/tests/test_webclient.pyt   <module>   s   "7u.