
    <fh                         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e j                        Zedk(  r e j                          yy)	    N)Mockpatch)_get_resp_headers)WebSocketBadStatusException)SSLErrorc                   $    e Zd Zd Zd Zd Zd Zy)HandshakeLargeResponseTestc           	         t               }dddz  z   g fd}t        d      5 }ddt        t                    idf|_        t        d	|
      5  | j                  t              5 }t        |       ddd       | j                  dt        j                  j                  d   t              r'|j                  j                  d   j                         n|j                  j                  d          | j                  t              d       | j                  t        d D                     ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)zHTest that large HTTP error responses during handshake are read in chunkss   Error details:    Ai N  c                 P    j                  |       |dkD  rt        d      d | S )N @  z[SSL: BAD_LENGTH] unknown error)appendr   )sockbufsizelarge_response
recv_callss     `/var/www/zara/venv/lib/python3.12/site-packages/websocket/tests/test_handshake_large_response.py	mock_recvzWHandshakeLargeResponseTest.test_large_error_response_chunked_reading.<locals>.mock_recv)   s1    g&@AA!(7++    !websocket._handshake.read_headersi  content-lengthzBad Requestwebsocket._socket.recvside_effectNs   Error details:r      c              3   &   K   | ]	  }|d k    yw)r   N ).0calls     r   	<genexpr>zWHandshakeLargeResponseTest.test_large_error_response_chunked_reading.<locals>.<genexpr>J   s     #IdDEM#Is   )r   r   strlenreturn_valueassertRaisesr   r   assertIn
isinstance	exceptionargsencodeassertGreater
assertTrueall)self	mock_sockr   mock_read_headerscmr   r   s        @@r   )test_large_error_response_chunked_readingzDHandshakeLargeResponseTest.test_large_error_response_chunked_reading   sT    F	 ,dUl: 
	, 67 	K;L!3s>':#;<.* /YG K&&'BC 1r%i01 % &bll&7&7&:C@ ))!,335\\..q1 ""3z?A6#Ij#I IJ#K	K 	K1 1K K	K 	Ks<   ,EE*E6B;E1EEEE	EE#c                    t               }ddz  dfd}t        d      5 }ddt        t                    idf|_        t        d	|
      5  | j                  t              5 }t        |       ddd       t        j                        }| j                  d|       ddd       ddd       y# 1 sw Y   AxY w# 1 sw Y   xY w# 1 sw Y   yxY w)zATest that the fix prevents SSL BAD_LENGTH errors during handshake   Xi   r   c                 f    dz  }t        ||z   t                    }|| }|rdz  |S dz  |S )Nr   r   r   )minr"   )r   r   chunk_start	chunk_endresultchunks_returnedlarge_contents        r   mock_recv_chunkedzbHandshakeLargeResponseTest.test_handshake_ssl_large_response_protection.<locals>.mock_recv_chunkedV   sQ     *E1KK'13}3EFI";y9FFq1OM 121OMr   r   i  r   zInternal Server Errorr   r   NXXXXX)
r   r   r!   r"   r#   r$   r   r   r'   r%   )r-   r.   r;   r/   r0   exception_strr9   r:   s         @@r   ,test_handshake_ssl_large_response_protectionzGHandshakeLargeResponseTest.test_handshake_ssl_large_response_protectionL   s     F	 u	 67 	6;L!3s='9#:;'.* /=NO 6&&'BC 1r%i01 !$BLL 1g}56	6 	61 16 6	6 	6s;   ,CB?'B33/B?"C3B<8B??C	CCc                    t               }dfd}t        d      5 }ddt        t                    idf|_        t        d|      5  | j                  t              5 }t        |       d	d	d	       | j                  d
t        j                               d	d	d	       d	d	d	       y	# 1 sw Y   ?xY w# 1 sw Y   xY w# 1 sw Y   y	xY w)z5Test that normal small responses still work correctlys   Small error messagec                     S )Nr   )r   r   small_responses     r   r   zRHandshakeLargeResponseTest.test_handshake_normal_small_response.<locals>.mock_recvv   s	    !!r   r   i  r   z	Not Foundr   r   NzSmall error message)
r   r   r!   r"   r#   r$   r   r   r%   r'   )r-   r.   r   r/   r0   rA   s        @r   $test_handshake_normal_small_responsez?HandshakeLargeResponseTest.test_handshake_normal_small_responsep   s     F	/	" 67 	H;L!3s>':#;<.* /YG H&&'BC 1r%i01 3S5FGH	H 	H1 1H H	H 	Hs;   ,C
B6 B*,-B6C*B3/B66B?	;CCc                 H   t               }t        d      5 }di df|_        | j                  t              5 }t        |       ddd       t        j                        }| j                  d|       | j                  d|       ddd       y# 1 sw Y   KxY w# 1 sw Y   yxY w)z;Test handshake error response without content-length headerr   i  	ForbiddenN403)	r   r   r#   r$   r   r   r!   r'   r%   )r-   r.   r/   r0   r=   s        r    test_handshake_no_content_lengthz;HandshakeLargeResponseTest.test_handshake_no_content_length   s     F	67 	6;L.* ""#>? -2!),-  -MMM%/MM+}5	6 	6- -	6 	6s#    BBABB	BB!N)__name__
__module____qualname__r1   r>   rB   rF   r   r   r   r	   r	      s    -K^"6HH.6r   r	   __main__)unittestunittest.mockr   r   websocket._handshaker   websocket._exceptionsr   websocket._ssl_compatr   TestCaser	   rG   mainr   r   r   <module>rR      sH     % 2 = *&~6!2!2 ~6B zHMMO r   