48
48
class CapturingServer (WSGIServer ):
49
49
def __init__ (self , host = "127.0.0.1" , port = 0 , ssl_context = None ):
50
50
WSGIServer .__init__ (self , host , port , self , ssl_context = ssl_context )
51
- self .code = 204
51
+ self .code = {}
52
52
self .headers = {}
53
- self .captured = []
53
+ self ._captured = {}
54
+ self .active_pytest_request = None
54
55
55
56
def respond_with (self , code = 200 , headers = None ):
56
- self .code = code
57
- if headers :
58
- self .headers = headers
57
+ assert self .active_pytest_request is not None
58
+ self .code [self .active_pytest_request ] = code
59
+ self .headers [self .active_pytest_request ] = headers or None
60
+
61
+ @property
62
+ def captured (self ):
63
+ if self .active_pytest_request is not None :
64
+ return self ._captured [self .active_pytest_request ]
65
+ else :
66
+ return []
59
67
60
68
def clear_captured (self ):
61
- del self .captured [:]
69
+ assert self .active_pytest_request is not None
70
+ del self ._captured [self .active_pytest_request ][:]
71
+
72
+ def set_pytest_request (self , pytest_request ):
73
+ self .active_pytest_request = pytest_request
74
+ self ._captured [self .active_pytest_request ] = []
75
+
76
+ def clear_pytest_request (self ):
77
+ if self .active_pytest_request is not None :
78
+ if self .active_pytest_request in self ._captured :
79
+ del self ._captured [self .active_pytest_request ]
80
+ if self .active_pytest_request in self .code :
81
+ del self .code [self .active_pytest_request ]
82
+ if self .active_pytest_request in self .headers :
83
+ del self .headers [self .active_pytest_request ]
84
+ self .active_pytest_request = None
62
85
63
86
def __call__ (self , environ , start_response ):
64
87
"""
@@ -82,25 +105,41 @@ def __call__(self, environ, start_response):
82
105
else :
83
106
envelope = Envelope .deserialize_from (rdr )
84
107
85
- self .captured .append (
86
- CapturedData (
87
- path = request .path ,
88
- event = event ,
89
- envelope = envelope ,
90
- compressed = compressed ,
108
+ if self .active_pytest_request is not None :
109
+ self ._captured [self .active_pytest_request ].append (
110
+ CapturedData (
111
+ path = request .path ,
112
+ event = event ,
113
+ envelope = envelope ,
114
+ compressed = compressed ,
115
+ )
91
116
)
92
- )
93
117
94
- response = Response (status = self .code )
95
- response .headers .extend (self .headers )
118
+ response = Response (status = self .code . get ( self . active_pytest_request , 204 ) )
119
+ response .headers .extend (self .headers . get ( self . active_pytest_request , {}) )
96
120
return response (environ , start_response )
97
121
98
122
99
- @pytest .fixture
100
- def capturing_server (request ):
123
+ server = None
124
+
125
+
126
+ @pytest .fixture (scope = "module" , autouse = True )
127
+ def make_capturing_server (request ):
128
+ global server
101
129
server = CapturingServer ()
102
130
server .start ()
103
131
request .addfinalizer (server .stop )
132
+
133
+
134
+ @pytest .fixture
135
+ def capturing_server (request ):
136
+ global server
137
+ server .set_pytest_request (request )
138
+
139
+ @request .addfinalizer
140
+ def cleanup ():
141
+ server .clear_pytest_request ()
142
+
104
143
return server
105
144
106
145
@@ -129,7 +168,6 @@ def mock_transaction_envelope(span_count):
129
168
return envelope
130
169
131
170
132
- @pytest .mark .forked
133
171
@pytest .mark .parametrize ("debug" , (True , False ))
134
172
@pytest .mark .parametrize ("client_flush_method" , ["close" , "flush" ])
135
173
@pytest .mark .parametrize ("use_pickle" , (True , False ))
0 commit comments