o
    nRaU%                     @   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	m
Z
mZ ddlZddlZddlZddlZzddlmZmZmZ W n   ddlmZmZmZ Y ejdddkZejdddkZejd	d
Zed
kZG dd dZG dd deZG dd dZG dd dejZedkre  dS dS )ac  
test_http.py
websocket - WebSocket client library for Python

Copyright 2021 engn33r

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)
proxy_inforead_headers_start_proxied_socket_tunnel_get_addrinfo_listconnect)
ProxyErrorProxyTimeoutErrorProxyConnectionErrorTEST_WITH_INTERNET01TEST_WITH_PROXYLOCAL_WS_SERVER_PORTz-1c                   @   s<   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   ;/usr/lib/python3/dist-packages/websocket/tests/test_http.py__init__-   s   
zSockMock.__init__c                 C   s   | j | d S r   )r   appendr   r   r   r   r   
add_packet1   s   zSockMock.add_packetc                 C      d S r   r   r   r   r   r   
gettimeout4      zSockMock.gettimeoutc                 C   sR   | j r'| j d}t|tr|t||kr!| j d||d   |d | S d S )Nr   )r   pop
isinstance	Exceptionleninsert)r   bufsizeer   r   r   recv7   s   
zSockMock.recvc                 C   s   | j | t|S r   )r   r   r"   r   r   r   r   send@   s   zSockMock.sendc                 C   r   r   r   r   r   r   r   closeD   r   zSockMock.closeN)	__name__
__module____qualname__r   r   r   r&   r'   r(   r   r   r   r   r   ,   s    	r   c                   @      e Zd Zdd ZdS )HeaderSockMockc                 C   s^   t |  tjtjt|}t|d}| |	  W d    d S 1 s(w   Y  d S )Nrb)
r   r   ospathjoindirname__file__openr   read)r   fnamer0   fr   r   r   r   J   s
   
"zHeaderSockMock.__init__Nr)   r*   r+   r   r   r   r   r   r-   H       r-   c                   @   r,   )OptsListc                 C   s   d| _ g | _dtji| _d S )N   Z	cert_reqs)timeoutZsockoptsslZ	CERT_NONEssloptr   r   r   r   r   S   s   zOptsList.__init__Nr8   r   r   r   r   r:   Q   r9   r:   c                   @   sx   e Zd Zdd Zdd Zeeddd Zeedee	dee
d	d
d Zeeddd Zdd ZdS )HttpTestc                 C   sF   t td\}}}| |d | |d d | tjt td d S )Ndata/header01.txte   Z
connectionZUpgradedata/header02.txt)r   r-   assertEqualassertRaiseswsZWebSocketException)r   statusheaderZstatus_messager   r   r   testReadHeader[   s   zHttpTest.testReadHeaderc                 C   s8   |  tjttdddd |  tjttdddd d S )Nr@   example.comP   )ZusernameZpasswordrB   )rD   rE   ZWebSocketProxyExceptionr   r-   r   r   r   r   
testTunnelb   s   zHttpTest.testTunnelz%Internet-requiring tests are disabledc                 C   s@  t jjr]| ttftdt tddddd | ttftdt tddddd | ttftdt tddddd | ttftdt tddd	dd | t	t
dt td
ddddd  | ttd ddtd
ddd | ttd ddtd
ddd | tjt
dt tdddddd  | t
dt tdddddd d S )Nzwss://example.comrI   8080Zsocks4r;   )http_proxy_hosthttp_proxy_port
proxy_typer<   Zsocks4aZsocks5Zsocks5h	127.0.0.1i'  rJ   TZ9999httprM   rN   rO   zwss://google.comz8.8.8.8i  )T)z
google.com  /)rE   Z_httpZHAVE_PYTHON_SOCKSrD   r	   OSErrorr   r:   r   r
   r   	TypeErrorr   socketr<   rC   r   r   r   r   testConnectf   s   $$$$"$zHttpTest.testConnectz:This test requires a HTTP proxy to be running on port 8899z/Tests using local websocket server are disabledc                 C   s   t  }|jdt dddd |d | }| |d | tddd	tddddt	
dd
dt	jt	jd	d f | tdt tdd
ddd d d d S )Nzws://127.0.0.1:rP   Z8899rQ   rR   zHello, Serverapi.bitfinex.comrS   Ti"  r   wss://api.bitfinex.com/ws/2r;   )rY   rS   z/ws/2)	websocket	WebSocketr   r   r'   r&   rC   r   r   rW   ZgetaddrinfoZSOCK_STREAMZSOL_TCPr:   )r   rE   Zserver_responser   r   r   testProxyConnect{   s   
*zHttpTest.testProxyConnectc                 C   s^   ddt jddd}tj|d}|d |d |  tjd	d
id}|d |j d S )NFZ
ServerNamea  TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:                        TLS_AES_128_GCM_SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:                        ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:                        ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:                        DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:                        ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:                        ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:                        DHE-RSA-AES256-SHA256:ECDHE-ECDSA-AES128-SHA256:                        ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA256:                        ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHAZ
prime256v1)check_hostnameZserver_hostnameZssl_versionZciphersZ
ecdh_curve)r>   rZ   ZHellor^   T)r=   ZPROTOCOL_TLS_CLIENTr[   r\   r   r'   r(   )r   Z
ssloptionsZws_ssl1Zws_ssl2r   r   r   
testSSLopt   s   




zHttpTest.testSSLoptc                 C   s   |  tddddjd | jttdddd |  tddddjd |  tddddjd |  tddddjd  |  tdddddjd	 d
 |  tdddddjd d d S )NrP   rL   rQ   rR   ZbadvalrI   )my_username123
my_pass321)rM   rN   rO   Zhttp_proxy_authr   r`   r;   ra   )rC   r   Zproxy_protocolrD   r   Z
proxy_hostZ
proxy_portZauthr   r   r   r   testProxyInfo   s   "zHttpTest.testProxyInfoN)r)   r*   r+   rH   rK   unittestZ
skipUnlessr   rX   r   TEST_WITH_LOCAL_SERVERr]   r_   rb   r   r   r   r   r?   Y   s    






r?   __main__) __doc__r/   os.pathr[   rE   Zwebsocket._httpr   r   r   r   r   r   rc   r=   rW   Zpython_socks._errorsr   r	   r
   environgetr   r   r   rd   r   r-   r:   ZTestCaser?   r)   mainr   r   r   r   <module>   s0    	V