
=\•jB  ã               @   s!  d  Z  d d l Z d d l Z d d l Z d d l m Z m Z m Z m	 Z	 d d l
 Z
 d g e
 j d d … <e
 j d d k r£ e
 j d d k  r£ d d l Z n d d l Z Gd d	 „  d	 e ƒ Z Gd
 d „  d e ƒ Z Gd d „  d ƒ Z Gd d „  d e j ƒ Z e d k re j ƒ  d S)aH  
websocket - WebSocket client library for Python

Copyright (C) 2010 Hiroki Ohtani(liris)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

é    N)Ú
proxy_infoÚread_headersÚ_open_proxied_socketÚ_tunnelÚ é   é   é   c               @   sX   e  Z d  Z d d „  Z d d „  Z d d „  Z d d „  Z d	 d
 „  Z d d „  Z d S)ÚSockMockc             C   s   g  |  _  g  |  _ d  S)N)ÚdataÚsent)Úself© r   úE/tmp/pip-build-jxowagp5/websocket-client/websocket/tests/test_http.pyÚ__init__&   s    	zSockMock.__init__c             C   s   |  j  j | ƒ d  S)N)r   Úappend)r   r   r   r   r   Ú
add_packet*   s    zSockMock.add_packetc             C   s   d  S)Nr   )r   r   r   r   Ú
gettimeout-   s    zSockMock.gettimeoutc             C   sq   |  j  rm |  j  j d ƒ } t | t ƒ r0 | ‚ t | ƒ | k r_ |  j  j d | | d  … ƒ | d  | … Sd  S)Nr   )r   ÚpopÚ
isinstanceÚ	ExceptionÚlenÚinsert)r   ÚbufsizeÚer   r   r   Úrecv0   s    	zSockMock.recvc             C   s   |  j  j | ƒ t | ƒ S)N)r   r   r   )r   r   r   r   r   Úsend9   s    zSockMock.sendc             C   s   d  S)Nr   )r   r   r   r   Úclose=   s    zSockMock.closeN)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   r   r
   %   s   	r
   c               @   s   e  Z d  Z d d „  Z d S)ÚHeaderSockMockc          	   C   s^   t  j |  ƒ t j j t j j t ƒ | ƒ } t | d ƒ � } |  j | j	 ƒ  ƒ Wd  QRXd  S)NÚrb)
r
   r   ÚosÚpathÚjoinÚdirnameÚ__file__Úopenr   Úread)r   Úfnamer$   Úfr   r   r   r   C   s    !zHeaderSockMock.__init__N)r   r   r    r   r   r   r   r   r!   A   s   r!   c               @   s   e  Z d  Z d d „  Z d S)ÚOptsListc             C   s   d |  _  g  |  _ d  S)Nr   )ÚtimeoutZsockopt)r   r   r   r   r   L   s    	zOptsList.__init__N)r   r   r    r   r   r   r   r   r,   J   s   r,   c               @   s@   e  Z d  Z d d „  Z d d „  Z d d „  Z d d „  Z d	 S)
ÚHttpTestc             C   s_   t  t d ƒ ƒ \ } } } |  j | d ƒ |  j | d d ƒ |  j t j t  t d ƒ ƒ d  S)Nzdata/header01.txtée   Ú
connectionZUpgradezdata/header02.txt)r   r!   ÚassertEqualÚassertRaisesÚwsZWebSocketException)r   ÚstatusÚheaderZstatus_messager   r   r   ÚtestReadHeaderS   s    zHttpTest.testReadHeaderc             C   sN   |  j  t j t t d ƒ d d d ƒ |  j  t j t t d ƒ d d d ƒ d  S)	Nzdata/header01.txtzexample.coméP   ÚusernameÚpasswordzdata/header02.txt)r8   r9   )r8   r9   )r2   r3   ZWebSocketProxyExceptionr   r!   )r   r   r   r   Ú
testTunnelZ   s    %zHttpTest.testTunnelc             C   s—   |  j  t t d t ƒ  t d d d d d d ƒ ƒ |  j  t t d t ƒ  t d d d d d d ƒ ƒ |  j  t t d t ƒ  t d d d d d d	 ƒ ƒ d  S)
Nzwss://example.comÚhttp_proxy_hostzexample.comÚhttp_proxy_portÚ8080Ú
proxy_typeÚhttpZsocks4Zsocks5h)r2   Ú	TypeErrorr   r,   r   )r   r   r   r   ÚtestConnect^   s    11zHttpTest.testConnectc          	   C   sÆ   |  j  t d d d d d d ƒ j d ƒ |  j t t d d d d d d ƒ|  j  t d d d d d d ƒ j d ƒ |  j  t d d d d d d ƒ j d ƒ |  j  t d d d d d d ƒ j d  ƒ d  S)	Nr;   z	127.0.0.1r<   r=   r>   r?   Zbadvalzexample.com)r1   r   Útyper2   Ú
ValueErrorÚhostÚportÚauth)r   r   r   r   ÚtestProxyInfod   s
    ("((zHttpTest.testProxyInfoN)r   r   r    r6   r:   rA   rG   r   r   r   r   r.   Q   s   r.   Ú__main__)Ú__doc__r#   Úos.pathZ	websocketr3   Zwebsocket._httpr   r   r   r   Úsysr$   Úversion_infoZ	unittest2ZunittestÚobjectr
   r!   r,   ZTestCaser.   r   Úmainr   r   r   r   Ú<module>   s   "&	