o
    M,h                     @   sh   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	 	 G dd de j
Zedkr2e   dS dS )	    N)Mockpatch)_get_resp_headers)WebSocketBadStatusException)SSLErrorc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
HandshakeLargeResponseTestc              
      s6  t  }ddd   g  fdd}td{}ddtt id	f|_td
|dP | t}t| W d   n1 s>w   Y  | dt	|j
jd trW|j
jd  n|j
jd  | td | tdd D  W d   n1 s|w   Y  W d   dS W d   dS 1 sw   Y  dS )zHTest that large HTTP error responses during handshake are read in chunkss   Error details:    Ai N  c                    s&    | |dkrtd d | S )N @  z[SSL: BAD_LENGTH] unknown error)appendr   sockbufsizelarge_response
recv_calls y/var/www/www-root/data/www/bot.pdev.uz/venv/lib/python3.10/site-packages/websocket/tests/test_handshake_large_response.py	mock_recv)   s   
zWHandshakeLargeResponseTest.test_large_error_response_chunked_reading.<locals>.mock_recv!websocket._handshake.read_headersi  content-lengthzBad Requestwebsocket._socket.recvside_effectNs   Error details:r      c                 s   s    | ]}|d kV  qdS )r	   Nr   ).0callr   r   r   	<genexpr>J   s    zWHandshakeLargeResponseTest.test_large_error_response_chunked_reading.<locals>.<genexpr>)r   r   strlenreturn_valueassertRaisesr   r   assertIn
isinstance	exceptionargsencodeassertGreater
assertTrueallself	mock_sockr   mock_read_headerscmr   r   r   )test_large_error_response_chunked_reading   s2   



"zDHandshakeLargeResponseTest.test_large_error_response_chunked_readingc              
      s   t  }dd d  fdd}tdW}ddttid	f|_td
|d, | t}t| W d   n1 s<w   Y  t|j}| 	d| W d   n1 sVw   Y  W d   dS W d   dS 1 snw   Y  dS )zATest that the fix prevents SSL BAD_LENGTH errors during handshake   Xi   r   c                    s@    d }t || t}|| } |rd7  |S d7  |S )Nr	   r   r   )minr   )r   r   chunk_start	chunk_endresultchunks_returnedlarge_contentr   r   mock_recv_chunkedV   s   zbHandshakeLargeResponseTest.test_handshake_ssl_large_response_protection.<locals>.mock_recv_chunkedr   i  r   zInternal Server Errorr   r   NXXXXX)
r   r   r   r   r   r    r   r   r#   r!   )r*   r+   r7   r,   r-   exception_strr   r4   r   ,test_handshake_ssl_large_response_protectionL   s&   
	

"zGHandshakeLargeResponseTest.test_handshake_ssl_large_response_protectionc              
      s   t  }d  fdd}tdU}ddtt idf|_td|d	* | t}t| W d
   n1 s7w   Y  | dt|j	 W d
   n1 sOw   Y  W d
   d
S W d
   d
S 1 sgw   Y  d
S )z5Test that normal small responses still work correctlys   Small error messagec                    s    S )Nr   r   small_responser   r   r   v   s   zRHandshakeLargeResponseTest.test_handshake_normal_small_response.<locals>.mock_recvr   i  r   z	Not Foundr   r   NzSmall error message)
r   r   r   r   r   r    r   r   r!   r#   r)   r   r;   r   $test_handshake_normal_small_responsep   s"   

"z?HandshakeLargeResponseTest.test_handshake_normal_small_responsec              	   C   s   t  }td9}di df|_| t}t| W d   n1 s"w   Y  t|j}| d| | d| W d   dS 1 sCw   Y  dS )z;Test handshake error response without content-length headerr   i  	ForbiddenN403)	r   r   r   r    r   r   r   r#   r!   )r*   r+   r,   r-   r9   r   r   r    test_handshake_no_content_length   s   


"z;HandshakeLargeResponseTest.test_handshake_no_content_lengthN)__name__
__module____qualname__r.   r:   r=   r@   r   r   r   r   r      s
    /$r   __main__)unittestunittest.mockr   r   websocket._handshaker   websocket._exceptionsr   websocket._ssl_compatr   TestCaser   rA   mainr   r   r   r   <module>   s    