1+ import contextlib
12import os
3+ import re
4+ import socket
25
36import pytest
4- import requests
7+ import requests . exceptions
58from httpbin import app as httpbin_app
69from util import get_raw_http_response
710
@@ -40,9 +43,33 @@ def test_server_should_be_http_1_1(httpbin):
4043
4144
4245def test_dont_crash_on_certificate_problems (httpbin_secure ):
43- with pytest .raises (Exception ):
46+ with pytest .raises (requests . exceptions . SSLError ):
4447 # this request used to hang
4548 requests .get (httpbin_secure + "/get" , verify = True , cert = __file__ )
49+
50+ # and this request would never happen
51+ requests .get (
52+ httpbin_secure + "/get" ,
53+ verify = True ,
54+ )
55+
56+
57+ def test_dont_crash_on_handshake_timeout (httpbin_secure , capsys ):
58+ with socket .socket () as sock :
59+ sock .connect ((httpbin_secure .host , httpbin_secure .port ))
60+ # this request used to hang
61+ assert sock .recv (1 ) == b""
62+
63+ assert (
64+ re .match (
65+ r"pytest-httpbin server hit an exception serving request: .* The "
66+ "handshake operation timed out\n attempting to ignore so the rest "
67+ "of the tests can run\n " ,
68+ capsys .readouterr ().out ,
69+ )
70+ is not None
71+ )
72+
4673 # and this request would never happen
4774 requests .get (
4875 httpbin_secure + "/get" ,
@@ -68,6 +95,7 @@ def test_fixed_port_environment_variables(protocol):
6895 # just have different port to avoid adrress already in use
6996 # if the second test run too fast after the first one (happens on pypy)
7097 port = 12345 + len (protocol )
98+ server = contextlib .nullcontext ()
7199
72100 try :
73101 envvar_original = os .environ .get (envvar , None )
@@ -76,10 +104,7 @@ def test_fixed_port_environment_variables(protocol):
76104 assert server .port == port
77105 finally :
78106 # if we don't do this, it blocks:
79- try :
80- server .start ()
81- server .stop ()
82- except UnboundLocalError :
107+ with server :
83108 pass
84109
85110 # restore the original environ:
0 commit comments